unittests, change to PB_LTYPE_BYTES data size
[apps/agl-service-can-low-level.git] / tests / decode_unittests.c
1 #include <stdio.h>
2 #include <string.h>
3 #include "pb_decode.h"
4 #include "unittests.h"
5
6 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x) - 1)
7
8 bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
9 {
10     if (stream->state != NULL)
11         return false; /* Simulate error */
12     
13     if (buf != NULL)
14         memset(buf, 'x', count);
15     return true;
16 }
17
18 int main()
19 {
20     int status = 0;
21     
22     {
23         uint8_t buffer1[] = "foobartest1234";
24         uint8_t buffer2[sizeof(buffer1)];
25         pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1));
26         
27         COMMENT("Test pb_read and pb_istream_t");
28         TEST(pb_read(&stream, buffer2, 6))
29         TEST(memcmp(buffer2, "foobar", 6) == 0)
30         TEST(stream.bytes_left == sizeof(buffer1) - 6)
31         TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left))
32         TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0)
33         TEST(stream.bytes_left == 0)
34         TEST(!pb_read(&stream, buffer2, 1))
35     }
36     
37     {
38         uint8_t buffer[20];
39         pb_istream_t stream = {&stream_callback, NULL, 20};
40         
41         COMMENT("Test pb_read with custom callback");
42         TEST(pb_read(&stream, buffer, 5))
43         TEST(memcmp(buffer, "xxxxx", 5) == 0)
44         TEST(!pb_read(&stream, buffer, 50))
45         stream.state = (void*)1; /* Simulated error return from callback */
46         TEST(!pb_read(&stream, buffer, 5))
47         stream.state = NULL;
48         TEST(pb_read(&stream, buffer, 15))
49     }
50     
51     {
52         pb_istream_t s;
53         uint64_t u;
54         int64_t i;
55         
56         COMMENT("Test pb_decode_varint");
57         TEST((s = S("\x00"), pb_decode_varint(&s, &u) && u == 0));
58         TEST((s = S("\x01"), pb_decode_varint(&s, &u) && u == 1));
59         TEST((s = S("\xAC\x02"), pb_decode_varint(&s, &u) && u == 300));
60         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, &u) && u == UINT32_MAX));
61         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint(&s, (uint64_t*)&i) && i == UINT32_MAX));
62         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
63               pb_decode_varint(&s, (uint64_t*)&i) && i == -1));
64         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
65               pb_decode_varint(&s, &u) && u == UINT64_MAX));
66     }
67     
68     {
69         pb_istream_t s;
70         COMMENT("Test pb_skip_varint");
71         TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 6))
72         TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 6))
73         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
74               pb_skip_varint(&s) && s.bytes_left == 6))
75     }
76     
77     {
78         pb_istream_t s;
79         COMMENT("Test pb_skip_string")
80         TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 6))
81         TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 6))
82     }
83     
84     {
85         pb_istream_t s = S("\x01\xFF\xFF\x03");
86         pb_field_t f = {1, PB_LTYPE_VARINT, 0, 0, 4, 0, 0};
87         uint32_t d;
88         COMMENT("Test pb_dec_varint using uint32_t")
89         TEST(pb_dec_varint(&s, &f, &d) && d == 1)
90         
91         /* Verify that no more than data_size is written. */
92         d = 0;
93         f.data_size = 1;
94         TEST(pb_dec_varint(&s, &f, &d) && (d == 0xFF || d == 0xFF000000))
95     }
96     
97     {
98         pb_istream_t s;
99         pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 4, 0, 0};
100         int32_t d;
101         
102         COMMENT("Test pb_dec_svarint using int32_t")
103         TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
104         TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
105         TEST((s = S("\xfe\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MAX))
106         TEST((s = S("\xff\xff\xff\xff\x0f"), pb_dec_svarint(&s, &f, &d) && d == INT32_MIN))
107     }
108     
109     {
110         pb_istream_t s;
111         pb_field_t f = {1, PB_LTYPE_SVARINT, 0, 0, 8, 0, 0};
112         uint64_t d;
113         
114         COMMENT("Test pb_dec_svarint using uint64_t")
115         TEST((s = S("\x01"), pb_dec_svarint(&s, &f, &d) && d == -1))
116         TEST((s = S("\x02"), pb_dec_svarint(&s, &f, &d) && d == 1))
117         TEST((s = S("\xFE\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MAX))
118         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"), pb_dec_svarint(&s, &f, &d) && d == INT64_MIN))
119     }
120     
121     {
122         pb_istream_t s;
123         pb_field_t f = {1, PB_LTYPE_FIXED, 0, 0, 4, 0, 0};
124         float d;
125         
126         COMMENT("Test pb_dec_fixed using float (failures here may be caused by imperfect rounding)")
127         TEST((s = S("\x00\x00\x00\x00"), pb_dec_fixed(&s, &f, &d) && d == 0.0f))
128         TEST((s = S("\x00\x00\xc6\x42"), pb_dec_fixed(&s, &f, &d) && d == 99.0f))
129         TEST((s = S("\x4e\x61\x3c\xcb"), pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
130         TEST((s = S("\x00"), !pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
131     }
132     
133     {
134         pb_istream_t s;
135         pb_field_t f = {1, PB_LTYPE_FIXED, 0, 0, 8, 0, 0};
136         double d;
137         
138         COMMENT("Test pb_dec_fixed using double (failures here may be caused by imperfect rounding)")
139         TEST((s = S("\x00\x00\x00\x00\x00\x00\x00\x00"), pb_dec_fixed(&s, &f, &d) && d == 0.0))
140         TEST((s = S("\x00\x00\x00\x00\x00\xc0\x58\x40"), pb_dec_fixed(&s, &f, &d) && d == 99.0))
141         TEST((s = S("\x00\x00\x00\xc0\x29\x8c\x67\xc1"), pb_dec_fixed(&s, &f, &d) && d == -12345678.0f))
142     }
143     
144     {
145         pb_istream_t s;
146         struct { size_t size; uint8_t bytes[5]; } d;
147         pb_field_t f = {1, PB_LTYPE_BYTES, 0, 0, 5, 0, 0};
148         
149         COMMENT("Test pb_dec_bytes")
150         TEST((s = S("\x00"), pb_dec_bytes(&s, &f, &d) && d.size == 0))
151         TEST((s = S("\x01\xFF"), pb_dec_bytes(&s, &f, &d) && d.size == 1 && d.bytes[0] == 0xFF))
152         TEST((s = S("\x06xxxxxx"), !pb_dec_bytes(&s, &f, &d)))
153         TEST((s = S("\x05xxxxx"), pb_dec_bytes(&s, &f, &d) && d.size == 5))
154         TEST((s = S("\x05xxxx"), !pb_dec_bytes(&s, &f, &d)))
155     }
156     
157     {
158         pb_istream_t s;
159         pb_field_t f = {1, PB_LTYPE_STRING, 0, 0, 5, 0, 0};
160         char d[5];
161         
162         COMMENT("Test pb_dec_string")
163         TEST((s = S("\x00"), pb_dec_string(&s, &f, &d) && d[0] == '\0'))
164         TEST((s = S("\x04xyzz"), pb_dec_string(&s, &f, &d) && strcmp(d, "xyzz") == 0))
165         TEST((s = S("\x05xyzzy"), !pb_dec_string(&s, &f, &d)))
166     }
167     
168     if (status != 0)
169         fprintf(stdout, "\n\nSome tests FAILED!\n");
170     
171     return status;
172 }