custom_allocator_tests.cpp
Go to the documentation of this file.
1 
24 #include <tsl/robin_map.h>
25 
26 #include <boost/test/unit_test.hpp>
27 #include <cstddef>
28 #include <cstdlib>
29 #include <functional>
30 #include <limits>
31 #include <stdexcept>
32 #include <type_traits>
33 #include <utility>
34 
35 #include "utils.h"
36 
37 static std::size_t nb_custom_allocs = 0;
38 
39 template <typename T>
41  public:
42  using value_type = T;
43  using pointer = T*;
44  using const_pointer = const T*;
45  using reference = T&;
46  using const_reference = const T&;
47  using size_type = std::size_t;
48  using difference_type = std::ptrdiff_t;
50 
51  template <typename U>
52  struct rebind {
54  };
55 
56  custom_allocator() = default;
57  custom_allocator(const custom_allocator&) = default;
58 
59  template <typename U>
61 
62  pointer address(reference x) const noexcept { return &x; }
63 
64  const_pointer address(const_reference x) const noexcept { return &x; }
65 
66  pointer allocate(size_type n, const void* /*hint*/ = 0) {
68 
69  pointer ptr = static_cast<pointer>(std::malloc(n * sizeof(T)));
70  if (ptr == nullptr) {
71 #ifdef TSL_RH_NO_EXCEPTIONS
72  std::abort();
73 #else
74  throw std::bad_alloc();
75 #endif
76  }
77 
78  return ptr;
79  }
80 
81  void deallocate(T* p, size_type /*n*/) { std::free(p); }
82 
83  size_type max_size() const noexcept {
84  return std::numeric_limits<size_type>::max() / sizeof(value_type);
85  }
86 
87  template <typename U, typename... Args>
88  void construct(U* p, Args&&... args) {
89  ::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
90  }
91 
92  template <typename U>
93  void destroy(U* p) {
94  p->~U();
95  }
96 };
97 
98 template <class T, class U>
100  return true;
101 }
102 
103 template <class T, class U>
105  return false;
106 }
107 
108 // TODO Avoid overloading new to check number of global new
109 // static std::size_t nb_global_new = 0;
110 // void* operator new(std::size_t sz) {
111 // nb_global_new++;
112 // return std::malloc(sz);
113 // }
114 //
115 // void operator delete(void* ptr) noexcept {
116 // std::free(ptr);
117 // }
118 
119 BOOST_AUTO_TEST_SUITE(test_custom_allocator)
120 
121 BOOST_AUTO_TEST_CASE(test_custom_allocator_1) {
122  // nb_global_new = 0;
123  nb_custom_allocs = 0;
124 
125  tsl::robin_map<int, int, std::hash<int>, std::equal_to<int>,
127  map;
128 
129  const int nb_elements = 1000;
130  for (int i = 0; i < nb_elements; i++) {
131  map.insert({i, i * 2});
132  }
133 
134  BOOST_CHECK_NE(nb_custom_allocs, 0);
135  // BOOST_CHECK_EQUAL(nb_global_new, 0);
136 }
137 
138 BOOST_AUTO_TEST_SUITE_END()
custom_allocator::address
const_pointer address(const_reference x) const noexcept
Definition: custom_allocator_tests.cpp:64
custom_allocator::construct
void construct(U *p, Args &&... args)
Definition: custom_allocator_tests.cpp:88
operator==
bool operator==(const custom_allocator< T > &, const custom_allocator< U > &)
Definition: custom_allocator_tests.cpp:99
robin_map.h
custom_allocator::address
pointer address(reference x) const noexcept
Definition: custom_allocator_tests.cpp:62
custom_allocator::reference
T & reference
Definition: custom_allocator_tests.cpp:45
custom_allocator::size_type
std::size_t size_type
Definition: custom_allocator_tests.cpp:47
custom_allocator::allocate
pointer allocate(size_type n, const void *=0)
Definition: custom_allocator_tests.cpp:66
tsl::robin_map::insert
std::pair< iterator, bool > insert(const value_type &value)
Definition: robin_map.h:233
testing::internal::true_type
bool_constant< true > true_type
Definition: gtest.h:2725
custom_allocator
Definition: custom_allocator_tests.cpp:40
custom_allocator::destroy
void destroy(U *p)
Definition: custom_allocator_tests.cpp:93
custom_allocator::const_pointer
const T * const_pointer
Definition: custom_allocator_tests.cpp:44
test.x
x
Definition: test.py:4
custom_allocator::propagate_on_container_move_assignment
std::true_type propagate_on_container_move_assignment
Definition: custom_allocator_tests.cpp:49
custom_allocator::value_type
T value_type
Definition: custom_allocator_tests.cpp:42
nb_custom_allocs
static std::size_t nb_custom_allocs
Definition: custom_allocator_tests.cpp:37
custom_allocator::deallocate
void deallocate(T *p, size_type)
Definition: custom_allocator_tests.cpp:81
custom_allocator::difference_type
std::ptrdiff_t difference_type
Definition: custom_allocator_tests.cpp:48
utils.h
custom_allocator::const_reference
const T & const_reference
Definition: custom_allocator_tests.cpp:46
custom_allocator::custom_allocator
custom_allocator(const custom_allocator< U > &)
Definition: custom_allocator_tests.cpp:60
custom_allocator::max_size
size_type max_size() const noexcept
Definition: custom_allocator_tests.cpp:83
custom_allocator::custom_allocator
custom_allocator()=default
custom_allocator::pointer
T * pointer
Definition: custom_allocator_tests.cpp:43
BOOST_AUTO_TEST_CASE
BOOST_AUTO_TEST_CASE(test_custom_allocator_1)
Definition: custom_allocator_tests.cpp:121
custom_allocator::rebind
Definition: custom_allocator_tests.cpp:52
tsl::robin_map
Definition: robin_map.h:91
operator!=
bool operator!=(const custom_allocator< T > &, const custom_allocator< U > &)
Definition: custom_allocator_tests.cpp:104


mp2p_icp
Author(s): Jose-Luis Blanco-Claraco
autogenerated on Wed Jun 26 2024 02:47:07