#include "test_simple_seg_storage.hpp"
#include "track_allocator.hpp"
#include "random_shuffle.hpp"
#include <boost/pool/simple_segregated_storage.hpp>
#include <boost/assert.hpp>
#include <boost/integer/common_factor_ct.hpp>
#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1600)
#pragma warning(push)
#pragma warning(disable: 4244)
#pragma warning(disable: 4127)
#endif
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int.hpp>
#include <boost/random/variate_generator.hpp>
#if defined(BOOST_MSVC) && (BOOST_MSVC <= 1600)
#pragma warning(pop)
#endif
#include <boost/core/lightweight_test.hpp>
#include <algorithm>
#include <functional>
#include <set>
#include <vector>
#include <cstddef>
#include <cstdlib>
#include <ctime>
#ifdef BOOST_MSVC
#pragma warning(disable:4267)
#endif
bool check_is_order(const std::vector<void*>& vs)
{
if(vs.size() < 2) { return true; }
void *lower, *higher;
std::vector<void*>::const_iterator ci = vs.begin();
lower = *(ci++);
while(ci != vs.end())
{
higher = *(ci++);
if(!std::less<void*>()(lower, higher)) { return false; }
}
return true;
}
std::size_t test_is_order(test_simp_seg_store& store)
{
std::vector<void*> vpv;
std::size_t nchunk = 0;
while(!store.empty())
{
void* const first = store.get_first();
void* const pv = store.malloc();
BOOST_TEST(first == pv);
vpv.push_back(pv);
++nchunk;
}
BOOST_TEST(check_is_order(vpv));
return nchunk;
}
boost::mt19937 gen;
int main()
{
std::srand(static_cast<unsigned>(std::time(0)));
gen.seed(static_cast<boost::uint32_t>(std::time(0)));
std::size_t partition_sz
= boost::integer::static_lcm<sizeof(void*), sizeof(int)>::value;
boost::uniform_int<> dist(partition_sz, 10000);
boost::variate_generator<boost::mt19937&,
boost::uniform_int<> > die(gen, dist);
std::size_t block_size = die();
BOOST_ASSERT(partition_sz >= sizeof(void*));
BOOST_ASSERT(partition_sz % sizeof(void*) == 0);
BOOST_ASSERT(block_size >= partition_sz);
{
char* const pc = track_allocator::malloc(block_size);
BOOST_ASSERT(pc);
int endadd = 0;
void* const pvret = test_simp_seg_store::segregate(pc, block_size,
partition_sz, &endadd);
BOOST_TEST(pvret == pc);
void* cur = test_simp_seg_store::get_nextof(static_cast<int*>(pvret));
void* last = pvret;
std::size_t nchunk = 1;
while(cur != &endadd)
{
++nchunk;
BOOST_TEST(std::less_equal<void*>()(static_cast<char*>(last)
+ partition_sz, cur));
BOOST_TEST(std::less_equal<void*>()(static_cast<char*>(cur)
+ partition_sz, pc + block_size));
last = cur;
cur = test_simp_seg_store::get_nextof(static_cast<int*>(cur));
}
BOOST_TEST(nchunk == block_size/partition_sz);
}
{
test_simp_seg_store tstore;
BOOST_TEST(tstore.empty());
char* const pc = track_allocator::malloc(block_size);
tstore.add_block(pc, block_size, partition_sz);
BOOST_TEST(tstore.get_first() == pc);
std::size_t nchunk = test_is_order(tstore);
BOOST_TEST(nchunk == block_size/partition_sz);
BOOST_ASSERT(partition_sz <= 23);
test_simp_seg_store tstore2;
char* const pc2 = track_allocator::malloc(88);
tstore2.add_block(pc2, 24, partition_sz);
tstore2.add_block(pc2 + 64, 24, partition_sz);
tstore2.add_block(pc2 + 32, 24, partition_sz);
tstore2.add_block(track_allocator::malloc(23), 23, partition_sz);
std::size_t nchunk_ref = (3*(24/partition_sz)) + (23/partition_sz);
for(nchunk = 0; !tstore2.empty(); tstore2.malloc(), ++nchunk) {}
BOOST_TEST(nchunk == nchunk_ref);
}
{
test_simp_seg_store tstore;
char* const pc = track_allocator::malloc(partition_sz);
tstore.add_block(pc, partition_sz, partition_sz);
void* pv = tstore.malloc();
BOOST_TEST(tstore.empty());
tstore.free(pv);
}
{
{
char* const pc = track_allocator::malloc(6 * partition_sz);
std::vector<void*> vpv;
vpv.push_back(pc);
vpv.push_back(pc + (2 * partition_sz));
vpv.push_back(pc + (4 * partition_sz));
do
{
test_simp_seg_store tstore;
tstore.add_ordered_block(vpv[0], 2*partition_sz, partition_sz);
tstore.add_ordered_block(vpv[1], 2*partition_sz, partition_sz);
tstore.add_ordered_block(vpv[2], 2*partition_sz, partition_sz);
test_is_order(tstore);
} while(std::next_permutation(vpv.begin(), vpv.end()));
}
{
test_simp_seg_store tstore;
char* const pc = track_allocator::malloc(6 * partition_sz);
tstore.add_ordered_block(pc, 2 * partition_sz, partition_sz);
tstore.add_ordered_block(pc + (4 * partition_sz),
(2 * partition_sz), partition_sz);
test_is_order(tstore);
}
{
test_simp_seg_store tstore;
char* const pc = track_allocator::malloc(6 * partition_sz);
tstore.add_ordered_block(pc + (4 * partition_sz),
(2 * partition_sz), partition_sz);
tstore.add_ordered_block(pc, 2 * partition_sz, partition_sz);
test_is_order(tstore);
}
}
{
char* const pc = track_allocator::malloc(6 * partition_sz);
test_simp_seg_store tstore;
tstore.add_block(pc, 6 * partition_sz, partition_sz);
std::vector<void*> vpv;
for(std::size_t i=0; i < 6; ++i) { vpv.push_back(tstore.malloc()); }
BOOST_ASSERT(tstore.empty());
pool_test_random_shuffle(vpv.begin(), vpv.end());
for(std::size_t i=0; i < 6; ++i)
{
tstore.ordered_free(vpv[i]);
}
test_is_order(tstore);
}
{
{
char* const pc = track_allocator::malloc(12 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 2 * partition_sz, partition_sz);
tstore.add_ordered_block(pc + (3 * partition_sz),
3 * partition_sz, partition_sz);
tstore.add_ordered_block(pc + (7 * partition_sz),
5 * partition_sz, partition_sz);
void* pvret = tstore.malloc_n(6, partition_sz);
BOOST_TEST(pvret == 0);
pvret = tstore.malloc_n(0, partition_sz);
BOOST_TEST(pvret == 0);
pvret = tstore.malloc_n(3, partition_sz);
BOOST_TEST(pvret == pc + (3 * partition_sz));
pvret = tstore.malloc_n(4, partition_sz);
BOOST_TEST(pvret == pc + (7 * partition_sz));
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 3);
}
{
char* const pc = track_allocator::malloc(4 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz);
void* pvret = tstore.malloc_n(1, partition_sz);
BOOST_TEST(pvret == pc);
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 3);
}
{
char* const pc = track_allocator::malloc(4 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 4 * partition_sz, partition_sz);
void* pvret = tstore.malloc_n(2, partition_sz);
BOOST_TEST(pvret == pc);
std::size_t nchunks = 0;
while(!tstore.empty())
{
tstore.malloc();
++nchunks;
}
BOOST_TEST(nchunks == 2);
}
{
char* const pc = track_allocator::malloc(12 * partition_sz);
test_simp_seg_store tstore;
tstore.add_ordered_block(pc, 2 * partition_sz, partition_sz);
tstore.add_ordered_block(pc + (3 * partition_sz),
3 * partition_sz, partition_sz);
tstore.add_ordered_block(pc + (7 * partition_sz),
5 * partition_sz, partition_sz);
tstore.malloc_n(3, partition_sz);
test_is_order(tstore);
}
}
for(std::set<char*>::iterator itr
= track_allocator::allocated_blocks.begin();
itr != track_allocator::allocated_blocks.end();
++itr)
{
delete [] *itr;
}
track_allocator::allocated_blocks.clear();
return boost::report_errors();
}