1 #include <stdio.h>
2 #include <string.h>
3 #include <pb_decode.h>
4 #include <pb_encode.h>
5 #include "unittests.h"
6 #include "inline.pb.h"
7 
8 int main()
9 {
10     int status = 0;
11     int i = 0;
12     COMMENT("Test inline byte fields");
13 
14     {
15       Message1 msg1 = Message1_init_zero;
16       TEST(sizeof(msg1.data) == 32);
17     }
18 
19     {
20       Message1 msg1 = Message1_init_zero;
21       pb_byte_t msg1_buffer[Message1_size];
22       pb_ostream_t ostream = pb_ostream_from_buffer(msg1_buffer, Message1_size);
23       Message1 msg1_deserialized = Message1_init_zero;
24       pb_istream_t istream = pb_istream_from_buffer(msg1_buffer, Message1_size);
25 
26       for (i = 0; i < 32; i++) {
27         msg1.data[i] = i;
28       }
29 
30       TEST(pb_encode(&ostream, Message1_fields, &msg1));
31       TEST(ostream.bytes_written == Message1_size);
32 
33       TEST(pb_decode(&istream, Message1_fields, &msg1_deserialized));
34 
35       TEST(istream.bytes_left == 0);
36       TEST(memcmp(&msg1_deserialized, &msg1, sizeof(msg1)) == 0);
37     }
38 
39     {
40       Message2 msg2 = {true, {0}};
41       Message2 msg2_no_data = {false, {1}};
42       pb_byte_t msg2_buffer[Message2_size];
43       pb_ostream_t ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
44       Message2 msg2_deserialized = Message2_init_zero;
45       pb_istream_t istream = pb_istream_from_buffer(msg2_buffer, Message2_size);
46 
47       for (i = 0; i < 64; i++) {
48         msg2.data[i] = i;
49       }
50 
51       TEST(pb_encode(&ostream, Message2_fields, &msg2));
52       TEST(ostream.bytes_written == Message2_size);
53 
54       TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
55 
56       TEST(istream.bytes_left == 0);
57       TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) == 0);
58       TEST(msg2_deserialized.has_data);
59 
60       memset(msg2_buffer, 0, sizeof(msg2_buffer));
61       ostream = pb_ostream_from_buffer(msg2_buffer, Message2_size);
62       TEST(pb_encode(&ostream, Message2_fields, &msg2_no_data));
63       istream = pb_istream_from_buffer(msg2_buffer, Message2_size);
64       TEST(pb_decode(&istream, Message2_fields, &msg2_deserialized));
65       TEST(!msg2_deserialized.has_data);
66       TEST(memcmp(&msg2_deserialized, &msg2, sizeof(msg2)) != 0);
67     }
68 
69     if (status != 0)
70         fprintf(stdout, "\n\nSome tests FAILED!\n");
71 
72     return status;
73 }
74