1 /* generate byte arrays that match the constructs in test-generated-code2.c.
2  * these are tested against eachother to make sure the c and c++ agree. */
3 
4 #define __STDC_LIMIT_MACROS
5 #include "t/test-full.pb.h"
6 #include <limits.h>
7 #  if defined(_MSC_VER)
8      /* On windows, in ms visual studio, define the types ourselves */
9 #    define int32_t      signed __int32
10 #    define INT32_MIN    _I32_MIN
11 #    define INT32_MAX    _I32_MAX
12 #    define uint32_t     unsigned __int32
13 #    define UINT32_MIN   _UI32_MIN
14 #    define UINT32_MAX   _UI32_MAX
15 #    define int64_t      signed __int64
16 #    define INT64_MIN    _I64_MIN
17 #    define INT64_MAX    _I64_MAX
18 #    define uint64_t     unsigned __int64
19 #    define UINT64_MIN   _UI64_MIN
20 #    define UINT64_MAX   _UI64_MAX
21 #    define uint8_t      unsigned char
22 #  else
23      /* Use the system inttypes.h */
24 #    include <inttypes.h>
25 #  endif
26 #include <stdio.h>
27 
28 using namespace foo;
29 
30 #define protobuf_c_boolean bool
31 #define TEST_ENUM_SMALL_TYPE_NAME  TestEnumSmall
32 #define TEST_ENUM_SMALL(NAME)      foo::NAME
33 #define TEST_ENUM_TYPE_NAME        TestEnum
34 #define TEST_ENUM(NAME)            foo::NAME
35 #include "common-test-arrays.h"
36 #define N_ELEMENTS(arr)   (sizeof(arr)/sizeof((arr)[0]))
37 
38 static void
dump_messages_bytes(size_t n_msgs,google::protobuf::Message ** messages,const char * label)39 dump_messages_bytes(size_t n_msgs,
40                     google::protobuf::Message **messages,
41                     const char *label)
42 {
43   printf ("static const uint8_t %s[] = { ", label);
44   for (unsigned m = 0; m < n_msgs; m++) {
45     std::string rv;
46     google::protobuf::Message *message = messages[m];
47     if (m)
48       printf (", ");
49     if (!message->SerializeToString(&rv))
50       assert(0);
51     unsigned char *bytes = (unsigned char *) rv.data();
52     for (unsigned i = 0; i < rv.size(); i++) {
53       if (i)
54         printf (", ");
55       printf ("0x%02x", bytes[i]);
56     }
57   }
58   printf (" };\n");
59 }
60 
61 static void
dump_message_bytes(google::protobuf::Message * message,const char * label)62 dump_message_bytes(google::protobuf::Message *message,
63                    const char *label)
64 {
65   dump_messages_bytes (1, &message, label);
66 }
67 
68 static void
dump_test_enum_small(void)69 dump_test_enum_small (void)
70 {
71   TestMessRequiredEnumSmall es;
72   es.set_test(NEG_VALUE);
73   dump_message_bytes(&es, "test_enum_small_NEG_VALUE");
74   es.set_test(VALUE);
75   dump_message_bytes(&es, "test_enum_small_VALUE");
76   es.set_test(OTHER_VALUE);
77   dump_message_bytes(&es, "test_enum_small_OTHER_VALUE");
78 }
79 static void
dump_test_enum_big(void)80 dump_test_enum_big (void)
81 {
82   TestMessRequiredEnum eb;
83   eb.set_test(VALUENEG123456); dump_message_bytes(&eb, "test_enum_big_VALUENEG123456");
84   eb.set_test(VALUENEG1); dump_message_bytes(&eb, "test_enum_big_VALUENEG1");
85   eb.set_test(VALUE0); dump_message_bytes(&eb, "test_enum_big_VALUE0");
86   eb.set_test(VALUE127); dump_message_bytes(&eb, "test_enum_big_VALUE127");
87   eb.set_test(VALUE128); dump_message_bytes(&eb, "test_enum_big_VALUE128");
88   eb.set_test(VALUE16383); dump_message_bytes(&eb, "test_enum_big_VALUE16383");
89   eb.set_test(VALUE16384); dump_message_bytes(&eb, "test_enum_big_VALUE16384");
90   eb.set_test(VALUE2097151); dump_message_bytes(&eb, "test_enum_big_VALUE2097151");
91   eb.set_test(VALUE2097152); dump_message_bytes(&eb, "test_enum_big_VALUE2097152");
92   eb.set_test(VALUE268435455); dump_message_bytes(&eb, "test_enum_big_VALUE268435455");
93   eb.set_test(VALUE268435456); dump_message_bytes(&eb, "test_enum_big_VALUE268435456");
94 }
95 
96 static void
dump_test_field_numbers(void)97 dump_test_field_numbers (void)
98 {
99 #define DUMP_ONE(num) \
100   { TestFieldNo##num f; \
101     f.set_test("tst"); \
102     dump_message_bytes(&f, "test_field_number_" #num); }
103   DUMP_ONE (15)
104   DUMP_ONE (16)
105   DUMP_ONE (2047)
106   DUMP_ONE (2048)
107   DUMP_ONE (262143)
108   DUMP_ONE (262144)
109   DUMP_ONE (33554431)
110   DUMP_ONE (33554432)
111 #undef DUMP_ONE
112 }
113 
dump_test_required_int32(void)114 static void dump_test_required_int32 (void)
115 {
116   TestMessRequiredInt32 mess;
117   mess.set_test (INT32_MIN);
118   dump_message_bytes (&mess, "test_required_int32_min");
119   mess.set_test (-1000);
120   dump_message_bytes (&mess, "test_required_int32_m1000");
121   mess.set_test (0);
122   dump_message_bytes (&mess, "test_required_int32_0");
123   mess.set_test (INT32_MAX);
124   dump_message_bytes (&mess, "test_required_int32_max");
125 }
126 
dump_test_required_sint32(void)127 static void dump_test_required_sint32 (void)
128 {
129   TestMessRequiredSInt32 mess;
130   mess.set_test (INT32_MIN);
131   dump_message_bytes (&mess, "test_required_sint32_min");
132   mess.set_test (-1000);
133   dump_message_bytes (&mess, "test_required_sint32_m1000");
134   mess.set_test (0);
135   dump_message_bytes (&mess, "test_required_sint32_0");
136   mess.set_test (INT32_MAX);
137   dump_message_bytes (&mess, "test_required_sint32_max");
138 }
139 
dump_test_required_sfixed32(void)140 static void dump_test_required_sfixed32 (void)
141 {
142   TestMessRequiredSFixed32 mess;
143   mess.set_test (INT32_MIN);
144   dump_message_bytes (&mess, "test_required_sfixed32_min");
145   mess.set_test (-1000);
146   dump_message_bytes (&mess, "test_required_sfixed32_m1000");
147   mess.set_test (0);
148   dump_message_bytes (&mess, "test_required_sfixed32_0");
149   mess.set_test (INT32_MAX);
150   dump_message_bytes (&mess, "test_required_sfixed32_max");
151 }
152 
dump_test_required_uint32(void)153 static void dump_test_required_uint32 (void)
154 {
155   TestMessRequiredUInt32 mess;
156   mess.set_test (0);
157   dump_message_bytes (&mess, "test_required_uint32_0");
158   mess.set_test (MILLION);
159   dump_message_bytes (&mess, "test_required_uint32_million");
160   mess.set_test (UINT32_MAX);
161   dump_message_bytes (&mess, "test_required_uint32_max");
162 }
163 
dump_test_required_fixed32(void)164 static void dump_test_required_fixed32 (void)
165 {
166   TestMessRequiredFixed32 mess;
167   mess.set_test (0);
168   dump_message_bytes (&mess, "test_required_fixed32_0");
169   mess.set_test (MILLION);
170   dump_message_bytes (&mess, "test_required_fixed32_million");
171   mess.set_test (UINT32_MAX);
172   dump_message_bytes (&mess, "test_required_fixed32_max");
173 }
174 
dump_test_required_int64(void)175 static void dump_test_required_int64 (void)
176 {
177   TestMessRequiredInt64 mess;
178   mess.set_test (INT64_MIN);
179   dump_message_bytes (&mess, "test_required_int64_min");
180   mess.set_test (-TRILLION);
181   dump_message_bytes (&mess, "test_required_int64_mtril");
182   mess.set_test (0);
183   dump_message_bytes (&mess, "test_required_int64_0");
184   mess.set_test (QUADRILLION);
185   dump_message_bytes (&mess, "test_required_int64_quad");
186   mess.set_test (INT64_MAX);
187   dump_message_bytes (&mess, "test_required_int64_max");
188 }
189 
dump_test_required_sint64(void)190 static void dump_test_required_sint64 (void)
191 {
192   TestMessRequiredSInt64 mess;
193   mess.set_test (INT64_MIN);
194   dump_message_bytes (&mess, "test_required_sint64_min");
195   mess.set_test (-TRILLION);
196   dump_message_bytes (&mess, "test_required_sint64_mtril");
197   mess.set_test (0);
198   dump_message_bytes (&mess, "test_required_sint64_0");
199   mess.set_test (QUADRILLION);
200   dump_message_bytes (&mess, "test_required_sint64_quad");
201   mess.set_test (INT64_MAX);
202   dump_message_bytes (&mess, "test_required_sint64_max");
203 }
204 
dump_test_required_sfixed64(void)205 static void dump_test_required_sfixed64 (void)
206 {
207   TestMessRequiredSFixed64 mess;
208   mess.set_test (INT64_MIN);
209   dump_message_bytes (&mess, "test_required_sfixed64_min");
210   mess.set_test (-TRILLION);
211   dump_message_bytes (&mess, "test_required_sfixed64_mtril");
212   mess.set_test (0);
213   dump_message_bytes (&mess, "test_required_sfixed64_0");
214   mess.set_test (QUADRILLION);
215   dump_message_bytes (&mess, "test_required_sfixed64_quad");
216   mess.set_test (INT64_MAX);
217   dump_message_bytes (&mess, "test_required_sfixed64_max");
218 }
219 
dump_test_required_uint64(void)220 static void dump_test_required_uint64 (void)
221 {
222   TestMessRequiredUInt64 mess;
223   mess.set_test (0);
224   dump_message_bytes (&mess, "test_required_uint64_0");
225   mess.set_test (THOUSAND);
226   dump_message_bytes (&mess, "test_required_uint64_thou");
227   mess.set_test (MILLION);
228   dump_message_bytes (&mess, "test_required_uint64_mill");
229   mess.set_test (BILLION);
230   dump_message_bytes (&mess, "test_required_uint64_bill");
231   mess.set_test (BILLION*20);
232   dump_message_bytes (&mess, "test_required_uint64_20_bill");
233   mess.set_test (TRILLION);
234   dump_message_bytes (&mess, "test_required_uint64_tril");
235   mess.set_test (TRILLION*20);
236   dump_message_bytes (&mess, "test_required_uint64_20_tril");
237   mess.set_test (QUADRILLION);
238   dump_message_bytes (&mess, "test_required_uint64_quad");
239   mess.set_test (QUINTILLION);
240   dump_message_bytes (&mess, "test_required_uint64_quint");
241   mess.set_test (UINT64_MAX);
242   dump_message_bytes (&mess, "test_required_uint64_max");
243 }
244 
dump_test_required_fixed64(void)245 static void dump_test_required_fixed64 (void)
246 {
247   TestMessRequiredFixed64 mess;
248   mess.set_test (0);
249   dump_message_bytes (&mess, "test_required_fixed64_0");
250   mess.set_test (THOUSAND);
251   dump_message_bytes (&mess, "test_required_fixed64_thou");
252   mess.set_test (MILLION);
253   dump_message_bytes (&mess, "test_required_fixed64_mill");
254   mess.set_test (BILLION);
255   dump_message_bytes (&mess, "test_required_fixed64_bill");
256   mess.set_test (TRILLION);
257   dump_message_bytes (&mess, "test_required_fixed64_tril");
258   mess.set_test (QUADRILLION);
259   dump_message_bytes (&mess, "test_required_fixed64_quad");
260   mess.set_test (QUINTILLION);
261   dump_message_bytes (&mess, "test_required_fixed64_quint");
262   mess.set_test (UINT64_MAX);
263   dump_message_bytes (&mess, "test_required_fixed64_max");
264 }
265 
dump_test_required_float(void)266 static void dump_test_required_float (void)
267 {
268   TestMessRequiredFloat mess;
269   mess.set_test(-THOUSAND);
270   dump_message_bytes (&mess, "test_required_float_mthou");
271   mess.set_test(0);
272   dump_message_bytes (&mess, "test_required_float_0");
273   mess.set_test(420);
274   dump_message_bytes (&mess, "test_required_float_420");
275 }
276 
dump_test_required_double(void)277 static void dump_test_required_double (void)
278 {
279   TestMessRequiredDouble mess;
280   mess.set_test(-THOUSAND);
281   dump_message_bytes (&mess, "test_required_double_mthou");
282   mess.set_test(0);
283   dump_message_bytes (&mess, "test_required_double_0");
284   mess.set_test(420);
285   dump_message_bytes (&mess, "test_required_double_420");
286 }
287 
dump_test_required_bool(void)288 static void dump_test_required_bool (void)
289 {
290   TestMessRequiredBool mess;
291   mess.set_test(false);
292   dump_message_bytes (&mess, "test_required_bool_0");
293   mess.set_test(true);
294   dump_message_bytes (&mess, "test_required_bool_1");
295 }
296 
dump_test_required_enum_small(void)297 static void dump_test_required_enum_small (void)
298 {
299   TestMessRequiredEnumSmall mess;
300   mess.set_test(VALUE);
301   dump_message_bytes (&mess, "test_required_enum_small_VALUE");
302   mess.set_test(OTHER_VALUE);
303   dump_message_bytes (&mess, "test_required_enum_small_OTHER_VALUE");
304 }
305 
dump_test_required_enum(void)306 static void dump_test_required_enum (void)
307 {
308   TestMessRequiredEnum mess;
309   mess.set_test (VALUENEG1);
310   dump_message_bytes (&mess, "test_required_enum_neg1");
311   mess.set_test (VALUE0);
312   dump_message_bytes (&mess, "test_required_enum_0");
313   mess.set_test (VALUE1);
314   dump_message_bytes (&mess, "test_required_enum_1");
315   mess.set_test (VALUE127);
316   dump_message_bytes (&mess, "test_required_enum_127");
317   mess.set_test (VALUE128);
318   dump_message_bytes (&mess, "test_required_enum_128");
319   mess.set_test (VALUE16383);
320   dump_message_bytes (&mess, "test_required_enum_16383");
321   mess.set_test (VALUE16384);
322   dump_message_bytes (&mess, "test_required_enum_16384");
323   mess.set_test (VALUE2097151);
324   dump_message_bytes (&mess, "test_required_enum_2097151");
325   mess.set_test (VALUE2097152);
326   dump_message_bytes (&mess, "test_required_enum_2097152");
327   mess.set_test (VALUE268435455);
328   dump_message_bytes (&mess, "test_required_enum_268435455");
329   mess.set_test (VALUE268435456);
330   dump_message_bytes (&mess, "test_required_enum_268435456");
331 }
332 
dump_test_required_string(void)333 static void dump_test_required_string (void)
334 {
335   TestMessRequiredString mess;
336   mess.set_test ("");
337   dump_message_bytes(&mess, "test_required_string_empty");
338   mess.set_test ("hello");
339   dump_message_bytes(&mess, "test_required_string_hello");
340   mess.set_test ("two hundred xs follow: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
341   dump_message_bytes(&mess, "test_required_string_long");
342 }
343 
dump_test_required_bytes(void)344 static void dump_test_required_bytes (void)
345 {
346   TestMessRequiredBytes mess;
347   mess.set_test ("");
348   dump_message_bytes (&mess, "test_required_bytes_empty");
349   mess.set_test ("hello");
350   dump_message_bytes (&mess, "test_required_bytes_hello");
351   mess.set_test (std::string("\1") + '\0' + "\375\2\4");
352   dump_message_bytes (&mess, "test_required_bytes_random");
353 }
354 
dump_test_required_message(void)355 static void dump_test_required_message (void)
356 {
357   TestMessRequiredMessage mess;
358   mess.mutable_test()->set_test(0);
359   dump_message_bytes (&mess, "test_required_submess_0");
360   mess.mutable_test()->set_test(42);
361   dump_message_bytes (&mess, "test_required_submess_42");
362 }
363 
dump_test_optional_int32(void)364 static void dump_test_optional_int32 (void)
365 {
366   TestMessOptional opt;
367   opt.set_test_int32 (INT32_MIN);
368   dump_message_bytes (&opt, "test_optional_int32_min");
369   opt.set_test_int32 (-1);
370   dump_message_bytes (&opt, "test_optional_int32_m1");
371   opt.set_test_int32 (0);
372   dump_message_bytes (&opt, "test_optional_int32_0");
373   opt.set_test_int32 (666);
374   dump_message_bytes (&opt, "test_optional_int32_666");
375   opt.set_test_int32 (INT32_MAX);
376   dump_message_bytes (&opt, "test_optional_int32_max");
377 }
378 
dump_test_optional_sint32(void)379 static void dump_test_optional_sint32 (void)
380 {
381   TestMessOptional opt;
382   opt.set_test_sint32 (INT32_MIN);
383   dump_message_bytes (&opt, "test_optional_sint32_min");
384   opt.set_test_sint32 (-1);
385   dump_message_bytes (&opt, "test_optional_sint32_m1");
386   opt.set_test_sint32 (0);
387   dump_message_bytes (&opt, "test_optional_sint32_0");
388   opt.set_test_sint32 (666);
389   dump_message_bytes (&opt, "test_optional_sint32_666");
390   opt.set_test_sint32 (INT32_MAX);
391   dump_message_bytes (&opt, "test_optional_sint32_max");
392 }
393 
dump_test_optional_sfixed32(void)394 static void dump_test_optional_sfixed32 (void)
395 {
396   TestMessOptional opt;
397   opt.set_test_sfixed32 (INT32_MIN);
398   dump_message_bytes (&opt, "test_optional_sfixed32_min");
399   opt.set_test_sfixed32 (-1);
400   dump_message_bytes (&opt, "test_optional_sfixed32_m1");
401   opt.set_test_sfixed32 (0);
402   dump_message_bytes (&opt, "test_optional_sfixed32_0");
403   opt.set_test_sfixed32 (666);
404   dump_message_bytes (&opt, "test_optional_sfixed32_666");
405   opt.set_test_sfixed32 (INT32_MAX);
406   dump_message_bytes (&opt, "test_optional_sfixed32_max");
407 }
408 
dump_test_optional_int64(void)409 static void dump_test_optional_int64 (void)
410 {
411   TestMessOptional opt;
412   opt.set_test_int64 (INT64_MIN);
413   dump_message_bytes (&opt, "test_optional_int64_min");
414   opt.set_test_int64 (-1111111111LL);
415   dump_message_bytes (&opt, "test_optional_int64_m1111111111LL");
416   opt.set_test_int64 (0);
417   dump_message_bytes (&opt, "test_optional_int64_0");
418   opt.set_test_int64 (QUINTILLION);
419   dump_message_bytes (&opt, "test_optional_int64_quintillion");
420   opt.set_test_int64 (INT64_MAX);
421   dump_message_bytes (&opt, "test_optional_int64_max");
422 }
dump_test_optional_sint64(void)423 static void dump_test_optional_sint64 (void)
424 {
425   TestMessOptional opt;
426   opt.set_test_sint64 (INT64_MIN);
427   dump_message_bytes (&opt, "test_optional_sint64_min");
428   opt.set_test_sint64 (-1111111111LL);
429   dump_message_bytes (&opt, "test_optional_sint64_m1111111111LL");
430   opt.set_test_sint64 (0);
431   dump_message_bytes (&opt, "test_optional_sint64_0");
432   opt.set_test_sint64 (QUINTILLION);
433   dump_message_bytes (&opt, "test_optional_sint64_quintillion");
434   opt.set_test_sint64 (INT64_MAX);
435   dump_message_bytes (&opt, "test_optional_sint64_max");
436 }
dump_test_optional_sfixed64(void)437 static void dump_test_optional_sfixed64 (void)
438 {
439   TestMessOptional opt;
440   opt.set_test_sfixed64 (INT64_MIN);
441   dump_message_bytes (&opt, "test_optional_sfixed64_min");
442   opt.set_test_sfixed64 (-1111111111LL);
443   dump_message_bytes (&opt, "test_optional_sfixed64_m1111111111LL");
444   opt.set_test_sfixed64 (0);
445   dump_message_bytes (&opt, "test_optional_sfixed64_0");
446   opt.set_test_sfixed64 (QUINTILLION);
447   dump_message_bytes (&opt, "test_optional_sfixed64_quintillion");
448   opt.set_test_sfixed64 (INT64_MAX);
449   dump_message_bytes (&opt, "test_optional_sfixed64_max");
450 }
dump_test_optional_uint32(void)451 static void dump_test_optional_uint32 (void)
452 {
453   TestMessOptional opt;
454   opt.set_test_uint32(0);
455   dump_message_bytes (&opt, "test_optional_uint32_0");
456   opt.set_test_uint32(669);
457   dump_message_bytes (&opt, "test_optional_uint32_669");
458   opt.set_test_uint32(UINT32_MAX);
459   dump_message_bytes (&opt, "test_optional_uint32_max");
460 }
dump_test_optional_fixed32(void)461 static void dump_test_optional_fixed32 (void)
462 {
463   TestMessOptional opt;
464   opt.set_test_fixed32(0);
465   dump_message_bytes (&opt, "test_optional_fixed32_0");
466   opt.set_test_fixed32(669);
467   dump_message_bytes (&opt, "test_optional_fixed32_669");
468   opt.set_test_fixed32(UINT32_MAX);
469   dump_message_bytes (&opt, "test_optional_fixed32_max");
470 }
471 
dump_test_optional_uint64(void)472 static void dump_test_optional_uint64 (void)
473 {
474   TestMessOptional opt;
475   opt.set_test_uint64(0);
476   dump_message_bytes (&opt, "test_optional_uint64_0");
477   opt.set_test_uint64(669669669669669ULL);
478   dump_message_bytes (&opt, "test_optional_uint64_669669669669669");
479   opt.set_test_uint64(UINT64_MAX);
480   dump_message_bytes (&opt, "test_optional_uint64_max");
481 }
482 
dump_test_optional_fixed64(void)483 static void dump_test_optional_fixed64 (void)
484 {
485   TestMessOptional opt;
486   opt.set_test_fixed64(0);
487   dump_message_bytes (&opt, "test_optional_fixed64_0");
488   opt.set_test_fixed64(669669669669669ULL);
489   dump_message_bytes (&opt, "test_optional_fixed64_669669669669669");
490   opt.set_test_fixed64(UINT64_MAX);
491   dump_message_bytes (&opt, "test_optional_fixed64_max");
492 }
493 
dump_test_optional_float(void)494 static void dump_test_optional_float (void)
495 {
496   TestMessOptional opt;
497   opt.set_test_float (-100);
498   dump_message_bytes (&opt, "test_optional_float_m100");
499   opt.set_test_float (0);
500   dump_message_bytes (&opt, "test_optional_float_0");
501   opt.set_test_float (141243);
502   dump_message_bytes (&opt, "test_optional_float_141243");
503 }
504 
dump_test_optional_double(void)505 static void dump_test_optional_double (void)
506 {
507   TestMessOptional opt;
508   opt.set_test_double (-100);
509   dump_message_bytes (&opt, "test_optional_double_m100");
510   opt.set_test_double (0);
511   dump_message_bytes (&opt, "test_optional_double_0");
512   opt.set_test_double (141243);
513   dump_message_bytes (&opt, "test_optional_double_141243");
514 }
515 
dump_test_optional_bool(void)516 static void dump_test_optional_bool (void)
517 {
518   TestMessOptional opt;
519   opt.set_test_boolean (false);
520   dump_message_bytes (&opt, "test_optional_bool_0");
521   opt.set_test_boolean (true);
522   dump_message_bytes (&opt, "test_optional_bool_1");
523 }
524 
dump_test_optional_enum_small(void)525 static void dump_test_optional_enum_small (void)
526 {
527   TestMessOptional opt;
528   opt.set_test_enum_small (NEG_VALUE);
529   dump_message_bytes (&opt, "test_optional_enum_small_neg1");
530   opt.set_test_enum_small (VALUE);
531   dump_message_bytes (&opt, "test_optional_enum_small_0");
532   opt.set_test_enum_small (OTHER_VALUE);
533   dump_message_bytes (&opt, "test_optional_enum_small_1");
534 }
dump_test_optional_enum(void)535 static void dump_test_optional_enum (void)
536 {
537   TestMessOptional opt;
538 //for a in 0 1 127 128 16383 16384 2097151 2097152 268435455 268435456 ; do
539 //echo '    opt.set_test_enum (VALUE'$a');
540 //dump_message_bytes (&opt, "test_optional_enum_'$a'");'
541 //done
542     opt.set_test_enum (VALUENEG1);
543     dump_message_bytes (&opt, "test_optional_enum_neg1");
544     opt.set_test_enum (VALUE0);
545     dump_message_bytes (&opt, "test_optional_enum_0");
546     opt.set_test_enum (VALUE1);
547     dump_message_bytes (&opt, "test_optional_enum_1");
548     opt.set_test_enum (VALUE127);
549     dump_message_bytes (&opt, "test_optional_enum_127");
550     opt.set_test_enum (VALUE128);
551     dump_message_bytes (&opt, "test_optional_enum_128");
552     opt.set_test_enum (VALUE16383);
553     dump_message_bytes (&opt, "test_optional_enum_16383");
554     opt.set_test_enum (VALUE16384);
555     dump_message_bytes (&opt, "test_optional_enum_16384");
556     opt.set_test_enum (VALUE2097151);
557     dump_message_bytes (&opt, "test_optional_enum_2097151");
558     opt.set_test_enum (VALUE2097152);
559     dump_message_bytes (&opt, "test_optional_enum_2097152");
560     opt.set_test_enum (VALUE268435455);
561     dump_message_bytes (&opt, "test_optional_enum_268435455");
562     opt.set_test_enum (VALUE268435456);
563     dump_message_bytes (&opt, "test_optional_enum_268435456");
564 }
dump_test_optional_string(void)565 static void dump_test_optional_string (void)
566 {
567   TestMessOptional opt;
568   opt.set_test_string ("");
569   dump_message_bytes (&opt, "test_optional_string_empty");
570   opt.set_test_string ("hello");
571   dump_message_bytes (&opt, "test_optional_string_hello");
572 }
dump_test_optional_bytes(void)573 static void dump_test_optional_bytes (void)
574 {
575   TestMessOptional opt;
576   opt.set_test_bytes ("");
577   dump_message_bytes (&opt, "test_optional_bytes_empty");
578   opt.set_test_bytes ("hello");
579   dump_message_bytes (&opt, "test_optional_bytes_hello");
580   opt.set_test_bytes (std::string("\1") + '\0' + "\375\2\4");
581   dump_message_bytes (&opt, "test_optional_bytes_random");
582 }
dump_test_optional_message(void)583 static void dump_test_optional_message (void)
584 {
585   TestMessOptional opt;
586   opt.mutable_test_message()->set_test(0);
587   dump_message_bytes (&opt, "test_optional_submess_0");
588   opt.mutable_test_message()->set_test(42);
589   dump_message_bytes (&opt, "test_optional_submess_42");
590 }
dump_test_oneof_merge(void)591 static void dump_test_oneof_merge (void)
592 {
593 #define SWAP(a, b) temp = a, a = b, b = temp
594   google::protobuf::Message *temp;
595   TestMessOptional opt[6];
596   google::protobuf::Message *msgs[6] = { &opt[0], &opt[1], &opt[2], &opt[3],
597     &opt[4], &opt[5] };
598   opt[0].set_test_bytes ("hello");
599   opt[1].mutable_test_message()->set_test (42);
600   opt[2].set_test_string ("");
601   opt[3].set_test_int32 (666);
602   opt[4].set_test_float (333);
603   opt[5].set_test_double (444455555);
604   dump_messages_bytes (6, msgs, "test_oneof_merge_double");
605   SWAP (msgs[5], msgs[4]);
606   dump_messages_bytes (6, msgs, "test_oneof_merge_float");
607   SWAP (msgs[5], msgs[3]);
608   dump_messages_bytes (6, msgs, "test_oneof_merge_int32");
609   SWAP (msgs[5], msgs[2]);
610   dump_messages_bytes (6, msgs, "test_oneof_merge_string");
611   SWAP (msgs[5], msgs[1]);
612   dump_messages_bytes (6, msgs, "test_oneof_merge_submess");
613   SWAP (msgs[5], msgs[0]);
614   dump_messages_bytes (6, msgs, "test_oneof_merge_bytes");
615 
616 #undef SWAP
617 }
618 
619 #define DUMP_STATIC_ARRAY_GENERIC(member, static_array, output_array_name) \
620   do{ \
621     TestMess mess; \
622     for (unsigned i = 0; i < N_ELEMENTS (static_array); i++) \
623       mess.add_##member(static_array[i]); \
624     dump_message_bytes(&mess, output_array_name); \
625   }while(0)
dump_test_repeated_int32(void)626 static void dump_test_repeated_int32 (void)
627 {
628 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
629   DUMP_STATIC_ARRAY_GENERIC(test_int32, static_array, output_array_name)
630 
631   DUMP_STATIC_ARRAY (int32_arr0, "test_repeated_int32_arr0");
632   DUMP_STATIC_ARRAY (int32_arr1, "test_repeated_int32_arr1");
633   DUMP_STATIC_ARRAY (int32_arr_min_max, "test_repeated_int32_arr_min_max");
634 
635 #undef DUMP_STATIC_ARRAY
636 }
dump_test_repeated_sint32(void)637 static void dump_test_repeated_sint32 (void)
638 {
639 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
640   DUMP_STATIC_ARRAY_GENERIC(test_sint32, static_array, output_array_name)
641 
642   DUMP_STATIC_ARRAY (int32_arr0, "test_repeated_sint32_arr0");
643   DUMP_STATIC_ARRAY (int32_arr1, "test_repeated_sint32_arr1");
644   DUMP_STATIC_ARRAY (int32_arr_min_max, "test_repeated_sint32_arr_min_max");
645 
646 #undef DUMP_STATIC_ARRAY
647 }
dump_test_repeated_uint32(void)648 static void dump_test_repeated_uint32 (void)
649 {
650 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
651   DUMP_STATIC_ARRAY_GENERIC(test_uint32, static_array, output_array_name)
652 
653   DUMP_STATIC_ARRAY (uint32_roundnumbers, "test_repeated_uint32_roundnumbers");
654   DUMP_STATIC_ARRAY (uint32_0_max, "test_repeated_uint32_0_max");
655 
656 #undef DUMP_STATIC_ARRAY
657 }
dump_test_repeated_sfixed32(void)658 static void dump_test_repeated_sfixed32 (void)
659 {
660 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
661   DUMP_STATIC_ARRAY_GENERIC(test_sfixed32, static_array, output_array_name)
662 
663   DUMP_STATIC_ARRAY (int32_arr0, "test_repeated_sfixed32_arr0");
664   DUMP_STATIC_ARRAY (int32_arr1, "test_repeated_sfixed32_arr1");
665   DUMP_STATIC_ARRAY (int32_arr_min_max, "test_repeated_sfixed32_arr_min_max");
666 
667 #undef DUMP_STATIC_ARRAY
668 }
669 
dump_test_repeated_fixed32(void)670 static void dump_test_repeated_fixed32 (void)
671 {
672 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
673   DUMP_STATIC_ARRAY_GENERIC(test_fixed32, static_array, output_array_name)
674 
675   DUMP_STATIC_ARRAY (uint32_roundnumbers, "test_repeated_fixed32_roundnumbers");
676   DUMP_STATIC_ARRAY (uint32_0_max, "test_repeated_fixed32_0_max");
677 
678 #undef DUMP_STATIC_ARRAY
679 }
dump_test_repeated_int64(void)680 static void dump_test_repeated_int64 (void)
681 {
682 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
683   DUMP_STATIC_ARRAY_GENERIC(test_int64, static_array, output_array_name)
684 
685   DUMP_STATIC_ARRAY (int64_roundnumbers, "test_repeated_int64_roundnumbers");
686   DUMP_STATIC_ARRAY (int64_min_max, "test_repeated_int64_min_max");
687 
688 #undef DUMP_STATIC_ARRAY
689 }
690 
dump_test_repeated_sint64(void)691 static void dump_test_repeated_sint64 (void)
692 {
693 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
694   DUMP_STATIC_ARRAY_GENERIC(test_sint64, static_array, output_array_name)
695 
696   DUMP_STATIC_ARRAY (int64_roundnumbers, "test_repeated_sint64_roundnumbers");
697   DUMP_STATIC_ARRAY (int64_min_max, "test_repeated_sint64_min_max");
698 
699 #undef DUMP_STATIC_ARRAY
700 }
701 
dump_test_repeated_sfixed64(void)702 static void dump_test_repeated_sfixed64 (void)
703 {
704 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
705   DUMP_STATIC_ARRAY_GENERIC(test_sfixed64, static_array, output_array_name)
706 
707   DUMP_STATIC_ARRAY (int64_roundnumbers, "test_repeated_sfixed64_roundnumbers");
708   DUMP_STATIC_ARRAY (int64_min_max, "test_repeated_sfixed64_min_max");
709 
710 #undef DUMP_STATIC_ARRAY
711 }
dump_test_repeated_uint64(void)712 static void dump_test_repeated_uint64 (void)
713 {
714 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
715   DUMP_STATIC_ARRAY_GENERIC (test_uint64, static_array, output_array_name)
716 
717   DUMP_STATIC_ARRAY(uint64_roundnumbers, "test_repeated_uint64_roundnumbers");
718   DUMP_STATIC_ARRAY(uint64_0_1_max, "test_repeated_uint64_0_1_max");
719   DUMP_STATIC_ARRAY(uint64_random, "test_repeated_uint64_random");
720 
721 #undef DUMP_STATIC_ARRAY
722 }
dump_test_repeated_fixed64(void)723 static void dump_test_repeated_fixed64 (void)
724 {
725 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
726   DUMP_STATIC_ARRAY_GENERIC(test_fixed64, static_array, output_array_name)
727 
728   DUMP_STATIC_ARRAY(uint64_roundnumbers, "test_repeated_fixed64_roundnumbers");
729   DUMP_STATIC_ARRAY(uint64_0_1_max, "test_repeated_fixed64_0_1_max");
730   DUMP_STATIC_ARRAY(uint64_random, "test_repeated_fixed64_random");
731 
732 #undef DUMP_STATIC_ARRAY
733 }
734 
dump_test_repeated_float(void)735 static void dump_test_repeated_float (void)
736 {
737 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
738   DUMP_STATIC_ARRAY_GENERIC(test_float, static_array, output_array_name)
739 
740   DUMP_STATIC_ARRAY(float_random, "test_repeated_float_random");
741 
742 #undef DUMP_STATIC_ARRAY
743 }
744 
dump_test_repeated_double(void)745 static void dump_test_repeated_double (void)
746 {
747 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
748   DUMP_STATIC_ARRAY_GENERIC(test_double, static_array, output_array_name)
749 
750   DUMP_STATIC_ARRAY(double_random, "test_repeated_double_random");
751 
752 #undef DUMP_STATIC_ARRAY
753 }
dump_test_repeated_boolean(void)754 static void dump_test_repeated_boolean (void)
755 {
756 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
757   DUMP_STATIC_ARRAY_GENERIC(test_boolean, static_array, output_array_name)
758 
759   DUMP_STATIC_ARRAY(boolean_0, "test_repeated_boolean_0");
760   DUMP_STATIC_ARRAY(boolean_1, "test_repeated_boolean_1");
761   DUMP_STATIC_ARRAY(boolean_random, "test_repeated_boolean_random");
762 
763 #undef DUMP_STATIC_ARRAY
764 }
dump_test_repeated_enum_small(void)765 static void dump_test_repeated_enum_small (void)
766 {
767 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
768   DUMP_STATIC_ARRAY_GENERIC (test_enum_small, static_array, output_array_name)
769 
770   DUMP_STATIC_ARRAY (enum_small_0, "test_repeated_enum_small_0");
771   DUMP_STATIC_ARRAY (enum_small_1, "test_repeated_enum_small_1");
772   DUMP_STATIC_ARRAY (enum_small_random, "test_repeated_enum_small_random");
773 
774 #undef DUMP_STATIC_ARRAY
775 }
776 
dump_test_repeated_enum(void)777 static void dump_test_repeated_enum (void)
778 {
779 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
780   DUMP_STATIC_ARRAY_GENERIC (test_enum, static_array, output_array_name)
781 
782   DUMP_STATIC_ARRAY (enum_0, "test_repeated_enum_0");
783   DUMP_STATIC_ARRAY (enum_1, "test_repeated_enum_1");
784   DUMP_STATIC_ARRAY (enum_random, "test_repeated_enum_random");
785 
786 #undef DUMP_STATIC_ARRAY
787 }
788 
dump_test_repeated_string(void)789 static void dump_test_repeated_string (void)
790 {
791 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
792   DUMP_STATIC_ARRAY_GENERIC (test_string, static_array, output_array_name)
793 
794   DUMP_STATIC_ARRAY (repeated_strings_0, "test_repeated_strings_0");
795   DUMP_STATIC_ARRAY (repeated_strings_1, "test_repeated_strings_1");
796   DUMP_STATIC_ARRAY (repeated_strings_2, "test_repeated_strings_2");
797   DUMP_STATIC_ARRAY (repeated_strings_3, "test_repeated_strings_3");
798 
799 #undef DUMP_STATIC_ARRAY
800 }
801 
dump_test_repeated_bytes(void)802 static void dump_test_repeated_bytes (void)
803 {
804   TestMess mess;
805   mess.add_test_bytes(std::string("text"));
806   mess.add_test_bytes(std::string("str\1\2\3\4\5") + '\0');
807   mess.add_test_bytes(std::string("gobble") + '\0' + "foo");
808   dump_message_bytes(&mess, "test_repeated_bytes_0");
809 }
810 
dump_test_repeated_SubMess(void)811 static void dump_test_repeated_SubMess (void)
812 {
813   TestMess mess;
814   mess.add_test_message()->set_test(0);
815   mess.add_test_message()->set_test(0);
816   mess.add_test_message()->set_test(0);
817   dump_message_bytes(&mess, "test_repeated_submess_0");
818 
819   mess.clear_test_message();
820   mess.add_test_message()->set_test(42);
821   mess.add_test_message()->set_test(-10000);
822   mess.add_test_message()->set_test(667);
823   dump_message_bytes(&mess, "test_repeated_submess_1");
824 }
825 #undef DUMP_STATIC_ARRAY_GENERIC
826 
827 #define DUMP_STATIC_ARRAY_GENERIC(member, static_array, output_array_name) \
828   do{ \
829     TestMessPacked mess; \
830     for (unsigned i = 0; i < N_ELEMENTS (static_array); i++) \
831       mess.add_##member(static_array[i]); \
832     dump_message_bytes(&mess, output_array_name); \
833   }while(0)
dump_test_packed_repeated_int32(void)834 static void dump_test_packed_repeated_int32 (void)
835 {
836 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
837   DUMP_STATIC_ARRAY_GENERIC(test_int32, static_array, output_array_name)
838 
839   DUMP_STATIC_ARRAY (int32_arr0, "test_packed_repeated_int32_arr0");
840   DUMP_STATIC_ARRAY (int32_arr1, "test_packed_repeated_int32_arr1");
841   DUMP_STATIC_ARRAY (int32_arr_min_max, "test_packed_repeated_int32_arr_min_max");
842 
843 #undef DUMP_STATIC_ARRAY
844 }
dump_test_packed_repeated_sint32(void)845 static void dump_test_packed_repeated_sint32 (void)
846 {
847 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
848   DUMP_STATIC_ARRAY_GENERIC(test_sint32, static_array, output_array_name)
849 
850   DUMP_STATIC_ARRAY (int32_arr0, "test_packed_repeated_sint32_arr0");
851   DUMP_STATIC_ARRAY (int32_arr1, "test_packed_repeated_sint32_arr1");
852   DUMP_STATIC_ARRAY (int32_arr_min_max, "test_packed_repeated_sint32_arr_min_max");
853 
854 #undef DUMP_STATIC_ARRAY
855 }
dump_test_packed_repeated_uint32(void)856 static void dump_test_packed_repeated_uint32 (void)
857 {
858 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
859   DUMP_STATIC_ARRAY_GENERIC(test_uint32, static_array, output_array_name)
860 
861   DUMP_STATIC_ARRAY (uint32_roundnumbers, "test_packed_repeated_uint32_roundnumbers");
862   DUMP_STATIC_ARRAY (uint32_0_max, "test_packed_repeated_uint32_0_max");
863 
864 #undef DUMP_STATIC_ARRAY
865 }
dump_test_packed_repeated_sfixed32(void)866 static void dump_test_packed_repeated_sfixed32 (void)
867 {
868 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
869   DUMP_STATIC_ARRAY_GENERIC(test_sfixed32, static_array, output_array_name)
870 
871   DUMP_STATIC_ARRAY (int32_arr0, "test_packed_repeated_sfixed32_arr0");
872   DUMP_STATIC_ARRAY (int32_arr1, "test_packed_repeated_sfixed32_arr1");
873   DUMP_STATIC_ARRAY (int32_arr_min_max, "test_packed_repeated_sfixed32_arr_min_max");
874 
875 #undef DUMP_STATIC_ARRAY
876 }
877 
dump_test_packed_repeated_fixed32(void)878 static void dump_test_packed_repeated_fixed32 (void)
879 {
880 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
881   DUMP_STATIC_ARRAY_GENERIC(test_fixed32, static_array, output_array_name)
882 
883   DUMP_STATIC_ARRAY (uint32_roundnumbers, "test_packed_repeated_fixed32_roundnumbers");
884   DUMP_STATIC_ARRAY (uint32_0_max, "test_packed_repeated_fixed32_0_max");
885 
886 #undef DUMP_STATIC_ARRAY
887 }
dump_test_packed_repeated_int64(void)888 static void dump_test_packed_repeated_int64 (void)
889 {
890 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
891   DUMP_STATIC_ARRAY_GENERIC(test_int64, static_array, output_array_name)
892 
893   DUMP_STATIC_ARRAY (int64_roundnumbers, "test_packed_repeated_int64_roundnumbers");
894   DUMP_STATIC_ARRAY (int64_min_max, "test_packed_repeated_int64_min_max");
895 
896 #undef DUMP_STATIC_ARRAY
897 }
898 
dump_test_packed_repeated_sint64(void)899 static void dump_test_packed_repeated_sint64 (void)
900 {
901 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
902   DUMP_STATIC_ARRAY_GENERIC(test_sint64, static_array, output_array_name)
903 
904   DUMP_STATIC_ARRAY (int64_roundnumbers, "test_packed_repeated_sint64_roundnumbers");
905   DUMP_STATIC_ARRAY (int64_min_max, "test_packed_repeated_sint64_min_max");
906 
907 #undef DUMP_STATIC_ARRAY
908 }
909 
dump_test_packed_repeated_sfixed64(void)910 static void dump_test_packed_repeated_sfixed64 (void)
911 {
912 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
913   DUMP_STATIC_ARRAY_GENERIC(test_sfixed64, static_array, output_array_name)
914 
915   DUMP_STATIC_ARRAY (int64_roundnumbers, "test_packed_repeated_sfixed64_roundnumbers");
916   DUMP_STATIC_ARRAY (int64_min_max, "test_packed_repeated_sfixed64_min_max");
917 
918 #undef DUMP_STATIC_ARRAY
919 }
dump_test_packed_repeated_uint64(void)920 static void dump_test_packed_repeated_uint64 (void)
921 {
922 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
923   DUMP_STATIC_ARRAY_GENERIC (test_uint64, static_array, output_array_name)
924 
925   DUMP_STATIC_ARRAY(uint64_roundnumbers, "test_packed_repeated_uint64_roundnumbers");
926   DUMP_STATIC_ARRAY(uint64_0_1_max, "test_packed_repeated_uint64_0_1_max");
927   DUMP_STATIC_ARRAY(uint64_random, "test_packed_repeated_uint64_random");
928 
929 #undef DUMP_STATIC_ARRAY
930 }
dump_test_packed_repeated_fixed64(void)931 static void dump_test_packed_repeated_fixed64 (void)
932 {
933 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
934   DUMP_STATIC_ARRAY_GENERIC(test_fixed64, static_array, output_array_name)
935 
936   DUMP_STATIC_ARRAY(uint64_roundnumbers, "test_packed_repeated_fixed64_roundnumbers");
937   DUMP_STATIC_ARRAY(uint64_0_1_max, "test_packed_repeated_fixed64_0_1_max");
938   DUMP_STATIC_ARRAY(uint64_random, "test_packed_repeated_fixed64_random");
939 
940 #undef DUMP_STATIC_ARRAY
941 }
942 
dump_test_packed_repeated_float(void)943 static void dump_test_packed_repeated_float (void)
944 {
945 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
946   DUMP_STATIC_ARRAY_GENERIC(test_float, static_array, output_array_name)
947 
948   DUMP_STATIC_ARRAY(float_random, "test_packed_repeated_float_random");
949 
950 #undef DUMP_STATIC_ARRAY
951 }
952 
dump_test_packed_repeated_double(void)953 static void dump_test_packed_repeated_double (void)
954 {
955 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
956   DUMP_STATIC_ARRAY_GENERIC(test_double, static_array, output_array_name)
957 
958   DUMP_STATIC_ARRAY(double_random, "test_packed_repeated_double_random");
959 
960 #undef DUMP_STATIC_ARRAY
961 }
dump_test_packed_repeated_boolean(void)962 static void dump_test_packed_repeated_boolean (void)
963 {
964 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
965   DUMP_STATIC_ARRAY_GENERIC(test_boolean, static_array, output_array_name)
966 
967   DUMP_STATIC_ARRAY(boolean_0, "test_packed_repeated_boolean_0");
968   DUMP_STATIC_ARRAY(boolean_1, "test_packed_repeated_boolean_1");
969   DUMP_STATIC_ARRAY(boolean_random, "test_packed_repeated_boolean_random");
970 
971 #undef DUMP_STATIC_ARRAY
972 }
dump_test_packed_repeated_enum_small(void)973 static void dump_test_packed_repeated_enum_small (void)
974 {
975 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
976   DUMP_STATIC_ARRAY_GENERIC (test_enum_small, static_array, output_array_name)
977 
978   DUMP_STATIC_ARRAY (enum_small_0, "test_packed_repeated_enum_small_0");
979   DUMP_STATIC_ARRAY (enum_small_1, "test_packed_repeated_enum_small_1");
980   DUMP_STATIC_ARRAY (enum_small_random, "test_packed_repeated_enum_small_random");
981 
982 #undef DUMP_STATIC_ARRAY
983 }
984 
dump_test_packed_repeated_enum(void)985 static void dump_test_packed_repeated_enum (void)
986 {
987 #define DUMP_STATIC_ARRAY(static_array, output_array_name) \
988   DUMP_STATIC_ARRAY_GENERIC (test_enum, static_array, output_array_name)
989 
990   DUMP_STATIC_ARRAY (enum_0, "test_packed_repeated_enum_0");
991   DUMP_STATIC_ARRAY (enum_1, "test_packed_repeated_enum_1");
992   DUMP_STATIC_ARRAY (enum_random, "test_packed_repeated_enum_random");
993 
994 #undef DUMP_STATIC_ARRAY
995 }
996 
997 
dump_test_unknown_fields(void)998 static void dump_test_unknown_fields (void)
999 {
1000   EmptyMess mess;
1001   const google::protobuf::Reflection *reflection = mess.GetReflection();
1002   google::protobuf::UnknownFieldSet *fs = reflection->MutableUnknownFields(&mess);
1003 
1004 #if GOOGLE_PROTOBUF_VERSION >= 2001000
1005   fs->AddVarint(5454, 255);
1006   fs->AddFixed32(5555, 260);
1007 #else
1008   google::protobuf::UnknownField *f;
1009   f = fs->AddField(5454);
1010   f->add_varint(255);
1011   f = fs->AddField(5555);
1012   f->add_fixed32(260);
1013 #endif
1014 
1015   dump_message_bytes (&mess, "test_unknown_fields_0");
1016 
1017   fs->Clear();
1018 
1019 #if GOOGLE_PROTOBUF_VERSION >= 2001000
1020   fs->AddLengthDelimited(6666, "xxxxxxxx");
1021   fs->AddFixed64(7777, 0x10101);
1022 #else
1023   f = fs->AddField(6666);
1024   f->add_length_delimited("xxxxxxxx");
1025   f = fs->AddField(7777);
1026   f->add_fixed64(0x10101);
1027 #endif
1028 
1029   dump_message_bytes (&mess, "test_unknown_fields_1");
1030 }
1031 
dump_test_submess_merge(void)1032 static void dump_test_submess_merge (void)
1033 {
1034   TestMessSubMess mess1, mess2, merged1, merged2;
1035 
1036   /* Repeated merge */
1037   mess1.mutable_rep_mess()->add_test_int32(1);
1038   mess1.mutable_rep_mess()->add_test_int32(2);
1039   mess2.mutable_rep_mess()->add_test_int32(3);
1040   mess2.mutable_rep_mess()->add_test_int32(4);
1041 
1042   mess1.mutable_rep_mess()->add_test_string("hello ");
1043   mess2.mutable_rep_mess()->add_test_string("world");
1044 
1045   mess1.mutable_rep_mess()->add_test_bytes("\001\002\003");
1046   mess2.mutable_rep_mess()->add_test_bytes("\004\005\006");
1047 
1048   mess1.mutable_rep_mess()->add_test_message()->set_test(111);
1049   mess2.mutable_rep_mess()->add_test_message()->set_test(222);
1050 
1051   /* Optional merge */
1052   mess1.mutable_opt_mess()->set_test_sint32(-1);
1053   mess2.mutable_opt_mess()->set_test_sint32(-2);
1054 
1055   mess1.mutable_opt_mess()->set_test_float(333);
1056   mess2.mutable_opt_mess()->set_test_double(444);
1057 
1058   mess1.mutable_opt_mess()->set_test_bytes("\001\002\003");
1059   mess1.mutable_opt_mess()->mutable_test_message()->set_test(111);
1060   mess2.mutable_opt_mess()->set_test_string("hello");
1061 
1062   /* Oneof merge */
1063   mess1.mutable_oneof_mess()->set_opt_int (1);
1064   mess2.mutable_oneof_mess()->mutable_test_message()->set_test(111);
1065 
1066   /* Required merge */
1067   mess1.mutable_req_mess()->set_test(1);
1068   mess2.mutable_req_mess()->set_test(2);
1069 
1070   /* Default value merge */
1071   mess1.mutable_def_mess()->set_v_int32(111);
1072   mess1.mutable_def_mess()->set_v_string("hello");
1073   mess2.mutable_def_mess()->set_v_bytes("\001\002\003");
1074   mess2.mutable_def_mess()->set_v_double(444);
1075 
1076   /* Merge both ways and encode the merged and unmerged messages */
1077   merged1.CopyFrom(mess1);
1078   merged1.MergeFrom(mess2);
1079   merged2.CopyFrom(mess2);
1080   merged2.MergeFrom(mess1);
1081 
1082   google::protobuf::Message *msgs[] = { &mess1, &mess2 };
1083   dump_messages_bytes (2, msgs, "test_submess_unmerged1");
1084   msgs[0] = &mess2;
1085   msgs[1] = &mess1;
1086   dump_messages_bytes (2, msgs, "test_submess_unmerged2");
1087   dump_message_bytes(&merged1, "test_submess_merged1");
1088   dump_message_bytes(&merged2, "test_submess_merged2");
1089 }
1090 
main()1091 int main()
1092 {
1093   dump_test_enum_small ();
1094   dump_test_enum_big ();
1095   dump_test_field_numbers ();
1096   dump_test_required_int32 ();
1097   dump_test_required_sint32 ();
1098   dump_test_required_sfixed32 ();
1099   dump_test_required_uint32 ();
1100   dump_test_required_fixed32 ();
1101   dump_test_required_int64 ();
1102   dump_test_required_sint64 ();
1103   dump_test_required_sfixed64 ();
1104   dump_test_required_uint64 ();
1105   dump_test_required_fixed64 ();
1106   dump_test_required_float ();
1107   dump_test_required_double ();
1108   dump_test_required_bool ();
1109   dump_test_required_enum_small ();
1110   dump_test_required_enum ();
1111   dump_test_required_string ();
1112   dump_test_required_bytes ();
1113   dump_test_required_message ();
1114   dump_test_optional_int32 ();
1115   dump_test_optional_sint32 ();
1116   dump_test_optional_sfixed32 ();
1117   dump_test_optional_int64 ();
1118   dump_test_optional_sint64 ();
1119   dump_test_optional_sfixed64 ();
1120   dump_test_optional_uint32 ();
1121   dump_test_optional_fixed32 ();
1122   dump_test_optional_uint64 ();
1123   dump_test_optional_fixed64 ();
1124   dump_test_optional_float ();
1125   dump_test_optional_double ();
1126   dump_test_optional_bool ();
1127   dump_test_optional_enum_small ();
1128   dump_test_optional_enum ();
1129   dump_test_optional_string ();
1130   dump_test_optional_bytes ();
1131   dump_test_optional_message ();
1132   dump_test_oneof_merge ();
1133   dump_test_repeated_int32 ();
1134   dump_test_repeated_sint32 ();
1135   dump_test_repeated_uint32 ();
1136   dump_test_repeated_sfixed32 ();
1137   dump_test_repeated_fixed32 ();
1138   dump_test_repeated_int64 ();
1139   dump_test_repeated_sint64 ();
1140   dump_test_repeated_sfixed64 ();
1141   dump_test_repeated_uint64 ();
1142   dump_test_repeated_fixed64 ();
1143   dump_test_repeated_float ();
1144   dump_test_repeated_double ();
1145   dump_test_repeated_boolean ();
1146   dump_test_repeated_enum_small ();
1147   dump_test_repeated_enum ();
1148   dump_test_repeated_string ();
1149   dump_test_repeated_bytes ();
1150   dump_test_repeated_SubMess ();
1151   dump_test_packed_repeated_int32 ();
1152   dump_test_packed_repeated_sint32 ();
1153   dump_test_packed_repeated_uint32 ();
1154   dump_test_packed_repeated_sfixed32 ();
1155   dump_test_packed_repeated_fixed32 ();
1156   dump_test_packed_repeated_int64 ();
1157   dump_test_packed_repeated_sint64 ();
1158   dump_test_packed_repeated_sfixed64 ();
1159   dump_test_packed_repeated_uint64 ();
1160   dump_test_packed_repeated_fixed64 ();
1161   dump_test_packed_repeated_float ();
1162   dump_test_packed_repeated_double ();
1163   dump_test_packed_repeated_boolean ();
1164   dump_test_packed_repeated_enum_small ();
1165   dump_test_packed_repeated_enum ();
1166   dump_test_unknown_fields ();
1167   dump_test_submess_merge ();
1168   return 0;
1169 }
1170