~stub/ubuntu/trusty/avro-c/trunk

« back to all changes in this revision

Viewing changes to tests/test_avro_schema_names.c

  • Committer: Stuart Bishop
  • Date: 2015-05-14 11:53:53 UTC
  • Revision ID: stuart@stuartbishop.net-20150514115353-0cvnrcyohcq5l7yj
Tags: upstream-1.7.7
ImportĀ upstreamĀ versionĀ 1.7.7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
 
3
 * contributor license agreements.  See the NOTICE file distributed with
 
4
 * this work for additional information regarding copyright ownership.
 
5
 * The ASF licenses this file to you under the Apache License, Version 2.0 
 
6
 * (the "License"); you may not use this file except in compliance with
 
7
 * the License.  You may obtain a copy of the License at
 
8
 * 
 
9
 * http://www.apache.org/licenses/LICENSE-2.0
 
10
 * 
 
11
 * Unless required by applicable law or agreed to in writing, software
 
12
 * distributed under the License is distributed on an "AS IS" BASIS,
 
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 
14
 * implied.  See the License for the specific language governing
 
15
 * permissions and limitations under the License. 
 
16
 */
 
17
 
 
18
#include "avro.h"
 
19
#include "avro_private.h"
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#include <string.h>
 
23
 
 
24
int test_cases = 0;
 
25
avro_writer_t avro_stderr;
 
26
 
 
27
static void test_helper(const char *json,
 
28
     const char *name,
 
29
     avro_schema_t expected)
 
30
{
 
31
 int rc;
 
32
 avro_schema_t base;
 
33
 avro_schema_error_t serror;
 
34
 
 
35
 rc = avro_schema_from_json(json, strlen(json), &base, &serror);
 
36
 if (rc != 0)
 
37
 {
 
38
   fprintf(stderr,
 
39
     "Error parsing Avro schema:\n%s\n",
 
40
     json);
 
41
   exit(EXIT_FAILURE);
 
42
 }
 
43
 
 
44
 avro_schema_t actual =
 
45
   avro_schema_get_subschema(base, name);
 
46
 
 
47
 if (actual == NULL)
 
48
 {
 
49
   fprintf(stderr,
 
50
     "No subschema named \"%s\" in %s\n",
 
51
     name, avro_schema_type_name(base));
 
52
   exit(EXIT_FAILURE);
 
53
 }
 
54
 
 
55
 if (!avro_schema_equal(actual, expected))
 
56
 {
 
57
   fprintf(stderr,
 
58
     "Subschema \"%s\" should be %s, "
 
59
     "is actually %s\n",
 
60
     name,
 
61
     avro_schema_type_name(expected),
 
62
     avro_schema_type_name(actual));
 
63
   exit(EXIT_FAILURE);
 
64
 }
 
65
 
 
66
 avro_schema_decref(base);
 
67
 avro_schema_decref(expected);
 
68
}
 
69
 
 
70
static void test_array_schema_01()
 
71
{
 
72
 static char *JSON =
 
73
   "{"
 
74
   "  \"type\": \"array\","
 
75
   "  \"items\": \"long\""
 
76
   "}";
 
77
 
 
78
 test_helper(JSON, "[]", avro_schema_long());
 
79
}
 
80
 
 
81
static void test_map_schema_01()
 
82
{
 
83
 static char *JSON =
 
84
   "{"
 
85
   "  \"type\": \"map\","
 
86
   "  \"values\": \"long\""
 
87
   "}";
 
88
 
 
89
 test_helper(JSON, "{}", avro_schema_long());
 
90
}
 
91
 
 
92
static void test_record_schema_01()
 
93
{
 
94
 static char *JSON =
 
95
   "{"
 
96
   "  \"type\": \"record\","
 
97
   "  \"name\": \"test\","
 
98
   "  \"fields\": ["
 
99
   "    { \"name\": \"a\", \"type\": \"long\" }"
 
100
   "  ]"
 
101
   "}";
 
102
 
 
103
 test_helper(JSON, "a", avro_schema_long());
 
104
}
 
105
 
 
106
static void test_union_schema_01()
 
107
{
 
108
 static char *JSON =
 
109
   "["
 
110
   "  \"long\","
 
111
   "  {"
 
112
   "    \"type\": \"record\","
 
113
   "    \"name\": \"test\","
 
114
   "    \"fields\": ["
 
115
   "      { \"name\": \"a\", \"type\": \"long\" }"
 
116
   "    ]"
 
117
   "  }"
 
118
   "]";
 
119
 
 
120
 test_helper(JSON, "long", avro_schema_long());
 
121
}
 
122
 
 
123
int main(int argc, char *argv[])
 
124
{
 
125
 AVRO_UNUSED(argc);
 
126
 AVRO_UNUSED(argv);
 
127
 
 
128
 test_array_schema_01();
 
129
 
 
130
 test_map_schema_01();
 
131
 
 
132
 test_record_schema_01();
 
133
 
 
134
 test_union_schema_01();
 
135
 
 
136
 return EXIT_SUCCESS;
 
137
}