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