xref: /llvm-project/libcxx/test/support/pointer_comparison_test_helper.h (revision 480cd780d63fd9c658cc2f51d0c54416b8b1a5c3)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef POINTER_COMPARISON_TEST_HELPER_H
10 #define POINTER_COMPARISON_TEST_HELPER_H
11 
12 #include <cstdint>
13 #include <cassert>
14 
15 #include "test_macros.h"
16 
17 template <template <class> class CompareTemplate>
do_pointer_comparison_test()18 void do_pointer_comparison_test() {
19   typedef CompareTemplate<int*> Compare;
20   typedef CompareTemplate<std::uintptr_t> UIntCompare;
21 #if TEST_STD_VER > 11
22   typedef CompareTemplate<void> VoidCompare;
23 #else
24   typedef Compare VoidCompare;
25 #endif
26 
27   Compare comp;
28   UIntCompare ucomp;
29   VoidCompare vcomp;
30   struct {
31     int a, b;
32   } local;
33   int* pointers[] = {&local.a, &local.b, nullptr, &local.a + 1};
34   for (int* lhs : pointers) {
35     for (int* rhs : pointers) {
36       std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs);
37       std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs);
38       assert(comp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
39       assert(vcomp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
40     }
41   }
42 }
43 
44 template <class Comp>
do_pointer_comparison_test(Comp comp)45 void do_pointer_comparison_test(Comp comp) {
46   struct {
47     int a, b;
48   } local;
49   int* pointers[] = {&local.a, &local.b, nullptr, &local.a + 1};
50   for (int* lhs : pointers) {
51     for (int* rhs : pointers) {
52       std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs);
53       std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs);
54       void*          lhs_void = static_cast<void*>(lhs);
55       void*          rhs_void = static_cast<void*>(rhs);
56       assert(comp(lhs, rhs) == comp(lhs_uint, rhs_uint));
57       assert(comp(lhs_void, rhs_void) == comp(lhs_uint, rhs_uint));
58     }
59   }
60 }
61 
62 #endif // POINTER_COMPARISON_TEST_HELPER_H
63