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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
/**
* MIT License
*
* Copyright (c) 2017 Thibaut Goetghebuer-Planchon <tessil@gmx.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include <tsl/sparse_map.h>
#include <boost/test/unit_test.hpp>
#include <cstddef>
#include <cstdlib>
#include <functional>
#include <limits>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include "utils.h"
static std::size_t nb_custom_allocs = 0;
template <typename T>
class custom_allocator {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using propagate_on_container_move_assignment = std::true_type;
template <typename U>
struct rebind {
using other = custom_allocator<U>;
};
custom_allocator() = default;
custom_allocator(const custom_allocator&) = default;
template <typename U>
custom_allocator(const custom_allocator<U>&) {}
custom_allocator& operator=(const custom_allocator& other) = default;
pointer address(reference x) const noexcept { return &x; }
const_pointer address(const_reference x) const noexcept { return &x; }
pointer allocate(size_type n, const void* /*hint*/ = 0) {
nb_custom_allocs++;
pointer ptr = static_cast<pointer>(std::malloc(n * sizeof(T)));
if (ptr == nullptr) {
#if TSL_NO_EXCEPTIONS
std::abort();
#else
throw std::bad_alloc();
#endif
}
return ptr;
}
void deallocate(T* p, size_type /*n*/) { std::free(p); }
size_type max_size() const noexcept {
return std::numeric_limits<size_type>::max() / sizeof(value_type);
}
template <typename U, typename... Args>
void construct(U* p, Args&&... args) {
::new (static_cast<void*>(p)) U(std::forward<Args>(args)...);
}
template <typename U>
void destroy(U* p) {
p->~U();
}
};
template <class T, class U>
bool operator==(const custom_allocator<T>&, const custom_allocator<U>&) {
return true;
}
template <class T, class U>
bool operator!=(const custom_allocator<T>&, const custom_allocator<U>&) {
return false;
}
// TODO Avoid overloading new to check number of global new
// static std::size_t nb_global_new = 0;
// void* operator new(std::size_t sz) {
// nb_global_new++;
// return std::malloc(sz);
// }
//
// void operator delete(void* ptr) noexcept {
// std::free(ptr);
// }
BOOST_AUTO_TEST_SUITE(test_custom_allocator)
BOOST_AUTO_TEST_CASE(test_custom_allocator_1) {
// nb_global_new = 0;
nb_custom_allocs = 0;
tsl::sparse_map<int, int, std::hash<int>, std::equal_to<int>,
custom_allocator<std::pair<int, int>>>
map;
const int nb_elements = 1000;
for (int i = 0; i < nb_elements; i++) {
map.insert({i, i * 2});
}
BOOST_CHECK_NE(nb_custom_allocs, 0);
// BOOST_CHECK_EQUAL(nb_global_new, 0);
}
BOOST_AUTO_TEST_SUITE_END()
|