Home | History | Annotate | Download | only in comparisons
      1 #ifndef POINTER_COMPARISON_TEST_HELPER_HPP
      2 #define POINTER_COMPARISON_TEST_HELPER_HPP
      3 
      4 #include <vector>
      5 #include <memory>
      6 #include <cstdint>
      7 #include <cassert>
      8 
      9 #include "test_macros.h"
     10 
     11 template <class T, template<class> class CompareTemplate>
     12 void do_pointer_comparison_test() {
     13     typedef CompareTemplate<T*> Compare;
     14     typedef CompareTemplate<std::uintptr_t> UIntCompare;
     15 #if TEST_STD_VER > 11
     16     typedef CompareTemplate<void> VoidCompare;
     17 #else
     18     typedef Compare VoidCompare;
     19 #endif
     20     std::vector<std::shared_ptr<T> > pointers;
     21     const std::size_t test_size = 100;
     22     for (size_t i=0; i < test_size; ++i)
     23         pointers.push_back(std::shared_ptr<T>(new T()));
     24     Compare comp;
     25     UIntCompare ucomp;
     26     VoidCompare vcomp;
     27     for (size_t i=0; i < test_size; ++i) {
     28         for (size_t j=0; j < test_size; ++j) {
     29             T* lhs = pointers[i].get();
     30             T* rhs = pointers[j].get();
     31             std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs);
     32             std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs);
     33             assert(comp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
     34             assert(vcomp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
     35         }
     36     }
     37 }
     38 
     39 #endif // POINTER_COMPARISON_TEST_HELPER_HPP
     40