LCOV - code coverage report
Current view: top level - enum_sizes - enumsizes_unittests.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 35 36 97.2 %
Date: 2023-02-14 20:10:26 Functions: 1 1 100.0 %

          Line data    Source code
       1             : #include <stdio.h>
       2             : #include <string.h>
       3             : #include <pb_decode.h>
       4             : #include <pb_encode.h>
       5             : #include "unittests.h"
       6             : #include "enumsizes.pb.h"
       7             : 
       8           1 : int main()
       9             : {
      10           1 :     int status = 0;
      11             : 
      12           1 :     UnpackedEnums msg1 = {
      13             :         UU8_MIN,  UU8_MAX,
      14             :         UI8_MIN,  UI8_MAX,
      15             :         UU16_MIN, UU16_MAX,
      16             :         UI16_MIN, UI16_MAX,
      17             :     };
      18             :     
      19             :     PackedEnums msg2;
      20             :     UnpackedEnums msg3;
      21             :     uint8_t buf[256];
      22             :     size_t msgsize;
      23             :     
      24           1 :     COMMENT("Step 1: unpacked enums -> protobuf");
      25             :     {
      26           1 :         pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
      27           1 :         TEST(pb_encode(&s, UnpackedEnums_fields, &msg1));
      28           1 :         msgsize = s.bytes_written;
      29             :     }
      30             :     
      31           1 :     COMMENT("Step 2: protobuf -> packed enums");
      32             :     {
      33           1 :         pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
      34           1 :         TEST(pb_decode(&s, PackedEnums_fields, &msg2));
      35             :         
      36           1 :         TEST(msg1.u8_min  == (int)msg2.u8_min);
      37           1 :         TEST(msg1.u8_max  == (int)msg2.u8_max);
      38           1 :         TEST(msg1.i8_min  == (int)msg2.i8_min);
      39           1 :         TEST(msg1.i8_max  == (int)msg2.i8_max);
      40           1 :         TEST(msg1.u16_min == (int)msg2.u16_min);
      41           1 :         TEST(msg1.u16_max == (int)msg2.u16_max);
      42           1 :         TEST(msg1.i16_min == (int)msg2.i16_min);
      43           1 :         TEST(msg1.i16_max == (int)msg2.i16_max);
      44             :     }
      45             :     
      46           1 :     COMMENT("Step 3: packed enums -> protobuf");
      47             :     {
      48           1 :         pb_ostream_t s = pb_ostream_from_buffer(buf, sizeof(buf));
      49           1 :         TEST(pb_encode(&s, PackedEnums_fields, &msg2));
      50           1 :         msgsize = s.bytes_written;
      51             :     }
      52             :     
      53           1 :     COMMENT("Step 4: protobuf -> unpacked enums");
      54             :     {
      55           1 :         pb_istream_t s = pb_istream_from_buffer(buf, msgsize);
      56           1 :         TEST(pb_decode(&s, UnpackedEnums_fields, &msg3));
      57             : 
      58           1 :         TEST(msg1.u8_min  == (int)msg3.u8_min);
      59           1 :         TEST(msg1.u8_max  == (int)msg3.u8_max);
      60           1 :         TEST(msg1.i8_min  == (int)msg3.i8_min);
      61           1 :         TEST(msg1.i8_max  == (int)msg3.i8_max);
      62           1 :         TEST(msg1.u16_min == (int)msg2.u16_min);
      63           1 :         TEST(msg1.u16_max == (int)msg2.u16_max);
      64           1 :         TEST(msg1.i16_min == (int)msg2.i16_min);
      65           1 :         TEST(msg1.i16_max == (int)msg2.i16_max);
      66             :     }
      67             : 
      68           1 :     if (status != 0)
      69           0 :         fprintf(stdout, "\n\nSome tests FAILED!\n");
      70             : 
      71           1 :     return status;
      72             : }

Generated by: LCOV version 1.14