LCOV - code coverage report
Current view: top level - alltypes_pointer - encode_alltypes_pointer.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 139 141 98.6 %
Date: 2023-02-14 20:10:26 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /* Attempts to test all the datatypes supported by ProtoBuf.
       2             :  */
       3             : 
       4             : #include <stdio.h>
       5             : #include <stdlib.h>
       6             : #include <string.h>
       7             : #include <pb_encode.h>
       8             : #include "alltypes.pb.h"
       9             : #include "test_helpers.h"
      10             : 
      11           2 : int main(int argc, char **argv)
      12             : {
      13           2 :     int mode = (argc > 1) ? atoi(argv[1]) : 0;
      14             :     
      15             :     /* Values for required fields */
      16           2 :     int32_t     req_int32         = -1001;
      17           2 :     int64_t     req_int64         = -1002;
      18           2 :     uint32_t    req_uint32        = 1003;
      19           2 :     uint64_t    req_uint64        = 1004;
      20           2 :     int32_t     req_sint32        = -1005;
      21           2 :     int64_t     req_sint64        = -1006;
      22           2 :     bool        req_bool          = true;
      23           2 :     uint32_t    req_fixed32       = 1008;
      24           2 :     int32_t     req_sfixed32      = -1009;
      25           2 :     float       req_float         = 1010.0f;
      26           2 :     uint64_t    req_fixed64       = 1011;
      27           2 :     int64_t     req_sfixed64      = -1012;
      28           2 :     double      req_double        = 1013.0;
      29           2 :     char*       req_string        = "1014";
      30           2 :     PB_BYTES_ARRAY_T(4) req_bytes = {4, {'1', '0', '1', '5'}};
      31             :     static int32_t req_substuff   = 1016;
      32           2 :     SubMessage  req_submsg        = {"1016", &req_substuff};
      33           2 :     MyEnum      req_enum          = MyEnum_Truth;
      34           2 :     EmptyMessage req_emptymsg     = {0};
      35           2 :     pb_byte_t   req_fbytes[4]     = {'1', '0', '1', '9'};
      36             :     
      37           2 :     int32_t     end               = 1099;
      38             : 
      39             :     /* Values for repeated fields */
      40           2 :     int32_t     rep_int32[5]      = {0, 0, 0, 0, -2001};
      41           2 :     int64_t     rep_int64[5]      = {0, 0, 0, 0, -2002};
      42           2 :     uint32_t    rep_uint32[5]     = {0, 0, 0, 0, 2003};
      43           2 :     uint64_t    rep_uint64[5]     = {0, 0, 0, 0, 2004};
      44           2 :     int32_t     rep_sint32[5]     = {0, 0, 0, 0, -2005};
      45           2 :     int64_t     rep_sint64[5]     = {0, 0, 0, 0, -2006};
      46           2 :     bool        rep_bool[5]       = {false, false, false, false, true};
      47           2 :     uint32_t    rep_fixed32[5]    = {0, 0, 0, 0, 2008};
      48           2 :     int32_t     rep_sfixed32[5]   = {0, 0, 0, 0, -2009};
      49           2 :     float       rep_float[5]      = {0, 0, 0, 0, 2010.0f};
      50           2 :     uint64_t    rep_fixed64[5]    = {0, 0, 0, 0, 2011};
      51           2 :     int64_t     rep_sfixed64[5]   = {0, 0, 0, 0, -2012};
      52           2 :     double      rep_double[5]     = {0, 0, 0, 0, 2013.0f};
      53           2 :     char*       rep_string[5]     = {"", "", "", "", "2014"};
      54             :     static PB_BYTES_ARRAY_T(4) rep_bytes_4 = {4, {'2', '0', '1', '5'}};
      55           2 :     pb_bytes_array_t *rep_bytes[5]= {NULL, NULL, NULL, NULL, (pb_bytes_array_t*)&rep_bytes_4};
      56             :     static int32_t rep_sub2zero   = 0;
      57             :     static int32_t rep_substuff2  = 2016;
      58             :     static uint32_t rep_substuff3 = 2016;
      59           2 :     SubMessage  rep_submsg[5]     = {{"", &rep_sub2zero},
      60             :                                      {"", &rep_sub2zero},
      61             :                                      {"", &rep_sub2zero},
      62             :                                      {"", &rep_sub2zero},
      63             :                                      {"2016", &rep_substuff2, &rep_substuff3}};
      64           2 :     MyEnum      rep_enum[5]       = {0, 0, 0, 0, MyEnum_Truth};
      65           2 :     EmptyMessage rep_emptymsg[5]  = {{0}, {0}, {0}, {0}, {0}};
      66           2 :     pb_byte_t   rep_fbytes[5][4]  = {{0}, {0}, {0}, {0}, {'2', '0', '1', '9'}};
      67           2 :     int32_t     rep_farray[5]     = {0, 0, 0, 0, 2040};
      68           2 :     uint32_t    rep_farray2[3]    = {0, 0, 2095};
      69             : 
      70             :     /* Values for optional fields */
      71           2 :     int32_t     opt_int32         = 3041;
      72           2 :     int64_t     opt_int64         = 3042;
      73           2 :     uint32_t    opt_uint32        = 3043;
      74           2 :     uint64_t    opt_uint64        = 3044;
      75           2 :     int32_t     opt_sint32        = 3045;
      76           2 :     int64_t     opt_sint64        = 3046;
      77           2 :     bool        opt_bool          = true;
      78           2 :     uint32_t    opt_fixed32       = 3048;
      79           2 :     int32_t     opt_sfixed32      = 3049;
      80           2 :     float       opt_float         = 3050.0f;
      81           2 :     uint64_t    opt_fixed64       = 3051;
      82           2 :     int64_t     opt_sfixed64      = 3052;
      83           2 :     double      opt_double        = 3053.0;
      84           2 :     char*       opt_string        = "3054";
      85           2 :     PB_BYTES_ARRAY_T(4) opt_bytes = {4, {'3', '0', '5', '5'}};
      86             :     static int32_t opt_substuff   = 3056;
      87           2 :     SubMessage  opt_submsg        = {"3056", &opt_substuff};
      88           2 :     MyEnum      opt_enum          = MyEnum_Truth;
      89           2 :     EmptyMessage opt_emptymsg     = {0};
      90           2 :     pb_byte_t   opt_fbytes[4]     = {'3', '0', '5', '9'};
      91             : 
      92             :     static int32_t oneof_substuff = 4059;
      93           2 :     SubMessage  oneof_msg1        = {"4059", &oneof_substuff};
      94             : 
      95           2 :     NonZeroBasedEnum opt_non_zero_based_enum = NonZeroBasedEnum_Three;
      96             : 
      97             :     /* Values for the Limits message. */
      98             :     static int32_t  int32_min  = INT32_MIN;
      99             :     static int32_t  int32_max  = INT32_MAX;
     100             :     static uint32_t uint32_min = 0;
     101             :     static uint32_t uint32_max = UINT32_MAX;
     102             :     static int64_t  int64_min  = INT64_MIN;
     103             :     static int64_t  int64_max  = INT64_MAX;
     104             :     static uint64_t uint64_min = 0;
     105             :     static uint64_t uint64_max = UINT64_MAX;
     106             :     static HugeEnum enum_min   = HugeEnum_Negative;
     107             :     static HugeEnum enum_max   = HugeEnum_Positive;
     108             :     static int32_t largetag    = 1001;
     109           2 :     Limits req_limits = {&int32_min,    &int32_max,
     110             :                          &uint32_min,   &uint32_max,
     111             :                          &int64_min,    &int64_max,
     112             :                          &uint64_min,   &uint64_max,
     113             :                          &enum_min,     &enum_max,
     114             :                          &largetag};
     115             : 
     116             :     /* Values for DescriptorSize8 message. */
     117             :     static int32_t first = 9991;
     118             :     static int32_t second = 9992;
     119           2 :     DescriptorSize8 req_ds8 = {&first, &second};
     120             : 
     121             :     /* Values for IntSizes message. */
     122             :     static int8_t req_int8 = -128;
     123             :     static uint8_t req_uint8 = 255;
     124             :     static int8_t req_sint8 = -128;
     125             :     static int16_t req_int16 = -32768;
     126             :     static uint16_t req_uint16 = 65535;
     127             :     static int16_t req_sint16 = -32768;
     128           2 :     IntSizes req_intsizes = {&req_int8, &req_uint8, &req_sint8,
     129             :                              &req_int16, &req_uint16, &req_sint16};
     130             : 
     131             :     /* Initialize the message struct with pointers to the fields. */
     132           2 :     AllTypes alltypes = {0};
     133             : 
     134           2 :     alltypes.req_int32         = &req_int32;
     135           2 :     alltypes.req_int64         = &req_int64;
     136           2 :     alltypes.req_uint32        = &req_uint32;
     137           2 :     alltypes.req_uint64        = &req_uint64;
     138           2 :     alltypes.req_sint32        = &req_sint32;
     139           2 :     alltypes.req_sint64        = &req_sint64;
     140           2 :     alltypes.req_bool          = &req_bool;
     141           2 :     alltypes.req_fixed32       = &req_fixed32;
     142           2 :     alltypes.req_sfixed32      = &req_sfixed32;
     143           2 :     alltypes.req_float         = &req_float;
     144           2 :     alltypes.req_fixed64       = &req_fixed64;
     145           2 :     alltypes.req_sfixed64      = &req_sfixed64;
     146           2 :     alltypes.req_double        = &req_double;
     147           2 :     alltypes.req_string        = req_string;
     148           2 :     alltypes.req_bytes         = (pb_bytes_array_t*)&req_bytes;
     149           2 :     alltypes.req_submsg        = &req_submsg;
     150           2 :     alltypes.req_enum          = &req_enum;
     151           2 :     alltypes.req_emptymsg      = &req_emptymsg;
     152           2 :     alltypes.req_fbytes        = &req_fbytes;
     153           2 :     alltypes.req_limits        = &req_limits;
     154           2 :     alltypes.req_ds8           = &req_ds8;
     155           2 :     alltypes.req_intsizes      = &req_intsizes;
     156             :     
     157           2 :     alltypes.rep_int32_count    = 5; alltypes.rep_int32     = rep_int32;
     158           2 :     alltypes.rep_int64_count    = 5; alltypes.rep_int64     = rep_int64;
     159           2 :     alltypes.rep_uint32_count   = 5; alltypes.rep_uint32    = rep_uint32;
     160           2 :     alltypes.rep_uint64_count   = 5; alltypes.rep_uint64    = rep_uint64;
     161           2 :     alltypes.rep_sint32_count   = 5; alltypes.rep_sint32    = rep_sint32;
     162           2 :     alltypes.rep_sint64_count   = 5; alltypes.rep_sint64    = rep_sint64;
     163           2 :     alltypes.rep_bool_count     = 5; alltypes.rep_bool      = rep_bool;
     164           2 :     alltypes.rep_fixed32_count  = 5; alltypes.rep_fixed32   = rep_fixed32;
     165           2 :     alltypes.rep_sfixed32_count = 5; alltypes.rep_sfixed32  = rep_sfixed32;
     166           2 :     alltypes.rep_float_count    = 5; alltypes.rep_float     = rep_float;
     167           2 :     alltypes.rep_fixed64_count  = 5; alltypes.rep_fixed64   = rep_fixed64;
     168           2 :     alltypes.rep_sfixed64_count = 5; alltypes.rep_sfixed64  = rep_sfixed64;
     169           2 :     alltypes.rep_double_count   = 5; alltypes.rep_double    = rep_double;
     170           2 :     alltypes.rep_string_count   = 5; alltypes.rep_string    = rep_string;
     171           2 :     alltypes.rep_bytes_count    = 5; alltypes.rep_bytes     = rep_bytes;
     172           2 :     alltypes.rep_submsg_count   = 5; alltypes.rep_submsg    = rep_submsg;
     173           2 :     alltypes.rep_enum_count     = 5; alltypes.rep_enum      = rep_enum;
     174           2 :     alltypes.rep_emptymsg_count = 5; alltypes.rep_emptymsg  = rep_emptymsg;
     175           2 :     alltypes.rep_fbytes_count   = 5; alltypes.rep_fbytes    = rep_fbytes;
     176           2 :     alltypes.rep_farray = &rep_farray;
     177           2 :     alltypes.rep_farray2 = &rep_farray2;
     178             :     
     179           2 :     if (mode != 0)
     180             :     {
     181             :         /* Fill in values for optional fields */
     182           1 :         alltypes.opt_int32         = &opt_int32;
     183           1 :         alltypes.opt_int64         = &opt_int64;
     184           1 :         alltypes.opt_uint32        = &opt_uint32;
     185           1 :         alltypes.opt_uint64        = &opt_uint64;
     186           1 :         alltypes.opt_sint32        = &opt_sint32;
     187           1 :         alltypes.opt_sint64        = &opt_sint64;
     188           1 :         alltypes.opt_bool          = &opt_bool;
     189           1 :         alltypes.opt_fixed32       = &opt_fixed32;
     190           1 :         alltypes.opt_sfixed32      = &opt_sfixed32;
     191           1 :         alltypes.opt_float         = &opt_float;
     192           1 :         alltypes.opt_fixed64       = &opt_fixed64;
     193           1 :         alltypes.opt_sfixed64      = &opt_sfixed64;
     194           1 :         alltypes.opt_double        = &opt_double;
     195           1 :         alltypes.opt_string        = opt_string;
     196           1 :         alltypes.opt_bytes         = (pb_bytes_array_t*)&opt_bytes;
     197           1 :         alltypes.opt_submsg        = &opt_submsg;
     198           1 :         alltypes.opt_enum          = &opt_enum;
     199           1 :         alltypes.opt_emptymsg      = &opt_emptymsg;
     200           1 :         alltypes.opt_fbytes        = &opt_fbytes;
     201             : 
     202           1 :         alltypes.which_oneof = AllTypes_oneof_msg1_tag;
     203           1 :         alltypes.oneof.oneof_msg1 = &oneof_msg1;
     204             : 
     205           1 :         alltypes.opt_non_zero_based_enum = &opt_non_zero_based_enum;
     206             :     }
     207             :     
     208           2 :     alltypes.end = &end;
     209             :     
     210             :     {
     211             :         uint8_t buffer[4096];
     212           2 :         pb_ostream_t stream = pb_ostream_from_buffer(buffer, sizeof(buffer));
     213             :         
     214             :         /* Now encode it and check if we succeeded. */
     215           2 :         if (pb_encode(&stream, AllTypes_fields, &alltypes))
     216             :         {
     217             :             SET_BINARY_MODE(stdout);
     218           2 :             fwrite(buffer, 1, stream.bytes_written, stdout);
     219           2 :             return 0; /* Success */
     220             :         }
     221             :         else
     222             :         {
     223           0 :             fprintf(stderr, "Encoding failed: %s\n", PB_GET_ERROR(&stream));
     224           0 :             return 1; /* Failure */
     225             :         }
     226             :     }
     227             : }

Generated by: LCOV version 1.14