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 (4400B)
0 #include <numeric>
2 #include "based/list.hpp"
4 #include <catch2/catch_test_macros.hpp>
6 #include "based/types/types.hpp"
8 template class based::list_pool<based::u8, based::u8>;
10 TEST_CASE("list_pool", "[list/list_pool]")
11 {
12 using namespace based::literals; // NOLINT
13 using list_pool = based::list_pool<based::u8, based::u8>;
15 auto pool = list_pool();
16 auto head = pool.node_empty();
18 SECTION("node_empty is empty")
19 {
20 REQUIRE(pool.is_empty(head) == true);
21 REQUIRE(pool.node_empty() == head);
22 }
24 SECTION("add one node")
25 {
26 head = pool.allocate(1_u8, head);
28 REQUIRE(pool.is_empty(head) == false);
29 REQUIRE(pool.value(head) == 1_u8);
31 REQUIRE(pool.next(head) == pool.node_empty());
33 SECTION("add two nodes")
34 {
35 head = pool.allocate(2_u8, head);
37 REQUIRE(pool.is_empty(head) == false);
38 REQUIRE(pool.value(head) == 2_u8);
40 REQUIRE(pool.value(pool.next(head)) == 1_u8);
41 REQUIRE(pool.next(pool.next(head)) == pool.node_empty());
43 head = pool.free(head);
44 }
46 SECTION("alloc after free")
47 {
48 head = pool.allocate(2_u8, head);
49 head = pool.free(head);
50 head = pool.allocate(3_u8, head);
52 REQUIRE(pool.is_empty(head) == false);
53 REQUIRE(pool.value(head) == 3_u8);
55 head = pool.free(head);
56 }
58 head = pool.free(head);
59 }
61 REQUIRE(pool.is_empty(head) == true);
62 REQUIRE(pool.node_empty() == head);
63 }
65 TEST_CASE("list_pool iterator", "[list/list_pool]")
66 {
67 using namespace based::literals; // NOLINT
68 using list_pool = based::list_pool<based::u8, based::u8>;
70 auto pool = list_pool();
71 auto head = pool.node_empty();
73 static constexpr auto iter_count = 0xFF_u8;
74 for (auto i = 0_u8; i < iter_count; i++) {
75 head = pool.allocate(i, head);
76 }
78 SECTION("for-loop")
79 {
80 using iter = list_pool::iterator;
82 auto sum = 0_u32;
83 for (auto it = iter(pool, head); it != iter(pool); it++) {
84 sum += *it.operator->();
85 sum += *it;
86 }
88 REQUIRE(sum == 0xFF_u32 * 0xFE_u32);
89 }
91 SECTION("accumulate")
92 {
93 using iter = list_pool::iterator;
95 const auto sum = std::accumulate(
96 iter(pool, head),
97 iter(pool),
98 based::u32 {0},
99 [](auto a, auto b)
100 {
101 return a + b;
102 }
103 );
105 REQUIRE(sum == 0xFF_u32 * 0xFE_u32 / 2_u32);
106 }
108 based::free_list(pool, head);
109 }
111 TEST_CASE("list_pool const iterator", "[list/list_pool]")
112 {
113 using namespace based::literals; // NOLINT
114 using list_pool = based::list_pool<based::u8, based::u8>;
116 auto pool = list_pool();
117 auto head = pool.node_empty();
119 static constexpr auto iter_count = 0xFF_u8;
120 for (auto i = 0_u8; i < iter_count; i++) {
121 head = pool.allocate(i, head);
122 }
124 SECTION("const for-loop")
125 {
126 using iter = list_pool::const_iterator;
128 auto sum = 0_u32;
129 for (auto it = iter(pool, head); it != iter(pool); it++) {
130 sum += *it.operator->();
131 sum += *it;
132 }
134 REQUIRE(sum == 0xFF_u32 * 0xFE_u32);
135 }
137 SECTION("const accumulate")
138 {
139 using iter = list_pool::const_iterator;
141 static const auto sum =
142 [](const list_pool& lpool, const list_pool::list_type& lhead)
143 {
144 return std::accumulate(
145 iter(lpool, lhead),
146 iter(lpool),
147 based::u32 {0},
148 [](auto a, auto b)
149 {
150 return a + b;
151 }
152 );
153 };
155 REQUIRE(sum(pool, head) == 0xFF_u32 * 0xFE_u32 / 2_u32);
156 }
158 based::free_list(pool, head);
159 }
161 TEST_CASE("list_pool queue", "[list/list_pool/queue]")
162 {
163 using list_pool = based::list_pool<based::u8, based::u8>;
164 using iter = list_pool::iterator;
166 auto pool = list_pool();
167 auto queue = pool.queue_empty();
169 SECTION("free(empty, empty)")
170 {
171 REQUIRE(pool.free(queue.first, queue.second) == pool.node_empty());
172 }
174 SECTION("pop_front(empty)")
175 {
176 REQUIRE(pool.pop_front(queue) == queue);
177 }
179 using namespace based::literals; // NOLINT
180 static constexpr auto iter_count = 0xFF_u8;
181 for (auto i = 0_u8; i < iter_count; i++) {
182 if (i % 2_u8 == 0_u8) {
183 queue = pool.push_front(queue, i);
184 } else {
185 queue = pool.push_back(queue, i);
186 }
188 if (i % 3_u8 == 0_u8) {
189 queue = pool.pop_front(queue);
190 }
191 }
193 auto sum = 0_u64;
194 for (auto it = iter(pool, queue.first); it != iter(pool); ++it) {
195 sum += *it;
196 }
198 pool.free(queue);
200 REQUIRE(sum == 21717_u64);
201 }