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