Update doc revision and pdf cover.
[apps/low-level-can-service.git] / CAN-binder / libs / nanopb / tests / field_size_16_proto3 / encode_alltypes.c
1 /* Attempts to test all the datatypes supported by ProtoBuf3.
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 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     alltypes.rep_int32_count = 5; alltypes.rep_int32[4] = -2001;
19     alltypes.rep_int64_count = 5; alltypes.rep_int64[4] = -2002;
20     alltypes.rep_uint32_count = 5; alltypes.rep_uint32[4] = 2003;
21     alltypes.rep_uint64_count = 5; alltypes.rep_uint64[4] = 2004;
22     alltypes.rep_sint32_count = 5; alltypes.rep_sint32[4] = -2005;
23     alltypes.rep_sint64_count = 5; alltypes.rep_sint64[4] = -2006;
24     alltypes.rep_bool_count = 5; alltypes.rep_bool[4] = true;
25     
26     alltypes.rep_fixed32_count = 5; alltypes.rep_fixed32[4] = 2008;
27     alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32[4] = -2009;
28     alltypes.rep_float_count = 5; alltypes.rep_float[4] = 2010.0f;
29     
30     alltypes.rep_fixed64_count = 5; alltypes.rep_fixed64[4] = 2011;
31     alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64[4] = -2012;
32     alltypes.rep_double_count = 5; alltypes.rep_double[4] = 2013.0;
33     
34     alltypes.rep_string_count = 5; strcpy(alltypes.rep_string[4], "2014");
35     alltypes.rep_bytes_count = 5; alltypes.rep_bytes[4].size = 4;
36     memcpy(alltypes.rep_bytes[4].bytes, "2015", 4);
37
38     alltypes.rep_submsg_count = 5;
39     strcpy(alltypes.rep_submsg[4].substuff1, "2016");
40     alltypes.rep_submsg[4].substuff2 = 2016;
41     alltypes.rep_submsg[4].substuff3 = 2016;
42     
43     alltypes.rep_enum_count = 5; alltypes.rep_enum[4] = MyEnum_Truth;
44     alltypes.rep_emptymsg_count = 5;
45     
46     alltypes.rep_fbytes_count = 5;
47     memcpy(alltypes.rep_fbytes[4], "2019", 4);
48     
49     alltypes.req_limits.int32_min  = INT32_MIN;
50     alltypes.req_limits.int32_max  = INT32_MAX;
51     alltypes.req_limits.uint32_min = 0;
52     alltypes.req_limits.uint32_max = UINT32_MAX;
53     alltypes.req_limits.int64_min  = INT64_MIN;
54     alltypes.req_limits.int64_max  = INT64_MAX;
55     alltypes.req_limits.uint64_min = 0;
56     alltypes.req_limits.uint64_max = UINT64_MAX;
57     alltypes.req_limits.enum_min   = HugeEnum_Negative;
58     alltypes.req_limits.enum_max   = HugeEnum_Positive;
59     
60     if (mode != 0)
61     {
62         /* Fill in values for singular fields */
63         alltypes.sng_int32         = 3041;
64         alltypes.sng_int64         = 3042;
65         alltypes.sng_uint32        = 3043;
66         alltypes.sng_uint64        = 3044;
67         alltypes.sng_sint32        = 3045;
68         alltypes.sng_sint64        = 3046;
69         alltypes.sng_bool          = true;
70         
71         alltypes.sng_fixed32       = 3048;
72         alltypes.sng_sfixed32      = 3049;
73         alltypes.sng_float         = 3050.0f;
74         
75         alltypes.sng_fixed64       = 3051;
76         alltypes.sng_sfixed64      = 3052;
77         alltypes.sng_double        = 3053.0;
78         
79         strcpy(alltypes.sng_string, "3054");
80         alltypes.sng_bytes.size = 4;
81         memcpy(alltypes.sng_bytes.bytes, "3055", 4);
82         strcpy(alltypes.sng_submsg.substuff1, "3056");
83         alltypes.sng_submsg.substuff2 = 3056;
84         alltypes.sng_enum = MyEnum_Truth;
85         memcpy(alltypes.sng_fbytes, "3059", 4);
86
87         alltypes.which_oneof = AllTypes_oneof_msg1_tag;
88         strcpy(alltypes.oneof.oneof_msg1.substuff1, "4059");
89         alltypes.oneof.oneof_msg1.substuff2 = 4059;
90     }
91     
92     alltypes.end = 1099;
93     
94     {
95         uint8_t buffer[AllTypes_size];
96         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
97         
98         /* Now encode it and check if we succeeded. */
99         if (pb_encode(&stream, AllTypes_fields, &alltypes))
100         {
101             SET_BINARY_MODE(stdout);
102             fwrite(buffer, 1, stream.bytes_written, stdout);
103             return 0; /* Success */
104         }
105         else
106         {
107             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
108             return 1; /* Failure */
109         }
110     }
111 }