|
| 1 | +#include "util/pool_allocator.hpp" |
| 2 | +#include "util/typedefs.hpp" |
| 3 | +#include <boost/test/unit_test.hpp> |
| 4 | + |
| 5 | +#include <unordered_map> |
| 6 | + |
| 7 | +BOOST_AUTO_TEST_SUITE(pool_allocator) |
| 8 | + |
| 9 | +using namespace osrm; |
| 10 | +using namespace osrm::util; |
| 11 | + |
| 12 | +// in many of these tests we hope on address sanitizer to alert in the case if we are doing something wrong |
| 13 | +BOOST_AUTO_TEST_CASE(smoke) |
| 14 | +{ |
| 15 | + PoolAllocator<int> pool; |
| 16 | + auto ptr = pool.allocate(1); |
| 17 | + *ptr = 42; |
| 18 | + BOOST_CHECK_NE(ptr, nullptr); |
| 19 | + pool.deallocate(ptr, 1); |
| 20 | + |
| 21 | + ptr = pool.allocate(2); |
| 22 | + *ptr = 42; |
| 23 | + *(ptr + 1) = 43; |
| 24 | + BOOST_CHECK_NE(ptr, nullptr); |
| 25 | + pool.deallocate(ptr, 2); |
| 26 | +} |
| 27 | + |
| 28 | +BOOST_AUTO_TEST_CASE(a_lot_of_items) |
| 29 | +{ |
| 30 | + PoolAllocator<int, 1024> pool; |
| 31 | + auto ptr = pool.allocate(2048); |
| 32 | + for (int i = 0; i < 2048; ++i) |
| 33 | + { |
| 34 | + ptr[i] = i; |
| 35 | + } |
| 36 | + |
| 37 | + for (int i = 0; i < 2048; ++i) |
| 38 | + { |
| 39 | + BOOST_CHECK_EQUAL(ptr[i], i); |
| 40 | + } |
| 41 | + |
| 42 | + pool.deallocate(ptr, 2048); |
| 43 | +} |
| 44 | + |
| 45 | +BOOST_AUTO_TEST_CASE(copy) |
| 46 | +{ |
| 47 | + PoolAllocator<int> pool; |
| 48 | + auto ptr = pool.allocate(1); |
| 49 | + *ptr = 42; |
| 50 | + BOOST_CHECK_NE(ptr, nullptr); |
| 51 | + pool.deallocate(ptr, 1); |
| 52 | + |
| 53 | + PoolAllocator<int> pool2(pool); |
| 54 | + ptr = pool2.allocate(1); |
| 55 | + *ptr = 42; |
| 56 | + BOOST_CHECK_NE(ptr, nullptr); |
| 57 | + pool2.deallocate(ptr, 1); |
| 58 | +} |
| 59 | + |
| 60 | +BOOST_AUTO_TEST_CASE(unordered_map) |
| 61 | +{ |
| 62 | + std::unordered_map<int, int, std::hash<int>, std::equal_to<int>, PoolAllocator<std::pair<const int, int>>> map; |
| 63 | + map[1] = 42; |
| 64 | + BOOST_CHECK_EQUAL(map[1], 42); |
| 65 | + |
| 66 | + map.clear(); |
| 67 | + |
| 68 | + map[2] = 43; |
| 69 | + |
| 70 | + BOOST_CHECK_EQUAL(map[2], 43); |
| 71 | +} |
| 72 | + |
| 73 | +BOOST_AUTO_TEST_SUITE_END() |
0 commit comments