LCOV - code coverage report
Current view: top level - test - test_handles.c (source / functions) Hit Total Coverage
Test: lcov.info Lines: 51 51 100.0 %
Date: 2021-11-24 03:28:48 Functions: 7 7 100.0 %

          Line data    Source code
       1             : /* Copyright (c) 2016-2021, The Tor Project, Inc. */
       2             : /* See LICENSE for licensing information */
       3             : 
       4             : #include "orconfig.h"
       5             : #include "test/test.h"
       6             : 
       7             : #include "lib/container/handles.h"
       8             : #include "lib/log/util_bug.h"
       9             : 
      10             : #include <stdio.h>
      11             : 
      12             : typedef struct demo_t {
      13             :   HANDLE_ENTRY(demo, demo_t);
      14             :   int val;
      15             : } demo_t;
      16             : 
      17             : HANDLE_DECL(demo, demo_t, static)
      18             : #define demo_handle_free(h)    \
      19             :   FREE_AND_NULL(demo_handle_t, demo_handle_free_, (h))
      20          25 : HANDLE_IMPL(demo, demo_t, static)
      21             : 
      22             : static demo_t *
      23           2 : demo_new(int val)
      24             : {
      25           2 :   demo_t *d = tor_malloc_zero(sizeof(demo_t));
      26           2 :   d->val = val;
      27           2 :   return d;
      28             : }
      29             : 
      30             : static void
      31           3 : demo_free(demo_t *d)
      32             : {
      33           3 :   if (d == NULL)
      34             :     return;
      35           2 :   demo_handles_clear(d);
      36           2 :   tor_free(d);
      37             : }
      38             : 
      39             : static void
      40           1 : test_handle_basic(void *arg)
      41             : {
      42           1 :   (void) arg;
      43           1 :   demo_t *d1 = NULL, *d2 = NULL;
      44           1 :   demo_handle_t *wr1 = NULL, *wr2 = NULL, *wr3 = NULL, *wr4 = NULL;
      45             : 
      46           1 :   d1 = demo_new(9000);
      47           1 :   d2 = demo_new(9009);
      48             : 
      49           1 :   wr1 = demo_handle_new(d1);
      50           1 :   wr2 = demo_handle_new(d1);
      51           1 :   wr3 = demo_handle_new(d1);
      52           1 :   wr4 = demo_handle_new(d2);
      53             : 
      54           1 :   tt_assert(wr1);
      55           1 :   tt_assert(wr2);
      56           1 :   tt_assert(wr3);
      57           1 :   tt_assert(wr4);
      58             : 
      59           1 :   tt_ptr_op(demo_handle_get(wr1), OP_EQ, d1);
      60           1 :   tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
      61           1 :   tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
      62           1 :   tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
      63             : 
      64           1 :   demo_handle_free(wr1);
      65           1 :   wr1 = NULL;
      66           1 :   tt_ptr_op(demo_handle_get(wr2), OP_EQ, d1);
      67           1 :   tt_ptr_op(demo_handle_get(wr3), OP_EQ, d1);
      68           1 :   tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
      69             : 
      70           1 :   demo_free(d1);
      71           1 :   d1 = NULL;
      72           1 :   tt_ptr_op(demo_handle_get(wr2), OP_EQ, NULL);
      73           1 :   tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
      74           1 :   tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
      75             : 
      76           1 :   demo_handle_free(wr2);
      77           1 :   wr2 = NULL;
      78           1 :   tt_ptr_op(demo_handle_get(wr3), OP_EQ, NULL);
      79           1 :   tt_ptr_op(demo_handle_get(wr4), OP_EQ, d2);
      80             : 
      81           1 :   demo_handle_free(wr3);
      82           1 :   wr3 = NULL;
      83           1 :  done:
      84           1 :   demo_handle_free(wr1);
      85           1 :   demo_handle_free(wr2);
      86           1 :   demo_handle_free(wr3);
      87           1 :   demo_handle_free(wr4);
      88           1 :   demo_free(d1);
      89           1 :   demo_free(d2);
      90           1 : }
      91             : 
      92             : #define HANDLE_TEST(name, flags)                       \
      93             :   { #name, test_handle_ ##name, (flags), NULL, NULL }
      94             : 
      95             : struct testcase_t handle_tests[] = {
      96             :   HANDLE_TEST(basic, 0),
      97             :   END_OF_TESTCASES
      98             : };

Generated by: LCOV version 1.14