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