Unittests for encode
[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     if (status != 0)
120         fprintf(stdout, "\n\nSome tests FAILED!\n");
121     
122     return status;
123 }