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

« back to all changes in this revision

Viewing changes to tests/test_avro_1379.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
 
 
23
static const char  *schema_json =
 
24
        "{"
 
25
        "  \"type\": \"record\","
 
26
        "  \"name\": \"test\","
 
27
        "  \"fields\": ["
 
28
        "    { \"name\": \"i\", \"type\": \"int\" },"
 
29
        "    { \"name\": \"l\", \"type\": \"long\" },"
 
30
        "    { \"name\": \"s\", \"type\": \"string\" },"
 
31
        "    {"
 
32
        "      \"name\": \"subrec\","
 
33
        "      \"type\": {"
 
34
        "        \"type\": \"record\","
 
35
        "        \"name\": \"sub\","
 
36
        "        \"fields\": ["
 
37
        "          { \"name\": \"f\", \"type\": \"float\" },"
 
38
        "          { \"name\": \"d\", \"type\": \"double\" }"
 
39
        "        ]"
 
40
        "      }"
 
41
        "    }"
 
42
        "  ]"
 
43
        "}";
 
44
 
 
45
static void
 
46
populate_complex_record(avro_value_t *p_val)
 
47
{
 
48
        avro_value_t  field;
 
49
 
 
50
        avro_value_get_by_index(p_val, 0, &field, NULL);
 
51
        avro_value_set_int(&field, 42);
 
52
 
 
53
        avro_value_get_by_index(p_val, 1, &field, NULL);
 
54
        avro_value_set_long(&field, 4242);
 
55
 
 
56
        avro_wrapped_buffer_t  wbuf;
 
57
        avro_wrapped_buffer_new_string(&wbuf, "Follow your bliss.");
 
58
        avro_value_get_by_index(p_val, 2, &field, NULL);
 
59
        avro_value_give_string_len(&field, &wbuf);
 
60
 
 
61
        avro_value_t  subrec;
 
62
        avro_value_get_by_index(p_val, 3, &subrec, NULL);
 
63
 
 
64
        avro_value_get_by_index(&subrec, 0, &field, NULL);
 
65
        avro_value_set_float(&field, 3.14159265);
 
66
 
 
67
        avro_value_get_by_index(&subrec, 1, &field, NULL);
 
68
        avro_value_set_double(&field, 2.71828183);
 
69
}
 
70
 
 
71
int main(void)
 
72
{
 
73
        int rval = 0;
 
74
        size_t len;
 
75
        static char  buf[4096];
 
76
        avro_writer_t  writer;
 
77
        avro_file_writer_t file_writer;
 
78
        avro_file_reader_t file_reader;
 
79
        const char *outpath = "test-1379.avro";
 
80
 
 
81
        avro_schema_t  schema = NULL;
 
82
        avro_schema_error_t  error = NULL;
 
83
        check(rval, avro_schema_from_json(schema_json, strlen(schema_json), &schema, &error));
 
84
 
 
85
        avro_value_iface_t  *iface = avro_generic_class_from_schema(schema);
 
86
 
 
87
        avro_value_t  val;
 
88
        avro_generic_value_new(iface, &val);
 
89
 
 
90
        avro_value_t  out;
 
91
        avro_generic_value_new(iface, &out);
 
92
 
 
93
        /* create the val */
 
94
        avro_value_reset(&val);
 
95
        populate_complex_record(&val);
 
96
 
 
97
        /* create the writers */
 
98
        writer = avro_writer_memory(buf, sizeof(buf));
 
99
        check(rval, avro_file_writer_create(outpath, schema, &file_writer));
 
100
 
 
101
        fprintf(stderr, "Writing to buffer\n");
 
102
        check(rval, avro_value_write(writer, &val));
 
103
 
 
104
        fprintf(stderr, "Writing buffer to %s "
 
105
                "using avro_file_writer_append_encoded()\n", outpath);
 
106
        len = avro_writer_tell(writer);
 
107
        check(rval, avro_file_writer_append_encoded(file_writer, buf, len));
 
108
        check(rval, avro_file_writer_close(file_writer));
 
109
 
 
110
        check(rval, avro_file_reader(outpath, &file_reader));
 
111
        fprintf(stderr, "Re-reading value to verify\n");
 
112
        check(rval, avro_file_reader_read_value(file_reader, &out));
 
113
        fprintf(stderr, "Verifying value...");
 
114
        if (!avro_value_equal(&val, &out)) {
 
115
                fprintf(stderr, "fail!\n");
 
116
                exit(EXIT_FAILURE);
 
117
        }
 
118
        fprintf(stderr, "ok\n");
 
119
        check(rval, avro_file_reader_close(file_reader));
 
120
        remove(outpath);
 
121
 
 
122
        exit(EXIT_SUCCESS);
 
123
}