xref: /llvm-project/libcxx/test/support/pointer_comparison_test_helper.h (revision 480cd780d63fd9c658cc2f51d0c54416b8b1a5c3)
1d7bd62a6Szoecarver //===----------------------------------------------------------------------===//
2d7bd62a6Szoecarver //
3d7bd62a6Szoecarver // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4d7bd62a6Szoecarver // See https://llvm.org/LICENSE.txt for license information.
5d7bd62a6Szoecarver // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6d7bd62a6Szoecarver //
7d7bd62a6Szoecarver //===----------------------------------------------------------------------===//
8*480cd780SLouis Dionne 
9879cbac0Szoecarver #ifndef POINTER_COMPARISON_TEST_HELPER_H
10879cbac0Szoecarver #define POINTER_COMPARISON_TEST_HELPER_H
11879cbac0Szoecarver 
12879cbac0Szoecarver #include <cstdint>
13879cbac0Szoecarver #include <cassert>
14879cbac0Szoecarver 
15879cbac0Szoecarver #include "test_macros.h"
16879cbac0Szoecarver 
17879cbac0Szoecarver template <template <class> class CompareTemplate>
do_pointer_comparison_test()18879cbac0Szoecarver void do_pointer_comparison_test() {
19879cbac0Szoecarver   typedef CompareTemplate<int*> Compare;
20879cbac0Szoecarver   typedef CompareTemplate<std::uintptr_t> UIntCompare;
21879cbac0Szoecarver #if TEST_STD_VER > 11
22879cbac0Szoecarver   typedef CompareTemplate<void> VoidCompare;
23879cbac0Szoecarver #else
24879cbac0Szoecarver   typedef Compare VoidCompare;
25879cbac0Szoecarver #endif
26879cbac0Szoecarver 
27879cbac0Szoecarver   Compare comp;
28879cbac0Szoecarver   UIntCompare ucomp;
29879cbac0Szoecarver   VoidCompare vcomp;
30879cbac0Szoecarver   struct {
31879cbac0Szoecarver     int a, b;
32879cbac0Szoecarver   } local;
33879cbac0Szoecarver   int* pointers[] = {&local.a, &local.b, nullptr, &local.a + 1};
34879cbac0Szoecarver   for (int* lhs : pointers) {
35879cbac0Szoecarver     for (int* rhs : pointers) {
36879cbac0Szoecarver       std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs);
37879cbac0Szoecarver       std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs);
38879cbac0Szoecarver       assert(comp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
39879cbac0Szoecarver       assert(vcomp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
40879cbac0Szoecarver     }
41879cbac0Szoecarver   }
42879cbac0Szoecarver }
43879cbac0Szoecarver 
44879cbac0Szoecarver template <class Comp>
do_pointer_comparison_test(Comp comp)45879cbac0Szoecarver void do_pointer_comparison_test(Comp comp) {
46879cbac0Szoecarver   struct {
47879cbac0Szoecarver     int a, b;
48879cbac0Szoecarver   } local;
49879cbac0Szoecarver   int* pointers[] = {&local.a, &local.b, nullptr, &local.a + 1};
50879cbac0Szoecarver   for (int* lhs : pointers) {
51879cbac0Szoecarver     for (int* rhs : pointers) {
52879cbac0Szoecarver       std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs);
53879cbac0Szoecarver       std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs);
54879cbac0Szoecarver       void*          lhs_void = static_cast<void*>(lhs);
55879cbac0Szoecarver       void*          rhs_void = static_cast<void*>(rhs);
56879cbac0Szoecarver       assert(comp(lhs, rhs) == comp(lhs_uint, rhs_uint));
57879cbac0Szoecarver       assert(comp(lhs_void, rhs_void) == comp(lhs_uint, rhs_uint));
58879cbac0Szoecarver     }
59879cbac0Szoecarver   }
60879cbac0Szoecarver }
61879cbac0Szoecarver 
62879cbac0Szoecarver #endif // POINTER_COMPARISON_TEST_HELPER_H
63