File: pointer_comparison_test_helper.hpp

package info (click to toggle)
llvm-toolchain-9 1%3A9.0.1-16
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 882,436 kB
  • sloc: cpp: 4,167,636; ansic: 714,256; asm: 457,610; python: 155,927; objc: 65,094; sh: 42,856; lisp: 26,908; perl: 7,786; pascal: 7,722; makefile: 6,881; ml: 5,581; awk: 3,648; cs: 2,027; xml: 888; javascript: 381; ruby: 156
file content (39 lines) | stat: -rw-r--r-- 1,262 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#ifndef POINTER_COMPARISON_TEST_HELPER_HPP
#define POINTER_COMPARISON_TEST_HELPER_HPP

#include <vector>
#include <memory>
#include <cstdint>
#include <cassert>

#include "test_macros.h"

template <class T, template<class> class CompareTemplate>
void do_pointer_comparison_test() {
    typedef CompareTemplate<T*> Compare;
    typedef CompareTemplate<std::uintptr_t> UIntCompare;
#if TEST_STD_VER > 11
    typedef CompareTemplate<void> VoidCompare;
#else
    typedef Compare VoidCompare;
#endif
    std::vector<std::shared_ptr<T> > pointers;
    const std::size_t test_size = 100;
    for (size_t i=0; i < test_size; ++i)
        pointers.push_back(std::shared_ptr<T>(new T()));
    Compare comp;
    UIntCompare ucomp;
    VoidCompare vcomp;
    for (size_t i=0; i < test_size; ++i) {
        for (size_t j=0; j < test_size; ++j) {
            T* lhs = pointers[i].get();
            T* rhs = pointers[j].get();
            std::uintptr_t lhs_uint = reinterpret_cast<std::uintptr_t>(lhs);
            std::uintptr_t rhs_uint = reinterpret_cast<std::uintptr_t>(rhs);
            assert(comp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
            assert(vcomp(lhs, rhs) == ucomp(lhs_uint, rhs_uint));
        }
    }
}

#endif // POINTER_COMPARISON_TEST_HELPER_HPP