MOS Source Code
Loading...
Searching...
No Matches
decode_alltypes_pointer.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include <stdlib.h>
4#include <pb_decode.h>
5#include "alltypes.pb.h"
6#include "test_helpers.h"
7#include "unittests.h"
8
9/* This function is called once from main(), it handles
10 the decoding and checks the fields. */
11bool check_alltypes(pb_istream_t *stream, int mode)
12{
13 int status = 0;
14 AllTypes alltypes;
15
16 /* Fill with garbage to better detect initialization errors */
17 memset(&alltypes, 0xAA, sizeof(alltypes));
18 alltypes.extensions = 0;
19
20 if (!pb_decode(stream, AllTypes_fields, &alltypes))
21 return false;
22
23 TEST(alltypes.req_int32 && *alltypes.req_int32 == -1001);
24 TEST(alltypes.req_int64 && *alltypes.req_int64 == -1002);
25 TEST(alltypes.req_uint32 && *alltypes.req_uint32 == 1003);
26 TEST(alltypes.req_uint64 && *alltypes.req_uint64 == 1004);
27 TEST(alltypes.req_sint32 && *alltypes.req_sint32 == -1005);
28 TEST(alltypes.req_sint64 && *alltypes.req_sint64 == -1006);
29 TEST(alltypes.req_bool && *alltypes.req_bool == true);
30
31 TEST(alltypes.req_fixed32 && *alltypes.req_fixed32 == 1008);
32 TEST(alltypes.req_sfixed32 && *alltypes.req_sfixed32 == -1009);
33 TEST(alltypes.req_float && *alltypes.req_float == 1010.0f);
34
35 TEST(alltypes.req_fixed64 && *alltypes.req_fixed64 == 1011);
36 TEST(alltypes.req_sfixed64 && *alltypes.req_sfixed64 == -1012);
37 TEST(alltypes.req_double && *alltypes.req_double == 1013.0f);
38
39 TEST(alltypes.req_string && strcmp(alltypes.req_string, "1014") == 0);
40 TEST(alltypes.req_bytes && alltypes.req_bytes->size == 4);
41 TEST(alltypes.req_bytes && memcmp(&alltypes.req_bytes->bytes, "1015", 4) == 0);
42 TEST(alltypes.req_submsg && strcmp(alltypes.req_submsg->substuff1, "1016") == 0);
43 TEST(alltypes.req_submsg && alltypes.req_submsg->substuff2
44 && *alltypes.req_submsg->substuff2 == 1016);
45 TEST(alltypes.req_enum && *alltypes.req_enum == MyEnum_Truth);
46 TEST(alltypes.req_fbytes && memcmp(alltypes.req_fbytes, "1019", 4) == 0);
47
48 TEST(alltypes.rep_int32_count == 5 && alltypes.rep_int32[4] == -2001 && alltypes.rep_int32[0] == 0);
49 TEST(alltypes.rep_int64_count == 5 && alltypes.rep_int64[4] == -2002 && alltypes.rep_int64[0] == 0);
50 TEST(alltypes.rep_uint32_count == 5 && alltypes.rep_uint32[4] == 2003 && alltypes.rep_uint32[0] == 0);
51 TEST(alltypes.rep_uint64_count == 5 && alltypes.rep_uint64[4] == 2004 && alltypes.rep_uint64[0] == 0);
52 TEST(alltypes.rep_sint32_count == 5 && alltypes.rep_sint32[4] == -2005 && alltypes.rep_sint32[0] == 0);
53 TEST(alltypes.rep_sint64_count == 5 && alltypes.rep_sint64[4] == -2006 && alltypes.rep_sint64[0] == 0);
54 TEST(alltypes.rep_bool_count == 5 && alltypes.rep_bool[4] == true && alltypes.rep_bool[0] == false);
55
56 TEST(alltypes.rep_fixed32_count == 5 && alltypes.rep_fixed32[4] == 2008 && alltypes.rep_fixed32[0] == 0);
57 TEST(alltypes.rep_sfixed32_count == 5 && alltypes.rep_sfixed32[4] == -2009 && alltypes.rep_sfixed32[0] == 0);
58 TEST(alltypes.rep_float_count == 5 && alltypes.rep_float[4] == 2010.0f && alltypes.rep_float[0] == 0.0f);
59
60 TEST(alltypes.rep_fixed64_count == 5 && alltypes.rep_fixed64[4] == 2011 && alltypes.rep_fixed64[0] == 0);
61 TEST(alltypes.rep_sfixed64_count == 5 && alltypes.rep_sfixed64[4] == -2012 && alltypes.rep_sfixed64[0] == 0);
62 TEST(alltypes.rep_double_count == 5 && alltypes.rep_double[4] == 2013.0 && alltypes.rep_double[0] == 0.0);
63
64 TEST(alltypes.rep_string_count == 5 && strcmp(alltypes.rep_string[4], "2014") == 0 && alltypes.rep_string[0][0] == '\0');
65 TEST(alltypes.rep_bytes_count == 5 && alltypes.rep_bytes[4]->size == 4 && alltypes.rep_bytes[0]->size == 0);
66 TEST(memcmp(&alltypes.rep_bytes[4]->bytes, "2015", 4) == 0);
67
68 TEST(alltypes.rep_submsg_count == 5);
69 TEST(strcmp(alltypes.rep_submsg[4].substuff1, "2016") == 0 && alltypes.rep_submsg[0].substuff1[0] == '\0');
70 TEST(*alltypes.rep_submsg[4].substuff2 == 2016 && *alltypes.rep_submsg[0].substuff2 == 0);
71 TEST(*alltypes.rep_submsg[4].substuff3 == 2016 && alltypes.rep_submsg[0].substuff3 == NULL);
72
73 TEST(alltypes.rep_enum_count == 5 && alltypes.rep_enum[4] == MyEnum_Truth && alltypes.rep_enum[0] == MyEnum_Zero);
74 TEST(alltypes.rep_emptymsg_count == 5);
75 TEST(alltypes.rep_fbytes_count == 5);
76 TEST(alltypes.rep_fbytes[0][0] == 0 && alltypes.rep_fbytes[0][3] == 0);
77 TEST(memcmp(alltypes.rep_fbytes[4], "2019", 4) == 0);
78 TEST(alltypes.rep_farray && (*alltypes.rep_farray)[0] == 0 && (*alltypes.rep_farray)[4] == 2040);
79 TEST(alltypes.rep_farray2 && (*alltypes.rep_farray2)[0] == 0 && (*alltypes.rep_farray2)[2] == 2095);
80
81 if (mode == 0)
82 {
83 /* Expect that optional values are not present */
84 TEST(alltypes.opt_int32 == NULL);
85 TEST(alltypes.opt_int64 == NULL);
86 TEST(alltypes.opt_uint32 == NULL);
87 TEST(alltypes.opt_uint64 == NULL);
88 TEST(alltypes.opt_sint32 == NULL);
89 TEST(alltypes.opt_sint64 == NULL);
90 TEST(alltypes.opt_bool == NULL);
91
92 TEST(alltypes.opt_fixed32 == NULL);
93 TEST(alltypes.opt_sfixed32 == NULL);
94 TEST(alltypes.opt_float == NULL);
95 TEST(alltypes.opt_fixed64 == NULL);
96 TEST(alltypes.opt_sfixed64 == NULL);
97 TEST(alltypes.opt_double == NULL);
98
99 TEST(alltypes.opt_string == NULL);
100 TEST(alltypes.opt_bytes == NULL);
101 TEST(alltypes.opt_submsg == NULL);
102 TEST(alltypes.opt_enum == NULL);
103 TEST(alltypes.opt_fbytes == NULL);
104
105 TEST(alltypes.which_oneof == 0);
106
107 TEST(alltypes.opt_non_zero_based_enum == NULL);
108 }
109 else
110 {
111 /* Expect filled-in values */
112 TEST(alltypes.opt_int32 && *alltypes.opt_int32 == 3041);
113 TEST(alltypes.opt_int64 && *alltypes.opt_int64 == 3042);
114 TEST(alltypes.opt_uint32 && *alltypes.opt_uint32 == 3043);
115 TEST(alltypes.opt_uint64 && *alltypes.opt_uint64 == 3044);
116 TEST(alltypes.opt_sint32 && *alltypes.opt_sint32 == 3045);
117 TEST(alltypes.opt_sint64 && *alltypes.opt_sint64 == 3046);
118 TEST(alltypes.opt_bool && *alltypes.opt_bool == true);
119
120 TEST(alltypes.opt_fixed32 && *alltypes.opt_fixed32 == 3048);
121 TEST(alltypes.opt_sfixed32 && *alltypes.opt_sfixed32== 3049);
122 TEST(alltypes.opt_float && *alltypes.opt_float == 3050.0f);
123 TEST(alltypes.opt_fixed64 && *alltypes.opt_fixed64 == 3051);
124 TEST(alltypes.opt_sfixed64 && *alltypes.opt_sfixed64== 3052);
125 TEST(alltypes.opt_double && *alltypes.opt_double == 3053.0);
126
127 TEST(alltypes.opt_string && strcmp(alltypes.opt_string, "3054") == 0);
128 TEST(alltypes.opt_bytes && alltypes.opt_bytes->size == 4);
129 TEST(alltypes.opt_bytes && memcmp(&alltypes.opt_bytes->bytes, "3055", 4) == 0);
130 TEST(alltypes.opt_submsg && strcmp(alltypes.opt_submsg->substuff1, "3056") == 0);
131 TEST(alltypes.opt_submsg && *alltypes.opt_submsg->substuff2 == 3056);
132 TEST(alltypes.opt_enum && *alltypes.opt_enum == MyEnum_Truth);
133 TEST(alltypes.opt_emptymsg);
134 TEST(alltypes.opt_fbytes && memcmp(alltypes.opt_fbytes, "3059", 4) == 0);
135
136 TEST(alltypes.which_oneof == AllTypes_oneof_msg1_tag);
137 TEST(alltypes.oneof.oneof_msg1 && strcmp(alltypes.oneof.oneof_msg1->substuff1, "4059") == 0);
138 TEST(alltypes.oneof.oneof_msg1->substuff2 && *alltypes.oneof.oneof_msg1->substuff2 == 4059);
139
140 TEST(alltypes.opt_non_zero_based_enum && *alltypes.opt_non_zero_based_enum == NonZeroBasedEnum_Three);
141 }
142
143 TEST(alltypes.req_limits->int32_min && *alltypes.req_limits->int32_min == INT32_MIN);
144 TEST(alltypes.req_limits->int32_max && *alltypes.req_limits->int32_max == INT32_MAX);
145 TEST(alltypes.req_limits->uint32_min && *alltypes.req_limits->uint32_min == 0);
146 TEST(alltypes.req_limits->uint32_max && *alltypes.req_limits->uint32_max == UINT32_MAX);
147 TEST(alltypes.req_limits->int64_min && *alltypes.req_limits->int64_min == INT64_MIN);
148 TEST(alltypes.req_limits->int64_max && *alltypes.req_limits->int64_max == INT64_MAX);
149 TEST(alltypes.req_limits->uint64_min && *alltypes.req_limits->uint64_min == 0);
150 TEST(alltypes.req_limits->uint64_max && *alltypes.req_limits->uint64_max == UINT64_MAX);
151 TEST(alltypes.req_limits->enum_min && *alltypes.req_limits->enum_min == HugeEnum_Negative);
152 TEST(alltypes.req_limits->enum_max && *alltypes.req_limits->enum_max == HugeEnum_Positive);
153 TEST(alltypes.req_limits->largetag && *alltypes.req_limits->largetag == 1001);
154
155 TEST(alltypes.req_ds8);
156 TEST(alltypes.req_ds8->first && *alltypes.req_ds8->first == 9991);
157 TEST(alltypes.req_ds8->first && *alltypes.req_ds8->second == 9992);
158
159 TEST(alltypes.req_intsizes);
160 TEST(*alltypes.req_intsizes->req_int8 == -128);
161 TEST(*alltypes.req_intsizes->req_uint8 == 255);
162 TEST(*alltypes.req_intsizes->req_sint8 == -128);
163 TEST(*alltypes.req_intsizes->req_int16 == -32768);
164 TEST(*alltypes.req_intsizes->req_uint16 == 65535);
165 TEST(*alltypes.req_intsizes->req_sint16 == -32768);
166
167 TEST(alltypes.end && *alltypes.end == 1099);
168
169 pb_release(AllTypes_fields, &alltypes);
170
171 return status == 0;
172}
173
174int main(int argc, char **argv)
175{
176 uint8_t buffer[1024];
177 size_t count;
178 pb_istream_t stream;
179
180 /* Whether to expect the optional values or the default values. */
181 int mode = (argc > 1) ? atoi(argv[1]) : 0;
182
183 /* Read the data into buffer */
185 count = fread(buffer, 1, sizeof(buffer), stdin);
186
187 /* Construct a pb_istream_t for reading from the buffer */
188 stream = pb_istream_from_buffer(buffer, count);
189
190 /* Decode and verify the message */
191 if (!check_alltypes(&stream, mode))
192 {
193 fprintf(stderr, "Test failed: %s\n", PB_GET_ERROR(&stream));
194 return 1;
195 }
196 else
197 {
198 return 0;
199 }
200}
#define AllTypes_fields
@ MyEnum_Zero
@ MyEnum_Truth
@ HugeEnum_Negative
@ HugeEnum_Positive
bool check_alltypes(pb_istream_t *stream, int mode)
MOSAPI int memcmp(const void *s1, const void *s2, size_t n)
Definition mos_string.c:142
MOSAPI s32 strcmp(const char *str1, const char *str2)
Definition mos_string.c:24
size_t fread(void *__restrict ptr, size_t size, size_t nmemb, FILE *__restrict stream)
#define stderr
Definition mos_stdio.h:32
#define stdin
Definition mos_stdio.h:30
MOSAPI s32 atoi(const char *nptr)
Definition mos_stdlib.c:36
const char ** argv
Definition kmain.c:44
size_t argc
Definition kmain.c:43
#define PB_GET_ERROR(stream)
Definition pb.h:891
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
void pb_release(const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1336
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
#define NULL
Definition pb_syshdr.h:46
static void * memset(void *s, int c, size_t n)
Definition pb_syshdr.h:101
unsigned char uint8_t
Definition pb_syshdr.h:20
int main()
Definition simple.cpp:6
pb_size_t rep_submsg_count
int64_t req_sfixed64
AllTypes_opt_bytes_t opt_bytes
pb_size_t rep_string_count
int32_t rep_int32[5]
int64_t opt_sint64
MyEnum rep_enum[5]
uint64_t opt_uint64
uint32_t rep_uint32[5]
AllTypes_req_bytes_t req_bytes
int32_t opt_int32
pb_size_t rep_double_count
uint32_t opt_fixed32
int32_t rep_sfixed32[5]
int64_t opt_int64
pb_extension_t * extensions
AllTypes_rep_bytes_t rep_bytes[5]
SubMessage opt_submsg
uint32_t req_fixed32
int32_t opt_sint32
double rep_double[5]
pb_size_t rep_bool_count
pb_size_t rep_fixed32_count
int64_t req_int64
float rep_float[5]
bool rep_bool[5]
uint64_t req_uint64
pb_size_t rep_int32_count
double opt_double
int64_t rep_sfixed64[5]
pb_size_t rep_float_count
char opt_string[16]
int32_t req_int32
pb_size_t rep_sfixed32_count
uint32_t opt_uint32
double req_double
SubMessage rep_submsg[5]
pb_size_t rep_fixed64_count
pb_size_t rep_bytes_count
pb_size_t rep_enum_count
char req_string[16]
int64_t req_sint64
int64_t rep_sint64[5]
char rep_string[5][16]
int64_t rep_int64[5]
uint64_t rep_uint64[5]
int64_t opt_sfixed64
MyEnum opt_enum
int32_t req_sint32
SubMessage req_submsg
uint32_t req_uint32
uint64_t opt_fixed64
int32_t req_sfixed32
pb_size_t rep_uint64_count
pb_size_t rep_sint64_count
int32_t opt_sfixed32
MyEnum req_enum
pb_size_t rep_sfixed64_count
pb_size_t rep_uint32_count
pb_size_t rep_int64_count
uint64_t rep_fixed64[5]
float req_float
uint32_t rep_fixed32[5]
pb_size_t rep_sint32_count
uint64_t req_fixed64
int32_t rep_sint32[5]
char substuff1[16]
int32_t substuff2
uint32_t substuff3
#define TEST(x)
Definition test.h:3
#define SET_BINARY_MODE(file)
static char buffer[2048]
Definition test_printf.c:7