MCPcopy Create free account
hub / github.com/ElementsProject/elements / BOOST_AUTO_TEST_CASE

Function BOOST_AUTO_TEST_CASE

src/test/allocator_tests.cpp:18–131  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16BOOST_AUTO_TEST_SUITE(allocator_tests)
17
18BOOST_AUTO_TEST_CASE(arena_tests)
19{
20 // Fake memory base address for testing
21 // without actually using memory.
22 void *synth_base = reinterpret_cast<void*>(0x08000000);
23 const size_t synth_size = 1024*1024;
24 Arena b(synth_base, synth_size, 16);
25 void *chunk = b.alloc(1000);
26#ifdef ARENA_DEBUG
27 b.walk();
28#endif
29 BOOST_CHECK(chunk != nullptr);
30 BOOST_CHECK(b.stats().used == 1008); // Aligned to 16
31 BOOST_CHECK(b.stats().total == synth_size); // Nothing has disappeared?
32 b.free(chunk);
33#ifdef ARENA_DEBUG
34 b.walk();
35#endif
36 BOOST_CHECK(b.stats().used == 0);
37 BOOST_CHECK(b.stats().free == synth_size);
38 try { // Test exception on double-free
39 b.free(chunk);
40 BOOST_CHECK(0);
41 } catch(std::runtime_error &)
42 {
43 }
44
45 void *a0 = b.alloc(128);
46 void *a1 = b.alloc(256);
47 void *a2 = b.alloc(512);
48 BOOST_CHECK(b.stats().used == 896);
49 BOOST_CHECK(b.stats().total == synth_size);
50#ifdef ARENA_DEBUG
51 b.walk();
52#endif
53 b.free(a0);
54#ifdef ARENA_DEBUG
55 b.walk();
56#endif
57 BOOST_CHECK(b.stats().used == 768);
58 b.free(a1);
59 BOOST_CHECK(b.stats().used == 512);
60 void *a3 = b.alloc(128);
61#ifdef ARENA_DEBUG
62 b.walk();
63#endif
64 BOOST_CHECK(b.stats().used == 640);
65 b.free(a2);
66 BOOST_CHECK(b.stats().used == 128);
67 b.free(a3);
68 BOOST_CHECK(b.stats().used == 0);
69 BOOST_CHECK_EQUAL(b.stats().chunks_used, 0U);
70 BOOST_CHECK(b.stats().total == synth_size);
71 BOOST_CHECK(b.stats().free == synth_size);
72 BOOST_CHECK_EQUAL(b.stats().chunks_free, 1U);
73
74 std::vector<void*> addr;
75 BOOST_CHECK(b.alloc(0) == nullptr); // allocating 0 always returns nullptr

Callers

nothing calls this directly

Calls 7

allocMethod · 0.80
walkMethod · 0.80
statsMethod · 0.80
freeMethod · 0.80
push_backMethod · 0.45
clearMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected