Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
container_edge_cases_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
47#include <gtest/gtest.h>
48#include <limits>
49#include <random>
50#include <vector>
51#include <string>
52#include <set>
53#include <deque>
54
55// Container headers
56#include <tpl_array.H>
57#include <tpl_dynArray.H>
58#include <tpl_arrayQueue.H>
59#include <tpl_arrayStack.H>
60#include <tpl_dynDlist.H>
61#include <htlist.H>
62#include <tpl_odhash.H>
63#include <tpl_olhash.H>
64#include <tpl_dynSetTree.H>
65#include <bitArray.H>
66
67using namespace std;
68using namespace Aleph;
69
70// ============================================================================
71// DynArray Edge Cases
72// ============================================================================
73
74class DynArrayEdgeCases : public ::testing::Test
75{
76protected:
78};
79
80TEST_F(DynArrayEdgeCases, EmptyArray_SizeIsZero)
81{
82 EXPECT_EQ(arr.size(), 0);
83 EXPECT_TRUE(arr.is_empty());
84}
85
86TEST_F(DynArrayEdgeCases, EmptyArray_ExistReturnsFalse)
87{
88 // Empty array - no valid indices
89 EXPECT_FALSE(arr.exist(0));
90 EXPECT_FALSE(arr.exist(100));
91}
92
93TEST_F(DynArrayEdgeCases, SingleElement_InsertAndAccess)
94{
95 arr.touch(0) = 42; // Use touch() which allocates
96 EXPECT_EQ(arr.size(), 1);
97 EXPECT_EQ(arr(0), 42); // Use () for fast read
98 EXPECT_FALSE(arr.is_empty());
99}
100
101TEST_F(DynArrayEdgeCases, SparseAccess_AutoAllocation)
102{
103 arr.touch(100) = 999; // Use touch() for allocation
104 EXPECT_EQ(arr.size(), 101);
105 EXPECT_EQ(arr(100), 999);
106 // Note: sparse arrays may not initialize intermediate elements
107 EXPECT_TRUE(arr.exist(100));
108}
109
110// Note: DynArray allocates blocks, so exist() can return true for indices
111// in allocated blocks even if not explicitly written
112TEST_F(DynArrayEdgeCases, ExistChecksAllocatedBlocks)
113{
114 arr.touch(50) = 42;
115 EXPECT_TRUE(arr.exist(50));
116 // exist() checks if the block is allocated, not just if i < size
117 EXPECT_EQ(arr.size(), 51);
118}
119
120TEST_F(DynArrayEdgeCases, LargeIndex_Grows)
121{
122 arr.touch(10000) = 1;
123 EXPECT_GE(arr.size(), 10001);
124 EXPECT_EQ(arr(10000), 1);
125}
126
127TEST_F(DynArrayEdgeCases, CutToSize_Shrinks)
128{
129 for (int i = 0; i < 100; ++i)
130 arr.touch(i) = i;
131
132 arr.cut(50);
133 EXPECT_EQ(arr.size(), 50);
134 EXPECT_EQ(arr(49), 49);
135 // Note: cut() may not deallocate memory, so exist() might still return true
136 // for previously allocated indices. The key invariant is size() == 50
137}
138
139TEST_F(DynArrayEdgeCases, CutToZero_EmptiesArray)
140{
141 arr.touch(10) = 42;
142 arr.cut(0);
143 EXPECT_EQ(arr.size(), 0);
144 EXPECT_TRUE(arr.is_empty());
145}
146
147// ============================================================================
148// ArrayQueue Edge Cases
149// ============================================================================
150
151class ArrayQueueEdgeCases : public ::testing::Test
152{
153protected:
155};
156
157TEST_F(ArrayQueueEdgeCases, EmptyQueue_IsEmpty)
158{
159 EXPECT_TRUE(queue.is_empty());
160 EXPECT_EQ(queue.size(), 0);
161}
162
163TEST_F(ArrayQueueEdgeCases, EmptyQueue_GetThrows)
164{
165 EXPECT_THROW(queue.get(), std::underflow_error);
166}
167
168TEST_F(ArrayQueueEdgeCases, EmptyQueue_SizeZero)
169{
170 // Empty queue should have size 0
171 EXPECT_EQ(queue.size(), 0);
172 // After put/get cycle, should return to empty
173 queue.put(1);
174 (void)queue.get();
175 EXPECT_EQ(queue.size(), 0);
176}
177
178TEST_F(ArrayQueueEdgeCases, SingleElement_FIFO)
179{
180 queue.put(42);
181 EXPECT_FALSE(queue.is_empty());
182 EXPECT_EQ(queue.size(), 1);
183 EXPECT_EQ(queue.front(), 42);
184 EXPECT_EQ(queue.rear(), 42);
185 EXPECT_EQ(queue.get(), 42);
186 EXPECT_TRUE(queue.is_empty());
187}
188
190{
191 for (int i = 0; i < 10; ++i)
192 queue.put(i);
193
194 EXPECT_EQ(queue.size(), 10);
195 EXPECT_EQ(queue.front(), 0);
196 EXPECT_EQ(queue.rear(), 9);
197}
198
200{
201 // ArrayQueue may auto-resize, so just verify it handles many elements
202 for (int i = 0; i < 20; ++i)
203 queue.put(i);
204
205 EXPECT_EQ(queue.size(), 20);
206 // Elements should be in FIFO order
207 for (int i = 0; i < 20; ++i)
208 EXPECT_EQ(queue.get(), i);
209}
210
212{
213 // Fill and partially empty
214 for (int i = 0; i < 8; ++i)
215 queue.put(i);
216
217 for (int i = 0; i < 5; ++i)
218 (void)queue.get();
219
220 // Add more to wrap around
221 for (int i = 0; i < 5; ++i)
222 queue.put(100 + i);
223
224 EXPECT_EQ(queue.size(), 8);
225 EXPECT_EQ(queue.front(), 5); // First remaining element
226}
227
228// ============================================================================
229// ArrayStack Edge Cases
230// ============================================================================
231
232class ArrayStackEdgeCases : public ::testing::Test
233{
234protected:
236};
237
238TEST_F(ArrayStackEdgeCases, EmptyStack_IsEmpty)
239{
240 EXPECT_TRUE(stack.is_empty());
241 EXPECT_EQ(stack.size(), 0);
242}
243
244TEST_F(ArrayStackEdgeCases, EmptyStack_PopThrows)
245{
246 EXPECT_THROW(stack.pop(), std::underflow_error);
247}
248
249TEST_F(ArrayStackEdgeCases, EmptyStack_TopThrows)
250{
251 EXPECT_THROW(stack.top(), std::underflow_error);
252}
253
254TEST_F(ArrayStackEdgeCases, SingleElement_LIFO)
255{
256 stack.push(42);
257 EXPECT_FALSE(stack.is_empty());
258 EXPECT_EQ(stack.size(), 1);
259 EXPECT_EQ(stack.top(), 42);
260 EXPECT_EQ(stack.pop(), 42);
261 EXPECT_TRUE(stack.is_empty());
262}
263
265{
266 for (int i = 0; i < 10; ++i)
267 stack.push(i);
268
269 EXPECT_EQ(stack.size(), 10);
270 EXPECT_EQ(stack.top(), 9);
271}
272
274{
275 // ArrayStack may auto-resize, so just verify it handles many elements
276 for (int i = 0; i < 20; ++i)
277 stack.push(i);
278
279 EXPECT_EQ(stack.size(), 20);
280 // Elements should be in LIFO order
281 for (int i = 19; i >= 0; --i)
282 EXPECT_EQ(stack.pop(), i);
283}
284
286{
287 stack.push(1);
288 stack.push(2);
289 EXPECT_EQ(stack.pop(), 2);
290 stack.push(3);
291 EXPECT_EQ(stack.pop(), 3);
292 EXPECT_EQ(stack.pop(), 1);
293 EXPECT_TRUE(stack.is_empty());
294}
295
296// ============================================================================
297// DynDlist Edge Cases
298// ============================================================================
299
300class DynDlistEdgeCases : public ::testing::Test
301{
302protected:
304};
305
306TEST_F(DynDlistEdgeCases, EmptyList_IsEmpty)
307{
308 EXPECT_TRUE(dlist.is_empty());
309 EXPECT_EQ(dlist.size(), 0);
310}
311
312TEST_F(DynDlistEdgeCases, EmptyList_RemoveThrows)
313{
314 EXPECT_THROW(dlist.remove_first(), std::underflow_error);
315 EXPECT_THROW(dlist.remove_last(), std::underflow_error);
316}
317
318TEST_F(DynDlistEdgeCases, EmptyList_GetThrows)
319{
320 EXPECT_THROW(dlist.get_first(), std::underflow_error);
321 EXPECT_THROW(dlist.get_last(), std::underflow_error);
322}
323
324TEST_F(DynDlistEdgeCases, SingleElement_FirstEqualsLast)
325{
326 dlist.append(42);
327 EXPECT_EQ(dlist.size(), 1);
328 EXPECT_EQ(dlist.get_first(), 42);
329 EXPECT_EQ(dlist.get_last(), 42);
330}
331
332TEST_F(DynDlistEdgeCases, SingleElement_RemoveFirst)
333{
334 dlist.append(42);
335 EXPECT_EQ(dlist.remove_first(), 42);
336 EXPECT_TRUE(dlist.is_empty());
337}
338
339TEST_F(DynDlistEdgeCases, SingleElement_RemoveLast)
340{
341 dlist.append(42);
342 EXPECT_EQ(dlist.remove_last(), 42);
343 EXPECT_TRUE(dlist.is_empty());
344}
345
346TEST_F(DynDlistEdgeCases, AppendAndInsert_Order)
347{
348 dlist.insert(1); // Insert at front
349 dlist.append(3); // Append at back
350 dlist.insert(0); // Insert at front again
351
352 EXPECT_EQ(dlist.get_first(), 0);
353 EXPECT_EQ(dlist.get_last(), 3);
354}
355
356TEST_F(DynDlistEdgeCases, IteratorOnEmpty)
357{
358 auto it = dlist.begin();
359 EXPECT_EQ(it, dlist.end());
360}
361
363{
364 dlist.reverse(); // Should not crash
365 EXPECT_TRUE(dlist.is_empty());
366}
367
368TEST_F(DynDlistEdgeCases, ReverseSingleElement)
369{
370 dlist.append(42);
371 dlist.reverse();
372 EXPECT_EQ(dlist.size(), 1);
373 EXPECT_EQ(dlist.get_first(), 42);
374}
375
376TEST_F(DynDlistEdgeCases, ReverseMultiple)
377{
378 for (int i = 1; i <= 5; ++i)
379 dlist.append(i);
380
381 dlist.reverse();
382
383 EXPECT_EQ(dlist.get_first(), 5);
384 EXPECT_EQ(dlist.get_last(), 1);
385}
386
387TEST_F(DynDlistEdgeCases, AppendListEmpty)
388{
389 DynDlist<int> other;
390 dlist.append(std::move(other));
391 EXPECT_TRUE(dlist.is_empty());
392}
393
394TEST_F(DynDlistEdgeCases, AppendListToEmpty)
395{
396 DynDlist<int> other;
397 other.append(1);
398 other.append(2);
399
400 dlist.append(std::move(other));
401
402 EXPECT_EQ(dlist.size(), 2);
403 EXPECT_TRUE(other.is_empty());
404}
405
406// ============================================================================
407// DynList (htlist) Edge Cases
408// ============================================================================
409
410class DynListEdgeCases : public ::testing::Test
411{
412protected:
414};
415
416TEST_F(DynListEdgeCases, EmptyList_IsEmpty)
417{
418 EXPECT_TRUE(slist.is_empty());
419 EXPECT_EQ(slist.size(), 0);
420}
421
422TEST_F(DynListEdgeCases, EmptyList_RemoveThrows)
423{
424 EXPECT_THROW(slist.remove_first(), std::underflow_error);
425}
426
427TEST_F(DynListEdgeCases, SingleElement_InsertAndRemove)
428{
429 slist.insert(42);
430 EXPECT_EQ(slist.size(), 1);
431 EXPECT_EQ(slist.get_first(), 42);
432 EXPECT_EQ(slist.remove_first(), 42);
433 EXPECT_TRUE(slist.is_empty());
434}
435
436TEST_F(DynListEdgeCases, AppendKeepsOrder)
437{
438 slist.append(1);
439 slist.append(2);
440 slist.append(3);
441
442 EXPECT_EQ(slist.remove_first(), 1);
443 EXPECT_EQ(slist.remove_first(), 2);
444 EXPECT_EQ(slist.remove_first(), 3);
445}
446
447TEST_F(DynListEdgeCases, InsertReverseOrder)
448{
449 slist.insert(1);
450 slist.insert(2);
451 slist.insert(3);
452
453 EXPECT_EQ(slist.remove_first(), 3);
454 EXPECT_EQ(slist.remove_first(), 2);
455 EXPECT_EQ(slist.remove_first(), 1);
456}
457
458// ============================================================================
459// ODhashTable Edge Cases
460// ============================================================================
461
462class ODhashTableEdgeCases : public ::testing::Test
463{
464protected:
465 ODhashTable<int> table; // Uses default hash and comparison
466};
467
468TEST_F(ODhashTableEdgeCases, EmptyTable_SizeIsZero)
469{
470 EXPECT_EQ(table.size(), 0);
471 EXPECT_TRUE(table.is_empty());
472}
473
474TEST_F(ODhashTableEdgeCases, EmptyTable_SearchReturnsNull)
475{
476 EXPECT_EQ(table.search(42), nullptr);
477 EXPECT_EQ(table.search(0), nullptr);
478 EXPECT_EQ(table.search(-1), nullptr);
479}
480
481TEST_F(ODhashTableEdgeCases, EmptyTable_RemoveThrows)
482{
483 EXPECT_THROW(table.remove(42), std::domain_error);
484}
485
486TEST_F(ODhashTableEdgeCases, SingleElement_InsertSearchRemove)
487{
488 int * p = table.insert(42);
489 EXPECT_NE(p, nullptr);
490 EXPECT_EQ(*p, 42);
491 EXPECT_EQ(table.size(), 1);
492
493 int * found = table.search(42);
494 EXPECT_EQ(found, p);
495
496 EXPECT_NO_THROW(table.remove(42));
497 EXPECT_TRUE(table.is_empty());
498}
499
500TEST_F(ODhashTableEdgeCases, DuplicateInsert_ReturnsNull)
501{
502 EXPECT_NE(table.insert(42), nullptr);
503 int * dup = table.insert(42);
504 EXPECT_EQ(dup, nullptr);
505 EXPECT_EQ(table.size(), 1);
506}
507
509{
510 // Insert values that will likely collide
511 for (int i = 0; i < 100; ++i)
512 {
513 int * p = table.insert(i);
514 EXPECT_NE(p, nullptr) << "Failed to insert " << i;
515 }
516
517 EXPECT_EQ(table.size(), 100);
518
519 // Verify all can be found
520 for (int i = 0; i < 100; ++i)
521 EXPECT_NE(table.search(i), nullptr) << "Failed to find " << i;
522}
523
524TEST_F(ODhashTableEdgeCases, RemoveNonExistent)
525{
526 EXPECT_NE(table.insert(1), nullptr);
527 EXPECT_NE(table.insert(2), nullptr);
528
529 EXPECT_THROW(table.remove(99), std::domain_error);
530 EXPECT_EQ(table.size(), 2);
531}
532
533TEST_F(ODhashTableEdgeCases, ZeroAndNegativeKeys)
534{
535 EXPECT_NE(table.insert(0), nullptr);
536 EXPECT_NE(table.insert(-1), nullptr);
537 EXPECT_NE(table.insert(-100), nullptr);
538
539 EXPECT_NE(table.search(0), nullptr);
540 EXPECT_NE(table.search(-1), nullptr);
541 EXPECT_NE(table.search(-100), nullptr);
542}
543
544// ============================================================================
545// OLhashTable Edge Cases
546// ============================================================================
547
548class OLhashTableEdgeCases : public ::testing::Test
549{
550protected:
551 OLhashTable<int> table; // Uses default hash and comparison
552};
553
554TEST_F(OLhashTableEdgeCases, EmptyTable_SizeIsZero)
555{
556 EXPECT_EQ(table.size(), 0);
557 EXPECT_TRUE(table.is_empty());
558}
559
560TEST_F(OLhashTableEdgeCases, EmptyTable_SearchReturnsNull)
561{
562 EXPECT_EQ(table.search(42), nullptr);
563}
564
565TEST_F(OLhashTableEdgeCases, SingleElement_Operations)
566{
567 int * p = table.insert(42);
568 EXPECT_NE(p, nullptr);
569 EXPECT_EQ(table.size(), 1);
570
571 EXPECT_NE(table.search(42), nullptr);
572 table.remove(42); // OLhashTable::remove returns void
573 EXPECT_TRUE(table.is_empty());
574}
575
576TEST_F(OLhashTableEdgeCases, LinearProbingCollisions)
577{
578 // Insert many elements to test linear probing
579 for (int i = 0; i < 50; ++i)
580 EXPECT_NE(table.insert(i), nullptr);
581
582 EXPECT_EQ(table.size(), 50);
583
584 // Verify all can be found
585 for (int i = 0; i < 50; ++i)
586 EXPECT_NE(table.search(i), nullptr);
587}
588
589// ============================================================================
590// DynSetTree Edge Cases
591// ============================================================================
592
593class DynSetTreeEdgeCases : public ::testing::Test
594{
595protected:
597};
598
599TEST_F(DynSetTreeEdgeCases, EmptyTree_SizeIsZero)
600{
601 EXPECT_EQ(tree.size(), 0);
602 EXPECT_TRUE(tree.is_empty());
603}
604
605TEST_F(DynSetTreeEdgeCases, EmptyTree_SearchReturnsNull)
606{
607 EXPECT_EQ(tree.search(42), nullptr);
608 EXPECT_FALSE(tree.has(42));
609 EXPECT_FALSE(tree.contains(42));
610}
611
612TEST_F(DynSetTreeEdgeCases, EmptyTree_RemoveReturnsDefault)
613{
614 EXPECT_FALSE(tree.exist(42));
615}
616
617TEST_F(DynSetTreeEdgeCases, SingleElement_Insert)
618{
619 int * p = tree.insert(42);
620 EXPECT_NE(p, nullptr);
621 EXPECT_EQ(*p, 42);
622 EXPECT_EQ(tree.size(), 1);
623 EXPECT_TRUE(tree.has(42));
624}
625
626TEST_F(DynSetTreeEdgeCases, SingleElement_MinMax)
627{
628 tree.insert(42);
629 EXPECT_EQ(tree.min(), 42);
630 EXPECT_EQ(tree.max(), 42);
631}
632
633TEST_F(DynSetTreeEdgeCases, DuplicateInsert_ReturnsNull)
634{
635 tree.insert(42);
636 int * dup = tree.insert(42);
637 EXPECT_EQ(dup, nullptr);
638 EXPECT_EQ(tree.size(), 1);
639}
640
641TEST_F(DynSetTreeEdgeCases, SortedInsert_Ascending)
642{
643 for (int i = 1; i <= 100; ++i)
644 tree.insert(i);
645
646 EXPECT_EQ(tree.size(), 100);
647 EXPECT_EQ(tree.min(), 1);
648 EXPECT_EQ(tree.max(), 100);
649}
650
651TEST_F(DynSetTreeEdgeCases, SortedInsert_Descending)
652{
653 for (int i = 100; i >= 1; --i)
654 tree.insert(i);
655
656 EXPECT_EQ(tree.size(), 100);
657 EXPECT_EQ(tree.min(), 1);
658 EXPECT_EQ(tree.max(), 100);
659}
660
662{
663 for (int i = 1; i <= 10; ++i)
664 tree.insert(i);
665
666 tree.remove(1);
667 EXPECT_EQ(tree.min(), 2);
668 EXPECT_EQ(tree.size(), 9);
669}
670
672{
673 for (int i = 1; i <= 10; ++i)
674 tree.insert(i);
675
676 tree.remove(10);
677 EXPECT_EQ(tree.max(), 9);
678 EXPECT_EQ(tree.size(), 9);
679}
680
682{
683 for (int i = 1; i <= 10; ++i)
684 tree.insert(i);
685
686 tree.remove(5);
687 EXPECT_FALSE(tree.has(5));
688 EXPECT_EQ(tree.size(), 9);
689}
690
691TEST_F(DynSetTreeEdgeCases, InorderTraversal)
692{
693 vector<int> values = {5, 3, 7, 1, 4, 6, 8};
694 for (int v : values)
695 tree.insert(v);
696
697 vector<int> result;
698 tree.for_each([&](int x) { result.push_back(x); });
699
700 EXPECT_TRUE(std::is_sorted(result.begin(), result.end()));
701}
702
704{
705 tree.insert(std::numeric_limits<int>::max());
706 tree.insert(std::numeric_limits<int>::min());
707 tree.insert(0);
708
709 EXPECT_EQ(tree.min(), std::numeric_limits<int>::min());
710 EXPECT_EQ(tree.max(), std::numeric_limits<int>::max());
711}
712
713// ============================================================================
714// BitArray Edge Cases
715// ============================================================================
716
717TEST(BitArrayEdgeCases, ZeroSized_NotAllowed)
718{
719 // BitArray requires at least 1 bit
720 BitArray bits(1);
721 EXPECT_EQ(bits.size(), 1);
722}
723
724TEST(BitArrayEdgeCases, SingleBit_SetAndRead)
725{
726 BitArray bits(1);
727
728 EXPECT_EQ(bits.read_bit(0), 0);
729 bits.write_bit(0, 1);
730 EXPECT_EQ(bits.read_bit(0), 1);
731 bits.write_bit(0, 0);
732 EXPECT_EQ(bits.read_bit(0), 0);
733}
734
735TEST(BitArrayEdgeCases, AllBitsSet)
736{
737 BitArray bits(64);
738
739 for (size_t i = 0; i < 64; ++i)
740 bits.write_bit(i, 1);
741
742 for (size_t i = 0; i < 64; ++i)
743 EXPECT_EQ(bits.read_bit(i), 1);
744}
745
746TEST(BitArrayEdgeCases, AllBitsClear)
747{
748 BitArray bits(64);
749
750 // Should start all zeros
751 for (size_t i = 0; i < 64; ++i)
752 EXPECT_EQ(bits.read_bit(i), 0);
753}
754
755TEST(BitArrayEdgeCases, AlternatingBits)
756{
757 BitArray bits(100);
758
759 for (size_t i = 0; i < 100; ++i)
760 bits.write_bit(i, i % 2);
761
762 for (size_t i = 0; i < 100; ++i)
763 EXPECT_EQ(bits.read_bit(i), i % 2);
764}
765
766TEST(BitArrayEdgeCases, BoundaryAccess)
767{
768 BitArray bits(128);
769
770 // Test at byte boundaries
771 bits.write_bit(7, 1); // End of first byte
772 bits.write_bit(8, 1); // Start of second byte
773 bits.write_bit(63, 1); // End of first long
774 bits.write_bit(64, 1); // Start of second long
775 bits.write_bit(127, 1); // Last bit
776
777 EXPECT_EQ(bits.read_bit(7), 1);
778 EXPECT_EQ(bits.read_bit(8), 1);
779 EXPECT_EQ(bits.read_bit(63), 1);
780 EXPECT_EQ(bits.read_bit(64), 1);
781 EXPECT_EQ(bits.read_bit(127), 1);
782}
783
784// ============================================================================
785// Array (fixed) Edge Cases
786// ============================================================================
787
788TEST(ArrayEdgeCases, SingleElement)
789{
790 auto arr = Array<int>::create(1);
791 arr(0) = 42;
792 EXPECT_EQ(arr(0), 42);
793 EXPECT_EQ(arr.size(), 1);
794}
795
796TEST(ArrayEdgeCases, BoundaryAccess)
797{
798 // Ten live items; create_reserved(10) would only reserve capacity.
799 auto arr = Array<int>::create(10);
800 arr(0) = 1;
801 arr(9) = 10;
802
803 EXPECT_EQ(arr(0), 1);
804 EXPECT_EQ(arr(9), 10);
805}
806
807// Array uses noexcept, so out of bounds is undefined behavior, not exception
808
809// ============================================================================
810// Stress Test - Mixed Operations
811// ============================================================================
812
813TEST(ContainerStress, DynSetTreeRandomOperations)
814{
815 DynSetTree<int> tree;
816 std::mt19937 gen(12345);
817 std::uniform_int_distribution<int> val_dist(0, 10000);
818 std::uniform_int_distribution<int> op_dist(0, 2);
819
820 std::set<int> reference;
821
822 for (int i = 0; i < 10000; ++i)
823 {
824 int op = op_dist(gen);
825 int val = val_dist(gen);
826
827 if (op == 0 || op == 1) // Insert
828 {
829 auto ref_result = reference.insert(val);
830 int * tree_result = tree.insert(val);
831
832 if (ref_result.second)
833 EXPECT_NE(tree_result, nullptr);
834 else
835 EXPECT_EQ(tree_result, nullptr);
836 }
837 else if (!reference.empty()) // Remove
838 {
839 auto it = reference.begin();
840 std::advance(it, gen() % reference.size());
841 int to_remove = *it;
842
843 reference.erase(to_remove);
844 tree.remove(to_remove);
845
846 EXPECT_FALSE(tree.has(to_remove));
847 }
848 }
849
850 // Verify consistency
851 EXPECT_EQ(tree.size(), reference.size());
852 for (int val : reference)
853 EXPECT_TRUE(tree.has(val));
854}
855
856TEST(ContainerStress, HashTableRandomOperations)
857{
858 ODhashTable<int> table;
859 std::mt19937 gen(54321);
860 std::uniform_int_distribution<int> val_dist(0, 5000);
861 std::uniform_int_distribution<int> op_dist(0, 2);
862
863 std::set<int> reference;
864
865 for (int i = 0; i < 5000; ++i)
866 {
867 int op = op_dist(gen);
868 int val = val_dist(gen);
869
870 if (op == 0 || op == 1) // Insert
871 {
872 auto ref_result = reference.insert(val);
873 int * table_result = table.insert(val);
874
875 if (ref_result.second)
876 EXPECT_NE(table_result, nullptr);
877 else
878 EXPECT_EQ(table_result, nullptr);
879 }
880 else if (!reference.empty()) // Remove
881 {
882 auto it = reference.begin();
883 std::advance(it, gen() % reference.size());
884 int to_remove = *it;
885
886 reference.erase(to_remove);
887 table.remove(to_remove); // Will throw if not found, but we know it exists
888
889 EXPECT_EQ(table.search(to_remove), nullptr);
890 }
891 }
892
893 // Verify consistency
894 EXPECT_EQ(table.size(), reference.size());
895 for (int val : reference)
896 EXPECT_NE(table.search(val), nullptr);
897}
898
899TEST(ContainerStress, DynDlistRandomOperations)
900{
901 DynDlist<int> dlist;
902 std::deque<int> reference;
903 std::mt19937 gen(99999);
904 std::uniform_int_distribution<int> val_dist(0, 1000);
905 std::uniform_int_distribution<int> op_dist(0, 3);
906
907 for (int i = 0; i < 5000; ++i)
908 {
909 int op = op_dist(gen);
910 int val = val_dist(gen);
911
912 switch (op)
913 {
914 case 0: // Insert at front
915 dlist.insert(val);
916 reference.push_front(val);
917 break;
918
919 case 1: // Append at back
920 dlist.append(val);
921 reference.push_back(val);
922 break;
923
924 case 2: // Remove from front (if not empty)
925 if (!reference.empty())
926 {
927 int dlist_val = dlist.remove_first();
928 int ref_val = reference.front();
929 reference.pop_front();
930 EXPECT_EQ(dlist_val, ref_val);
931 }
932 break;
933
934 case 3: // Remove from back (if not empty)
935 if (!reference.empty())
936 {
937 int dlist_val = dlist.remove_last();
938 int ref_val = reference.back();
939 reference.pop_back();
940 EXPECT_EQ(dlist_val, ref_val);
941 }
942 break;
943 }
944 }
945
946 // Verify consistency
947 EXPECT_EQ(dlist.size(), reference.size());
948}
949
950TEST(ContainerStress, ArrayQueueCycles)
951{
952 ArrayQueue<int> queue = ArrayQueue<int>(100);
953 std::mt19937 gen(77777);
954 std::uniform_int_distribution<int> val_dist(0, 1000);
955
956 // Do multiple fill/empty cycles
957 for (int cycle = 0; cycle < 100; ++cycle)
958 {
959 // Fill partially
960 int fill_count = gen() % 50 + 1;
961 for (int i = 0; i < fill_count && queue.size() < 100; ++i)
962 queue.put(val_dist(gen));
963
964 // Empty partially
965 int empty_count = gen() % queue.size() + 1;
966 for (int i = 0; i < empty_count && !queue.is_empty(); ++i)
967 (void)queue.get();
968 }
969
970 // Should not crash and queue should be valid
971 EXPECT_LE(queue.size(), 100);
972}
973
974// ============================================================================
975// Main
976// ============================================================================
977
978int main(int argc, char **argv)
979{
980 ::testing::InitGoogleTest(&argc, argv);
981 return RUN_ALL_TESTS();
982}
int main()
Space-efficient bit array implementation.
Queue implemented with a single dynamic array.
T & put(const T &item)
Copy and put an item in the queue.
T get()
Remove the oldest item of the queue and return a copy.
Stack implemented with simple dynamic array and with bounds verification.
static Array create(const size_t n)
Create an array with n logical elements.
Definition tpl_array.H:245
Contiguous array of bits.
Definition bitArray.H:201
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T remove_first()
Remove the first item of the list; return a copy of removed item.
T & append(const T &item)
Append a copied item at the end of the list.
T remove_last()
Remove the last item of the list; return a copy of removed item.
T & insert(const T &item)
Insert a copy of item at the beginning of the list.
Doubly-linked list (defined in tpl_dynList.H).
Definition htlist.H:1231
Dynamic set backed by balanced binary search trees with automatic memory management.
const size_t & size() const
Returns the cardinality of the set.
Key * insert(const Key &key)
Inserts a key into the dynamic set.
bool has(const Key &key) const
size_t remove(const Key &key)
Removes a key from the dynamic set.
size_t size() const noexcept
Return the number of elements.
bool is_empty() const noexcept
Return true is the array is empty.
Open addressing hash table with double hashing collision resolution.
Definition tpl_odhash.H:193
void remove(const Key &key)
Remove a key from the hash table.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tpl_odhash.H:657
Open addressing hash table with linear probing collision resolution.
Definition tpl_olhash.H:175
constexpr size_t size() const noexcept
Returns the number of entries in the table.
Definition hashDry.H:954
Key * insert(const Key &key)
Inserts a key into the hash table (copy version).
Definition hashDry.H:507
TEST_F(DynArrayEdgeCases, EmptyArray_SizeIsZero)
#define TEST(name)
Singly linked list implementations with head-tail access.
Main namespace for Aleph-w library functions.
constexpr Bit_Column bits(const unsigned n)
Column of n bits without sign: the integers of [0, 2^n - 1].
STL namespace.
Circular queue implementations backed by arrays.
Stack implementations backed by dynamic or fixed arrays.
Dynamic array container with automatic resizing.
Lazy and scalable dynamic array implementation.
Dynamic doubly linked list implementation.
Dynamic set implementations based on balanced binary search trees.
Open addressing hash table with double hashing.
Open addressing hash table with linear probing.