1 /* Decode a message using oneof fields */
8 #include "test_helpers.h"
11 /* Test the 'AnonymousOneOfMessage' */
12 int test_oneof_1(pb_istream_t *stream, int option)
14 AnonymousOneOfMessage msg;
17 /* To better catch initialization errors */
18 memset(&msg, 0xAA, sizeof(msg));
20 if (!pb_decode(stream, AnonymousOneOfMessage_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 == AnonymousOneOfMessage_first_tag);
34 TEST(msg.first == 999);
38 TEST(msg.which_values == AnonymousOneOfMessage_second_tag);
39 TEST(strcmp(msg.second, "abcd") == 0);
43 TEST(msg.which_values == AnonymousOneOfMessage_third_tag);
44 TEST(msg.third.array[0] == 1);
45 TEST(msg.third.array[1] == 2);
46 TEST(msg.third.array[2] == 3);
47 TEST(msg.third.array[3] == 4);
48 TEST(msg.third.array[4] == 5);
54 int main(int argc, char **argv)
56 uint8_t buffer[AnonymousOneOfMessage_size];
62 fprintf(stderr, "Usage: decode_oneof [number]\n");
65 option = atoi(argv[1]);
67 SET_BINARY_MODE(stdin);
68 count = fread(buffer, 1, sizeof(buffer), stdin);
72 printf("Message does not fit in buffer\n");
80 stream = pb_istream_from_buffer(buffer, count);
81 status = test_oneof_1(&stream, option);