basedOpinionated utility library |
git clone git://git.dimitrijedobrota.com/based.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING |
list_test.cpp (4569B)
0 #include <numeric>
2 #include "based/list.hpp"
4 #include <catch2/catch_test_macros.hpp>
6 #include "based/types/literals.hpp"
8 template class based::list_pool<based::u8, based::u8>;
10 // NOLINTBEGIN(*complexity*)
12 TEST_CASE("list_pool", "[list/list_pool]")
13 {
14 using namespace based::literals; // NOLINT(*namespace*)
15 using list_pool = based::list_pool<based::u8, based::u8>;
17 auto pool = list_pool();
18 auto head = pool.node_empty();
20 SECTION("node_empty is empty")
21 {
22 REQUIRE(pool.is_empty(head) == true);
23 REQUIRE(pool.node_empty() == head);
24 }
26 SECTION("add one node")
27 {
28 head = pool.allocate(1_u8, head);
30 REQUIRE(pool.is_empty(head) == false);
31 REQUIRE(pool.value(head) == 1_u8);
33 REQUIRE(pool.next(head) == pool.node_empty());
35 SECTION("add two nodes")
36 {
37 head = pool.allocate(2_u8, head);
39 REQUIRE(pool.is_empty(head) == false);
40 REQUIRE(pool.value(head) == 2_u8);
42 REQUIRE(pool.value(pool.next(head)) == 1_u8);
43 REQUIRE(pool.next(pool.next(head)) == pool.node_empty());
45 head = pool.free(head);
46 }
48 SECTION("alloc after free")
49 {
50 head = pool.allocate(2_u8, head);
51 head = pool.free(head);
52 head = pool.allocate(3_u8, head);
54 REQUIRE(pool.is_empty(head) == false);
55 REQUIRE(pool.value(head) == 3_u8);
57 head = pool.free(head);
58 }
60 head = pool.free(head);
61 }
63 REQUIRE(pool.is_empty(head) == true);
64 REQUIRE(pool.node_empty() == head);
65 }
67 TEST_CASE("list_pool iterator", "[list/list_pool]")
68 {
69 using namespace based::literals; // NOLINT(*namespace*)
70 using list_pool = based::list_pool<based::u8, based::u8>;
72 auto pool = list_pool();
73 auto head = pool.node_empty();
75 static constexpr auto iter_count = 255_u8;
76 for (auto i = 0_u8; i < iter_count; i++) {
77 head = pool.allocate(i, head);
78 }
80 SECTION("for-loop")
81 {
82 using iter = list_pool::iterator;
84 auto sum = 0_u32;
85 for (auto it = iter(pool, head); it != iter(pool); it++) {
86 sum += *it.operator->();
87 sum += *it;
88 }
90 REQUIRE(sum == 255_u32 * 254_u32);
91 }
93 SECTION("accumulate")
94 {
95 using iter = list_pool::iterator;
97 const auto sum = std::accumulate(
98 iter(pool, head),
99 iter(pool),
100 based::u32 {0},
101 [](auto a, auto b)
102 {
103 return a + b;
104 }
105 );
107 REQUIRE(sum == 255_u32 * 254_u32 / 2_u32);
108 }
110 based::free_list(pool, head);
111 }
113 TEST_CASE("list_pool const iterator", "[list/list_pool]")
114 {
115 using namespace based::literals; // NOLINT(*namespace*)
116 using list_pool = based::list_pool<based::u8, based::u8>;
118 auto pool = list_pool();
119 auto head = pool.node_empty();
121 static constexpr auto iter_count = 255_u8;
122 for (auto i = 0_u8; i < iter_count; i++) {
123 head = pool.allocate(i, head);
124 }
126 SECTION("const for-loop")
127 {
128 using iter = list_pool::const_iterator;
130 auto sum = 0_u32;
131 for (auto it = iter(pool, head); it != iter(pool); it++) {
132 sum += *it.operator->();
133 sum += *it;
134 }
136 REQUIRE(sum == 255_u32 * 254_u32);
137 }
139 SECTION("const accumulate")
140 {
141 using iter = list_pool::const_iterator;
143 static const auto sum =
144 [](const list_pool& lpool, const list_pool::list_type& lhead)
145 {
146 return std::accumulate(
147 iter(lpool, lhead),
148 iter(lpool),
149 based::u32 {0},
150 [](auto a, auto b)
151 {
152 return a + b;
153 }
154 );
155 };
157 REQUIRE(sum(pool, head) == 255_u32 * 254_u32 / 2_u32);
158 }
160 based::free_list(pool, head);
161 }
163 TEST_CASE("list_pool queue", "[list/list_pool/queue]")
164 {
165 using list_pool = based::list_pool<based::u8, based::u8>;
166 using iter = list_pool::iterator;
168 auto pool = list_pool();
169 auto queue = pool.queue_empty();
171 SECTION("free(empty, empty)")
172 {
173 REQUIRE(pool.free(queue.first, queue.second) == pool.node_empty());
174 }
176 SECTION("pop_front(empty)")
177 {
178 REQUIRE(pool.pop_front(queue) == queue);
179 }
181 SECTION("operation")
182 {
183 using namespace based::literals; // NOLINT(*namespace*)
184 static constexpr auto iter_count = 255_u8;
185 for (auto i = 0_u8; i < iter_count; i++) {
186 if (i % 2_u8 == 0_u8) {
187 queue = pool.push_front(queue, i);
188 } else {
189 queue = pool.push_back(queue, i);
190 }
192 if (i % 3_u8 == 0_u8) {
193 queue = pool.pop_front(queue);
194 }
195 }
197 auto sum = 0_u64;
198 for (auto it = iter(pool, queue.first); it != iter(pool); ++it) {
199 sum += *it;
200 }
202 pool.free(queue);
204 REQUIRE(sum == 21717_u64);
205 }
206 }
208 // NOLINTEND(*complexity*)