Line data Source code
1 : // Author(s): Maurice Laveaux 2 : // Copyright: see the accompanying file COPYING or copy at 3 : // https://github.com/mCRL2org/mCRL2/blob/master/COPYING 4 : // 5 : // Distributed under the Boost Software License, Version 1.0. 6 : // (See accompanying file LICENSE_1_0.txt or copy at 7 : // http://www.boost.org/LICENSE_1_0.txt) 8 : // 9 : 10 : #include "mcrl2/utilities/block_allocator.h" 11 : 12 : #define BOOST_AUTO_TEST_MAIN 13 : #include <boost/test/included/unit_test.hpp> 14 : 15 : #include <vector> 16 : 17 : using namespace mcrl2::utilities; 18 : 19 2 : BOOST_AUTO_TEST_CASE(test_has_extension) 20 : { 21 : // This test assumes that 16 allocations all end up in the same block, if that is not true 22 : // this should be adapted. 23 1 : block_allocator<int, 16> allocator; 24 : 25 1 : BOOST_CHECK(allocator.consolidate() == 0); // Should be empty initially. 26 : 27 : // Allocate a full block and keep track of pointers 28 1 : std::vector<int*> test; 29 17 : for (std::size_t i = 0; i < 16; ++i) 30 : { 31 16 : test.push_back(allocator.allocate(1)); 32 : } 33 : 34 : // Allocate more spaces to fill the allocator. 35 1 : std::vector<int*> extra; 36 1025 : for (std::size_t i = 0; i < 1024; ++i) 37 : { 38 1024 : extra.push_back(allocator.allocate(1)); 39 : } 40 : 41 : // Remove the first 5 elements. 42 1 : int counter = 5; 43 1 : auto it = test.begin(); 44 5 : for (; it != test.end(); ++it) 45 : { 46 5 : allocator.deallocate(*it, 1); 47 5 : if (--counter == 0) 48 : { 49 1 : ++it; 50 1 : break; 51 : } 52 : } 53 : 54 1 : BOOST_CHECK(allocator.consolidate() == 0); // Block still contains 9 elements. 55 : 56 12 : for (; it != test.end(); ++it) 57 : { 58 11 : allocator.deallocate(*it, 1); 59 : } 60 : 61 1 : BOOST_CHECK(allocator.consolidate() == 1); // Block is empty. 62 : 63 1025 : for (auto& element : extra) 64 : { 65 1024 : allocator.deallocate(element, 1); 66 : } 67 : 68 1 : BOOST_CHECK(allocator.consolidate() == 1024/16); // Whole allocator should be emptied. 69 1 : }