MOS Source Code
Loading...
Searching...
No Matches
enumsizes_unittests.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <string.h>
3#include <pb_decode.h>
4#include <pb_encode.h>
5#include "unittests.h"
6#include "enumsizes.pb.h"
8
9int main()
10{
11 int status = 0;
12
13 UnpackedEnums msg1 = {
14 UU8_MIN, UU8_MAX,
15 UI8_MIN, UI8_MAX,
16 UU16_MIN, UU16_MAX,
17 UI16_MIN, UI16_MAX,
18 };
19
20 PackedEnums msg2;
21 UnpackedEnums msg3;
22 uint8_t buf[256];
23 size_t msgsize;
24
25 COMMENT("Step 1: unpacked enums -> protobuf");
26 {
27 pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
28 TEST(pb_encode(&s, UnpackedEnums_fields, &msg1));
29 msgsize = s.bytes_written;
30 }
31
32 COMMENT("Step 2: protobuf -> packed enums");
33 {
34 pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
35 TEST(pb_decode(&s, PackedEnums_fields, &msg2));
36
37 TEST(msg1.u8_min == (int)msg2.u8_min);
38 TEST(msg1.u8_max == (int)msg2.u8_max);
39 TEST(msg1.i8_min == (int)msg2.i8_min);
40 TEST(msg1.i8_max == (int)msg2.i8_max);
41 TEST(msg1.u16_min == (int)msg2.u16_min);
42 TEST(msg1.u16_max == (int)msg2.u16_max);
43 TEST(msg1.i16_min == (int)msg2.i16_min);
44 TEST(msg1.i16_max == (int)msg2.i16_max);
45 }
46
47 COMMENT("Step 3: packed enums -> protobuf");
48 {
49 pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
50 TEST(pb_encode(&s, PackedEnums_fields, &msg2));
51 msgsize = s.bytes_written;
52 }
53
54 COMMENT("Step 4: protobuf -> unpacked enums");
55 {
56 pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
57 TEST(pb_decode(&s, UnpackedEnums_fields, &msg3));
58
59 TEST(msg1.u8_min == (int)msg3.u8_min);
60 TEST(msg1.u8_max == (int)msg3.u8_max);
61 TEST(msg1.i8_min == (int)msg3.i8_min);
62 TEST(msg1.i8_max == (int)msg3.i8_max);
63 TEST(msg1.u16_min == (int)msg2.u16_min);
64 TEST(msg1.u16_max == (int)msg2.u16_max);
65 TEST(msg1.i16_min == (int)msg2.i16_min);
66 TEST(msg1.i16_max == (int)msg2.i16_max);
67 }
68
69 status |= TestIntSize();
70
71 if (status != 0)
72 fprintf(stdout, "\n\nSome tests FAILED!\n");
73
74 return status;
75}
int main()
#define stdout
Definition mos_stdio.h:31
bool pb_decode(pb_istream_t *stream, const pb_msgdesc_t *fields, void *dest_struct)
Definition pb_decode.c:1182
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
pb_ostream_t pb_ostream_from_buffer(pb_byte_t *buf, size_t bufsize)
Definition pb_encode.c:63
bool pb_encode(pb_ostream_t *stream, const pb_msgdesc_t *fields, const void *src_struct)
Definition pb_encode.c:512
unsigned char uint8_t
Definition pb_syshdr.h:20
#define TEST(x)
Definition test.h:3
#define COMMENT(x)
Definition unittests.h:17