Check roundtrip for each bit of {unsigned} int values
[tor.git] / src / test / test_ptr_slow.c
blob0142fd1c8a04274e9f1fd20ab5556ebfb9545727
1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2019, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
6 #include "orconfig.h"
7 #include "core/or/or.h"
8 #include "test/test.h"
9 #include "test/ptr_helpers.h"
11 #include <stdint.h>
12 #include <limits.h>
14 /** Assert that <b>a</b> can be cast to void * and back. */
15 static void
16 assert_int_voidptr_roundtrip(int a)
18 intptr_t ap = (intptr_t)a;
19 void *b = cast_intptr_to_voidstar(ap);
20 intptr_t c = cast_voidstar_to_intptr(b);
21 void *d = cast_intptr_to_voidstar(c);
23 tt_assert(ap == c);
24 tt_assert(b == d);
26 done:
27 return;
30 static void
31 test_int_voidstar_interop(void *arg)
33 int a;
34 (void)arg;
36 for (a = -1024; a <= 1024; a++) {
37 assert_int_voidptr_roundtrip(a);
40 for (a = INT_MIN; a <= INT_MIN+1024; a++) {
41 assert_int_voidptr_roundtrip(a);
44 for (a = INT_MAX-1024; a < INT_MAX; a++) {
45 assert_int_voidptr_roundtrip(a);
48 a = 1;
49 for (unsigned long i = 0; i < sizeof(int) * 8; i++) {
50 assert_int_voidptr_roundtrip(a);
51 a = (a << 1);
55 static void
56 assert_uint_voidptr_roundtrip(unsigned int a)
58 uintptr_t ap = (uintptr_t)a;
59 void *b = cast_uintptr_to_voidstar(ap);
60 uintptr_t c = cast_voidstar_to_uintptr(b);
61 void *d = cast_uintptr_to_voidstar(c);
63 tt_assert(ap == c);
64 tt_assert(b == d);
66 done:
67 return;
70 static void
71 test_uint_voidstar_interop(void *arg)
73 unsigned int a;
74 (void)arg;
76 for (a = 0; a <= 1024; a++) {
77 assert_uint_voidptr_roundtrip(a);
80 for (a = UINT_MAX-1024; a < UINT_MAX; a++) {
81 assert_uint_voidptr_roundtrip(a);
84 a = 1;
85 for (unsigned long i = 0; i < sizeof(int) * 8; i++) {
86 assert_uint_voidptr_roundtrip(a);
87 a = (a << 1);
91 struct testcase_t slow_ptr_tests[] = {
92 { .name = "int_voidstar_interop",
93 .fn = test_int_voidstar_interop,
94 .flags = 0,
95 .setup = NULL,
96 .setup_data = NULL },
97 { .name = "uint_voidstar_interop",
98 .fn = test_uint_voidstar_interop,
99 .flags = 0,
100 .setup = NULL,
101 .setup_data = NULL },
102 END_OF_TESTCASES