libcruft-util/test/alloc/linear.cpp

46 lines
1.2 KiB
C++
Raw Normal View History

2015-11-13 17:17:37 +11:00
#include "tap.hpp"
#include "alloc/linear.hpp"
int
main (void)
{
util::TAP::logger tap;
constexpr size_t BUFFER_SIZE = 1024;
alignas (std::max_align_t) char memory[BUFFER_SIZE];
util::alloc::linear store (std::begin (memory), std::end (memory));
2016-06-22 19:51:18 +10:00
tap.expect_eq (store.base (), std::begin (memory), "base pointers match");
tap.expect_eq (store.offset (std::begin (memory)), 0u, "base offset is 0");
2015-11-30 16:08:07 +11:00
tap.expect_eq (store.capacity (), BUFFER_SIZE, "bytes capacity matches");
2015-11-13 17:17:37 +11:00
tap.expect_throw<std::bad_alloc> (
[&] (void) { store.allocate (BUFFER_SIZE + 1, 1); },
"excessive allocation throws bad_alloc"
);
tap.expect_nothrow (
[&] (void) { store.allocate (BUFFER_SIZE); },
"maximum allocation succeeds"
);
2015-11-30 16:08:07 +11:00
tap.expect_eq (store.used (), BUFFER_SIZE, "bytes used matches");
tap.expect_eq (store.remain (), 0u, "bytes remain matches");
2015-11-13 17:17:37 +11:00
tap.expect_throw<std::bad_alloc> (
[&] (void) { store.allocate (1, 1); },
"minimum allocation fails after exhaustion"
);
store.reset ();
tap.expect_nothrow (
[&] (void) { store.allocate (1, 1); },
"minimum allocation succeeds after reset"
);
return tap.status ();
}