41#include <gtest/gtest.h>
49using namespace testing;
64 EXPECT_TRUE(list.is_empty());
65 EXPECT_EQ(list.get_next(), &list);
66 EXPECT_EQ(list.get_prev(), &list);
67 EXPECT_TRUE(list.is_unitarian_or_empty());
68 EXPECT_FALSE(list.is_unitarian());
74 EXPECT_TRUE(
copy.is_empty());
81 Dlink moved(std::move(list));
83 EXPECT_TRUE(list.is_empty());
118 EXPECT_TRUE(stack.is_empty());
122 FAIL() <<
"Expected underflow_error";
124 catch (
const std::underflow_error &e)
126 std::string msg = e.what();
127 EXPECT_NE(msg.find(
"empty"), std::string::npos)
128 <<
"Error message should mention 'empty': " << msg;
129 EXPECT_EQ(msg.find(
"not empty"), std::string::npos)
130 <<
"Error message should NOT say 'not empty': " << msg;
137 EXPECT_TRUE(stack.is_empty());
141 FAIL() <<
"Expected underflow_error";
143 catch (
const std::underflow_error &e)
145 std::string msg = e.what();
146 EXPECT_NE(msg.find(
"empty"), std::string::npos)
147 <<
"Error message should mention 'empty': " << msg;
148 EXPECT_EQ(msg.find(
"not empty"), std::string::npos)
149 <<
"Error message should NOT say 'not empty': " << msg;
156 EXPECT_EQ(stack.top(), &n1);
157 EXPECT_FALSE(stack.is_empty());
158 EXPECT_TRUE(stack.is_unitarian());
161 EXPECT_EQ(stack.top(), &n2);
162 EXPECT_FALSE(stack.is_unitarian());
165 EXPECT_EQ(stack.top(), &n3);
167 EXPECT_EQ(stack.pop(), &n3);
168 EXPECT_EQ(stack.top(), &n2);
170 EXPECT_EQ(stack.pop(), &n2);
171 EXPECT_EQ(stack.top(), &n1);
173 EXPECT_EQ(stack.pop(), &n1);
174 EXPECT_TRUE(stack.is_empty());
180 EXPECT_EQ(stack.pop(), &n1);
181 EXPECT_TRUE(stack.is_empty());
185 EXPECT_EQ(stack.pop(), &n3);
188 EXPECT_EQ(stack.pop(), &n4);
189 EXPECT_EQ(stack.pop(), &n2);
190 EXPECT_TRUE(stack.is_empty());
211 list.insert(&
nodes[0]);
212 EXPECT_FALSE(list.is_empty());
213 EXPECT_TRUE(list.is_unitarian());
214 EXPECT_EQ(list.get_first(), &
nodes[0]);
215 EXPECT_EQ(list.get_last(), &
nodes[0]);
220 list.append(&
nodes[0]);
221 EXPECT_FALSE(list.is_empty());
222 EXPECT_TRUE(list.is_unitarian());
223 EXPECT_EQ(list.get_first(), &
nodes[0]);
224 EXPECT_EQ(list.get_last(), &
nodes[0]);
231 for (
size_t i = 0; i < 5; ++i)
232 list.insert(&
nodes[i]);
235 for (
int i = 4; i >= 0; --i, it.
next())
243 for (
size_t i = 0; i < 5; ++i)
244 list.append(&
nodes[i]);
247 for (
size_t i = 0; i < 5; ++i, it.
next())
253 list.append(&
nodes[2]);
254 list.insert(&
nodes[1]);
255 list.append(&
nodes[3]);
256 list.insert(&
nodes[0]);
282 EXPECT_TRUE(list1.is_empty());
283 EXPECT_TRUE(list2.is_empty());
294 EXPECT_TRUE(list1.is_empty());
295 EXPECT_FALSE(list2.is_empty());
296 EXPECT_EQ(list2.get_first(), &n1);
297 EXPECT_EQ(list2.get_last(), &n3);
307 EXPECT_FALSE(list1.is_empty());
308 EXPECT_TRUE(list2.is_empty());
309 EXPECT_EQ(list1.get_first(), &n1);
310 EXPECT_EQ(list1.get_last(), &n2);
325 EXPECT_EQ(list1.get_first(), &n4);
326 EXPECT_EQ(list1.get_last(), &n6);
327 EXPECT_EQ(list2.get_first(), &n1);
328 EXPECT_EQ(list2.get_last(), &n3);
338 EXPECT_EQ(list1.get_first(), &n2);
339 EXPECT_EQ(list2.get_first(), &n1);
359 for (
size_t i = start; i < start +
count; ++i)
366 list.concat_list(&aux);
367 EXPECT_TRUE(list.is_empty());
368 EXPECT_TRUE(aux.is_empty());
373 populateList(aux, 0, 3);
375 list.concat_list(&aux);
377 EXPECT_FALSE(list.is_empty());
378 EXPECT_TRUE(aux.is_empty());
379 EXPECT_EQ(list.get_first(), &
nodes[0]);
380 EXPECT_EQ(list.get_last(), &
nodes[2]);
385 populateList(list, 0, 3);
387 list.concat_list(&aux);
389 EXPECT_FALSE(list.is_empty());
390 EXPECT_TRUE(aux.is_empty());
391 EXPECT_EQ(list.get_first(), &
nodes[0]);
392 EXPECT_EQ(list.get_last(), &
nodes[2]);
397 populateList(list, 0, 3);
398 populateList(aux, 3, 3);
400 list.concat_list(&aux);
402 EXPECT_TRUE(aux.is_empty());
403 EXPECT_EQ(list.get_first(), &
nodes[0]);
404 EXPECT_EQ(list.get_last(), &
nodes[5]);
408 for (
size_t i = 0; i < 6; ++i, it.
next())
414 populateList(list, 0, 3);
416 nodes[1].insert_list(&aux);
419 EXPECT_EQ(list.get_first(), &
nodes[0]);
420 EXPECT_EQ(list.get_last(), &
nodes[2]);
425 populateList(list, 0, 3);
426 populateList(aux, 10, 2);
428 nodes[0].insert_list(&aux);
431 EXPECT_TRUE(aux.is_empty());
432 EXPECT_EQ(list.get_first(), &
nodes[0]);
433 EXPECT_EQ(list.get_last(), &
nodes[2]);
449 populateList(list, 0, 3);
451 nodes[1].append_list(&aux);
454 EXPECT_EQ(list.get_first(), &
nodes[0]);
455 EXPECT_EQ(list.get_last(), &
nodes[2]);
461 size_t count = list.split_list(
l,
r);
465 EXPECT_TRUE(
r.is_empty());
470 list.append(&
nodes[0]);
473 size_t count = list.split_list(
l,
r);
476 EXPECT_TRUE(list.is_empty());
483 populateList(list, 0, 6);
486 size_t count = list.split_list(
l,
r);
489 EXPECT_TRUE(list.is_empty());
491 EXPECT_FALSE(
r.is_empty());
496 populateList(list, 0, 5);
499 size_t count = list.split_list(
l,
r);
502 EXPECT_TRUE(list.is_empty());
511 const Dlink * before = &
h;
515 EXPECT_EQ(p->get_prev(), before);
516 out.push_back(
static_cast<size_t>(p -
nodes.data()));
518 EXPECT_EQ(
h.get_prev(), before);
526 for (
size_t n = 0; n <= 9; ++n)
529 for (
size_t i = 0; i < n; ++i)
539 vector<size_t> left, right;
540 for (
size_t i = 0; i < n; ++i)
541 (i < n - n / 2 ? left : right).push_back(i);
549 populateList(list, 0, 5);
553 EXPECT_TRUE(list.is_empty());
560 populateList(list, 0, 5);
564 EXPECT_FALSE(list.is_empty());
565 EXPECT_EQ(list.get_first(), &
nodes[0]);
566 EXPECT_EQ(list.get_last(), &
nodes[3]);
573 populateList(list, 0, 5);
577 EXPECT_EQ(list.get_first(), &
nodes[0]);
578 EXPECT_EQ(list.get_last(), &
nodes[1]);
601 size_t count = list.reverse_list();
603 EXPECT_TRUE(list.is_empty());
608 list.append(&
nodes[0]);
609 size_t count = list.reverse_list();
611 EXPECT_EQ(list.get_first(), &
nodes[0]);
616 list.append(&
nodes[0]);
617 list.append(&
nodes[1]);
621 EXPECT_EQ(list.get_first(), &
nodes[1]);
622 EXPECT_EQ(list.get_last(), &
nodes[0]);
627 for (
size_t i = 0; i < 5; ++i)
628 list.append(&
nodes[i]);
630 size_t count = list.reverse_list();
633 EXPECT_EQ(list.get_first(), &
nodes[4]);
634 EXPECT_EQ(list.get_last(), &
nodes[0]);
638 for (
int i = 4; i >= 0; --i, it.
next())
644 for (
size_t i = 0; i < 5; ++i)
645 list.append(&
nodes[i]);
651 for (
size_t i = 0; i < 5; ++i, it.
next())
668 for (
size_t i = 0; i < 5; ++i)
678 for (
size_t i = 0; i < 5; ++i, it.
next())
703 for (
size_t i = 0; i < 5; ++i, it.
next())
709 list.rotate_right(1);
720 EXPECT_THROW(empty.
rotate_left(1), std::domain_error);
746 EXPECT_THROW(it.
get_curr(), std::overflow_error);
747 EXPECT_THROW(it.
next(), std::overflow_error);
748 EXPECT_THROW(it.
prev(), std::underflow_error);
753 for (
size_t i = 0; i < 5; ++i)
754 list.append(&
nodes[i]);
765 for (
size_t i = 0; i < 5; ++i)
766 list.append(&
nodes[i]);
778 EXPECT_EQ(
count, -1);
783 for (
size_t i = 0; i < 5; ++i)
784 list.append(&
nodes[i]);
790 EXPECT_TRUE(list.is_empty());
795 for (
size_t i = 0; i < 5; ++i)
796 list.append(&
nodes[i]);
803 EXPECT_EQ(deleted, &
nodes[2]);
812 for (
size_t i = 0; i < 4; ++i)
813 list.append(&
nodes[i]);
816 static_assert(
noexcept(it.
del_ne()));
825 EXPECT_TRUE(list.check());
826 EXPECT_EQ(list.get_next(), &
nodes[1]);
827 EXPECT_EQ(list.get_prev(), &
nodes[1]);
836TEST(DlinkIteratorDeathTest, DelNeAssertsThatThereIsACurrentNode)
840 EXPECT_DEATH((
void) on_empty.
del_ne(),
"has_curr");
848 EXPECT_DEATH((
void) past_the_end.
del_ne(),
"has_curr");
851TEST(DlinkIteratorDeathTest, DelNeDoesNotCheckThatThereIsACurrentNode)
858 EXPECT_EXIT({ (void) it.del_ne(); std::_Exit(empty.
is_empty() ? 0 : 1); },
859 ExitedWithCode(0),
"");
865 for (
size_t i = 0; i < 3; ++i)
866 list.append(&
nodes[i]);
883 for (
size_t i = 0; i < 3; ++i)
884 list.append(&
nodes[i]);
889 EXPECT_TRUE(it1 == it2);
890 EXPECT_FALSE(it1 != it2);
893 EXPECT_FALSE(it1 == it2);
894 EXPECT_TRUE(it1 != it2);
901TEST(DlinkConsistency, CheckEmptyList)
904 EXPECT_TRUE(list.
check());
907TEST(DlinkConsistency, CheckAfterOperations)
910 vector<Dlink>
nodes(10);
912 for (
size_t i = 0; i < 10; ++i)
915 EXPECT_TRUE(list.
check());
918 EXPECT_TRUE(list.
check());
921 EXPECT_TRUE(list.
check());
925 EXPECT_TRUE(
l.check());
926 EXPECT_TRUE(
r.check());
933TEST(DlinkStress, LargeListOperations)
935 constexpr size_t N = 10000;
940 for (
size_t i = 0; i <
N; ++i)
943 EXPECT_TRUE(list.
check());
948 EXPECT_TRUE(list.
check());
954 EXPECT_TRUE(
l.check());
955 EXPECT_TRUE(
r.check());
959 EXPECT_TRUE(
l.check());
962TEST(DlinkStress, RandomOperations)
964 constexpr size_t N = 1000;
965 constexpr size_t OPS = 5000;
969 vector<bool> inList(
N,
false);
971 std::mt19937
rng(42);
972 std::uniform_int_distribution<size_t> nodeDist(0,
N - 1);
973 std::uniform_int_distribution<int> opDist(0, 3);
975 for (
size_t op = 0; op < OPS; ++op)
977 int operation = opDist(
rng);
978 size_t nodeIdx = nodeDist(
rng);
983 if (!inList[nodeIdx])
986 inList[nodeIdx] =
true;
990 if (!inList[nodeIdx])
993 inList[nodeIdx] =
true;
1000 for (
size_t i = 0; i <
N; ++i)
1002 if (&
nodes[i] == removed)
1014 for (
size_t i = 0; i <
N; ++i)
1016 if (&
nodes[i] == removed)
1027 EXPECT_TRUE(list.
check());
1034TEST(DlinkMoveSemantics, MoveConstructor)
1042 Dlink moved(std::move(list));
1050TEST(DlinkMoveSemantics, MoveAssignment)
1058 list2 = std::move(list1);
1085TEST(DlinkEdgeCases, RemoveOnlyElement)
1094 EXPECT_EQ(removed, &n1);
1098TEST(DlinkEdgeCases, IteratorOnUnitaryList)
1118TEST(DlinkStress, MassiveListOperations)
1120 constexpr size_t N = 50000;
1125 for (
size_t i = 0; i <
N; ++i)
1128 EXPECT_TRUE(list.
check());
1139 for (
int i = 0; i < 5; ++i)
1142 EXPECT_TRUE(list.
check());
1152TEST(DlinkStress, RepeatedSplitConcat)
1154 constexpr size_t N = 1000;
1155 constexpr size_t ITERATIONS = 100;
1160 for (
size_t i = 0; i <
N; ++i)
1163 std::mt19937
rng(54321);
1165 for (
size_t iter = 0; iter < ITERATIONS; ++iter)
1171 EXPECT_TRUE(
l.check());
1172 EXPECT_TRUE(
r.check());
1186 EXPECT_TRUE(list.
check());
1190TEST(DlinkFuzz, RandomInsertRemoveOperations)
1192 constexpr size_t N = 2000;
1193 constexpr size_t OPS = 10000;
1197 vector<bool> inList(
N,
false);
1198 size_t listSize = 0;
1200 std::mt19937
rng(12345);
1201 std::uniform_int_distribution<size_t> nodeDist(0,
N - 1);
1202 std::uniform_int_distribution<int> opDist(0, 4);
1204 for (
size_t op = 0; op < OPS; ++op)
1206 int operation = opDist(
rng);
1207 size_t nodeIdx = nodeDist(
rng);
1212 if (!inList[nodeIdx])
1215 inList[nodeIdx] =
true;
1220 if (!inList[nodeIdx])
1223 inList[nodeIdx] =
true;
1231 for (
size_t i = 0; i <
N; ++i)
1233 if (&
nodes[i] == removed)
1246 for (
size_t i = 0; i <
N; ++i)
1248 if (&
nodes[i] == removed)
1265 EXPECT_TRUE(list.
check());
1268 EXPECT_TRUE(list.
check());
1271 size_t actualSize = 0;
1274 EXPECT_EQ(actualSize, listSize);
1279 constexpr size_t N = 1000;
1284 for (
size_t i = 0; i <
N; ++i)
1288 for (
size_t i = 0; i < 100; ++i)
1291 EXPECT_TRUE(list.
check());
1294 EXPECT_TRUE(list.
check());
1299 EXPECT_TRUE(list.
check());
1302TEST(DlinkStress, IteratorDeletionUnderStress)
1304 constexpr size_t N = 500;
1309 for (
size_t i = 0; i <
N; ++i)
1324 EXPECT_TRUE(list.
check());
1327 size_t remaining = 0;
1330 EXPECT_EQ(remaining,
N / 2);
size_t size_t int32_t * out
void next_ne() noexcept
Move the iterator one position backward guaranteeing no exception.
constexpr bool is_in_first() const noexcept
Return true if the iterator is positiones on the first item.
bool has_curr() const noexcept
Return true if the iterator has current item.
Dlink * get_curr() const
Return the current node of iterator.
void next()
Move the iterator one position forward.
Dlink * del()
Remove from the list the current node and move the iterator one position forward.
void prev()
Move the iterator one position backward.
Dlink * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
Dlink * del_ne() noexcept
Remove from the list the current node and move the iterator one position forward, without checking th...
void reset_last() noexcept
Reset the iterator to the last item of list.
bool is_last() const noexcept
Doubly linked circular list node.
constexpr Dlink *& get_last() const noexcept
If this is a header node, it return the last node of this
Dlink * remove_first() noexcept
See remove_next().
constexpr Dlink *& get_first() const noexcept
If this is a header node, it return the first node of this
void init() noexcept
See reset().
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
void rotate_right(size_t n)
Analogous to rotate_left() but to right.
void append(Dlink *node) noexcept
Insert node before this.
void swap(Dlink *link) noexcept
Swap this with list whose header is link.
constexpr bool is_unitarian() const noexcept
Return true if this (as header node) has exactly one element.
Dlink * remove_last() noexcept
See remove_prev().
void reset() noexcept
Reset this
void rotate_left(size_t n)
Rotate to left the list n positions.
bool check()
Return true if the list is consistent.
size_t split_list(Dlink &l, Dlink &r) noexcept
void insert(Dlink *node) noexcept
Insert node after this.
Dlink *& get_prev() const noexcept
Return the link that is before this
Dlink cut_list(Dlink *link) noexcept
Cut this from link.
Dlink *& get_next() const noexcept
Return the link that is after this
size_t reverse_list() noexcept
Reverse the list.
T & append(const T &item)
void concat_list(DynList &l) noexcept
See concat(DynList & l).
constexpr bool is_empty() const noexcept
Number Theoretic Transform over Z / MOD Z.
void populateList(Dlink &l, size_t start, size_t count)
Doubly linked circular list implementation.
static vector< size_t > positions_of(const Dlink &h, const vector< Dlink > &nodes)
TEST_F(DlinkBasicTest, DefaultConstructorCreatesEmptyList)
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.