MOS Source Code
Loading...
Searching...
No Matches
fixed_count_unittests.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include <pb_decode.h>
4#include <pb_encode.h>
5#include <malloc_wrappers.h>
6#include "unittests.h"
7#include "fixed_count.pb.h"
8
9int main()
10{
11 int status = 0;
12
13 {
14 pb_byte_t buffer[Message1_size];
15 Message1 msg_a = Message1_init_zero;
16 Message1 msg_b = Message1_init_zero;
17
18 pb_ostream_t ostream;
19 pb_istream_t istream;
20 size_t message_length;
21
22 msg_a.data[0] = 1;
23 msg_a.data[0] = 2;
24 msg_a.data[0] = 3;
25
26 COMMENT("Test encode and decode with three entries");
27
28 ostream = pb_ostream_from_buffer(buffer, Message1_size);
29 TEST(pb_encode(&ostream, Message1_fields, &msg_a));
30 message_length = ostream.bytes_written;
31
32 istream = pb_istream_from_buffer(buffer, message_length);
33 TEST(pb_decode(&istream, Message1_fields, &msg_b));
34
35 TEST(istream.bytes_left == 0);
36 TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
37 }
38
39 {
40 pb_byte_t input[] = {0x08, 0x00, 0x08, 0x01, 0x08, 0x02, 0x08, 0x03};
41 pb_istream_t stream;
42 Message1 msg = Message1_init_zero;
43
44 COMMENT("Test wrong number of entries");
45
46 stream = pb_istream_from_buffer(input, 6);
47 TEST(pb_decode(&stream, Message1_fields, &msg));
48 TEST(msg.data[0] == 0 && msg.data[1] == 1 && msg.data[2] == 2);
49
50 stream = pb_istream_from_buffer(input, 8);
51 TEST(!pb_decode(&stream, Message1_fields, &msg));
52 TEST(strcmp(stream.errmsg, "array overflow") == 0);
53
54 stream = pb_istream_from_buffer(input, 4);
55 TEST(!pb_decode(&stream, Message1_fields, &msg));
56 TEST(strcmp(stream.errmsg, "wrong size for fixed count field") == 0);
57 }
58
59 {
60 pb_byte_t buffer[Message2_size];
61 Message2 msg_a = Message2_init_zero;
62 Message2 msg_b = Message2_init_zero;
63
64 pb_ostream_t ostream;
65 pb_istream_t istream;
66 size_t message_length;
67
68 COMMENT("Test encode and decode with nested messages");
69
70 msg_a.data[0].data[0] = 1;
71 msg_a.data[0].data[1] = 2;
72 msg_a.data[0].data[2] = 3;
73 msg_a.data[1].data[0] = 4;
74 msg_a.data[1].data[1] = 5;
75 msg_a.data[1].data[2] = 6;
76
77 ostream = pb_ostream_from_buffer(buffer, Message2_size);
78 TEST(pb_encode(&ostream, Message2_fields, &msg_a));
79 message_length = ostream.bytes_written;
80
81 istream = pb_istream_from_buffer(buffer, message_length);
82 TEST(pb_decode(&istream, Message2_fields, &msg_b));
83
84 TEST(istream.bytes_left == 0);
85 TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
86 }
87
88 {
89 pb_byte_t buffer[Message3_size];
90 Message3 msg_a = Message3_init_zero;
91 Message3 msg_b = Message3_init_zero;
92
93 pb_ostream_t ostream;
94 pb_istream_t istream;
95 size_t message_length;
96
97 COMMENT("Test encode and decode with two nested messages");
98
99 msg_a.data1[0].data[0].data[0] = 1;
100 msg_a.data1[0].data[0].data[1] = 2;
101 msg_a.data1[0].data[0].data[2] = 3;
102 msg_a.data1[0].data[1].data[0] = 4;
103 msg_a.data1[0].data[1].data[1] = 5;
104 msg_a.data1[0].data[1].data[2] = 6;
105
106 msg_a.data1[1].data[0].data[0] = 7;
107 msg_a.data1[1].data[0].data[1] = 8;
108 msg_a.data1[1].data[0].data[2] = 9;
109 msg_a.data1[1].data[1].data[0] = 10;
110 msg_a.data1[1].data[1].data[1] = 11;
111 msg_a.data1[1].data[1].data[2] = 12;
112
113 msg_a.data2[0].data[0].data[0] = 11;
114 msg_a.data2[0].data[0].data[1] = 12;
115 msg_a.data2[0].data[0].data[2] = 13;
116 msg_a.data2[0].data[1].data[0] = 14;
117 msg_a.data2[0].data[1].data[1] = 15;
118 msg_a.data2[0].data[1].data[2] = 16;
119
120 msg_a.data2[1].data[0].data[0] = 17;
121 msg_a.data2[1].data[0].data[1] = 18;
122 msg_a.data2[1].data[0].data[2] = 19;
123 msg_a.data2[1].data[1].data[0] = 110;
124 msg_a.data2[1].data[1].data[1] = 111;
125 msg_a.data2[1].data[1].data[2] = 112;
126
127 ostream = pb_ostream_from_buffer(buffer, Message3_size);
128 TEST(pb_encode(&ostream, Message3_fields, &msg_a));
129 message_length = ostream.bytes_written;
130
131 istream = pb_istream_from_buffer(buffer, message_length);
132 TEST(pb_decode(&istream, Message3_fields, &msg_b));
133
134 TEST(istream.bytes_left == 0);
135 TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
136 }
137
138 {
139 pb_byte_t buffer[256];
140 Message4 msg_a = Message4_init_zero;
141 Message4 msg_b = Message4_init_zero;
142
143 pb_ostream_t ostream;
144 pb_istream_t istream;
145 size_t message_length;
146
147 COMMENT("Test encode and decode with pointer type fixarray");
148
149 SubMessage submsgs[pb_arraysize(Message4, submsgs[0])] = {SubMessage_init_zero};
150 submsgs[0].a = 1;
151 submsgs[1].a = 5;
152 submsgs[2].a = 999;
153
154 char a[5] = "a";
155 char b[5] = "b";
156 char abc[5] = "abc";
157 char *strings[pb_arraysize(Message4, strings[0])] = {a, b, abc};
158
159 msg_a.submsgs = &submsgs;
160 msg_a.strings = &strings;
161
162 ostream = pb_ostream_from_buffer(buffer, Message3_size);
163 TEST(pb_encode(&ostream, Message4_fields, &msg_a));
164 message_length = ostream.bytes_written;
165
166 TEST(get_alloc_count() == 0);
167
168 istream = pb_istream_from_buffer(buffer, message_length);
169 TEST(pb_decode(&istream, Message4_fields, &msg_b));
170
171 TEST(istream.bytes_left == 0);
172
173 TEST((*msg_b.submsgs)[0].a == 1);
174 TEST((*msg_b.submsgs)[1].a == 5);
175 TEST((*msg_b.submsgs)[2].a == 999);
176
177 TEST(strcmp((*msg_b.strings)[0], "a") == 0);
178 TEST(strcmp((*msg_b.strings)[1], "b") == 0);
179 TEST(strcmp((*msg_b.strings)[2], "abc") == 0);
180
181 pb_release(Message4_fields, &msg_b);
182
183 TEST(get_alloc_count() == 0);
184 }
185
186 if (status != 0)
187 fprintf(stdout, "\n\nSome tests FAILED!\n");
188
189 return status;
190}
#define SubMessage_init_zero
int main()
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
#define stdout
Definition mos_stdio.h:31
size_t get_alloc_count()
uint_least8_t pb_byte_t
Definition pb.h:227
#define pb_arraysize(st, m)
Definition pb.h:497
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
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
#define TEST(x)
Definition test.h:3
static char buffer[2048]
Definition test_printf.c:7
#define COMMENT(x)
Definition unittests.h:17