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