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 #ifdef __cplusplus
12 extern "C"
13 #endif
main(int argc,char ** argv)14 int main(int argc, char **argv)
15 {
16     int mode = (argc > 1) ? atoi(argv[1]) : 0;
17 
18     /* Initialize the structure with constants */
19     AllTypes alltypes = AllTypes_init_zero;
20 
21     if (mode == 0 || mode == 1)
22     {
23         alltypes.req_int32         = -1001;
24         alltypes.req_int64         = -1002;
25         alltypes.req_uint32        = 1003;
26         alltypes.req_uint64        = 1004;
27         alltypes.req_sint32        = -1005;
28         alltypes.req_sint64        = -1006;
29         alltypes.req_bool          = true;
30 
31         alltypes.req_fixed32       = 1008;
32         alltypes.req_sfixed32      = -1009;
33         alltypes.req_float         = 1010.0f;
34 
35         alltypes.req_fixed64       = 1011;
36         alltypes.req_sfixed64      = -1012;
37         alltypes.req_double        = 1013.0;
38 
39         strcpy(alltypes.req_string, "1014");
40         alltypes.req_bytes.size = 4;
41         memcpy(alltypes.req_bytes.bytes, "1015", 4);
42         strcpy(alltypes.req_submsg.substuff1, "1016");
43         alltypes.req_submsg.substuff2 = 1016;
44         alltypes.req_enum = MyEnum_Truth;
45         memcpy(alltypes.req_fbytes, "1019", 4);
46 
47         alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = -2001;
48         alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = -2002;
49         alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003;
50         alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004;
51         alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = -2005;
52         alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = -2006;
53         alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true;
54 
55         alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008;
56         alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = -2009;
57         alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f;
58 
59         alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011;
60         alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = -2012;
61         alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0;
62 
63         alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014");
64         alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4;
65         memcpy(alltypes.rep_bytes[4].bytes, "2015", 4);
66 
67         alltypes.rep_submsg_count = 5;
68         strcpy(alltypes.rep_submsg[4].substuff1, "2016");
69         alltypes.rep_submsg[4].substuff2 = 2016;
70         alltypes.rep_submsg[4].has_substuff3 = true;
71         alltypes.rep_submsg[4].substuff3 = 2016;
72 
73         alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
74         alltypes.rep_emptymsg_count = 5;
75 
76         alltypes.rep_fbytes_count = 5;
77         memcpy(alltypes.rep_fbytes[4], "2019", 4);
78 
79         alltypes.rep_farray[4] = 2040;
80         alltypes.rep_farray2[2] = 2095;
81 
82         alltypes.req_limits.int32_min  = INT32_MIN;
83         alltypes.req_limits.int32_max  = INT32_MAX;
84         alltypes.req_limits.uint32_min = 0;
85         alltypes.req_limits.uint32_max = UINT32_MAX;
86         alltypes.req_limits.int64_min  = INT64_MIN;
87         alltypes.req_limits.int64_max  = INT64_MAX;
88         alltypes.req_limits.uint64_min = 0;
89         alltypes.req_limits.uint64_max = UINT64_MAX;
90         alltypes.req_limits.enum_min   = HugeEnum_Negative;
91         alltypes.req_limits.enum_max   = HugeEnum_Positive;
92         alltypes.req_limits.largetag   = 1001;
93 
94         alltypes.req_ds8.first = 9991;
95         alltypes.req_ds8.second = 9992;
96 
97         alltypes.req_intsizes.req_int8 = -128;
98         alltypes.req_intsizes.req_uint8 = 255;
99         alltypes.req_intsizes.req_sint8 = -128;
100         alltypes.req_intsizes.req_int16 = -32768;
101         alltypes.req_intsizes.req_uint16 = 65535;
102         alltypes.req_intsizes.req_sint16 = -32768;
103     }
104 
105     if (mode == 1)
106     {
107         /* Fill in values for optional fields */
108         alltypes.has_opt_int32 = true;
109         alltypes.opt_int32         = 3041;
110         alltypes.has_opt_int64 = true;
111         alltypes.opt_int64         = 3042;
112         alltypes.has_opt_uint32 = true;
113         alltypes.opt_uint32        = 3043;
114         alltypes.has_opt_uint64 = true;
115         alltypes.opt_uint64        = 3044;
116         alltypes.has_opt_sint32 = true;
117         alltypes.opt_sint32        = 3045;
118         alltypes.has_opt_sint64 = true;
119         alltypes.opt_sint64        = 3046;
120         alltypes.has_opt_bool = true;
121         alltypes.opt_bool          = true;
122 
123         alltypes.has_opt_fixed32 = true;
124         alltypes.opt_fixed32       = 3048;
125         alltypes.has_opt_sfixed32 = true;
126         alltypes.opt_sfixed32      = 3049;
127         alltypes.has_opt_float = true;
128         alltypes.opt_float         = 3050.0f;
129 
130         alltypes.has_opt_fixed64 = true;
131         alltypes.opt_fixed64       = 3051;
132         alltypes.has_opt_sfixed64 = true;
133         alltypes.opt_sfixed64      = 3052;
134         alltypes.has_opt_double = true;
135         alltypes.opt_double        = 3053.0;
136 
137         alltypes.has_opt_string = true;
138         strcpy(alltypes.opt_string, "3054");
139         alltypes.has_opt_bytes = true;
140         alltypes.opt_bytes.size = 4;
141         memcpy(alltypes.opt_bytes.bytes, "3055", 4);
142         alltypes.has_opt_submsg = true;
143         strcpy(alltypes.opt_submsg.substuff1, "3056");
144         alltypes.opt_submsg.substuff2 = 3056;
145         alltypes.has_opt_enum = true;
146         alltypes.opt_enum = MyEnum_Truth;
147         alltypes.has_opt_emptymsg = true;
148         alltypes.has_opt_fbytes = true;
149         memcpy(alltypes.opt_fbytes, "3059", 4);
150 
151         alltypes.which_oneof = AllTypes_oneof_msg1_tag;
152         strcpy(alltypes.oneof.oneof_msg1.substuff1, "4059");
153         alltypes.oneof.oneof_msg1.substuff2 = 4059;
154 
155         alltypes.has_opt_non_zero_based_enum = true;
156         alltypes.opt_non_zero_based_enum = NonZeroBasedEnum_Three;
157     }
158 
159     alltypes.end = 1099;
160 
161     {
162         uint8_t buffer[AllTypes_size];
163         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
164 
165         /* Now encode it and check if we succeeded. */
166         if (pb_encode(&stream, AllTypes_fields, &alltypes))
167         {
168             SET_BINARY_MODE(stdout);
169             fwrite(buffer, 1, stream.bytes_written, stdout);
170             return 0; /* Success */
171         }
172         else
173         {
174             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
175             return 1; /* Failure */
176         }
177     }
178 }
179