aboutsummaryrefslogtreecommitdiffstats
path: root/libs/nanopb/tests/regression/issue_229/multiple_oneof.c
blob: 902248d0a5dd7e155527c82254564e4177e94c19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include "multiple_oneof.pb.h"
#include <unittests.h>
#include <pb_encode.h>
#include <pb_decode.h>

int main()
{
    int status = 0;
    uint8_t buf[128];
    size_t msglen;

    {
        pb_ostream_t stream = pb_ostream_from_buffer(buf, sizeof(buf));
        MainMessage msg = MainMessage_init_zero;
        msg.which_oneof1 = MainMessage_oneof1_uint32_tag;
        msg.oneof1.oneof1_uint32 = 1234;
        msg.which_oneof2 = MainMessage_oneof2_uint32_tag;
        msg.oneof2.oneof2_uint32 = 5678;
        TEST(pb_encode(&stream, MainMessage_fields, &msg));
        msglen = stream.bytes_written;
    }
    
    {
        pb_istream_t stream = pb_istream_from_buffer(buf, msglen);
        MainMessage msg = MainMessage_init_zero;
        TEST(pb_decode(&stream, MainMessage_fields, &msg));
        TEST(msg.which_oneof1 == MainMessage_oneof1_uint32_tag);
        TEST(msg.oneof1.oneof1_uint32 == 1234);
        TEST(msg.which_oneof2 == MainMessage_oneof2_uint32_tag);
        TEST(msg.oneof2.oneof2_uint32 == 5678);
    }
    
    return status;
}