LCOV - code coverage report
Current view: top level - test - test_byte.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 42 42 100.0 %
Date: 2026-07-14 21:28:07 Functions: 6 6 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 15 26 57.7 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2011-2025 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #undef NDEBUG
       5                 :            : 
       6                 :            : #include <exess/exess.h>
       7                 :            : 
       8                 :            : #include <assert.h>
       9                 :            : #include <stdint.h>
      10                 :            : #include <string.h>
      11                 :            : 
      12                 :            : static void
      13                 :          5 : check_read(const char* const string,
      14                 :            :            const ExessStatus expected_status,
      15                 :            :            const int8_t      expected_value,
      16                 :            :            const size_t      expected_count)
      17                 :            : {
      18                 :          5 :   int8_t            value = 0;
      19                 :          5 :   const ExessResult r     = exess_read_byte(string, &value);
      20                 :            : 
      21         [ -  + ]:          5 :   assert(r.status == expected_status);
      22         [ -  + ]:          5 :   assert(r.count == expected_count);
      23         [ -  + ]:          5 :   assert(value == expected_value);
      24                 :          5 : }
      25                 :            : 
      26                 :            : static void
      27                 :          1 : test_read_byte(void)
      28                 :            : {
      29                 :            :   // Limits
      30                 :          1 :   check_read("-128", EXESS_SUCCESS, INT8_MIN, EXESS_MAX_BYTE_LENGTH);
      31                 :          1 :   check_read("127", EXESS_SUCCESS, INT8_MAX, 3);
      32                 :            : 
      33                 :            :   // Out of range
      34                 :          1 :   check_read("-129", EXESS_OUT_OF_RANGE, 0, 4);
      35                 :          1 :   check_read("128", EXESS_OUT_OF_RANGE, 0, 3);
      36                 :            : 
      37                 :            :   // Garbage
      38                 :          1 :   check_read("+", EXESS_EXPECTED_DIGIT, 0, 1);
      39                 :          1 : }
      40                 :            : 
      41                 :            : static void
      42                 :          3 : check_write(const int8_t      value,
      43                 :            :             const ExessStatus expected_status,
      44                 :            :             const size_t      buf_size,
      45                 :            :             const char* const expected_string)
      46                 :            : {
      47                 :          3 :   char buf[EXESS_MAX_BYTE_LENGTH + 1] = {1, 2, 3, 4, 5};
      48         [ -  + ]:          3 :   assert(buf_size <= sizeof(buf));
      49                 :            : 
      50                 :          3 :   const ExessResult r = exess_write_byte(value, buf_size, buf);
      51         [ -  + ]:          3 :   assert(r.status == expected_status);
      52         [ +  + ]:          3 :   if (!r.status) {
      53         [ -  + ]:          2 :     assert(!strcmp(buf, expected_string));
      54         [ -  + ]:          2 :     assert(r.count == strlen(buf));
      55         [ -  + ]:          2 :     assert(exess_write_byte(value, 0, NULL).count == r.count);
      56                 :            :   }
      57                 :          3 : }
      58                 :            : 
      59                 :            : static void
      60                 :          1 : test_write_byte(void)
      61                 :            : {
      62                 :          1 :   check_write(INT8_MIN, EXESS_SUCCESS, 5, "-128");
      63                 :          1 :   check_write(INT8_MAX, EXESS_SUCCESS, 4, "127");
      64                 :          1 :   check_write(INT8_MAX, EXESS_NO_SPACE, 3, "127");
      65                 :          1 : }
      66                 :            : 
      67                 :            : static void
      68                 :          1 : test_round_trip(void)
      69                 :            : {
      70                 :          1 :   int8_t value                          = 0;
      71                 :          1 :   char   buf[EXESS_MAX_BYTE_LENGTH + 1] = {1, 2, 3, 4, 5};
      72                 :            : 
      73         [ +  + ]:        257 :   for (int16_t i = INT8_MIN; i <= INT8_MAX; ++i) {
      74         [ -  + ]:        256 :     assert(!exess_write_byte((int8_t)i, sizeof(buf), buf).status);
      75         [ -  + ]:        256 :     assert(!exess_read_byte(buf, &value).status);
      76         [ -  + ]:        256 :     assert(value == i);
      77                 :            :   }
      78                 :          1 : }
      79                 :            : 
      80                 :            : int
      81                 :          1 : main(void)
      82                 :            : {
      83                 :          1 :   test_read_byte();
      84                 :          1 :   test_write_byte();
      85                 :          1 :   test_round_trip();
      86                 :          1 :   return 0;
      87                 :            : }

Generated by: LCOV version 1.16