LCOV - code coverage report
Current view: top level - anonymous_oneof - decode_oneof.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 30 37 81.1 %
Date: 2023-02-14 20:10:26 Functions: 2 2 100.0 %

          Line data    Source code
       1             : /* Decode a message using oneof fields */
       2             : 
       3             : #include <stdio.h>
       4             : #include <stdlib.h>
       5             : #include <string.h>
       6             : #include <pb_decode.h>
       7             : #include "oneof.pb.h"
       8             : #include "test_helpers.h"
       9             : #include "unittests.h"
      10             : 
      11             : /* Test the 'AnonymousOneOfMessage' */
      12           3 : int test_oneof_1(pb_istream_t *stream, int option)
      13             : {
      14             :     AnonymousOneOfMessage msg;
      15           3 :     int status = 0;
      16             : 
      17             :     /* To better catch initialization errors */
      18           3 :     memset(&msg, 0xAA, sizeof(msg));
      19             : 
      20           3 :     if (!pb_decode(stream, AnonymousOneOfMessage_fields, &msg))
      21             :     {
      22           0 :         printf("Decoding failed: %s\n", PB_GET_ERROR(stream));
      23           0 :         return 1;
      24             :     }
      25             : 
      26             :     /* Check that the basic fields work normally */
      27           3 :     TEST(msg.prefix == 123);
      28           3 :     TEST(msg.suffix == 321);
      29             : 
      30             :     /* Check that we got the right oneof according to command line */
      31           3 :     if (option == 1)
      32             :     {
      33           1 :         TEST(msg.which_values == AnonymousOneOfMessage_first_tag);
      34           1 :         TEST(msg.first == 999);
      35             :     }
      36           2 :     else if (option == 2)
      37             :     {
      38           1 :         TEST(msg.which_values == AnonymousOneOfMessage_second_tag);
      39           1 :         TEST(strcmp(msg.second, "abcd") == 0);
      40             :     }
      41           1 :     else if (option == 3)
      42             :     {
      43           1 :         TEST(msg.which_values == AnonymousOneOfMessage_third_tag);
      44           1 :         TEST(msg.third.array[0] == 1);
      45           1 :         TEST(msg.third.array[1] == 2);
      46           1 :         TEST(msg.third.array[2] == 3);
      47           1 :         TEST(msg.third.array[3] == 4);
      48           1 :         TEST(msg.third.array[4] == 5);
      49             :     }
      50             : 
      51           3 :     return status;
      52             : }
      53             : 
      54           3 : int main(int argc, char **argv)
      55             : {
      56             :     uint8_t buffer[AnonymousOneOfMessage_size];
      57             :     size_t count;
      58             :     int option;
      59             : 
      60           3 :     if (argc != 2)
      61             :     {
      62           0 :         fprintf(stderr, "Usage: decode_oneof [number]\n");
      63           0 :         return 1;
      64             :     }
      65           3 :     option = atoi(argv[1]);
      66             : 
      67             :     SET_BINARY_MODE(stdin);
      68           3 :     count = fread(buffer, 1, sizeof(buffer), stdin);
      69             : 
      70           3 :     if (!feof(stdin))
      71             :     {
      72           0 :         printf("Message does not fit in buffer\n");
      73           0 :         return 1;
      74             :     }
      75             : 
      76             :     {
      77           3 :         int status = 0;
      78             :         pb_istream_t stream;
      79             : 
      80           3 :         stream = pb_istream_from_buffer(buffer, count);
      81           3 :         status = test_oneof_1(&stream, option);
      82             : 
      83           3 :         if (status != 0)
      84           0 :             return status;
      85             :     }
      86             : 
      87           3 :     return 0;
      88             : }

Generated by: LCOV version 1.14