LCOV - code coverage report
Current view: top level - test - test_ring.c (source / functions) Hit Total Coverage
Test: Code coverage Lines: 108 108 100.0 %
Date: 2026-11-26 02:32:31 Functions: 8 8 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 57 98 58.2 %

           Branch data     Line data    Source code
       1                 :            : // Copyright 2011-2023 David Robillard <d@drobilla.net>
       2                 :            : // SPDX-License-Identifier: ISC
       3                 :            : 
       4                 :            : #undef NDEBUG
       5                 :            : 
       6                 :            : #include "failing_allocator.h"
       7                 :            : #include "test_args.h"
       8                 :            : 
       9                 :            : #include <zix/attributes.h>
      10                 :            : #include <zix/ring.h>
      11                 :            : #include <zix/status.h>
      12                 :            : #include <zix/thread.h>
      13                 :            : 
      14                 :            : #include <assert.h>
      15                 :            : #include <limits.h>
      16                 :            : #include <stdbool.h>
      17                 :            : #include <stdint.h>
      18                 :            : #include <stdio.h>
      19                 :            : #include <stdlib.h>
      20                 :            : 
      21                 :            : #define MSG_SIZE 20U
      22                 :            : 
      23                 :            : static ZixRing* ring       = 0;
      24                 :            : static unsigned n_writes   = 0;
      25                 :            : static bool     read_error = false;
      26                 :            : 
      27                 :            : static int
      28                 :     467574 : gen_msg(int* const msg, int start)
      29                 :            : {
      30         [ +  + ]:    9819054 :   for (unsigned i = 0U; i < MSG_SIZE; ++i) {
      31                 :    9351480 :     msg[i] = start;
      32                 :    9351480 :     start  = (start + 1) % INT_MAX;
      33                 :            :   }
      34                 :     467574 :   return start;
      35                 :            : }
      36                 :            : 
      37                 :            : ZIX_PURE_FUNC static int
      38                 :     233785 : cmp_msg(const int* const msg1, const int* const msg2)
      39                 :            : {
      40         [ +  + ]:    4909485 :   for (unsigned i = 0U; i < MSG_SIZE; ++i) {
      41         [ -  + ]:    4675700 :     assert(msg1[i] == msg2[i]);
      42                 :            :   }
      43                 :            : 
      44                 :     233785 :   return 1;
      45                 :            : }
      46                 :            : 
      47                 :            : static ZixThreadResult ZIX_THREAD_FUNC
      48                 :          2 : reader(void* ZIX_UNUSED(arg))
      49                 :            : {
      50                 :          2 :   fprintf(stderr, "Reader starting\n");
      51                 :            : 
      52                 :            :   int ref_msg[MSG_SIZE];  // Reference generated for comparison
      53                 :            :   int read_msg[MSG_SIZE]; // Read from ring
      54                 :          2 :   int start = gen_msg(ref_msg, 0);
      55         [ +  + ]:    1049602 :   for (unsigned i = 0; i < n_writes; ++i) {
      56   [ +  +  +  - ]:    1283385 :     if (zix_ring_read_space(ring) >= MSG_SIZE * sizeof(int) &&
      57                 :     233785 :         zix_ring_read(ring, read_msg, MSG_SIZE * sizeof(int))) {
      58         [ -  + ]:     233785 :       assert(cmp_msg(ref_msg, read_msg));
      59                 :     233785 :       start = gen_msg(ref_msg, start);
      60                 :            :     }
      61                 :            :   }
      62                 :            : 
      63                 :          2 :   fprintf(stderr, "Reader finished\n");
      64                 :          2 :   return ZIX_THREAD_RESULT;
      65                 :            : }
      66                 :            : 
      67                 :            : static ZixThreadResult ZIX_THREAD_FUNC
      68                 :          2 : writer(void* ZIX_UNUSED(arg))
      69                 :            : {
      70                 :          2 :   fprintf(stderr, "Writer starting\n");
      71                 :            : 
      72                 :            :   int write_msg[MSG_SIZE]; // Written to ring
      73                 :          2 :   int start = gen_msg(write_msg, 0);
      74         [ +  + ]:    1049602 :   for (unsigned i = 0; i < n_writes; ++i) {
      75   [ +  +  +  - ]:    1283385 :     if (zix_ring_write_space(ring) >= MSG_SIZE * sizeof(int) &&
      76                 :     233785 :         zix_ring_write(ring, write_msg, MSG_SIZE * sizeof(int))) {
      77                 :     233785 :       start = gen_msg(write_msg, start);
      78                 :            :     }
      79                 :            :   }
      80                 :            : 
      81                 :          2 :   fprintf(stderr, "Writer finished\n");
      82                 :          2 :   return ZIX_THREAD_RESULT;
      83                 :            : }
      84                 :            : 
      85                 :            : static int
      86                 :          2 : test_ring(const unsigned size)
      87                 :            : {
      88                 :          2 :   zix_ring_free(NULL);
      89                 :            : 
      90                 :          2 :   fprintf(stderr,
      91                 :            :           "Testing %u writes of %u ints to a %u int ring...\n",
      92                 :            :           n_writes,
      93                 :            :           MSG_SIZE,
      94                 :            :           size);
      95                 :            : 
      96                 :          2 :   ring = zix_ring_new(NULL, size);
      97         [ -  + ]:          2 :   assert(ring);
      98         [ -  + ]:          2 :   assert(zix_ring_read_space(ring) == 0);
      99         [ -  + ]:          2 :   assert(zix_ring_write_space(ring) == zix_ring_capacity(ring));
     100                 :            : 
     101                 :          2 :   const ZixStatus st = zix_ring_mlock(ring);
     102   [ -  +  -  -  :          2 :   assert(!st || st == ZIX_STATUS_NOT_SUPPORTED || st == ZIX_STATUS_UNAVAILABLE);
                   -  - ]
     103                 :            : 
     104                 :            :   static const size_t stack_size = (size_t)MSG_SIZE * 4U;
     105                 :            : 
     106                 :            :   ZixThread reader_thread; // NOLINT(cppcoreguidelines-init-variables)
     107         [ -  + ]:          2 :   assert(!zix_thread_create(&reader_thread, stack_size, reader, NULL));
     108                 :            : 
     109                 :            :   ZixThread writer_thread; // NOLINT(cppcoreguidelines-init-variables)
     110         [ -  + ]:          2 :   assert(!zix_thread_create(&writer_thread, stack_size, writer, NULL));
     111                 :            : 
     112         [ -  + ]:          2 :   assert(!zix_thread_join(reader_thread));
     113         [ -  + ]:          2 :   assert(!zix_thread_join(writer_thread));
     114                 :            : 
     115         [ -  + ]:          2 :   assert(!read_error);
     116         [ -  + ]:          2 :   assert(ring);
     117                 :          2 :   zix_ring_reset(ring);
     118         [ -  + ]:          2 :   assert(zix_ring_read_space(ring) == 0);
     119         [ -  + ]:          2 :   assert(zix_ring_write_space(ring) == zix_ring_capacity(ring));
     120                 :            : 
     121                 :          2 :   zix_ring_write(ring, "a", 1);
     122                 :          2 :   zix_ring_write(ring, "b", 1);
     123                 :            : 
     124                 :          2 :   char     buf = 0;
     125                 :          2 :   uint32_t n   = zix_ring_peek(ring, &buf, 1);
     126         [ -  + ]:          2 :   assert(n == 1);
     127         [ -  + ]:          2 :   assert(buf == 'a');
     128                 :            : 
     129                 :          2 :   n = zix_ring_skip(ring, 1);
     130         [ -  + ]:          2 :   assert(n == 1);
     131                 :            : 
     132         [ -  + ]:          2 :   assert(zix_ring_read_space(ring) == 1);
     133                 :            : 
     134                 :          2 :   n = zix_ring_read(ring, &buf, 1);
     135         [ -  + ]:          2 :   assert(n == 1);
     136         [ -  + ]:          2 :   assert(buf == 'b');
     137                 :            : 
     138         [ -  + ]:          2 :   assert(zix_ring_read_space(ring) == 0);
     139                 :            : 
     140                 :          2 :   n = zix_ring_peek(ring, &buf, 1);
     141         [ -  + ]:          2 :   assert(n == 0);
     142                 :            : 
     143                 :          2 :   n = zix_ring_read(ring, &buf, 1);
     144         [ -  + ]:          2 :   assert(n == 0);
     145                 :            : 
     146                 :          2 :   n = zix_ring_skip(ring, 1);
     147         [ -  + ]:          2 :   assert(n == 0);
     148                 :            : 
     149                 :          2 :   char* big_buf = (char*)calloc(size, 1);
     150                 :          2 :   n             = zix_ring_write(ring, big_buf, size - 1);
     151         [ -  + ]:          2 :   assert(n == (uint32_t)size - 1);
     152                 :            : 
     153                 :          2 :   n = zix_ring_write(ring, big_buf, size);
     154         [ -  + ]:          2 :   assert(n == 0);
     155                 :            : 
     156                 :          2 :   free(big_buf);
     157                 :          2 :   zix_ring_free(ring);
     158                 :          2 :   return 0;
     159                 :            : }
     160                 :            : 
     161                 :            : static void
     162                 :          2 : test_capacity(void)
     163                 :            : {
     164         [ -  + ]:          2 :   assert(!zix_ring_new(NULL, 0));
     165         [ -  + ]:          2 :   assert(!zix_ring_new(NULL, 1));
     166         [ -  + ]:          2 :   assert(!zix_ring_new(NULL, 2147483649U));
     167                 :            : 
     168                 :            :   {
     169                 :          2 :     ZixRing* const smallest = zix_ring_new(NULL, 2U);
     170         [ -  + ]:          2 :     assert(smallest);
     171         [ -  + ]:          2 :     assert(zix_ring_capacity(smallest) == 1U);
     172                 :          2 :     zix_ring_free(smallest);
     173                 :            :   }
     174                 :            :   {
     175                 :          2 :     ZixRing* const rounded = zix_ring_new(NULL, 6U);
     176         [ -  + ]:          2 :     assert(rounded);
     177         [ -  + ]:          2 :     assert(zix_ring_capacity(rounded) == 7U);
     178                 :          2 :     zix_ring_free(rounded);
     179                 :            :   }
     180                 :          2 : }
     181                 :            : 
     182                 :            : static void
     183                 :          2 : test_failed_alloc(void)
     184                 :            : {
     185                 :          2 :   ZixFailingAllocator allocator = zix_failing_allocator();
     186                 :            : 
     187                 :            :   // Successfully allocate a ring to count the number of allocations
     188                 :          2 :   ring = zix_ring_new(&allocator.base, 512);
     189         [ -  + ]:          2 :   assert(ring);
     190                 :            : 
     191                 :            :   // Test that each allocation failing is handled gracefully
     192                 :          2 :   const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0);
     193         [ +  + ]:          6 :   for (size_t i = 0U; i < n_new_allocs; ++i) {
     194                 :          4 :     zix_failing_allocator_reset(&allocator, i);
     195         [ -  + ]:          4 :     assert(!zix_ring_new(&allocator.base, 512));
     196                 :            :   }
     197                 :            : 
     198                 :          2 :   zix_ring_free(ring);
     199                 :          2 : }
     200                 :            : 
     201                 :            : int
     202                 :          3 : main(int argc, char** argv)
     203                 :            : {
     204         [ +  + ]:          3 :   if (argc > 3) {
     205                 :          1 :     fprintf(stderr, "Usage: %s SIZE [N_WRITES]\n", argv[0]);
     206                 :          1 :     return 1;
     207                 :            :   }
     208                 :            : 
     209                 :          2 :   const unsigned size =
     210         [ +  + ]:          2 :     (argc > 1) ? (unsigned)zix_test_size_arg(argv[1], 4U, 1U << 20U) : 1024U;
     211                 :            : 
     212                 :          1 :   n_writes = (argc > 2) ? (unsigned)zix_test_size_arg(argv[2], 4U, 1U << 20U)
     213         [ +  + ]:          3 :                         : size * 1024;
     214                 :            : 
     215                 :          2 :   test_capacity();
     216                 :          2 :   test_failed_alloc();
     217                 :          2 :   test_ring(size);
     218                 :          2 :   return 0;
     219                 :            : }

Generated by: LCOV version 1.16