1 #include <stdio.h>
2 #include <string.h>
3 #include <pb_decode.h>
4 #include <pb_encode.h>
5 #include "unittests.h"
6 #include "fixed_count.pb.h"
7
main()8 int main()
9 {
10 int status = 0;
11 COMMENT("Test encoding and decoding of repeated fixed count fields");
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 ostream = pb_ostream_from_buffer(buffer, Message1_size);
27 TEST(pb_encode(&ostream, Message1_fields, &msg_a));
28 message_length = ostream.bytes_written;
29
30 istream = pb_istream_from_buffer(buffer, message_length);
31 TEST(pb_decode(&istream, Message1_fields, &msg_b));
32
33 TEST(istream.bytes_left == 0);
34 TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
35 }
36
37 {
38 pb_byte_t buffer[Message2_size];
39 Message2 msg_a = Message2_init_zero;
40 Message2 msg_b = Message2_init_zero;
41
42 pb_ostream_t ostream;
43 pb_istream_t istream;
44 size_t message_length;
45
46 msg_a.data[0].data[0] = 1;
47 msg_a.data[0].data[1] = 2;
48 msg_a.data[0].data[2] = 3;
49 msg_a.data[1].data[0] = 4;
50 msg_a.data[1].data[1] = 5;
51 msg_a.data[1].data[2] = 6;
52
53 ostream = pb_ostream_from_buffer(buffer, Message2_size);
54 TEST(pb_encode(&ostream, Message2_fields, &msg_a));
55 message_length = ostream.bytes_written;
56
57 istream = pb_istream_from_buffer(buffer, message_length);
58 TEST(pb_decode(&istream, Message2_fields, &msg_b));
59
60 TEST(istream.bytes_left == 0);
61 TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
62 }
63
64 {
65 pb_byte_t buffer[Message3_size];
66 Message3 msg_a = Message3_init_zero;
67 Message3 msg_b = Message3_init_zero;
68
69 pb_ostream_t ostream;
70 pb_istream_t istream;
71 size_t message_length;
72
73 msg_a.data1[0].data[0].data[0] = 1;
74 msg_a.data1[0].data[0].data[1] = 2;
75 msg_a.data1[0].data[0].data[2] = 3;
76 msg_a.data1[0].data[1].data[0] = 4;
77 msg_a.data1[0].data[1].data[1] = 5;
78 msg_a.data1[0].data[1].data[2] = 6;
79
80 msg_a.data1[1].data[0].data[0] = 7;
81 msg_a.data1[1].data[0].data[1] = 8;
82 msg_a.data1[1].data[0].data[2] = 9;
83 msg_a.data1[1].data[1].data[0] = 10;
84 msg_a.data1[1].data[1].data[1] = 11;
85 msg_a.data1[1].data[1].data[2] = 12;
86
87 msg_a.data2[0].data[0].data[0] = 11;
88 msg_a.data2[0].data[0].data[1] = 12;
89 msg_a.data2[0].data[0].data[2] = 13;
90 msg_a.data2[0].data[1].data[0] = 14;
91 msg_a.data2[0].data[1].data[1] = 15;
92 msg_a.data2[0].data[1].data[2] = 16;
93
94 msg_a.data2[1].data[0].data[0] = 17;
95 msg_a.data2[1].data[0].data[1] = 18;
96 msg_a.data2[1].data[0].data[2] = 19;
97 msg_a.data2[1].data[1].data[0] = 110;
98 msg_a.data2[1].data[1].data[1] = 111;
99 msg_a.data2[1].data[1].data[2] = 112;
100
101 ostream = pb_ostream_from_buffer(buffer, Message3_size);
102 TEST(pb_encode(&ostream, Message3_fields, &msg_a));
103 message_length = ostream.bytes_written;
104
105 istream = pb_istream_from_buffer(buffer, message_length);
106 TEST(pb_decode(&istream, Message3_fields, &msg_b));
107
108 TEST(istream.bytes_left == 0);
109 TEST(memcmp(&msg_b, &msg_a, sizeof(msg_a)) == 0);
110 }
111
112 if (status != 0)
113 fprintf(stdout, "\n\nSome tests FAILED!\n");
114
115 return status;
116 }
117