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

          Line data    Source code
       1             : /* Copyright (c) 2001-2004, Roger Dingledine.
       2             :  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
       3             :  * Copyright (c) 2007-2021, The Tor Project, Inc. */
       4             : /* See LICENSE for licensing information */
       5             : 
       6             : #include "orconfig.h"
       7             : #include "core/or/or.h"
       8             : #include "test/test.h"
       9             : #include "test/ptr_helpers.h"
      10             : 
      11             : #include <stdint.h>
      12             : #include <limits.h>
      13             : 
      14             : /** Assert that <b>a</b> can be cast to void * and back. */
      15             : static void
      16        4130 : assert_int_voidptr_roundtrip(int a)
      17             : {
      18        4130 :   intptr_t ap = (intptr_t)a;
      19        4130 :   void *b = cast_intptr_to_voidstar(ap);
      20        4130 :   intptr_t c = cast_voidstar_to_intptr(b);
      21        4130 :   void *d = cast_intptr_to_voidstar(c);
      22             : 
      23        4130 :   tt_assert(ap == c);
      24        4130 :   tt_assert(b == d);
      25             : 
      26        4130 :  done:
      27        4130 :   return;
      28             : }
      29             : 
      30             : /** Test for possibility of casting `int` to `void *` and back. */
      31             : static void
      32           1 : test_int_voidstar_interop(void *arg)
      33             : {
      34           1 :   int a;
      35           1 :   (void)arg;
      36             : 
      37        2050 :   for (a = -1024; a <= 1024; a++) {
      38        2049 :     assert_int_voidptr_roundtrip(a);
      39             :   }
      40             : 
      41        1026 :   for (a = INT_MIN; a <= INT_MIN+1024; a++) {
      42        1025 :     assert_int_voidptr_roundtrip(a);
      43             :   }
      44             : 
      45        1025 :   for (a = INT_MAX-1024; a < INT_MAX; a++) {
      46        1024 :     assert_int_voidptr_roundtrip(a);
      47             :   }
      48             : 
      49             :   a = 1;
      50          33 :   for (unsigned long i = 0; i < sizeof(int) * 8; i++) {
      51          32 :     assert_int_voidptr_roundtrip(a);
      52          32 :     a = (a << 1);
      53             :   }
      54           1 : }
      55             : 
      56             : /** Assert that <b>a</b> can be cast to void * and back. */
      57             : static void
      58        2081 : assert_uint_voidptr_roundtrip(unsigned int a)
      59             : {
      60        2081 :  uintptr_t ap = (uintptr_t)a;
      61        2081 :  void *b = cast_uintptr_to_voidstar(ap);
      62        2081 :  uintptr_t c = cast_voidstar_to_uintptr(b);
      63        2081 :  void *d = cast_uintptr_to_voidstar(c);
      64             : 
      65        2081 :  tt_assert(ap == c);
      66        2081 :  tt_assert(b == d);
      67             : 
      68        2081 :  done:
      69        2081 :   return;
      70             : }
      71             : 
      72             : /** Test for possibility of casting `int` to `void *` and back. */
      73             : static void
      74           1 : test_uint_voidstar_interop(void *arg)
      75             : {
      76           1 :   unsigned int a;
      77           1 :   (void)arg;
      78             : 
      79        1026 :   for (a = 0; a <= 1024; a++) {
      80        1025 :     assert_uint_voidptr_roundtrip(a);
      81             :   }
      82             : 
      83        1025 :   for (a = UINT_MAX-1024; a < UINT_MAX; a++) {
      84        1024 :     assert_uint_voidptr_roundtrip(a);
      85             :   }
      86             : 
      87             :   a = 1;
      88          33 :   for (unsigned long i = 0; i < sizeof(int) * 8; i++) {
      89          32 :     assert_uint_voidptr_roundtrip(a);
      90          32 :     a = (a << 1);
      91             :   }
      92           1 : }
      93             : 
      94             : struct testcase_t slow_ptr_tests[] = {
      95             :   { .name = "int_voidstar_interop",
      96             :     .fn = test_int_voidstar_interop,
      97             :     .flags = 0,
      98             :     .setup = NULL,
      99             :     .setup_data = NULL },
     100             :   { .name = "uint_voidstar_interop",
     101             :     .fn = test_uint_voidstar_interop,
     102             :     .flags = 0,
     103             :     .setup = NULL,
     104             :     .setup_data = NULL },
     105             :   END_OF_TESTCASES
     106             : };

Generated by: LCOV version 1.14