Lower required CMake version in example
[apps/agl-service-can-low-level.git] / tests / alltypes_pointer / encode_alltypes_pointer.c
1 /* Attempts to test all the datatypes supported by ProtoBuf.
2  */
3
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <pb_encode.h>
8 #include "alltypes.pb.h"
9 #include "test_helpers.h"
10
11 int main(int argc, char **argv)
12 {
13     int mode = (argc > 1) ? atoi(argv[1]) : 0;
14     
15     /* Values for required fields */
16     int32_t     req_int32         = -1001;
17     int64_t     req_int64         = -1002;
18     uint32_t    req_uint32        = 1003;
19     uint64_t    req_uint64        = 1004;
20     int32_t     req_sint32        = -1005;
21     int64_t     req_sint64        = -1006;
22     bool        req_bool          = true;
23     uint32_t    req_fixed32       = 1008;
24     int32_t     req_sfixed32      = -1009;
25     float       req_float         = 1010.0f;
26     uint64_t    req_fixed64       = 1011;
27     int64_t     req_sfixed64      = -1012;
28     double      req_double        = 1013.0;
29     char*       req_string        = "1014";
30     PB_BYTES_ARRAY_T(4) req_bytes = {4, {'1', '0', '1', '5'}};
31     static int32_t req_substuff   = 1016;
32     SubMessage  req_submsg        = {"1016", &req_substuff};
33     MyEnum      req_enum          = MyEnum_Truth;
34     EmptyMessage req_emptymsg     = {0};
35     
36     int32_t     end               = 1099;
37
38     /* Values for repeated fields */
39     int32_t     rep_int32[5]      = {0, 0, 0, 0, -2001};
40     int64_t     rep_int64[5]      = {0, 0, 0, 0, -2002};
41     uint32_t    rep_uint32[5]     = {0, 0, 0, 0, 2003};
42     uint64_t    rep_uint64[5]     = {0, 0, 0, 0, 2004};
43     int32_t     rep_sint32[5]     = {0, 0, 0, 0, -2005};
44     int64_t     rep_sint64[5]     = {0, 0, 0, 0, -2006};
45     bool        rep_bool[5]       = {false, false, false, false, true};
46     uint32_t    rep_fixed32[5]    = {0, 0, 0, 0, 2008};
47     int32_t     rep_sfixed32[5]   = {0, 0, 0, 0, -2009};
48     float       rep_float[5]      = {0, 0, 0, 0, 2010.0f};
49     uint64_t    rep_fixed64[5]    = {0, 0, 0, 0, 2011};
50     int64_t     rep_sfixed64[5]   = {0, 0, 0, 0, -2012};
51     double      rep_double[5]     = {0, 0, 0, 0, 2013.0f};
52     char*       rep_string[5]     = {"", "", "", "", "2014"};
53     static PB_BYTES_ARRAY_T(4) rep_bytes_4 = {4, {'2', '0', '1', '5'}};
54     pb_bytes_array_t *rep_bytes[5]= {NULL, NULL, NULL, NULL, (pb_bytes_array_t*)&rep_bytes_4};
55     static int32_t rep_sub2zero   = 0;
56     static int32_t rep_substuff2  = 2016;
57     static uint32_t rep_substuff3 = 2016;
58     SubMessage  rep_submsg[5]     = {{"", &rep_sub2zero},
59                                      {"", &rep_sub2zero},
60                                      {"", &rep_sub2zero},
61                                      {"", &rep_sub2zero},
62                                      {"2016", &rep_substuff2, &rep_substuff3}};
63     MyEnum      rep_enum[5]       = {0, 0, 0, 0, MyEnum_Truth};
64     EmptyMessage rep_emptymsg[5]  = {{0}, {0}, {0}, {0}, {0}};
65
66     /* Values for optional fields */
67     int32_t     opt_int32         = 3041;
68     int64_t     opt_int64         = 3042;
69     uint32_t    opt_uint32        = 3043;
70     uint64_t    opt_uint64        = 3044;
71     int32_t     opt_sint32        = 3045;
72     int64_t     opt_sint64        = 3046;
73     bool        opt_bool          = true;
74     uint32_t    opt_fixed32       = 3048;
75     int32_t     opt_sfixed32      = 3049;
76     float       opt_float         = 3050.0f;
77     uint64_t    opt_fixed64       = 3051;
78     int64_t     opt_sfixed64      = 3052;
79     double      opt_double        = 3053.0;
80     char*       opt_string        = "3054";
81     PB_BYTES_ARRAY_T(4) opt_bytes = {4, {'3', '0', '5', '5'}};
82     static int32_t opt_substuff   = 3056;
83     SubMessage  opt_submsg        = {"3056", &opt_substuff};
84     MyEnum      opt_enum          = MyEnum_Truth;
85     EmptyMessage opt_emptymsg     = {0};
86
87     static int32_t oneof_substuff = 4059;
88     SubMessage  oneof_msg1        = {"4059", &oneof_substuff};
89
90     /* Values for the Limits message. */
91     static int32_t  int32_min  = INT32_MIN;
92     static int32_t  int32_max  = INT32_MAX;
93     static uint32_t uint32_min = 0;
94     static uint32_t uint32_max = UINT32_MAX;
95     static int64_t  int64_min  = INT64_MIN;
96     static int64_t  int64_max  = INT64_MAX;
97     static uint64_t uint64_min = 0;
98     static uint64_t uint64_max = UINT64_MAX;
99     static HugeEnum enum_min   = HugeEnum_Negative;
100     static HugeEnum enum_max   = HugeEnum_Positive;
101     Limits req_limits = {&int32_min,    &int32_max,
102                          &uint32_min,   &uint32_max,
103                          &int64_min,    &int64_max,
104                          &uint64_min,   &uint64_max,
105                          &enum_min,     &enum_max};
106
107     /* Initialize the message struct with pointers to the fields. */
108     AllTypes alltypes = {0};
109
110     alltypes.req_int32         = &req_int32;
111     alltypes.req_int64         = &req_int64;
112     alltypes.req_uint32        = &req_uint32;
113     alltypes.req_uint64        = &req_uint64;
114     alltypes.req_sint32        = &req_sint32;
115     alltypes.req_sint64        = &req_sint64;
116     alltypes.req_bool          = &req_bool;
117     alltypes.req_fixed32       = &req_fixed32;
118     alltypes.req_sfixed32      = &req_sfixed32;
119     alltypes.req_float         = &req_float;
120     alltypes.req_fixed64       = &req_fixed64;
121     alltypes.req_sfixed64      = &req_sfixed64;
122     alltypes.req_double        = &req_double;
123     alltypes.req_string        = req_string;
124     alltypes.req_bytes         = (pb_bytes_array_t*)&req_bytes;
125     alltypes.req_submsg        = &req_submsg;
126     alltypes.req_enum          = &req_enum;
127     alltypes.req_emptymsg      = &req_emptymsg;
128     alltypes.req_limits        = &req_limits;
129     
130     alltypes.rep_int32_count    = 5; alltypes.rep_int32     = rep_int32;
131     alltypes.rep_int64_count    = 5; alltypes.rep_int64     = rep_int64;
132     alltypes.rep_uint32_count   = 5; alltypes.rep_uint32    = rep_uint32;
133     alltypes.rep_uint64_count   = 5; alltypes.rep_uint64    = rep_uint64;
134     alltypes.rep_sint32_count   = 5; alltypes.rep_sint32    = rep_sint32;
135     alltypes.rep_sint64_count   = 5; alltypes.rep_sint64    = rep_sint64;
136     alltypes.rep_bool_count     = 5; alltypes.rep_bool      = rep_bool;
137     alltypes.rep_fixed32_count  = 5; alltypes.rep_fixed32   = rep_fixed32;
138     alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32  = rep_sfixed32;
139     alltypes.rep_float_count    = 5; alltypes.rep_float     = rep_float;
140     alltypes.rep_fixed64_count  = 5; alltypes.rep_fixed64   = rep_fixed64;
141     alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64  = rep_sfixed64;
142     alltypes.rep_double_count   = 5; alltypes.rep_double    = rep_double;
143     alltypes.rep_string_count   = 5; alltypes.rep_string    = rep_string;
144     alltypes.rep_bytes_count    = 5; alltypes.rep_bytes     = rep_bytes;
145     alltypes.rep_submsg_count   = 5; alltypes.rep_submsg    = rep_submsg;
146     alltypes.rep_enum_count     = 5; alltypes.rep_enum      = rep_enum;
147     alltypes.rep_emptymsg_count = 5; alltypes.rep_emptymsg  = rep_emptymsg;
148     
149     if (mode != 0)
150     {
151         /* Fill in values for optional fields */
152         alltypes.opt_int32         = &opt_int32;
153         alltypes.opt_int64         = &opt_int64;
154         alltypes.opt_uint32        = &opt_uint32;
155         alltypes.opt_uint64        = &opt_uint64;
156         alltypes.opt_sint32        = &opt_sint32;
157         alltypes.opt_sint64        = &opt_sint64;
158         alltypes.opt_bool          = &opt_bool;
159         alltypes.opt_fixed32       = &opt_fixed32;
160         alltypes.opt_sfixed32      = &opt_sfixed32;
161         alltypes.opt_float         = &opt_float;
162         alltypes.opt_fixed64       = &opt_fixed64;
163         alltypes.opt_sfixed64      = &opt_sfixed64;
164         alltypes.opt_double        = &opt_double;
165         alltypes.opt_string        = opt_string;
166         alltypes.opt_bytes         = (pb_bytes_array_t*)&opt_bytes;
167         alltypes.opt_submsg        = &opt_submsg;
168         alltypes.opt_enum          = &opt_enum;
169         alltypes.opt_emptymsg      = &opt_emptymsg;
170
171         alltypes.which_oneof = AllTypes_oneof_msg1_tag;
172         alltypes.oneof.oneof_msg1 = &oneof_msg1;
173     }
174     
175     alltypes.end = &end;
176     
177     {
178         uint8_t buffer[4096];
179         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
180         
181         /* Now encode it and check if we succeeded. */
182         if (pb_encode(&stream, AllTypes_fields, &alltypes))
183         {
184             SET_BINARY_MODE(stdout);
185             fwrite(buffer, 1, stream.bytes_written, stdout);
186             return 0; /* Success */
187         }
188         else
189         {
190             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
191             return 1; /* Failure */
192         }
193     }
194 }