2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/tap.hpp>
|
|
|
|
#include <cruft/util/alloc/linear.hpp>
|
2015-11-13 17:17:37 +11:00
|
|
|
|
|
|
|
|
2017-08-29 16:53:11 +10:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2015-11-13 17:17:37 +11:00
|
|
|
int
|
|
|
|
main (void)
|
|
|
|
{
|
2018-08-05 14:42:02 +10:00
|
|
|
cruft::TAP::logger tap;
|
2015-11-13 17:17:37 +11:00
|
|
|
|
|
|
|
constexpr size_t BUFFER_SIZE = 1024;
|
|
|
|
|
2018-12-19 17:16:57 +11:00
|
|
|
alignas (std::max_align_t) u08 memory[BUFFER_SIZE];
|
2018-12-19 17:55:24 +11:00
|
|
|
cruft::alloc::linear store (cruft::make_view (memory));
|
2015-11-13 17:17:37 +11:00
|
|
|
|
2017-08-31 13:48:33 +10:00
|
|
|
tap.expect_eq (store.begin (), std::begin (memory), "base pointers match");
|
2016-06-22 19:51:18 +10:00
|
|
|
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> (
|
2018-12-19 17:16:57 +11:00
|
|
|
[&] (void) { store.allocate (BUFFER_SIZE + 1, 1); },
|
2015-11-13 17:17:37 +11:00
|
|
|
"excessive allocation throws bad_alloc"
|
|
|
|
);
|
|
|
|
|
|
|
|
tap.expect_nothrow (
|
2018-12-19 17:16:57 +11:00
|
|
|
[&] (void) { store.allocate (BUFFER_SIZE); },
|
2015-11-13 17:17:37 +11:00
|
|
|
"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> (
|
2018-12-19 17:16:57 +11:00
|
|
|
[&] (void) { store.allocate (1, 1); },
|
2015-11-13 17:17:37 +11:00
|
|
|
"minimum allocation fails after exhaustion"
|
|
|
|
);
|
|
|
|
|
|
|
|
store.reset ();
|
|
|
|
|
|
|
|
tap.expect_nothrow (
|
2018-12-19 17:16:57 +11:00
|
|
|
[&] (void) { store.allocate (1, 1); },
|
2015-11-13 17:17:37 +11:00
|
|
|
"minimum allocation succeeds after reset"
|
|
|
|
);
|
|
|
|
|
|
|
|
return tap.status ();
|
|
|
|
}
|