LCOV - code coverage report
Current view: top level - callbacks - encode_callbacks.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 37 45 82.2 %
Date: 2023-02-14 20:10:26 Functions: 6 6 100.0 %

          Line data    Source code
       1             : /* Encoding testcase for callback fields */
       2             : 
       3             : #include <stdio.h>
       4             : #include <string.h>
       5             : #include <pb_encode.h>
       6             : #include "callbacks.pb.h"
       7             : #include "test_helpers.h"
       8             : 
       9           3 : bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
      10             : {
      11           3 :     char *str = "Hello world!";
      12             :     
      13           3 :     if (!pb_encode_tag_for_field(stream, field))
      14           0 :         return false;
      15             :     
      16           3 :     return pb_encode_string(stream, (uint8_t*)str, strlen(str));
      17             : }
      18             : 
      19           3 : bool encode_int32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
      20             : {
      21           3 :     if (!pb_encode_tag_for_field(stream, field))
      22           0 :         return false;
      23             :     
      24           3 :     return pb_encode_varint(stream, 42);
      25             : }
      26             : 
      27           3 : bool encode_fixed32(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
      28             : {
      29           3 :     uint32_t value = 42;
      30             : 
      31           3 :     if (!pb_encode_tag_for_field(stream, field))
      32           0 :         return false;
      33             :     
      34           3 :     return pb_encode_fixed32(stream, &value);
      35             : }
      36             : 
      37           3 : bool encode_fixed64(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
      38             : {
      39           3 :     uint64_t value = 42;
      40             : 
      41           3 :     if (!pb_encode_tag_for_field(stream, field))
      42           0 :         return false;
      43             :     
      44           3 :     return pb_encode_fixed64(stream, &value);
      45             : }
      46             : 
      47           1 : bool encode_repeatedstring(pb_ostream_t *stream, const pb_field_t *field, void * const *arg)
      48             : {
      49           1 :     char *str[4] = {"Hello world!", "", "Test", "Test2"};
      50             :     int i;
      51             :     
      52           5 :     for (i = 0; i < 4; i++)
      53             :     {
      54           4 :         if (!pb_encode_tag_for_field(stream, field))
      55           0 :             return false;
      56             :         
      57           4 :         if (!pb_encode_string(stream, (uint8_t*)str[i], strlen(str[i])))
      58           0 :             return false;
      59             :     }
      60           1 :     return true;
      61             : }
      62             : 
      63           1 : int main()
      64             : {
      65             :     uint8_t buffer[1024];
      66             :     pb_ostream_t stream;
      67           1 :     TestMessage testmessage = {{{NULL}}};
      68             :     
      69           1 :     stream = pb_ostream_from_buffer(buffer, 1024);
      70             :     
      71           1 :     testmessage.stringvalue.funcs.encode = &encode_string;
      72           1 :     testmessage.int32value.funcs.encode = &encode_int32;
      73           1 :     testmessage.fixed32value.funcs.encode = &encode_fixed32;
      74           1 :     testmessage.fixed64value.funcs.encode = &encode_fixed64;
      75             :     
      76           1 :     testmessage.has_submsg = true;
      77           1 :     testmessage.submsg.stringvalue.funcs.encode = &encode_string;
      78           1 :     testmessage.submsg.int32value.funcs.encode = &encode_int32;
      79           1 :     testmessage.submsg.fixed32value.funcs.encode = &encode_fixed32;
      80           1 :     testmessage.submsg.fixed64value.funcs.encode = &encode_fixed64;
      81             : 
      82           1 :     testmessage.repeatedstring.funcs.encode = &encode_repeatedstring;
      83             :     
      84           1 :     if (!pb_encode(&stream, TestMessage_fields, &testmessage))
      85           0 :         return 1;
      86             :     
      87             :     SET_BINARY_MODE(stdout);
      88           1 :     if (fwrite(buffer, stream.bytes_written, 1, stdout) != 1)
      89           0 :         return 2;
      90             :     
      91           1 :     return 0;
      92             : }

Generated by: LCOV version 1.14