1 /* Decode a message using oneof fields */
8 #include "test_helpers.h"
11 /* Test the 'OneOfMessage' */
12 int test_oneof_1(pb_istream_t *stream, int option)
17 /* To better catch initialization errors */
18 memset(&msg, 0xAA, sizeof(msg));
20 if (!pb_decode(stream, OneOfMessage_fields, &msg))
22 printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
26 /* Check that the basic fields work normally */
27 TEST(msg.prefix == 123);
28 TEST(msg.suffix == 321);
30 /* Check that we got the right oneof according to command line */
33 TEST(msg.which_values == OneOfMessage_first_tag);
34 TEST(msg.values.first == 999);
38 TEST(msg.which_values == OneOfMessage_second_tag);
39 TEST(strcmp(msg.values.second, "abcd") == 0);
43 TEST(msg.which_values == OneOfMessage_third_tag);
44 TEST(msg.values.third.array[0] == 1);
45 TEST(msg.values.third.array[1] == 2);
46 TEST(msg.values.third.array[2] == 3);
47 TEST(msg.values.third.array[3] == 4);
48 TEST(msg.values.third.array[4] == 5);
55 /* Test the 'PlainOneOfMessage' */
56 int test_oneof_2(pb_istream_t *stream, int option)
58 PlainOneOfMessage msg = PlainOneOfMessage_init_zero;
61 if (!pb_decode(stream, PlainOneOfMessage_fields, &msg))
63 printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
67 /* Check that we got the right oneof according to command line */
70 TEST(msg.which_values == OneOfMessage_first_tag);
71 TEST(msg.values.first == 999);
75 TEST(msg.which_values == OneOfMessage_second_tag);
76 TEST(strcmp(msg.values.second, "abcd") == 0);
80 TEST(msg.which_values == OneOfMessage_third_tag);
81 TEST(msg.values.third.array[0] == 1);
82 TEST(msg.values.third.array[1] == 2);
83 TEST(msg.values.third.array[2] == 3);
84 TEST(msg.values.third.array[3] == 4);
85 TEST(msg.values.third.array[4] == 5);
91 int main(int argc, char **argv)
93 uint8_t buffer[OneOfMessage_size];
99 fprintf(stderr, "Usage: decode_oneof [number]\n");
102 option = atoi(argv[1]);
104 SET_BINARY_MODE(stdin);
105 count = fread(buffer, 1, sizeof(buffer), stdin);
109 printf("Message does not fit in buffer\n");
117 stream = pb_istream_from_buffer(buffer, count);
118 status = test_oneof_1(&stream, option);
123 stream = pb_istream_from_buffer(buffer, count);
124 status = test_oneof_2(&stream, option);