Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynliststack_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
48# include <gtest/gtest.h>
49
50# include <string>
51# include <memory>
52# include <vector>
53# include <stdexcept>
54# include <sstream>
55
56# include <tpl_dynListStack.H>
57# include <ahFunctional.H>
58
59using namespace std;
60using namespace testing;
61using namespace Aleph;
62
63// =============================================================================
64// Test Fixtures
65// =============================================================================
66
67constexpr size_t N = 17;
68constexpr size_t LARGE_N = 10000;
69
71struct SimpleStack : public Test
72{
73 size_t n = 0;
75
77 {
78 for (size_t i = 0; i < N; ++i, ++n)
79 s.push(i);
80 }
81};
82
84struct ComplexStack : public Test
85{
86 size_t n = 0;
88
90 {
91 for (size_t i = 0; i < N; ++i, ++n)
92 s.push({ int(i), 0, 1, 2, int(i) });
93 }
94};
95
97struct StringStack : public Test
98{
100
102 {
103 s.push("first");
104 s.push("second");
105 s.push("third");
106 }
107};
108
111{
112 int value;
113 bool moved_from = false;
114
115 explicit MoveOnly(int v) : value(v) {}
116 MoveOnly(const MoveOnly&) = delete;
117 MoveOnly& operator=(const MoveOnly&) = delete;
118 MoveOnly(MoveOnly&& other) noexcept : value(other.value)
119 {
120 other.moved_from = true;
121 }
122 MoveOnly& operator=(MoveOnly&& other) noexcept
123 {
124 value = other.value;
125 other.moved_from = true;
126 return *this;
127 }
128
129 bool operator==(const MoveOnly& other) const { return value == other.value; }
130};
131
134{
135 static int constructions;
136 static int destructions;
137 static int copies;
138 static int moves;
139
140 int value;
141
142 static void reset()
143 {
144 constructions = destructions = copies = moves = 0;
145 }
146
147 explicit Counted(int v = 0) : value(v) { ++constructions; }
148 Counted(const Counted& other) : value(other.value)
149 {
150 ++constructions;
151 ++copies;
152 }
153 Counted(Counted&& other) noexcept : value(other.value)
154 {
155 ++constructions;
156 ++moves;
157 }
158 ~Counted() { ++destructions; }
159
160 Counted& operator=(const Counted& other)
161 {
162 value = other.value;
163 ++copies;
164 return *this;
165 }
166 Counted& operator=(Counted&& other) noexcept
167 {
168 value = other.value;
169 ++moves;
170 return *this;
171 }
172
173 bool operator==(const Counted& other) const { return value == other.value; }
174};
175
178int Counted::copies = 0;
179int Counted::moves = 0;
180
181// =============================================================================
182// Basic Construction Tests
183// =============================================================================
184
185TEST(DynListStack, default_constructor_creates_empty_stack)
186{
188 EXPECT_TRUE(s.is_empty());
189 EXPECT_EQ(s.size(), 0);
190}
191
192TEST(DynListStack, default_constructor_with_various_types)
193{
198
199 EXPECT_TRUE(si.is_empty());
200 EXPECT_TRUE(sd.is_empty());
201 EXPECT_TRUE(ss.is_empty());
202 EXPECT_TRUE(sv.is_empty());
203}
204
205TEST(DynListStack, initializer_list_constructor)
206{
207 DynListStack<int> s = {1, 2, 3, 4, 5};
208
209 EXPECT_FALSE(s.is_empty());
210 EXPECT_EQ(s.size(), 5);
211
212 // Elements should be in LIFO order - last pushed is first out
213 // With initializer list {1,2,3,4,5}, append is called in order
214 // So 1 is at bottom, 5 is at top
215 EXPECT_EQ(s.pop(), 5);
216 EXPECT_EQ(s.pop(), 4);
217 EXPECT_EQ(s.pop(), 3);
218 EXPECT_EQ(s.pop(), 2);
219 EXPECT_EQ(s.pop(), 1);
220 EXPECT_TRUE(s.is_empty());
221}
222
223TEST(DynListStack, initializer_list_empty)
224{
225 DynListStack<int> s = {};
226 EXPECT_TRUE(s.is_empty());
227 EXPECT_EQ(s.size(), 0);
228}
229
230TEST(DynListStack, initializer_list_with_strings)
231{
232 DynListStack<string> s = {"hello", "world", "test"};
233
234 EXPECT_EQ(s.size(), 3);
235 EXPECT_EQ(s.top(), "test");
236}
237
238// =============================================================================
239// Copy Constructor Tests
240// =============================================================================
241
242TEST_F(SimpleStack, copy_constructor_creates_independent_copy)
243{
245
246 EXPECT_EQ(copy.size(), s.size());
247 EXPECT_EQ(copy.top(), s.top());
248
249 // Modify original, copy should be unchanged
250 (void) s.pop();
251 EXPECT_NE(copy.size(), s.size());
252 EXPECT_EQ(copy.size(), n);
253}
254
255TEST_F(SimpleStack, copy_constructor_preserves_order)
256{
258
259 for (size_t i = 0; i < n; ++i)
260 {
261 ASSERT_EQ(copy.top(), s.top());
262 (void) copy.pop();
263 (void) s.pop();
264 }
265}
266
267TEST(DynListStack, copy_constructor_empty_stack)
268{
269 DynListStack<int> empty;
270 DynListStack<int> copy(empty);
271
272 EXPECT_TRUE(copy.is_empty());
273 EXPECT_EQ(copy.size(), 0);
274}
275
276TEST_F(ComplexStack, copy_constructor_deep_copies_elements)
277{
279
280 // Modify element in original
281 s.top().append(999);
282
283 // Copy should be unchanged
284 EXPECT_FALSE(copy.top().exists([](int x) { return x == 999; }));
285}
286
287// =============================================================================
288// Move Constructor Tests
289// =============================================================================
290
291TEST_F(SimpleStack, move_constructor_transfers_ownership)
292{
293 size_t original_size = s.size();
294 int original_top = s.top();
295
296 DynListStack<int> moved(std::move(s));
297
298 EXPECT_EQ(moved.size(), original_size);
299 EXPECT_EQ(moved.top(), original_top);
300 EXPECT_TRUE(s.is_empty());
301}
302
303TEST(DynListStack, move_constructor_empty_stack)
304{
305 DynListStack<int> empty;
306 DynListStack<int> moved(std::move(empty));
307
308 EXPECT_TRUE(moved.is_empty());
309 EXPECT_TRUE(empty.is_empty());
310}
311
312TEST(DynListStack, move_constructor_with_move_only_type)
313{
315 s.push(make_unique<int>(42));
316 s.push(make_unique<int>(43));
317
318 DynListStack<unique_ptr<int>> moved(std::move(s));
319
320 EXPECT_EQ(moved.size(), 2);
321 EXPECT_EQ(*moved.top(), 43);
322 EXPECT_TRUE(s.is_empty());
323}
324
325// =============================================================================
326// Copy Assignment Tests
327// =============================================================================
328
329TEST_F(SimpleStack, copy_assignment_replaces_contents)
330{
331 DynListStack<int> other;
332 other.push(100);
333 other.push(200);
334
335 other = s;
336
337 EXPECT_EQ(other.size(), n);
338 EXPECT_EQ(other.top(), s.top());
339}
340
341TEST_F(SimpleStack, copy_assignment_self_assignment_is_safe)
342{
343 s = s;
344
345 EXPECT_EQ(s.size(), n);
346 EXPECT_EQ(s.top(), int(n - 1));
347}
348
349TEST(DynListStack, copy_assignment_to_empty_stack)
350{
351 DynListStack<int> source = {1, 2, 3};
353
354 dest = source;
355
356 EXPECT_EQ(dest.size(), 3);
357 EXPECT_EQ(dest.top(), 3);
358}
359
360TEST(DynListStack, copy_assignment_from_empty_stack)
361{
362 DynListStack<int> source;
363 DynListStack<int> dest = {1, 2, 3};
364
365 dest = source;
366
367 EXPECT_TRUE(dest.is_empty());
368}
369
370// =============================================================================
371// Move Assignment Tests
372// =============================================================================
373
374TEST_F(SimpleStack, move_assignment_transfers_ownership)
375{
376 size_t original_size = s.size();
378 dest.push(999);
379
380 dest = std::move(s);
381
382 EXPECT_EQ(dest.size(), original_size);
383 // After move, s is in a valid but unspecified state (may or may not be empty)
384 // The C++ standard does not require moved-from objects to be empty
385 EXPECT_NO_THROW(s.push(123)); // verify moved-from can be used
386 EXPECT_FALSE(s.is_empty());
387}
388
389TEST_F(SimpleStack, move_assignment_roundtrip_is_safe)
390{
391 size_t original_size = s.size();
393 tmp = std::move(s);
394 s = std::move(tmp);
395
396 EXPECT_EQ(s.size(), original_size);
397}
398
399// =============================================================================
400// Swap Tests
401// =============================================================================
402
403TEST_F(SimpleStack, swap_exchanges_contents)
404{
405 DynListStack<int> other = {100, 200, 300};
406
407 size_t s_size = s.size();
408 size_t other_size = other.size();
409 int s_top = s.top();
410 int other_top = other.top();
411
412 s.swap(other);
413
414 EXPECT_EQ(s.size(), other_size);
415 EXPECT_EQ(other.size(), s_size);
416 EXPECT_EQ(s.top(), other_top);
417 EXPECT_EQ(other.top(), s_top);
418}
419
420TEST(DynListStack, swap_with_empty_stack)
421{
422 DynListStack<int> s = {1, 2, 3};
423 DynListStack<int> empty;
424
425 s.swap(empty);
426
427 EXPECT_TRUE(s.is_empty());
428 EXPECT_EQ(empty.size(), 3);
429}
430
431TEST(DynListStack, swap_is_noexcept)
432{
433 DynListStack<int> s1, s2;
434 EXPECT_TRUE(noexcept(s1.swap(s2)));
435}
436
437// =============================================================================
438// Push Operation Tests
439// =============================================================================
440
441TEST(DynListStack, push_by_copy)
442{
444 string value = "test";
445
446 s.push(value);
447
448 EXPECT_EQ(s.size(), 1);
449 EXPECT_EQ(s.top(), "test");
450 EXPECT_EQ(value, "test"); // Original unchanged
451}
452
453TEST(DynListStack, push_by_move)
454{
456 string value = "test";
457
458 s.push(std::move(value));
459
460 EXPECT_EQ(s.size(), 1);
461 EXPECT_EQ(s.top(), "test");
462 EXPECT_TRUE(value.empty()); // Moved-from string is empty
463}
464
465TEST(DynListStack, push_returns_reference_to_inserted_element)
466{
468
469 int& ref = s.push(42);
470
471 EXPECT_EQ(ref, 42);
472 EXPECT_EQ(&ref, &s.top());
473
474 ref = 100;
475 EXPECT_EQ(s.top(), 100);
476}
477
478TEST(DynListStack, push_multiple_elements_maintains_lifo_order)
479{
481
482 for (int i = 0; i < 10; ++i)
483 s.push(i);
484
485 for (int i = 9; i >= 0; --i)
486 ASSERT_EQ(s.pop(), i);
487}
488
489// =============================================================================
490// Emplace Tests
491// =============================================================================
492
493TEST(DynListStack, emplace_constructs_in_place)
494{
496
497 s.emplace(42, "hello");
498
499 EXPECT_EQ(s.size(), 1);
500 EXPECT_EQ(s.top().first, 42);
501 EXPECT_EQ(s.top().second, "hello");
502}
503
504TEST(DynListStack, emplace_with_multiple_arguments)
505{
506 struct ThreeArgs
507 {
508 int a;
509 double b;
510 string c;
511 ThreeArgs(int a_, double b_, string c_) : a(a_), b(b_), c(c_) {}
512 };
513
515 s.emplace(1, 2.5, "test");
516
517 EXPECT_EQ(s.top().a, 1);
518 EXPECT_DOUBLE_EQ(s.top().b, 2.5);
519 EXPECT_EQ(s.top().c, "test");
520}
521
522TEST(DynListStack, emplace_returns_reference)
523{
525
526 int& ref = s.emplace(42);
527
528 EXPECT_EQ(ref, 42);
529 ref = 100;
530 EXPECT_EQ(s.top(), 100);
531}
532
533// =============================================================================
534// Pop Operation Tests
535// =============================================================================
536
537TEST_F(SimpleStack, pop_removes_and_returns_top)
538{
539 int top_value = s.top();
540
541 int popped = s.pop();
542
543 EXPECT_EQ(popped, top_value);
544 EXPECT_EQ(s.size(), n - 1);
545}
546
547TEST(DynListStack, pop_on_empty_stack_throws)
548{
550
551 EXPECT_THROW(s.pop(), underflow_error);
552}
553
554TEST(DynListStack, pop_until_empty)
555{
556 DynListStack<int> s = {1, 2, 3};
557
558 EXPECT_EQ(s.pop(), 3);
559 EXPECT_EQ(s.pop(), 2);
560 EXPECT_EQ(s.pop(), 1);
561 EXPECT_TRUE(s.is_empty());
562 EXPECT_THROW(s.pop(), underflow_error);
563}
564
565TEST(DynListStack, pop_with_move_only_type)
566{
568 s.push(make_unique<int>(42));
569
570 unique_ptr<int> ptr = s.pop();
571
572 EXPECT_EQ(*ptr, 42);
573 EXPECT_TRUE(s.is_empty());
574}
575
576// =============================================================================
577// Top/Peek Tests
578// =============================================================================
579
580TEST_F(SimpleStack, top_returns_reference_to_top_element)
581{
582 EXPECT_EQ(s.top(), int(n - 1));
583
584 s.top() = 999;
585 EXPECT_EQ(s.top(), 999);
586}
587
588TEST_F(SimpleStack, top_const_returns_const_reference)
589{
590 const DynListStack<int>& cs = s;
591
592 EXPECT_EQ(cs.top(), int(n - 1));
593}
594
595TEST(DynListStack, top_on_empty_stack_throws)
596{
598
599 EXPECT_THROW(s.top(), underflow_error);
600}
601
602TEST(DynListStack, top_const_on_empty_stack_throws)
603{
604 const DynListStack<int> s;
605
606 EXPECT_THROW(s.top(), underflow_error);
607}
608
609TEST_F(SimpleStack, peek_is_alias_for_top)
610{
611 EXPECT_EQ(s.peek(), s.top());
612 EXPECT_EQ(&s.peek(), &s.top());
613}
614
615TEST_F(SimpleStack, peek_const_is_alias_for_top_const)
616{
617 const DynListStack<int>& cs = s;
618 EXPECT_EQ(cs.peek(), cs.top());
619}
620
621// =============================================================================
622// Get (alias for pop) Tests
623// =============================================================================
624
625TEST_F(SimpleStack, get_is_alias_for_pop)
626{
627 int pop_result = s.pop();
628 int get_result = s.get();
629
630 EXPECT_EQ(s.size(), n - 2);
631 EXPECT_NE(pop_result, get_result);
632}
633
634TEST(DynListStack, get_on_empty_stack_throws)
635{
637 EXPECT_THROW(s.get(), underflow_error);
638}
639
640// =============================================================================
641// Size and Empty Tests
642// =============================================================================
643
644TEST(DynListStack, size_is_zero_for_empty_stack)
645{
647 EXPECT_EQ(s.size(), 0);
648}
649
650TEST_F(SimpleStack, size_reflects_number_of_elements)
651{
652 EXPECT_EQ(s.size(), n);
653
654 s.push(999);
655 EXPECT_EQ(s.size(), n + 1);
656
657 (void) s.pop();
658 (void) s.pop();
659 EXPECT_EQ(s.size(), n - 1);
660}
661
662TEST(DynListStack, is_empty_true_for_new_stack)
663{
665 EXPECT_TRUE(s.is_empty());
666}
667
668TEST_F(SimpleStack, is_empty_false_for_non_empty_stack)
669{
670 EXPECT_FALSE(s.is_empty());
671}
672
673TEST(DynListStack, is_empty_after_all_elements_removed)
674{
675 DynListStack<int> s = {1, 2, 3};
676
677 (void) s.pop();
678 (void) s.pop();
679 (void) s.pop();
680
681 EXPECT_TRUE(s.is_empty());
682}
683
684// =============================================================================
685// Empty/Clear Tests
686// =============================================================================
687
688TEST_F(SimpleStack, empty_removes_all_elements)
689{
690 EXPECT_FALSE(s.is_empty());
691
692 s.empty();
693
694 EXPECT_TRUE(s.is_empty());
695 EXPECT_EQ(s.size(), 0);
696}
697
698TEST(DynListStack, empty_on_empty_stack_is_safe)
699{
701
702 s.empty(); // Should not throw
703
704 EXPECT_TRUE(s.is_empty());
705}
706
707TEST_F(SimpleStack, clear_is_alias_for_empty)
708{
709 s.clear();
710
711 EXPECT_TRUE(s.is_empty());
712 EXPECT_EQ(s.size(), 0);
713}
714
715TEST(DynListStack, empty_is_noexcept)
716{
718 EXPECT_TRUE(noexcept(s.empty()));
719}
720
721TEST(DynListStack, clear_is_noexcept)
722{
724 EXPECT_TRUE(noexcept(s.clear()));
725}
726
727// =============================================================================
728// Alias Methods Tests (put, insert, append)
729// =============================================================================
730
731TEST(DynListStack, put_is_alias_for_push)
732{
734
735 s.put(1);
736 s.push(2);
737 s.put(3);
738
739 EXPECT_EQ(s.size(), 3);
740 EXPECT_EQ(s.pop(), 3);
741 EXPECT_EQ(s.pop(), 2);
742 EXPECT_EQ(s.pop(), 1);
743}
744
745TEST(DynListStack, insert_is_alias_for_push)
746{
748
749 s.insert(1);
750 s.push(2);
751 s.insert(3);
752
753 EXPECT_EQ(s.size(), 3);
754 EXPECT_EQ(s.pop(), 3);
755}
756
757TEST(DynListStack, append_is_alias_for_push)
758{
760
761 s.append(1);
762 s.push(2);
763 s.append(3);
764
765 EXPECT_EQ(s.size(), 3);
766 EXPECT_EQ(s.pop(), 3);
767}
768
769// =============================================================================
770// Search Tests
771// =============================================================================
772
773TEST_F(SimpleStack, search_finds_existing_element)
774{
775 int* ptr = s.search(5);
776
777 ASSERT_NE(ptr, nullptr);
778 EXPECT_EQ(*ptr, 5);
779}
780
781TEST_F(SimpleStack, search_returns_nullptr_for_missing_element)
782{
783 int* ptr = s.search(999);
784
785 EXPECT_EQ(ptr, nullptr);
786}
787
788TEST(DynListStack, search_on_empty_stack_returns_nullptr)
789{
791
792 EXPECT_EQ(s.search(1), nullptr);
793}
794
795TEST_F(SimpleStack, search_const_version)
796{
797 const DynListStack<int>& cs = s;
798
799 const int* ptr = cs.search(5);
800
801 ASSERT_NE(ptr, nullptr);
802 EXPECT_EQ(*ptr, 5);
803}
804
805TEST_F(StringStack, search_with_string_type)
806{
807 string* ptr = s.search("second");
808
809 ASSERT_NE(ptr, nullptr);
810 EXPECT_EQ(*ptr, "second");
811
812 EXPECT_EQ(s.search("missing"), nullptr);
813}
814
815// =============================================================================
816// Contains/Has Tests
817// =============================================================================
818
819TEST_F(SimpleStack, contains_returns_true_for_existing_element)
820{
821 EXPECT_TRUE(s.contains(5));
822 EXPECT_TRUE(s.contains(0));
823 EXPECT_TRUE(s.contains(int(n - 1)));
824}
825
826TEST_F(SimpleStack, contains_returns_false_for_missing_element)
827{
828 EXPECT_FALSE(s.contains(-1));
829 EXPECT_FALSE(s.contains(int(n)));
830 EXPECT_FALSE(s.contains(999));
831}
832
833TEST(DynListStack, contains_on_empty_stack)
834{
836 EXPECT_FALSE(s.contains(1));
837}
838
839TEST_F(SimpleStack, has_is_alias_for_contains)
840{
841 EXPECT_EQ(s.has(5), s.contains(5));
842 EXPECT_EQ(s.has(999), s.contains(999));
843}
844
845// =============================================================================
846// Iterator Tests
847// =============================================================================
848
849TEST(DynListStack, iterator_on_empty_stack)
850{
852 auto it = s.get_it();
853
854 EXPECT_FALSE(it.has_curr());
855 EXPECT_THROW(it.get_curr(), overflow_error);
856 EXPECT_THROW(it.next(), overflow_error);
857}
858
859TEST_F(SimpleStack, iterator_traverses_in_lifo_order)
860{
861 auto it = s.get_it();
862
863 for (size_t i = 0; it.has_curr(); it.next(), ++i)
864 ASSERT_EQ(it.get_curr(), int(n - i - 1));
865}
866
867TEST_F(SimpleStack, iterator_count_matches_size)
868{
869 size_t count = 0;
870 for (auto it = s.get_it(); it.has_curr(); it.next())
871 ++count;
872
873 EXPECT_EQ(count, n);
874}
875
876TEST_F(SimpleStack, range_based_for_loop)
877{
878 vector<int> elements;
879
880 for (const auto& item : s)
881 elements.push_back(item);
882
883 ASSERT_EQ(elements.size(), n);
884 for (size_t i = 0; i < n; ++i)
885 EXPECT_EQ(elements[i], int(n - i - 1));
886}
887
888TEST_F(ComplexStack, iterator_with_complex_type)
889{
890 auto it = s.get_it();
891
892 for (size_t i = 0; it.has_curr(); it.next(), ++i)
893 {
894 const auto& list = it.get_curr();
895 ASSERT_EQ(list.get_first(), int(n - i - 1));
896 ASSERT_EQ(list.get_last(), int(n - i - 1));
897 }
898}
899
900// =============================================================================
901// Traverse Tests
902// =============================================================================
903
904TEST_F(SimpleStack, traverse_visits_all_elements)
905{
906 size_t count = 0;
907 bool result = s.traverse([&count](int) { ++count; return true; });
908
909 EXPECT_TRUE(result);
910 EXPECT_EQ(count, n);
911}
912
913TEST_F(SimpleStack, traverse_can_stop_early)
914{
915 size_t count = 0;
916 bool result = s.traverse([&count](int)
917 {
918 ++count;
919 return count < 5;
920 });
921
922 EXPECT_FALSE(result);
923 EXPECT_EQ(count, 5);
924}
925
926TEST_F(SimpleStack, traverse_visits_in_lifo_order)
927{
928 size_t i = 0;
929 s.traverse([&i, this](int value)
930 {
931 EXPECT_EQ(value, int(n - i - 1));
932 ++i;
933 return true;
934 });
935}
936
937TEST(DynListStack, traverse_on_empty_stack)
938{
940 bool called = false;
941
942 bool result = s.traverse([&called](int) { called = true; return true; });
943
944 EXPECT_TRUE(result);
945 EXPECT_FALSE(called);
946}
947
948TEST_F(SimpleStack, traverse_const_version)
949{
950 const DynListStack<int>& cs = s;
951 size_t count = 0;
952
953 cs.traverse([&count](const int&) { ++count; return true; });
954
955 EXPECT_EQ(count, n);
956}
957
958// =============================================================================
959// Functional Methods Tests (inherited from FunctionalMethods mixin)
960// =============================================================================
961
962TEST_F(SimpleStack, maps_transforms_elements)
963{
964 auto doubled = s.maps([](int x) { return x * 2; });
965
966 EXPECT_EQ(doubled.size(), n);
967
968 auto it = doubled.get_it();
969 for (size_t i = 0; it.has_curr(); it.next(), ++i)
970 EXPECT_EQ(it.get_curr(), int((n - i - 1) * 2));
971}
972
973TEST_F(SimpleStack, maps_to_different_type)
974{
975 auto strings = s.template maps<string>([](int x)
976 {
977 return to_string(x);
978 });
979
980 EXPECT_EQ(strings.size(), n);
981 EXPECT_EQ(strings.get_first(), to_string(int(n - 1)));
982}
983
984TEST_F(SimpleStack, filter_selects_matching_elements)
985{
986 auto evens = s.filter([](int x) { return x % 2 == 0; });
987
988 for (auto it = evens.get_it(); it.has_curr(); it.next())
989 EXPECT_EQ(it.get_curr() % 2, 0);
990}
991
992TEST_F(SimpleStack, filter_returns_empty_when_none_match)
993{
994 auto result = s.filter([](int x) { return x > 1000; });
995
996 EXPECT_TRUE(result.is_empty());
997}
998
999TEST_F(SimpleStack, foldl_accumulates_values)
1000{
1001 int sum = s.foldl(0, [](int acc, int x) { return acc + x; });
1002
1003 int expected = (n - 1) * n / 2; // Sum of 0 to n-1
1004 EXPECT_EQ(sum, expected);
1005}
1006
1007TEST_F(SimpleStack, foldl_with_different_accumulator_type)
1008{
1009 string result = s.template foldl<string>("", [](const string& acc, int x)
1010 {
1011 return acc + to_string(x) + ",";
1012 });
1013
1014 EXPECT_FALSE(result.empty());
1015}
1016
1017TEST_F(SimpleStack, all_returns_true_when_all_match)
1018{
1019 bool result = s.all([](int x) { return x >= 0; });
1020 EXPECT_TRUE(result);
1021}
1022
1023TEST_F(SimpleStack, all_returns_false_when_any_fails)
1024{
1025 bool result = s.all([](int x) { return x < 10; });
1026 EXPECT_FALSE(result); // n > 10, so some elements are >= 10
1027}
1028
1029TEST_F(SimpleStack, exists_returns_true_when_any_matches)
1030{
1031 bool result = s.exists([](int x) { return x == 5; });
1032 EXPECT_TRUE(result);
1033}
1034
1035TEST_F(SimpleStack, exists_returns_false_when_none_match)
1036{
1037 bool result = s.exists([](int x) { return x == 999; });
1038 EXPECT_FALSE(result);
1039}
1040
1041TEST_F(SimpleStack, for_each_visits_all_elements)
1042{
1043 int sum = 0;
1044 s.for_each([&sum](int x) { sum += x; });
1045
1046 int expected = (n - 1) * n / 2;
1047 EXPECT_EQ(sum, expected);
1048}
1049
1050// =============================================================================
1051// Type Aliases Tests
1052// =============================================================================
1053
1054TEST(DynListStack, type_aliases_are_correct)
1055{
1056 using Stack = DynListStack<int>;
1057
1058 static_assert(is_same_v<Stack::Item_Type, int>);
1059 static_assert(is_same_v<Stack::Key_Type, int>);
1060 static_assert(is_same_v<Stack::Set_Type, Stack>);
1061}
1062
1063// =============================================================================
1064// Memory Management Tests
1065// =============================================================================
1066
1067TEST(DynListStack, destructor_frees_all_memory)
1068{
1069 Counted::reset();
1070
1071 {
1073 for (int i = 0; i < 100; ++i)
1074 s.emplace(i);
1075 }
1076
1077 // All elements should be destroyed
1078 EXPECT_EQ(Counted::constructions, Counted::destructions);
1079}
1080
1081TEST(DynListStack, clear_destroys_all_elements)
1082{
1083 Counted::reset();
1084
1086 for (int i = 0; i < 50; ++i)
1087 s.emplace(i);
1088
1089 int constructions_before = Counted::constructions;
1090 s.clear();
1091
1092 EXPECT_EQ(Counted::destructions, constructions_before);
1093}
1094
1095// =============================================================================
1096// Move Semantics Tests
1097// =============================================================================
1098
1099TEST(DynListStack, push_move_only_type)
1100{
1102
1103 s.push(make_unique<int>(42));
1104 s.push(make_unique<int>(43));
1105
1106 EXPECT_EQ(s.size(), 2);
1107 EXPECT_EQ(*s.top(), 43);
1108
1109 auto ptr = s.pop();
1110 EXPECT_EQ(*ptr, 43);
1111}
1112
1113TEST(DynListStack, move_semantics_avoid_copies)
1114{
1115 Counted::reset();
1116
1118 Counted c(42);
1119
1120 s.push(std::move(c));
1121
1122 // Should use move, not copy
1123 EXPECT_GE(Counted::moves, 1);
1124}
1125
1126// =============================================================================
1127// Edge Cases and Boundary Tests
1128// =============================================================================
1129
1130TEST(DynListStack, single_element_stack)
1131{
1133 s.push(42);
1134
1135 EXPECT_EQ(s.size(), 1);
1136 EXPECT_EQ(s.top(), 42);
1137 EXPECT_FALSE(s.is_empty());
1138
1139 EXPECT_EQ(s.pop(), 42);
1140 EXPECT_TRUE(s.is_empty());
1141}
1142
1143TEST(DynListStack, repeated_push_pop_cycles)
1144{
1146
1147 for (int cycle = 0; cycle < 10; ++cycle)
1148 {
1149 for (int i = 0; i < 100; ++i)
1150 s.push(i);
1151
1152 EXPECT_EQ(s.size(), 100);
1153
1154 for (int i = 99; i >= 0; --i)
1155 ASSERT_EQ(s.pop(), i);
1156
1157 EXPECT_TRUE(s.is_empty());
1158 }
1159}
1160
1161TEST(DynListStack, interleaved_push_pop)
1162{
1164
1165 s.push(1);
1166 s.push(2);
1167 EXPECT_EQ(s.pop(), 2);
1168 s.push(3);
1169 s.push(4);
1170 EXPECT_EQ(s.pop(), 4);
1171 EXPECT_EQ(s.pop(), 3);
1172 s.push(5);
1173 EXPECT_EQ(s.pop(), 5);
1174 EXPECT_EQ(s.pop(), 1);
1175 EXPECT_TRUE(s.is_empty());
1176}
1177
1178// =============================================================================
1179// Stress Tests
1180// =============================================================================
1181
1182TEST(DynListStack, stress_large_number_of_elements)
1183{
1185
1186 for (size_t i = 0; i < LARGE_N; ++i)
1187 s.push(i);
1188
1189 EXPECT_EQ(s.size(), LARGE_N);
1190
1191 for (size_t i = 0; i < LARGE_N; ++i)
1192 ASSERT_EQ(s.pop(), int(LARGE_N - i - 1));
1193
1194 EXPECT_TRUE(s.is_empty());
1195}
1196
1197TEST(DynListStack, stress_with_strings)
1198{
1200
1201 for (size_t i = 0; i < 1000; ++i)
1202 s.push("string_" + to_string(i));
1203
1204 EXPECT_EQ(s.size(), 1000);
1205
1206 for (size_t i = 999; i != size_t(-1); --i)
1207 ASSERT_EQ(s.pop(), "string_" + to_string(i));
1208}
1209
1210TEST(DynListStack, stress_copy_large_stack)
1211{
1212 DynListStack<int> original;
1213
1214 for (size_t i = 0; i < LARGE_N; ++i)
1215 original.push(i);
1216
1217 DynListStack<int> copy(original);
1218
1219 EXPECT_EQ(copy.size(), LARGE_N);
1220 EXPECT_EQ(original.size(), LARGE_N);
1221
1222 // Verify contents match
1223 auto it1 = original.get_it();
1224 auto it2 = copy.get_it();
1225 while (it1.has_curr() && it2.has_curr())
1226 {
1227 ASSERT_EQ(it1.get_curr(), it2.get_curr());
1228 it1.next();
1229 it2.next();
1230 }
1231}
1232
1233// =============================================================================
1234// Exception Safety Tests
1235// =============================================================================
1236
1238{
1240 static bool should_throw;
1241
1242 ThrowOnCopy(int v) : value(v) {}
1243 ThrowOnCopy(const ThrowOnCopy& other) : value(other.value)
1244 {
1245 if (should_throw)
1246 throw runtime_error("Copy failed");
1247 }
1251};
1252
1253bool ThrowOnCopy::should_throw = false;
1254
1255TEST(DynListStack, exception_safety_on_push_copy)
1256{
1258 s.push(ThrowOnCopy(1));
1259 s.push(ThrowOnCopy(2));
1260
1261 ThrowOnCopy::should_throw = true;
1262 ThrowOnCopy item(3);
1263
1264 EXPECT_THROW(s.push(item), runtime_error);
1265
1266 ThrowOnCopy::should_throw = false;
1267
1268 // Stack should still be usable
1269 EXPECT_EQ(s.size(), 2);
1270 EXPECT_EQ(s.top().value, 2);
1271}
1272
1273// =============================================================================
1274// Comparison with STL stack Tests
1275// =============================================================================
1276
1277TEST(DynListStack, compatible_with_algorithm_patterns)
1278{
1279 DynListStack<int> s = {5, 3, 8, 1, 9, 2};
1280
1281 // Find max using foldl
1282 int max_val = s.foldl(numeric_limits<int>::min(),
1283 [](int acc, int x) { return std::max(acc, x); });
1284
1285 EXPECT_EQ(max_val, 9);
1286
1287 // Find min using foldl
1288 int min_val = s.foldl(numeric_limits<int>::max(),
1289 [](int acc, int x) { return std::min(acc, x); });
1290
1291 EXPECT_EQ(min_val, 1);
1292}
1293
1294// =============================================================================
1295// Regression Tests
1296// =============================================================================
1297
1298TEST(DynListStack, regression_size_after_move)
1299{
1300 DynListStack<int> s1 = {1, 2, 3};
1301 DynListStack<int> s2 = std::move(s1);
1302
1303 EXPECT_EQ(s2.size(), 3);
1304 EXPECT_EQ(s1.size(), 0);
1305 EXPECT_TRUE(s1.is_empty());
1306}
1307
1308TEST(DynListStack, regression_iterator_after_modification)
1309{
1310 DynListStack<int> s = {1, 2, 3, 4, 5};
1311
1312 // Get count before modification
1313 size_t count_before = 0;
1314 for (auto it = s.get_it(); it.has_curr(); it.next())
1315 ++count_before;
1316
1317 s.push(6);
1318 (void) s.pop();
1319
1320 // Count should be same
1321 size_t count_after = 0;
1322 for (auto it = s.get_it(); it.has_curr(); it.next())
1323 ++count_after;
1324
1325 EXPECT_EQ(count_before, count_after);
1326}
1327
1328// =============================================================================
1329// Main
1330// =============================================================================
1331
1332int main(int argc, char **argv)
1333{
1334 ::testing::InitGoogleTest(&argc, argv);
1335 return RUN_ALL_TESTS();
1336}
Functional programming utilities for Aleph-w containers.
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
T & push(const T &data)
Push into stack a copy of data
Dynamic stack of elements of generic type T based on a singly linked list.
T * search(const T &key) noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Search for an item in the stack using equality comparison.
T & put(const T &data)
Alias for push() - for compatibility with queue-like interfaces.
void clear() noexcept
Empties the container.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
T & insert(const T &data)
Alias for push() - for STL-like insert semantics.
bool contains(const T &key) const noexcept(noexcept(static_cast< bool >(std::declval< const T & >()==std::declval< const T & >())))
Check if the stack contains a specific value.
T & emplace(Args &&... args)
Construct an item in place at the top of the stack.
size_t size() const noexcept
Return the number of elements in the stack.
T & peek()
Alias for top() - returns reference to top item.
void swap(DynListStack &other) noexcept
Swap the contents of this stack with another.
T get()
Alias for pop() - removes and returns the top item.
T & append(const T &data)
Alias for push() - required by Special_Ctors macro.
bool traverse(Operation &operation) noexcept(noexcept(s.traverse(operation)))
Traverse all elements from top to bottom.
T pop()
Remove and return the top item of the stack.
T & push(const T &data)
Push an item by copy onto the top of the stack.
void empty() noexcept
Remove all elements from the stack.
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
Minimal std::expected-style result type for C++20.
Acc foldl(const Acc &init, Op &op) const
Fold the elements of the container to a specific result.
Definition ah-dry.H:2145
auto get_it()
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:663
#define TEST(name)
TEST_F(SimpleStack, copy_constructor_creates_independent_copy)
constexpr size_t LARGE_N
constexpr size_t N
Definition fixedstack.cc:47
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
Definition ah-date.H:140
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Definition ahAlgo.H:127
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
STL namespace.
Fixture with a stack of complex objects (DynList<int>)
Definition arraystack.cc:65
ArrayStack< DynList< int > > s
Definition arraystack.cc:67
Type that counts constructions/destructions.
Counted & operator=(Counted &&other) noexcept
Counted & operator=(const Counted &other)
Counted(Counted &&other) noexcept
static int moves
static int constructions
bool operator==(const Counted &other) const
static int destructions
static int copies
static void reset()
Counted(int v=0)
Counted(const Counted &other)
Move-only type for testing move semantics.
bool operator==(const MoveOnly &other) const
MoveOnly(MoveOnly &&other) noexcept
MoveOnly & operator=(const MoveOnly &)=delete
MoveOnly & operator=(MoveOnly &&other) noexcept
MoveOnly(const MoveOnly &)=delete
Fixture with a stack of integers.
Definition arraystack.cc:54
ArrayStack< int > s
Definition arraystack.cc:56
Fixture with a stack of strings.
DynListStack< string > s
static bool should_throw
ThrowOnCopy(const ThrowOnCopy &other)
ThrowOnCopy & operator=(ThrowOnCopy &&)=default
ThrowOnCopy(ThrowOnCopy &&)=default
ThrowOnCopy & operator=(const ThrowOnCopy &)=default
Dnode< int > Test
Definition testDnode.C:42
Dynamic stack implementation based on linked lists.