1 /* Decode a message using oneof fields */
7 #include "test_helpers.h"
10 /* Test the 'OneOfMessage' */
11 int test_oneof_1(pb_istream_t *stream, int option)
13 OneOfMessage msg = OneOfMessage_init_zero;
16 if (!pb_decode(stream, OneOfMessage_fields, &msg))
18 printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
22 /* Check that the basic fields work normally */
23 TEST(msg.prefix == 123);
24 TEST(msg.suffix == 321);
26 /* Check that we got the right oneof according to command line */
29 TEST(msg.which_values == OneOfMessage_first_tag);
30 TEST(msg.values.first == 999);
34 TEST(msg.which_values == OneOfMessage_second_tag);
35 TEST(strcmp(msg.values.second, "abcd") == 0);
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);
51 /* Test the 'PlainOneOfMessage' */
52 int test_oneof_2(pb_istream_t *stream, int option)
54 PlainOneOfMessage msg = PlainOneOfMessage_init_zero;
57 if (!pb_decode(stream, PlainOneOfMessage_fields, &msg))
59 printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
63 /* Check that we got the right oneof according to command line */
66 TEST(msg.which_values == OneOfMessage_first_tag);
67 TEST(msg.values.first == 999);
71 TEST(msg.which_values == OneOfMessage_second_tag);
72 TEST(strcmp(msg.values.second, "abcd") == 0);
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);
87 int main(int argc, char **argv)
89 uint8_t buffer[OneOfMessage_size];
95 fprintf(stderr, "Usage: decode_oneof [number]\n");
98 option = atoi(argv[1]);
100 SET_BINARY_MODE(stdin);
101 count = fread(buffer, 1, sizeof(buffer), stdin);
105 printf("Message does not fit in buffer\n");
113 stream = pb_istream_from_buffer(buffer, count);
114 status = test_oneof_1(&stream, option);
119 stream = pb_istream_from_buffer(buffer, count);
120 status = test_oneof_2(&stream, option);