5578a0abfbc7168231c230eecc5e8c22f8ac9dc0
[apps/low-level-can-service.git] / tests / encode_unittests.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "pb_encode.h"
4 #include "unittests.h"
5 #include "unittestproto.pb.h"
6
7 bool streamcallback(pb_ostream_t *stream, const uint8_t *buf, size_t count)
8 {
9     /* Allow only 'x' to be written */
10     while (count--)
11     {
12         if (*buf++ != 'x')
13             return false;
14     }
15     return true;
16 }
17
18 /* Check that expression x writes data y.
19  * Y is a string, which may contain null bytes. Null terminator is ignored.
20  */
21 #define WRITES(x, y) \
22 memset(buffer, 0xAA, sizeof(buffer)), \
23 s = pb_ostream_from_buffer(buffer, sizeof(buffer)), \
24 (x) && \
25 memcmp(buffer, y, sizeof(y) - 1) == 0 && \
26 buffer[sizeof(y) - 1] == 0xAA
27
28 int main()
29 {
30     int status = 0;
31     
32     {
33         uint8_t buffer1[] = "foobartest1234";
34         uint8_t buffer2[sizeof(buffer1)];
35         pb_ostream_t stream = pb_ostream_from_buffer(buffer2, sizeof(buffer1));
36         
37         COMMENT("Test pb_write and pb_ostream_t");
38         TEST(pb_write(&stream, buffer1, sizeof(buffer1)));
39         TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0);
40         TEST(!pb_write(&stream, buffer1, 1));
41         TEST(stream.bytes_written == sizeof(buffer1));
42     }
43     
44     {
45         uint8_t buffer1[] = "xxxxxxx";
46         pb_ostream_t stream = {&streamcallback, 0, SIZE_MAX, 0};
47         
48         COMMENT("Test pb_write with custom callback");
49         TEST(pb_write(&stream, buffer1, 5));
50         buffer1[0] = 'a';
51         TEST(!pb_write(&stream, buffer1, 5));
52     }
53     
54     {
55         uint8_t buffer[30];
56         pb_ostream_t s;
57         
58         COMMENT("Test pb_encode_varint")
59         TEST(WRITES(pb_encode_varint(&s, 0), "\0"));
60         TEST(WRITES(pb_encode_varint(&s, 1), "\1"));
61         TEST(WRITES(pb_encode_varint(&s, 0x7F), "\x7F"));
62         TEST(WRITES(pb_encode_varint(&s, 0x80), "\x80\x01"));
63         TEST(WRITES(pb_encode_varint(&s, UINT32_MAX), "\xFF\xFF\xFF\xFF\x0F"));
64         TEST(WRITES(pb_encode_varint(&s, UINT64_MAX), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
65     }
66     
67     {
68         uint8_t buffer[30];
69         pb_ostream_t s;
70         
71         COMMENT("Test pb_encode_tag")
72         TEST(WRITES(pb_encode_tag(&s, PB_WT_STRING, 5), "\x2A"));
73         TEST(WRITES(pb_encode_tag(&s, PB_WT_VARINT, 99), "\x98\x06"));
74     }
75     
76     {
77         uint8_t buffer[30];
78         pb_ostream_t s;
79         pb_field_t field = {10, PB_LTYPE_SVARINT};
80         
81         COMMENT("Test pb_encode_tag_for_field")
82         TEST(WRITES(pb_encode_tag_for_field(&s, &field), "\x50"));
83     }
84     
85     {
86         uint8_t buffer[30];
87         pb_ostream_t s;
88         
89         COMMENT("Test pb_encode_string")
90         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd", 4), "\x04""abcd"));
91         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"abcd\x00", 5), "\x05""abcd\x00"));
92         TEST(WRITES(pb_encode_string(&s, (const uint8_t*)"", 0), "\x00"));
93     }
94     
95     {
96         uint8_t buffer[30];
97         pb_ostream_t s;
98         uint8_t value = 1;
99         int8_t svalue = -1;
100         int32_t max = INT32_MAX;
101         int32_t min = INT32_MIN;
102         int64_t lmax = INT64_MAX;
103         int64_t lmin = INT64_MIN;
104         pb_field_t field = {1, PB_LTYPE_VARINT, 0, 0, sizeof(value)};
105         
106         COMMENT("Test pb_enc_varint and pb_enc_svarint")
107         TEST(WRITES(pb_enc_varint(&s, &field, &value), "\x01"));
108         TEST(WRITES(pb_enc_svarint(&s, &field, &svalue), "\x01"));
109         TEST(WRITES(pb_enc_svarint(&s, &field, &value), "\x02"));
110         
111         field.data_size = sizeof(max);
112         TEST(WRITES(pb_enc_svarint(&s, &field, &max), "\xfe\xff\xff\xff\x0f"));
113         TEST(WRITES(pb_enc_svarint(&s, &field, &min), "\xff\xff\xff\xff\x0f"));
114         
115         field.data_size = sizeof(lmax);
116         TEST(WRITES(pb_enc_svarint(&s, &field, &lmax), "\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
117         TEST(WRITES(pb_enc_svarint(&s, &field, &lmin), "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"));
118     }
119     
120     {
121         uint8_t buffer[30];
122         pb_ostream_t s;
123         pb_field_t field = {1, PB_LTYPE_FIXED, 0, 0, sizeof(float)};
124         float fvalue;
125         double dvalue;
126         
127         COMMENT("Test pb_enc_fixed using float")
128         fvalue = 0.0f;
129         TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x00\x00\x00\x00"))
130         fvalue = 99.0f;
131         TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x00\x00\xc6\x42"))
132         fvalue = -12345678.0f;
133         TEST(WRITES(pb_enc_fixed(&s, &field, &fvalue), "\x4e\x61\x3c\xcb"))
134     
135         COMMENT("Test pb_enc_fixed using double")
136         field.data_size = sizeof(double);
137         dvalue = 0.0;
138         TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\x00\x00\x00\x00\x00"))
139         dvalue = 99.0;
140         TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\x00\x00\xc0\x58\x40"))
141         dvalue = -12345678.0;
142         TEST(WRITES(pb_enc_fixed(&s, &field, &dvalue), "\x00\x00\x00\xc0\x29\x8c\x67\xc1"))
143     }
144     
145     {
146         uint8_t buffer[30];
147         pb_ostream_t s;
148         struct { size_t size; uint8_t bytes[5]; } value = {5, {'x', 'y', 'z', 'z', 'y'}};
149     
150         COMMENT("Test pb_enc_bytes")
151         TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x05xyzzy"))
152         value.size = 0;
153         TEST(WRITES(pb_enc_bytes(&s, NULL, &value), "\x00"))
154     }
155     
156     {
157         uint8_t buffer[30];
158         pb_ostream_t s;
159         char value[] = "xyzzy";
160         
161         COMMENT("Test pb_enc_string")
162         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x05xyzzy"))
163         value[0] = '\0';
164         TEST(WRITES(pb_enc_string(&s, NULL, &value), "\x00"))
165     }
166     
167     {
168         uint8_t buffer[10];
169         pb_ostream_t s;
170         IntegerArray msg = {5, {1, 2, 3, 4, 5}};
171         
172         COMMENT("Test pb_encode with int32 array")
173         
174         TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), "\x0A\x05\x01\x02\x03\x04\x05"))
175         
176         msg.data_count = 0;
177         TEST(WRITES(pb_encode(&s, IntegerArray_fields, &msg), ""))
178         
179         msg.data_count = 10;
180         TEST(!pb_encode(&s, IntegerArray_fields, &msg))
181     }
182     
183     {
184         
185         
186     }
187     
188     if (status != 0)
189         fprintf(stdout, "\n\nSome tests FAILED!\n");
190     
191     return status;
192 }