746b7e9656d2e039e6cfbdc2841f126aaa2eb916
[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
5 #define COMMENT(x) printf("\n----" x "----\n");
6 #define STR(x) #x
7 #define STR2(x) STR(x)
8 #define TEST(x) \
9     if (!(x)) { \
10         fprintf(stderr, __FILE__ ":" STR2(__LINE__) " FAILED:" #x "\n"); \
11         status = 1; \
12     } else { \
13         printf("OK: " #x "\n"); \
14     }
15
16 #define S(x) pb_istream_from_buffer((uint8_t*)x, sizeof(x))
17
18 bool stream_callback(pb_istream_t *stream, uint8_t *buf, size_t count)
19 {
20     if (stream->state != NULL)
21         return false; /* Simulate error */
22     
23     if (buf != NULL)
24         memset(buf, 'x', count);
25     return true;
26 }
27
28 int main()
29 {
30     int status = 0;
31     
32     {
33         uint8_t buffer1[] = "foobartest1234";
34         uint8_t buffer2[sizeof(buffer1)];
35         pb_istream_t stream = pb_istream_from_buffer(buffer1, sizeof(buffer1));
36         
37         COMMENT("Test pb_read and pb_istream_t");
38         TEST(pb_read(&stream, buffer2, 6))
39         TEST(memcmp(buffer2, "foobar", 6) == 0)
40         TEST(stream.bytes_left == sizeof(buffer1) - 6)
41         TEST(pb_read(&stream, buffer2 + 6, stream.bytes_left))
42         TEST(memcmp(buffer1, buffer2, sizeof(buffer1)) == 0)
43         TEST(stream.bytes_left == 0)
44         TEST(!pb_read(&stream, buffer2, 1))
45     }
46     
47     {
48         uint8_t buffer[20];
49         pb_istream_t stream = {&stream_callback, NULL, 20};
50         
51         COMMENT("Test pb_read with custom callback");
52         TEST(pb_read(&stream, buffer, 5))
53         TEST(memcmp(buffer, "xxxxx", 5) == 0)
54         TEST(!pb_read(&stream, buffer, 50))
55         stream.state = (void*)1; /* Simulated error return from callback */
56         TEST(!pb_read(&stream, buffer, 5))
57         stream.state = NULL;
58         TEST(pb_read(&stream, buffer, 15))
59     }
60     
61     {
62         pb_istream_t s;
63         uint32_t u;
64         int32_t i;
65         
66         COMMENT("Test pb_decode_varint32");
67         TEST((s = S("\x00"), pb_decode_varint32(&s, &u) && u == 0));
68         TEST((s = S("\x01"), pb_decode_varint32(&s, &u) && u == 1));
69         TEST((s = S("\xAC\x02"), pb_decode_varint32(&s, &u) && u == 300));
70         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, &u) && u == UINT32_MAX));
71         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint32(&s, (uint32_t*)&i) && i == -1));
72     }
73     
74     {
75         pb_istream_t s;
76         uint64_t u;
77         int64_t i;
78         
79         COMMENT("Test pb_decode_varint64");
80         TEST((s = S("\x00"), pb_decode_varint64(&s, &u) && u == 0));
81         TEST((s = S("\x01"), pb_decode_varint64(&s, &u) && u == 1));
82         TEST((s = S("\xAC\x02"), pb_decode_varint64(&s, &u) && u == 300));
83         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint64(&s, &u) && u == UINT32_MAX));
84         TEST((s = S("\xFF\xFF\xFF\xFF\x0F"), pb_decode_varint64(&s, (uint64_t*)&i) && i == UINT32_MAX));
85         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
86               pb_decode_varint64(&s, (uint64_t*)&i) && i == -1));
87         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01"),
88               pb_decode_varint64(&s, &u) && u == UINT64_MAX));
89     }
90     
91     {
92         pb_istream_t s;
93         COMMENT("Test pb_skip_varint");
94         TEST((s = S("\x00""foobar"), pb_skip_varint(&s) && s.bytes_left == 7))
95         TEST((s = S("\xAC\x02""foobar"), pb_skip_varint(&s) && s.bytes_left == 7))
96         TEST((s = S("\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01""foobar"),
97               pb_skip_varint(&s) && s.bytes_left == 7))
98     }
99     
100     {
101         pb_istream_t s;
102         COMMENT("Test pb_skip_string")
103         TEST((s = S("\x00""foobar"), pb_skip_string(&s) && s.bytes_left == 7))
104         TEST((s = S("\x04""testfoobar"), pb_skip_string(&s) && s.bytes_left == 7))
105     }
106     
107     if (status != 0)
108         fprintf(stdout, "\n\nSome tests FAILED!\n");
109     
110     return status;
111 }