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 #ifndef POINTER_COMPARISON_TEST_HELPER_H 9 #define POINTER_COMPARISON_TEST_HELPER_H 10 11 #include <cstdint> 12 #include <cassert> 13 14 #include "test_macros.h" 15 16 template <template <class> class CompareTemplate> 17 void do_pointer_comparison_test() { 18 typedef CompareTemplate<int*> Compare; 19 typedef CompareTemplate<std::uintptr_t> UIntCompare; 20 #if TEST_STD_VER > 11 21 typedef CompareTemplate<void> VoidCompare; 22 #else 23 typedef Compare VoidCompare; 24 #endif 25 26 Compare comp; 27 UIntCompare ucomp; 28 VoidCompare vcomp; 29 struct { 30 int a, b; 31 } local; 32 int* pointers[] = {&local.a, &local.b, nullptr, &local.a + 1}; 33 for (int* lhs : pointers) { 34 for (int* rhs : pointers) { 35 std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs); 36 std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs); 37 assert(comp(lhs, rhs) == ucomp(lhs_uint, rhs_uint)); 38 assert(vcomp(lhs, rhs) == ucomp(lhs_uint, rhs_uint)); 39 } 40 } 41 } 42 43 template <class Comp> 44 void do_pointer_comparison_test(Comp comp) { 45 struct { 46 int a, b; 47 } local; 48 int* pointers[] = {&local.a, &local.b, nullptr, &local.a + 1}; 49 for (int* lhs : pointers) { 50 for (int* rhs : pointers) { 51 std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs); 52 std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs); 53 void* lhs_void = static_cast<void*>(lhs); 54 void* rhs_void = static_cast<void*>(rhs); 55 assert(comp(lhs, rhs) == comp(lhs_uint, rhs_uint)); 56 assert(comp(lhs_void, rhs_void) == comp(lhs_uint, rhs_uint)); 57 } 58 } 59 } 60 61 #endif // POINTER_COMPARISON_TEST_HELPER_H 62