Aleph-w 3.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;
119 {
120 other.moved_from = true;
121 }
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 {
145 }
146
147 explicit Counted(int v = 0) : value(v) { ++constructions; }
149 {
151 ++copies;
152 }
154 {
156 ++moves;
157 }
159
161 {
162 value = other.value;
163 ++copies;
164 return *this;
165 }
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
191
204
206{
207 DynListStack<int> s = {1, 2, 3, 4, 5};
208
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);
221}
222
229
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
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
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
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
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
292{
293 size_t original_size = s.size();
294 int original_top = s.top();
295
296 DynListStack<int> moved(std::move(s));
297
300 EXPECT_TRUE(s.is_empty());
301}
302
311
324
325// =============================================================================
326// Copy Assignment Tests
327// =============================================================================
328
330{
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
342{
343 s = s;
344
345 EXPECT_EQ(s.size(), n);
346 EXPECT_EQ(s.top(), int(n - 1));
347}
348
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
361{
362 DynListStack<int> source;
363 DynListStack<int> dest = {1, 2, 3};
364
365 dest = source;
366
368}
369
370// =============================================================================
371// Move Assignment Tests
372// =============================================================================
373
375{
376 size_t original_size = s.size();
378 dest.push(999);
379
380 dest = std::move(s);
381
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
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
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);
416 EXPECT_EQ(s.top(), other_top);
418}
419
421{
422 DynListStack<int> s = {1, 2, 3};
423 DynListStack<int> empty;
424
425 s.swap(empty);
426
428 EXPECT_EQ(empty.size(), 3);
429}
430
436
437// =============================================================================
438// Push Operation Tests
439// =============================================================================
440
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
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
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
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
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
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
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
538{
539 int top_value = s.top();
540
541 int popped = s.pop();
542
544 EXPECT_EQ(s.size(), n - 1);
545}
546
548{
550
551 EXPECT_THROW(s.pop(), underflow_error);
552}
553
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);
562 EXPECT_THROW(s.pop(), underflow_error);
563}
564
575
576// =============================================================================
577// Top/Peek Tests
578// =============================================================================
579
581{
582 EXPECT_EQ(s.top(), int(n - 1));
583
584 s.top() = 999;
585 EXPECT_EQ(s.top(), 999);
586}
587
589{
590 const DynListStack<int>& cs = s;
591
592 EXPECT_EQ(cs.top(), int(n - 1));
593}
594
596{
598
599 EXPECT_THROW(s.top(), underflow_error);
600}
601
603{
604 const DynListStack<int> s;
605
606 EXPECT_THROW(s.top(), underflow_error);
607}
608
610{
611 EXPECT_EQ(s.peek(), s.top());
612 EXPECT_EQ(&s.peek(), &s.top());
613}
614
620
621// =============================================================================
622// Get (alias for pop) Tests
623// =============================================================================
624
626{
627 int pop_result = s.pop();
628 int get_result = s.get();
629
630 EXPECT_EQ(s.size(), n - 2);
632}
633
635{
637 EXPECT_THROW(s.get(), underflow_error);
638}
639
640// =============================================================================
641// Size and Empty Tests
642// =============================================================================
643
649
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
667
672
674{
675 DynListStack<int> s = {1, 2, 3};
676
677 (void) s.pop();
678 (void) s.pop();
679 (void) s.pop();
680
682}
683
684// =============================================================================
685// Empty/Clear Tests
686// =============================================================================
687
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
699{
701
702 s.empty(); // Should not throw
703
705}
706
708{
709 s.clear();
710
711 EXPECT_TRUE(s.is_empty());
712 EXPECT_EQ(s.size(), 0);
713}
714
720
726
727// =============================================================================
728// Alias Methods Tests (put, insert, append)
729// =============================================================================
730
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
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
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
774{
775 int* ptr = s.search(5);
776
777 ASSERT_NE(ptr, nullptr);
778 EXPECT_EQ(*ptr, 5);
779}
780
782{
783 int* ptr = s.search(999);
784
785 EXPECT_EQ(ptr, nullptr);
786}
787
794
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
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
820{
821 EXPECT_TRUE(s.contains(5));
822 EXPECT_TRUE(s.contains(0));
823 EXPECT_TRUE(s.contains(int(n - 1)));
824}
825
827{
828 EXPECT_FALSE(s.contains(-1));
829 EXPECT_FALSE(s.contains(int(n)));
830 EXPECT_FALSE(s.contains(999));
831}
832
838
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
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
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
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
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
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
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
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
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
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
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
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
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
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
993{
994 auto result = s.filter([](int x) { return x > 1000; });
995
996 EXPECT_TRUE(result.is_empty());
997}
998
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
1005}
1006
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
1018{
1019 bool result = s.all([](int x) { return x >= 0; });
1020 EXPECT_TRUE(result);
1021}
1022
1024{
1025 bool result = s.all([](int x) { return x < 10; });
1026 EXPECT_FALSE(result); // n > 10, so some elements are >= 10
1027}
1028
1030{
1031 bool result = s.exists([](int x) { return x == 5; });
1032 EXPECT_TRUE(result);
1033}
1034
1036{
1037 bool result = s.exists([](int x) { return x == 999; });
1038 EXPECT_FALSE(result);
1039}
1040
1042{
1043 int sum = 0;
1044 s.for_each([&sum](int x) { sum += x; });
1045
1046 int expected = (n - 1) * n / 2;
1048}
1049
1050// =============================================================================
1051// Type Aliases Tests
1052// =============================================================================
1053
1062
1063// =============================================================================
1064// Memory Management Tests
1065// =============================================================================
1066
1068{
1070
1071 {
1073 for (int i = 0; i < 100; ++i)
1074 s.emplace(i);
1075 }
1076
1077 // All elements should be destroyed
1079}
1080
1082{
1084
1086 for (int i = 0; i < 50; ++i)
1087 s.emplace(i);
1088
1090 s.clear();
1091
1093}
1094
1095// =============================================================================
1096// Move Semantics Tests
1097// =============================================================================
1098
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
1114{
1116
1118 Counted c(42);
1119
1120 s.push(std::move(c));
1121
1122 // Should use move, not copy
1124}
1125
1126// =============================================================================
1127// Edge Cases and Boundary Tests
1128// =============================================================================
1129
1131{
1133 s.push(42);
1134
1135 EXPECT_EQ(s.size(), 1);
1136 EXPECT_EQ(s.top(), 42);
1138
1139 EXPECT_EQ(s.pop(), 42);
1140 EXPECT_TRUE(s.is_empty());
1141}
1142
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
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
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
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
1211{
1213
1214 for (size_t i = 0; i < LARGE_N; ++i)
1215 original.push(i);
1216
1218
1219 EXPECT_EQ(copy.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) {}
1244 {
1245 if (should_throw)
1246 throw runtime_error("Copy failed");
1247 }
1251};
1252
1253bool ThrowOnCopy::should_throw = false;
1254
1256{
1258 s.push(ThrowOnCopy(1));
1259 s.push(ThrowOnCopy(2));
1260
1262 ThrowOnCopy item(3);
1263
1264 EXPECT_THROW(s.push(item), runtime_error);
1265
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
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
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);
1306}
1307
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
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()
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 & append(const T &data)
Alias for push() - required by Special_Ctors macro.
void clear() noexcept
Alias for empty() - removes all elements.
T & top()
Return a modifiable reference to the top item of the stack.
bool is_empty() const noexcept
Check if the stack is empty.
constexpr size_t size() const noexcept
Return the number of elements in the stack.
T & emplace(Args &&... args)
Construct an item in place at the top of the stack.
void swap(DynListStack &other) noexcept
Swap the contents of this stack with another.
T * search(const T &key) noexcept
Search for an item in the stack using equality comparison.
T get()
Alias for pop() - removes and returns the top item.
bool traverse(Operation &operation)
Traverse all elements from top to bottom.
bool contains(const T &key) const noexcept
Check if the stack contains a specific value.
T & put(const T &data)
Alias for push() - for compatibility with queue-like interfaces.
T & insert(const T &data)
Alias for push() - for STL-like insert semantics.
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.
T & top() const
Definition htlist.H:1683
T & get_first() const
Return the first item of the list.
Definition htlist.H:1675
T & push(const T &item)
Definition htlist.H:1523
DynList & swap(DynList &l) noexcept
Swap this with l.
Definition htlist.H:1448
constexpr bool is_empty() const noexcept
Return true if list is empty.
Definition htlist.H:523
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
__T foldl(const __T &init, Op &op) const
Fold the elements of the container to a specific result.
Definition ah-dry.H:1034
Aleph::DynList< T > filter(Operation &operation) const
Filter the elements of a container according to a matching criteria.
Definition ah-dry.H:1135
Aleph::DynList< __T > maps(Operation &op) const
Map the elements of the container.
Definition ah-dry.H:904
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
Definition ah-dry.H:190
#define TEST(name)
TEST_F(SimpleStack, copy_constructor_creates_independent_copy)
constexpr size_t LARGE_N
constexpr size_t N
constexpr size_t N
Definition fixedstack.cc:47
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
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
DynList< T > maps(const C &c, Op op)
Classic map operation.
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:61
ArrayStack< DynList< int > > s
Definition arraystack.cc:63
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)
bool traverse(Operation &operation) noexcept(traverse_is_noexcept< Operation >())
Traverse the container via its iterator and performs a conditioned operation on each item.
Definition ah-dry.H:95
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:50
ArrayStack< int > s
Definition arraystack.cc:52
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
Dynamic stack implementation based on linked lists.