MOS Source Code
Loading...
Searching...
No Matches
optional.c
Go to the documentation of this file.
1#include <pb_encode.h>
2#include <pb_decode.h>
3#include <unittests.h>
4#include "optional.pb.h"
5
6int main()
7{
8 int status = 0;
9 uint8_t buf[256];
10 size_t msglen;
11
12 COMMENT("Test encoding message with optional field")
13 {
14 pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
15 TestMessage msg = TestMessage_init_zero;
16
17 msg.has_opt_int = true;
18 msg.opt_int = 99;
19 msg.normal_int = 100;
20 msg.opt_int2 = 101;
21
22 TEST(pb_encode(&stream, TestMessage_fields, &msg));
23 msglen = stream.bytes_written;
24 }
25
26 COMMENT("Test decoding message with optional field")
27 {
28 pb_istream_t stream = pb_istream_from_buffer(buf, msglen);
29 TestMessage msg = TestMessage_init_zero;
30
31 /* These fields should be missing from the message
32 * so the values wouldn't be overwritten. */
33 msg.opt_int2 = 5;
34 msg.normal_int2 = 6;
35
36 TEST(pb_decode_noinit(&stream, TestMessage_fields, &msg));
37 TEST(msg.has_opt_int);
38 TEST(msg.opt_int == 99);
39 TEST(msg.normal_int == 100);
40 TEST(!msg.has_opt_int2);
41 TEST(msg.opt_int2 == 5);
42 TEST(msg.normal_int2 == 6);
43 }
44
45 return status;
46}
47
int main()
Definition optional.c:6
pb_istream_t pb_istream_from_buffer(const pb_byte_t *buf, size_t msglen)
Definition pb_decode.c:143
#define pb_decode_noinit(s, f, d)
Definition pb_decode.h:105
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