Fix generator bug when oneof is first field in a message.
[apps/agl-service-can-low-level.git] / tests / oneof / decode_oneof.c
1 /* Decode a message using oneof fields */
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <pb_decode.h>
6 #include "oneof.pb.h"
7 #include "test_helpers.h"
8 #include "unittests.h"
9
10 /* Test the 'OneOfMessage' */
11 int test_oneof_1(pb_istream_t *stream, int option)
12 {
13     OneOfMessage msg = OneOfMessage_init_zero;
14     int status = 0;
15
16     if (!pb_decode(stream, OneOfMessage_fields, &msg))
17     {
18         printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
19         return 1;
20     }
21
22     /* Check that the basic fields work normally */
23     TEST(msg.prefix == 123);
24     TEST(msg.suffix == 321);
25
26     /* Check that we got the right oneof according to command line */
27     if (option == 1)
28     {
29         TEST(msg.which_values == OneOfMessage_first_tag);
30         TEST(msg.values.first == 999);
31     }
32     else if (option == 2)
33     {
34         TEST(msg.which_values == OneOfMessage_second_tag);
35         TEST(strcmp(msg.values.second, "abcd") == 0);
36     }
37     else if (option == 3)
38     {
39         TEST(msg.which_values == OneOfMessage_third_tag);
40         TEST(msg.values.third.array[0] == 1);
41         TEST(msg.values.third.array[1] == 2);
42         TEST(msg.values.third.array[2] == 3);
43         TEST(msg.values.third.array[3] == 4);
44         TEST(msg.values.third.array[4] == 5);
45     }
46
47     return status;
48 }
49
50
51 /* Test the 'PlainOneOfMessage' */
52 int test_oneof_2(pb_istream_t *stream, int option)
53 {
54     PlainOneOfMessage msg = PlainOneOfMessage_init_zero;
55     int status = 0;
56
57     if (!pb_decode(stream, PlainOneOfMessage_fields, &msg))
58     {
59         printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
60         return 1;
61     }
62
63     /* Check that we got the right oneof according to command line */
64     if (option == 1)
65     {
66         TEST(msg.which_values == OneOfMessage_first_tag);
67         TEST(msg.values.first == 999);
68     }
69     else if (option == 2)
70     {
71         TEST(msg.which_values == OneOfMessage_second_tag);
72         TEST(strcmp(msg.values.second, "abcd") == 0);
73     }
74     else if (option == 3)
75     {
76         TEST(msg.which_values == OneOfMessage_third_tag);
77         TEST(msg.values.third.array[0] == 1);
78         TEST(msg.values.third.array[1] == 2);
79         TEST(msg.values.third.array[2] == 3);
80         TEST(msg.values.third.array[3] == 4);
81         TEST(msg.values.third.array[4] == 5);
82     }
83
84     return status;
85 }
86
87 int main(int argc, char **argv)
88 {
89     uint8_t buffer[OneOfMessage_size];
90     size_t count;
91     int option;
92
93     if (argc != 2)
94     {
95         fprintf(stderr, "Usage: decode_oneof [number]\n");
96         return 1;
97     }
98     option = atoi(argv[1]);
99
100     SET_BINARY_MODE(stdin);
101     count = fread(buffer, 1, sizeof(buffer), stdin);
102
103     if (!feof(stdin))
104     {
105         printf("Message does not fit in buffer\n");
106         return 1;
107     }
108
109     {
110         int status = 0;
111         pb_istream_t stream;
112
113         stream = pb_istream_from_buffer(buffer, count);
114         status = test_oneof_1(&stream, option);
115
116         if (status != 0)
117             return status;
118
119         stream = pb_istream_from_buffer(buffer, count);
120         status = test_oneof_2(&stream, option);
121
122         if (status != 0)
123             return status;
124     }
125
126     return 0;
127 }