MOS Source Code
Loading...
Searching...
No Matches
decode_alltypes.c
Go to the documentation of this file.
1/* Tests the decoding of all types.
2 * This is the counterpart of test_encode3.
3 * Run e.g. ./test_encode3 | ./test_decode3
4 */
5
6#include <stdio.h>
7#include <string.h>
8#include <stdlib.h>
9#include <pb_decode.h>
10#include "alltypes.pb.h"
11#include "unittests.h"
12#include "test_helpers.h"
13
14/* This function is called once from main(), it handles
15 the decoding and checks the fields. */
16bool check_alltypes(pb_istream_t *stream, int mode)
17{
18 int status = 0;
19
20 /* Uses _init_default to just make sure that the macro works. */
22
23 /* Fill with garbage to better detect initialization errors */
24 memset(&alltypes, 0xAA, sizeof(alltypes));
25 alltypes.extensions = 0;
26
27 if (!pb_decode(stream, AllTypes_fields, &alltypes))
28 return false;
29
30 if (mode == 0 || mode == 1)
31 {
32 TEST(alltypes.req_int32 == -1001);
33 TEST(alltypes.req_int64 == -1002);
34 TEST(alltypes.req_uint32 == 1003);
35 TEST(alltypes.req_uint64 == 1004);
36 TEST(alltypes.req_sint32 == -1005);
37 TEST(alltypes.req_sint64 == -1006);
38 TEST(alltypes.req_bool == true);
39
40 TEST(alltypes.req_fixed32 == 1008);
41 TEST(alltypes.req_sfixed32 == -1009);
42 TEST(alltypes.req_float == 1010.0f);
43
44 TEST(alltypes.req_fixed64 == 1011);
45 TEST(alltypes.req_sfixed64 == -1012);
46 TEST(alltypes.req_double == 1013.0f);
47
48 TEST(strcmp(alltypes.req_string, "1014") == 0);
49 TEST(alltypes.req_bytes.size == 4);
50 TEST(memcmp(alltypes.req_bytes.bytes, "1015", 4) == 0);
51 TEST(strcmp(alltypes.req_submsg.substuff1, "1016") == 0);
52 TEST(alltypes.req_submsg.substuff2 == 1016);
53 TEST(alltypes.req_submsg.substuff3 == 3);
54 TEST(alltypes.req_enum == MyEnum_Truth);
55 TEST(memcmp(alltypes.req_fbytes, "1019", 4) == 0);
56
57 TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
58 TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
59 TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
60 TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
61 TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
62 TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
63 TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
64
65 TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
66 TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
67 TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
68
69 TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
70 TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
71 TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
72
73 TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
74 TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4].size == 4 && alltypes.rep_bytes[0].size == 0);
75 TEST(memcmp(alltypes.rep_bytes[4].bytes, "2015", 4) == 0);
76
77 TEST(alltypes.rep_submsg_count == 5);
78 TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
79 TEST(alltypes.rep_submsg[4].substuff2 == 2016 && alltypes.rep_submsg[0].substuff2 == 0);
80 TEST(alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == 3);
81
82 TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
83 TEST(alltypes.rep_emptymsg_count == 5);
84 TEST(alltypes.rep_fbytes_count == 5);
85 TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0);
86 TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0);
87
88 TEST(alltypes.rep_farray[0] == 0 && alltypes.rep_farray[4] == 2040);
89 TEST(alltypes.rep_farray2[0] == 0 && alltypes.rep_farray2[2] == 2095);
90
91 TEST(alltypes.req_limits.int32_min == INT32_MIN);
92 TEST(alltypes.req_limits.int32_max == INT32_MAX);
93 TEST(alltypes.req_limits.uint32_min == 0);
94 TEST(alltypes.req_limits.uint32_max == UINT32_MAX);
95 TEST(alltypes.req_limits.int64_min == INT64_MIN);
96 TEST(alltypes.req_limits.int64_max == INT64_MAX);
97 TEST(alltypes.req_limits.uint64_min == 0);
98 TEST(alltypes.req_limits.uint64_max == UINT64_MAX);
99 TEST(alltypes.req_limits.enum_min == HugeEnum_Negative);
100 TEST(alltypes.req_limits.enum_max == HugeEnum_Positive);
101 TEST(alltypes.req_limits.largetag == 1001);
102
103 TEST(alltypes.req_ds8.first == 9991);
104 TEST(alltypes.req_ds8.second == 9992);
105
106 TEST(alltypes.req_intsizes.req_int8 == -128);
107 TEST(alltypes.req_intsizes.req_uint8 == 255);
108 TEST(alltypes.req_intsizes.req_sint8 == -128);
109 TEST(alltypes.req_intsizes.req_int16 == -32768);
110 TEST(alltypes.req_intsizes.req_uint16 == 65535);
111 TEST(alltypes.req_intsizes.req_sint16 == -32768);
112 }
113
114 if (mode == 0)
115 {
116 /* Expect default values */
117 TEST(alltypes.has_opt_int32 == false);
118 TEST(alltypes.opt_int32 == 4041);
119 TEST(alltypes.has_opt_int64 == false);
120 TEST(alltypes.opt_int64 == 4042);
121 TEST(alltypes.has_opt_uint32 == false);
122 TEST(alltypes.opt_uint32 == 4043);
123 TEST(alltypes.has_opt_uint64 == false);
124 TEST(alltypes.opt_uint64 == 4044);
125 TEST(alltypes.has_opt_sint32 == false);
126 TEST(alltypes.opt_sint32 == 4045);
127 TEST(alltypes.has_opt_sint64 == false);
128 TEST(alltypes.opt_sint64 == 4046);
129 TEST(alltypes.has_opt_bool == false);
130 TEST(alltypes.opt_bool == false);
131
132 TEST(alltypes.has_opt_fixed32 == false);
133 TEST(alltypes.opt_fixed32 == 4048);
134 TEST(alltypes.has_opt_sfixed32 == false);
135 TEST(alltypes.opt_sfixed32 == 4049);
136 TEST(alltypes.has_opt_float == false);
137 TEST(alltypes.opt_float == 4050.0f);
138
139 TEST(alltypes.has_opt_fixed64 == false);
140 TEST(alltypes.opt_fixed64 == 4051);
141 TEST(alltypes.has_opt_sfixed64 == false);
142 TEST(alltypes.opt_sfixed64 == 4052);
143 TEST(alltypes.has_opt_double == false);
144 TEST(alltypes.opt_double == 4053.0);
145
146 TEST(alltypes.has_opt_string == false);
147 TEST(strcmp(alltypes.opt_string, "4054") == 0);
148 TEST(alltypes.has_opt_bytes == false);
149 TEST(alltypes.opt_bytes.size == 4);
150 TEST(memcmp(alltypes.opt_bytes.bytes, "\x34\x5C\x00\xff", 4) == 0);
151 TEST(alltypes.has_opt_submsg == false);
152 TEST(strcmp(alltypes.opt_submsg.substuff1, "1") == 0);
153 TEST(alltypes.opt_submsg.substuff2 == 2);
154 TEST(alltypes.opt_submsg.substuff3 == 3);
155 TEST(alltypes.has_opt_enum == false);
156 TEST(alltypes.opt_enum == MyEnum_Second);
157 TEST(alltypes.has_opt_emptymsg == false);
158 TEST(alltypes.has_opt_fbytes == false);
159 TEST(memcmp(alltypes.opt_fbytes, "4059", 4) == 0);
160
161 TEST(alltypes.which_oneof == 0);
162
163 TEST(alltypes.has_opt_non_zero_based_enum == false);
164 TEST(alltypes.opt_non_zero_based_enum == NonZeroBasedEnum_Two);
165 }
166 else if (mode == 1)
167 {
168 /* Expect filled-in values */
169 TEST(alltypes.has_opt_int32 == true);
170 TEST(alltypes.opt_int32 == 3041);
171 TEST(alltypes.has_opt_int64 == true);
172 TEST(alltypes.opt_int64 == 3042);
173 TEST(alltypes.has_opt_uint32 == true);
174 TEST(alltypes.opt_uint32 == 3043);
175 TEST(alltypes.has_opt_uint64 == true);
176 TEST(alltypes.opt_uint64 == 3044);
177 TEST(alltypes.has_opt_sint32 == true);
178 TEST(alltypes.opt_sint32 == 3045);
179 TEST(alltypes.has_opt_sint64 == true);
180 TEST(alltypes.opt_sint64 == 3046);
181 TEST(alltypes.has_opt_bool == true);
182 TEST(alltypes.opt_bool == true);
183
184 TEST(alltypes.has_opt_fixed32 == true);
185 TEST(alltypes.opt_fixed32 == 3048);
186 TEST(alltypes.has_opt_sfixed32 == true);
187 TEST(alltypes.opt_sfixed32 == 3049);
188 TEST(alltypes.has_opt_float == true);
189 TEST(alltypes.opt_float == 3050.0f);
190
191 TEST(alltypes.has_opt_fixed64 == true);
192 TEST(alltypes.opt_fixed64 == 3051);
193 TEST(alltypes.has_opt_sfixed64 == true);
194 TEST(alltypes.opt_sfixed64 == 3052);
195 TEST(alltypes.has_opt_double == true);
196 TEST(alltypes.opt_double == 3053.0);
197
198 TEST(alltypes.has_opt_string == true);
199 TEST(strcmp(alltypes.opt_string, "3054") == 0);
200 TEST(alltypes.has_opt_bytes == true);
201 TEST(alltypes.opt_bytes.size == 4);
202 TEST(memcmp(alltypes.opt_bytes.bytes, "3055", 4) == 0);
203 TEST(alltypes.has_opt_submsg == true);
204 TEST(strcmp(alltypes.opt_submsg.substuff1, "3056") == 0);
205 TEST(alltypes.opt_submsg.substuff2 == 3056);
206 TEST(alltypes.opt_submsg.substuff3 == 3);
207 TEST(alltypes.has_opt_enum == true);
208 TEST(alltypes.opt_enum == MyEnum_Truth);
209 TEST(alltypes.has_opt_emptymsg == true);
210 TEST(alltypes.has_opt_fbytes == true);
211 TEST(memcmp(alltypes.opt_fbytes, "3059", 4) == 0);
212
213 TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
214 TEST(strcmp(alltypes.oneof.oneof_msg1.substuff1, "4059") == 0);
215 TEST(alltypes.oneof.oneof_msg1.substuff2 == 4059);
216 TEST(alltypes.oneof.oneof_msg1.substuff3 == 3);
217
218 TEST(alltypes.has_opt_non_zero_based_enum == true);
219 TEST(alltypes.opt_non_zero_based_enum == NonZeroBasedEnum_Three);
220 }
221 else if (mode == 2)
222 {
223 /* Expect zero values */
224 TEST(alltypes.req_int32 == 0);
225 TEST(alltypes.req_int64 == 0);
226 TEST(alltypes.req_uint32 == 0);
227 TEST(alltypes.req_uint64 == 0);
228 TEST(alltypes.req_sint32 == 0);
229 TEST(alltypes.req_sint64 == 0);
230 TEST(alltypes.req_bool == false);
231
232 TEST(alltypes.req_fixed32 == 0);
233 TEST(alltypes.req_sfixed32 == 0);
234 TEST(alltypes.req_float == 0.0f);
235
236 TEST(alltypes.req_fixed64 == 0);
237 TEST(alltypes.req_sfixed64 == 0);
238 TEST(alltypes.req_double == 0.0f);
239
240 TEST(strcmp(alltypes.req_string, "") == 0);
241 TEST(alltypes.req_bytes.size == 0);
242 TEST(strcmp(alltypes.req_submsg.substuff1, "") == 0);
243 TEST(alltypes.req_submsg.substuff2 == 0);
244 TEST(alltypes.req_enum == MyEnum_Zero);
245
246 TEST(alltypes.rep_int32_count == 0);
247 TEST(alltypes.rep_int64_count == 0);
248 TEST(alltypes.rep_uint32_count == 0);
249 TEST(alltypes.rep_uint64_count == 0);
250 TEST(alltypes.rep_sint32_count == 0);
251 TEST(alltypes.rep_sint64_count == 0);
252 TEST(alltypes.rep_bool_count == 0);
253
254 TEST(alltypes.rep_fixed32_count == 0);
255 TEST(alltypes.rep_sfixed32_count == 0);
256 TEST(alltypes.rep_float_count == 0);
257
258 TEST(alltypes.rep_fixed64_count == 0);
259 TEST(alltypes.rep_sfixed64_count == 0);
260 TEST(alltypes.rep_double_count == 0);
261
262 TEST(alltypes.rep_string_count == 0);
263 TEST(alltypes.rep_bytes_count == 0);
264
265 TEST(alltypes.rep_submsg_count == 0);
266
267 TEST(alltypes.rep_enum_count == 0);
268 TEST(alltypes.rep_emptymsg_count == 0);
269 TEST(alltypes.rep_fbytes_count == 0);
270 TEST(alltypes.rep_farray[0] == 0 && alltypes.rep_farray[4] == 0);
271
272 TEST(alltypes.has_opt_int32 == false);
273 TEST(alltypes.has_opt_int64 == false);
274 TEST(alltypes.has_opt_uint32 == false);
275 TEST(alltypes.has_opt_uint64 == false);
276 TEST(alltypes.has_opt_sint32 == false);
277 TEST(alltypes.has_opt_sint64 == false);
278 TEST(alltypes.has_opt_bool == false);
279
280 TEST(alltypes.has_opt_fixed32 == false);
281 TEST(alltypes.has_opt_sfixed32 == false);
282 TEST(alltypes.has_opt_float == false);
283
284 TEST(alltypes.has_opt_fixed64 == false);
285 TEST(alltypes.has_opt_sfixed64 == false);
286 TEST(alltypes.has_opt_double == false);
287
288 TEST(alltypes.has_opt_string == false);
289 TEST(alltypes.has_opt_bytes == false);
290 TEST(alltypes.has_opt_submsg == false);
291 TEST(alltypes.has_opt_enum == false);
292 TEST(alltypes.has_opt_emptymsg == false);
293 TEST(alltypes.has_opt_fbytes == false);
294
295 TEST(alltypes.which_oneof == 0);
296
297 TEST(alltypes.has_opt_non_zero_based_enum == false);
298 }
299
300 TEST(alltypes.end == 1099);
301
302 return status == 0;
303}
304
305#ifdef __cplusplus
306extern "C"
307#endif
308int main(int argc, char **argv)
309{
310 uint8_t buffer[1024];
311 size_t count;
312 pb_istream_t stream;
313
314 /* Whether to expect the optional values or the default values. */
315 int mode = (argc > 1) ? atoi(argv[1]) : 0;
316
317 /* Read the data into buffer */
319 count = fread(buffer, 1, sizeof(buffer), stdin);
320
321 /* Construct a pb_istream_t for reading from the buffer */
322 stream = pb_istream_from_buffer(buffer, count);
323
324 /* Decode and print out the stuff */
325 if (!check_alltypes(&stream, mode))
326 {
327 printf("Parsing failed: %s\n", PB_GET_ERROR(&stream));
328 return 1;
329 } else {
330 return 0;
331 }
332}
bool check_alltypes(pb_istream_t *stream, int mode)
#define AllTypes_init_default
#define AllTypes_fields
@ MyEnum_Zero
@ MyEnum_Truth
@ MyEnum_Second
@ HugeEnum_Negative
@ HugeEnum_Positive
MOSAPI int memcmp(const void *s1, const void *s2, size_t n)
Definition mos_string.c:142
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
size_t fread(void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
#define stdin
Definition mos_stdio.h:30
MOSAPI s32 atoi(const char *nptr)
Definition mos_stdlib.c:36
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
#define PB_GET_ERROR(stream)
Definition pb.h:891
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
unsigned char uint8_t
Definition pb_syshdr.h:20
int main()
Definition simple.cpp:6
pb_size_t rep_submsg_count
bool has_opt_double
int64_t req_sfixed64
bool has_opt_uint64
AllTypes_opt_bytes_t opt_bytes
bool has_opt_fixed32
pb_size_t rep_string_count
int32_t rep_int32[5]
int64_t opt_sint64
MyEnum rep_enum[5]
uint64_t opt_uint64
uint32_t rep_uint32[5]
AllTypes_req_bytes_t req_bytes
int32_t opt_int32
pb_size_t rep_double_count
bool has_opt_sfixed32
uint32_t opt_fixed32
int32_t rep_sfixed32[5]
int64_t opt_int64
bool has_opt_sint64
pb_extension_t * extensions
AllTypes_rep_bytes_t rep_bytes[5]
SubMessage opt_submsg
uint32_t req_fixed32
int32_t opt_sint32
bool has_opt_sint32
double rep_double[5]
pb_size_t rep_bool_count
pb_size_t rep_fixed32_count
int64_t req_int64
float rep_float[5]
bool rep_bool[5]
uint64_t req_uint64
bool has_opt_submsg
pb_size_t rep_int32_count
double opt_double
int64_t rep_sfixed64[5]
pb_size_t rep_float_count
char opt_string[16]
int32_t req_int32
bool has_opt_string
pb_size_t rep_sfixed32_count
uint32_t opt_uint32
double req_double
SubMessage rep_submsg[5]
pb_size_t rep_fixed64_count
pb_size_t rep_bytes_count
pb_size_t rep_enum_count
char req_string[16]
int64_t req_sint64
int64_t rep_sint64[5]
char rep_string[5][16]
int64_t rep_int64[5]
uint64_t rep_uint64[5]
int64_t opt_sfixed64
MyEnum opt_enum
int32_t req_sint32
SubMessage req_submsg
uint32_t req_uint32
uint64_t opt_fixed64
int32_t req_sfixed32
pb_size_t rep_uint64_count
pb_size_t rep_sint64_count
int32_t opt_sfixed32
MyEnum req_enum
pb_size_t rep_sfixed64_count
pb_size_t rep_uint32_count
pb_size_t rep_int64_count
bool has_opt_uint32
uint64_t rep_fixed64[5]
float req_float
uint32_t rep_fixed32[5]
bool has_opt_fixed64
pb_size_t rep_sint32_count
uint64_t req_fixed64
bool has_opt_sfixed64
int32_t rep_sint32[5]
char substuff1[16]
int32_t substuff2
uint32_t substuff3
#define TEST(x)
Definition test.h:3
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7