1 /* Copyright (c) 2001-2004, Roger Dingledine.
2 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3 * Copyright (c) 2007-2020, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
7 #include "core/or/or.h"
9 #include "test/ptr_helpers.h"
14 /** Assert that <b>a</b> can be cast to void * and back. */
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
);
30 /** Test for possibility of casting `int` to `void *` and back. */
32 test_int_voidstar_interop(void *arg
)
37 for (a
= -1024; a
<= 1024; a
++) {
38 assert_int_voidptr_roundtrip(a
);
41 for (a
= INT_MIN
; a
<= INT_MIN
+1024; a
++) {
42 assert_int_voidptr_roundtrip(a
);
45 for (a
= INT_MAX
-1024; a
< INT_MAX
; a
++) {
46 assert_int_voidptr_roundtrip(a
);
50 for (unsigned long i
= 0; i
< sizeof(int) * 8; i
++) {
51 assert_int_voidptr_roundtrip(a
);
56 /** Assert that <b>a</b> can be cast to void * and back. */
58 assert_uint_voidptr_roundtrip(unsigned int a
)
60 uintptr_t ap
= (uintptr_t)a
;
61 void *b
= cast_uintptr_to_voidstar(ap
);
62 uintptr_t c
= cast_voidstar_to_uintptr(b
);
63 void *d
= cast_uintptr_to_voidstar(c
);
72 /** Test for possibility of casting `int` to `void *` and back. */
74 test_uint_voidstar_interop(void *arg
)
79 for (a
= 0; a
<= 1024; a
++) {
80 assert_uint_voidptr_roundtrip(a
);
83 for (a
= UINT_MAX
-1024; a
< UINT_MAX
; a
++) {
84 assert_uint_voidptr_roundtrip(a
);
88 for (unsigned long i
= 0; i
< sizeof(int) * 8; i
++) {
89 assert_uint_voidptr_roundtrip(a
);
94 struct testcase_t slow_ptr_tests
[] = {
95 { .name
= "int_voidstar_interop",
96 .fn
= test_int_voidstar_interop
,
100 { .name
= "uint_voidstar_interop",
101 .fn
= test_uint_voidstar_interop
,
104 .setup_data
= NULL
},