MOS Source Code
Loading...
Searching...
No Matches
mixed.c
Go to the documentation of this file.
1#include <pb_encode.h>
2#include <pb_decode.h>
3
4#include "mixed.pb.h"
5#include "unittests.h"
6
7int main()
8{
9 int status = 0;
10 pb_byte_t buf[64];
11 size_t msglen;
12
13 {
14 pb_ostream_t ostream = pb_ostream_from_buffer(buf, sizeof(buf));
15 MixedMessage msg = MixedMessage_init_default;
16
17 msg.has_proto2_value = true;
18 msg.proto2_value = 0;
19 msg.proto3_value = 0;
20
21 if (!pb_encode(&ostream, MixedMessage_fields, &msg))
22 {
23 fprintf(stderr, "pb_encode() failed: %s\n", PB_GET_ERROR(&ostream));
24 return 1;
25 }
26
27 msglen = ostream.bytes_written;
28 TEST(msglen > 0);
29 }
30
31 {
32 pb_istream_t istream = pb_istream_from_buffer(buf, msglen);
33 MixedMessage msg = MixedMessage_init_default;
34
35 TEST(msg.proto2_value == 100);
36 TEST(msg.proto3_value == 200);
37
38 if (!pb_decode(&istream, MixedMessage_fields, &msg))
39 {
40 fprintf(stderr, "pb_decode() failed: %s\n", PB_GET_ERROR(&istream));
41 return 1;
42 }
43
44 TEST(msg.proto2_value == 0);
45 TEST(msg.proto3_value == 0);
46 }
47
48 return status;
49}
#define stderr
Definition mos_stdio.h:32
int main()
Definition mixed.c:7
uint_least8_t pb_byte_t
Definition pb.h:227
#define PB_GET_ERROR(stream)
Definition pb.h:891
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
#define TEST(x)
Definition test.h:3