based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

list_test.cpp (2924B)


1 #include <numeric> 2 3 #include "based/list.hpp" 4 5 #include <catch2/catch_test_macros.hpp> 6 7 TEST_CASE("list_pool", "[list/list_pool]") 8 { 9 using list_pool = based::list_pool<std::uint8_t, std::uint8_t>; 10 11 auto pool = list_pool(); 12 auto head = pool.node_empty(); 13 14 SECTION("node_empty is empty") 15 { 16 REQUIRE(pool.is_empty(head) == true); 17 REQUIRE(pool.node_empty() == head); 18 } 19 20 SECTION("add one node") 21 { 22 head = pool.allocate(1, head); 23 24 REQUIRE(pool.is_empty(head) == false); 25 REQUIRE(pool.value(head) == 1); 26 27 REQUIRE(pool.next(head) == pool.node_empty()); 28 29 SECTION("add two nodes") 30 { 31 head = pool.allocate(2, head); 32 33 REQUIRE(pool.is_empty(head) == false); 34 REQUIRE(pool.value(head) == 2); 35 36 REQUIRE(pool.value(pool.next(head)) == 1); 37 REQUIRE(pool.next(pool.next(head)) == pool.node_empty()); 38 39 head = pool.free(head); 40 } 41 42 SECTION("alloc after free") 43 { 44 head = pool.allocate(2, head); 45 head = pool.free(head); 46 head = pool.allocate(3, head); 47 48 REQUIRE(pool.is_empty(head) == false); 49 REQUIRE(pool.value(head) == 3); 50 51 head = pool.free(head); 52 } 53 54 head = pool.free(head); 55 } 56 57 REQUIRE(pool.is_empty(head) == true); 58 REQUIRE(pool.node_empty() == head); 59 } 60 61 TEST_CASE("list_pool iterator", "[list/list_pool/iterator]") 62 { 63 using list_pool = based::list_pool<std::uint8_t, std::uint8_t>; 64 using iter = list_pool::iterator; 65 66 auto pool = list_pool(); 67 auto head = pool.node_empty(); 68 69 for (std::uint8_t i = 0; i < 0xFF; i++) { 70 head = pool.allocate(i, head); 71 } 72 73 SECTION("for-loop") 74 { 75 std::uint64_t sum = 0; 76 for (auto it = iter(pool, head); it != iter(pool); ++it) { 77 sum += *it; 78 } 79 80 REQUIRE(sum == 0xFF * 0xFE / 2); 81 } 82 83 SECTION("accumulate") 84 { 85 const auto sum = std::accumulate(iter(pool, head), 86 iter(pool), 87 std::uint64_t {0}, 88 [](auto a, auto b) { return a + b; }); 89 90 REQUIRE(sum == 0xFF * 0xFE / 2); 91 } 92 93 based::free_list(pool, head); 94 } 95 96 TEST_CASE("list_pool queue", "[list/list_pool/queue]") 97 { 98 using list_pool = based::list_pool<std::uint8_t, std::uint8_t>; 99 using iter = list_pool::iterator; 100 101 auto pool = list_pool(); 102 auto queue = pool.queue_empty(); 103 104 SECTION("free(empty, empty)") 105 { 106 REQUIRE(pool.free(queue.first, queue.second) == pool.node_empty()); 107 } 108 109 SECTION("pop_front(empty)") 110 { 111 REQUIRE(pool.pop_front(queue) == queue); 112 } 113 114 for (std::uint8_t i = 0; i < 0xFF; i++) { 115 if (i % 2 == 0) { 116 queue = pool.push_front(queue, i); 117 } else { 118 queue = pool.push_back(queue, i); 119 } 120 121 if (i % 3 == 0) { 122 queue = pool.pop_front(queue); 123 } 124 } 125 126 std::uint64_t sum = 0; 127 for (auto it = iter(pool, queue.first); it != iter(pool); ++it) { 128 sum += *it; 129 } 130 131 pool.free(queue); 132 133 REQUIRE(sum == 21717); 134 }