Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
dynmapohash_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
33
38#include <gtest/gtest.h>
39#include <string>
40#include <type_traits>
41#include <utility>
42#include <vector>
43#include <tpl_dynMapOhash.H>
44
45using namespace Aleph;
46
47// =============================================================================
48// Test Fixtures
49// =============================================================================
50
51class MapODhashTest : public ::testing::Test
52{
53protected:
56
57 void SetUp() override
58 {
59 // Start with empty maps
60 }
61
63 {
64 map.insert("one", 1);
65 map.insert("two", 2);
66 map.insert("three", 3);
67 map.insert("four", 4);
68 map.insert("five", 5);
69 }
70
72 {
73 int_map.insert(1, "one");
74 int_map.insert(2, "two");
75 int_map.insert(3, "three");
76 }
77};
78
79class MapOLhashTest : public ::testing::Test
80{
81protected:
83
85 {
86 map.insert("alpha", 1);
87 map.insert("beta", 2);
88 map.insert("gamma", 3);
89 }
90};
91
92// =============================================================================
93// Construction Tests
94// =============================================================================
95
96TEST_F(MapODhashTest, DefaultConstruction)
97{
98 EXPECT_EQ(map.size(), 0u);
99 EXPECT_TRUE(map.is_empty());
100}
101
102TEST_F(MapOLhashTest, DefaultConstruction)
103{
104 EXPECT_EQ(map.size(), 0u);
105 EXPECT_TRUE(map.is_empty());
106}
107
108TEST(MapOpenHashConstruction, WithCustomSize)
109{
110 MapODhash<int, int> map(101);
111 EXPECT_EQ(map.size(), 0u);
112}
113
114TEST(MapOpenHashConstruction, CopyConstruction)
115{
117 original.insert("a", 1);
118 original.insert("b", 2);
119
121 EXPECT_EQ(copy.size(), original.size());
122 EXPECT_TRUE(copy.has("a"));
123 EXPECT_TRUE(copy.has("b"));
124 EXPECT_EQ(copy["a"], 1);
125 EXPECT_EQ(copy["b"], 2);
126}
127
128TEST(MapOpenHashConstruction, MoveConstruction)
129{
131 original.insert("x", 10);
132 original.insert("y", 20);
133 size_t orig_size = original.size();
134
135 MapODhash<std::string, int> moved(std::move(original));
136 EXPECT_EQ(moved.size(), orig_size);
137 EXPECT_TRUE(moved.has("x"));
138 EXPECT_TRUE(moved.has("y"));
139}
140
141// =============================================================================
142// Insert Tests
143// =============================================================================
144
145TEST_F(MapODhashTest, InsertCopySemanticsKeyValue)
146{
147 std::string key = "test";
148 int value = 42;
149
150 auto * pair = map.insert(key, value);
151 ASSERT_NE(pair, nullptr);
152 EXPECT_EQ(pair->first, "test");
153 EXPECT_EQ(pair->second, 42);
154 EXPECT_EQ(map.size(), 1u);
155}
156
157TEST_F(MapODhashTest, InsertMoveValue)
158{
159 std::string key = "moved";
160 std::string value = "hello_world";
161
162 auto * pair = int_map.insert(100, std::move(value));
163 ASSERT_NE(pair, nullptr);
164 EXPECT_EQ(pair->first, 100);
165 EXPECT_EQ(pair->second, "hello_world");
166}
167
168TEST_F(MapODhashTest, InsertMoveKeyAndValue)
169{
170 std::string key = "key";
171 int value = 99;
172
173 auto * pair = map.insert(std::move(key), std::move(value));
174 ASSERT_NE(pair, nullptr);
175 EXPECT_EQ(pair->first, "key");
176 EXPECT_EQ(pair->second, 99);
177}
178
179TEST_F(MapODhashTest, InsertMoveKeyCopyValue)
180{
181 std::string key = "movekey";
182 int value = 77;
183
184 auto * pair = map.insert(std::move(key), value);
185 ASSERT_NE(pair, nullptr);
186 EXPECT_EQ(pair->first, "movekey");
187 EXPECT_EQ(pair->second, 77);
188}
189
190TEST_F(MapODhashTest, InsertDuplicateKeyReturnsNullptr)
191{
192 map.insert("dup", 1);
193 auto * pair = map.insert("dup", 2);
194 EXPECT_EQ(pair, nullptr);
195 EXPECT_EQ(map["dup"], 1); // Original value preserved
196}
197
198TEST_F(MapODhashTest, InsertMultipleEntries)
199{
200 populate_map();
201 EXPECT_EQ(map.size(), 5u);
202 EXPECT_TRUE(map.has("one"));
203 EXPECT_TRUE(map.has("five"));
204}
205
206// =============================================================================
207// Search Tests
208// =============================================================================
209
210TEST_F(MapODhashTest, SearchExistingKey)
211{
212 populate_map();
213
214 auto * pair = map.search("three");
215 ASSERT_NE(pair, nullptr);
216 EXPECT_EQ(pair->first, "three");
217 EXPECT_EQ(pair->second, 3);
218}
219
220TEST_F(MapODhashTest, SearchNonexistentKey)
221{
222 populate_map();
223
224 auto * pair = map.search("nonexistent");
225 EXPECT_EQ(pair, nullptr);
226}
227
228TEST_F(MapODhashTest, SearchWithMoveSemantics)
229{
230 populate_map();
231
232 std::string key = "two";
233 auto * pair = map.search(std::move(key));
234 ASSERT_NE(pair, nullptr);
235 EXPECT_EQ(pair->second, 2);
236}
237
238TEST_F(MapODhashTest, SearchEmptyMap)
239{
240 auto * pair = map.search("anything");
241 EXPECT_EQ(pair, nullptr);
242}
243
244// =============================================================================
245// Has/Contains Tests
246// =============================================================================
247
248TEST_F(MapODhashTest, HasExistingKey)
249{
250 populate_map();
251 EXPECT_TRUE(map.has("one"));
252 EXPECT_TRUE(map.has("five"));
253}
254
255TEST_F(MapODhashTest, HasNonexistentKey)
256{
257 populate_map();
258 EXPECT_FALSE(map.has("nonexistent"));
259 EXPECT_FALSE(map.has(""));
260}
261
262TEST_F(MapODhashTest, HasWithMoveSemantics)
263{
264 populate_map();
265 std::string key = "three";
266 EXPECT_TRUE(map.has(std::move(key)));
267}
268
269TEST_F(MapODhashTest, ContainsIsAliasForHas)
270{
271 populate_map();
272 EXPECT_EQ(map.contains("one"), map.has("one"));
273 EXPECT_EQ(map.contains("nonexistent"), map.has("nonexistent"));
274}
275
276TEST_F(MapODhashTest, ContainsWithMoveSemantics)
277{
278 populate_map();
279 std::string key = "two";
280 EXPECT_TRUE(map.contains(std::move(key)));
281}
282
283// =============================================================================
284// Find Tests
285// =============================================================================
286
287TEST_F(MapODhashTest, FindExistingKey)
288{
289 populate_map();
290
291 int & value = map.find("three");
292 EXPECT_EQ(value, 3);
293}
294
295TEST_F(MapODhashTest, FindAndModify)
296{
297 populate_map();
298
299 map.find("two") = 22;
300 EXPECT_EQ(map.find("two"), 22);
301}
302
303TEST_F(MapODhashTest, FindNonexistentKeyThrows)
304{
305 populate_map();
306 EXPECT_THROW((void)map.find("nonexistent"), std::domain_error);
307}
308
309TEST_F(MapODhashTest, FindWithMoveSemantics)
310{
311 populate_map();
312 std::string key = "four";
313 EXPECT_EQ(map.find(std::move(key)), 4);
314}
315
316TEST_F(MapODhashTest, FindConstVersion)
317{
318 populate_map();
319 const auto & const_map = map;
320
321 const int & value = const_map.find("one");
322 EXPECT_EQ(value, 1);
323}
324
325TEST_F(MapODhashTest, FindConstNonexistentThrows)
326{
327 populate_map();
328 const auto & const_map = map;
329
330 EXPECT_THROW((void)const_map.find("missing"), std::domain_error);
331}
332
333// =============================================================================
334// Operator[] Tests
335// =============================================================================
336
337TEST_F(MapODhashTest, SubscriptAccessExisting)
338{
339 populate_map();
340 EXPECT_EQ(map["one"], 1);
341 EXPECT_EQ(map["five"], 5);
342}
343
344TEST_F(MapODhashTest, SubscriptInsertsNewKey)
345{
346 map["new_key"] = 100;
347 EXPECT_TRUE(map.has("new_key"));
348 EXPECT_EQ(map["new_key"], 100);
349}
350
351TEST_F(MapODhashTest, SubscriptDefaultInitializes)
352{
353 int & value = map["defaulted"];
354 EXPECT_EQ(value, 0); // Default-initialized int
355 value = 42;
356 EXPECT_EQ(map["defaulted"], 42);
357}
358
359TEST_F(MapODhashTest, SubscriptWithMoveSemantics)
360{
361 std::string key = "moved_key";
362 map[std::move(key)] = 55;
363 EXPECT_EQ(map["moved_key"], 55);
364}
365
366TEST_F(MapODhashTest, SubscriptConstVersion)
367{
368 populate_map();
369 const auto & const_map = map;
370
371 EXPECT_EQ(const_map["two"], 2);
372}
373
374TEST_F(MapODhashTest, SubscriptConstNonexistentThrows)
375{
376 populate_map();
377 const auto & const_map = map;
378
379 EXPECT_THROW((void)const_map["missing"], std::domain_error);
380}
381
382// =============================================================================
383// Remove Tests
384// =============================================================================
385
386TEST_F(MapODhashTest, RemoveExistingKey)
387{
388 populate_map();
389 size_t original_size = map.size();
390
391 map.remove("three");
392
393 EXPECT_EQ(map.size(), original_size - 1);
394 EXPECT_FALSE(map.has("three"));
395}
396
397TEST_F(MapODhashTest, RemoveNonexistentKeyThrows)
398{
399 populate_map();
400 EXPECT_THROW(map.remove("nonexistent"), std::domain_error);
401}
402
403TEST_F(MapODhashTest, RemoveWithMoveSemantics)
404{
405 populate_map();
406 std::string key = "two";
407
408 map.remove(std::move(key));
409 EXPECT_FALSE(map.has("two"));
410}
411
412TEST_F(MapODhashTest, RemoveAllEntries)
413{
414 populate_map();
415
416 map.remove("one");
417 map.remove("two");
418 map.remove("three");
419 map.remove("four");
420 map.remove("five");
421
422 EXPECT_EQ(map.size(), 0u);
423 EXPECT_TRUE(map.is_empty());
424}
425
426TEST_F(MapODhashTest, RemoveByData)
427{
428 populate_map();
429 auto * pair = map.search("three");
430 ASSERT_NE(pair, nullptr);
431
432 map.remove_by_data(pair->second);
433 EXPECT_FALSE(map.has("three"));
434}
435
436// =============================================================================
437// Keys and Values Tests
438// =============================================================================
439
440TEST_F(MapODhashTest, KeysReturnsAllKeys)
441{
442 populate_map();
443
444 auto key_list = map.keys();
445 EXPECT_EQ(key_list.size(), 5u);
446
447 // Check all expected keys are present
448 bool has_one = false, has_two = false, has_three = false;
449 bool has_four = false, has_five = false;
450
451 key_list.traverse([&](const std::string & k) {
452 if (k == "one") has_one = true;
453 else if (k == "two") has_two = true;
454 else if (k == "three") has_three = true;
455 else if (k == "four") has_four = true;
456 else if (k == "five") has_five = true;
457 return true;
458 });
459
460 EXPECT_TRUE(has_one);
461 EXPECT_TRUE(has_two);
462 EXPECT_TRUE(has_three);
463 EXPECT_TRUE(has_four);
464 EXPECT_TRUE(has_five);
465}
466
467TEST_F(MapODhashTest, ValuesReturnsAllValues)
468{
469 populate_map();
470
471 auto value_list = map.values();
472 EXPECT_EQ(value_list.size(), 5u);
473
474 // Sum should be 1+2+3+4+5 = 15
475 int sum = 0;
476 value_list.traverse([&sum](int v) {
477 sum += v;
478 return true;
479 });
480 EXPECT_EQ(sum, 15);
481}
482
483TEST_F(MapODhashTest, ValuesPtrReturnsPointers)
484{
485 populate_map();
486
487 auto ptr_list = map.values_ptr();
488 EXPECT_EQ(ptr_list.size(), 5u);
489
490 // Modify through pointers
491 ptr_list.traverse([](int * p) {
492 *p *= 10;
493 return true;
494 });
495
496 // Verify modifications
497 EXPECT_EQ(map["one"], 10);
498 EXPECT_EQ(map["two"], 20);
499}
500
501TEST_F(MapODhashTest, ItemsPtrReturnsPairPointers)
502{
503 populate_map();
504
505 auto ptr_list = map.items_ptr();
506 EXPECT_EQ(ptr_list.size(), 5u);
507
508 // Verify we can access both key and value
509 ptr_list.traverse([](auto * pair) {
510 EXPECT_FALSE(pair->first.empty());
511 return true;
512 });
513}
514
515TEST_F(MapODhashTest, KeysOnEmptyMap)
516{
517 auto key_list = map.keys();
518 EXPECT_EQ(key_list.size(), 0u);
519}
520
521TEST_F(MapODhashTest, ValuesOnEmptyMap)
522{
523 auto value_list = map.values();
524 EXPECT_EQ(value_list.size(), 0u);
525}
526
527// =============================================================================
528// Static Helper Function Tests
529// =============================================================================
530
532{
533 populate_map();
534 auto * pair = map.search("one");
535 ASSERT_NE(pair, nullptr);
536
537 auto * recovered_pair = MapODhash<std::string, int>::key_to_pair(&pair->first);
538 EXPECT_EQ(recovered_pair, pair);
539}
540
542{
543 populate_map();
544 auto * pair = map.search("two");
545 ASSERT_NE(pair, nullptr);
546
547 auto * recovered_pair = MapODhash<std::string, int>::data_to_pair(&pair->second);
548 EXPECT_EQ(recovered_pair, pair);
549 EXPECT_EQ(recovered_pair->first, "two");
550}
551
552TEST_F(MapODhashTest, GetDataFromKey)
553{
554 populate_map();
555 auto * pair = map.search("three");
556 ASSERT_NE(pair, nullptr);
557
558 int & data = MapODhash<std::string, int>::get_data(pair->first);
559 EXPECT_EQ(data, 3);
560
561 // Modify through this reference
562 data = 333;
563 EXPECT_EQ(map["three"], 333);
564}
565
566TEST_F(MapODhashTest, GetKeyFromDataPtr)
567{
568 populate_map();
569 auto * pair = map.search("four");
570 ASSERT_NE(pair, nullptr);
571
572 const std::string & key = MapODhash<std::string, int>::get_key(&pair->second);
573 EXPECT_EQ(key, "four");
574}
575
576TEST_F(MapODhashTest, KeyToPairConstOverload)
577{
578 populate_map();
579 const auto & const_map = map;
580 const auto * pair = const_map.search("one");
581 ASSERT_NE(pair, nullptr);
582
583 const auto * recovered_pair = MapODhash<std::string, int>::key_to_pair(&pair->first);
584 EXPECT_EQ(recovered_pair, pair);
585}
586
587TEST_F(MapODhashTest, DataToPairConstOverload)
588{
589 populate_map();
590 const auto & const_map = map;
591 const auto * pair = const_map.search("two");
592 ASSERT_NE(pair, nullptr);
593
594 const auto * recovered_pair = MapODhash<std::string, int>::data_to_pair(&pair->second);
595 EXPECT_EQ(recovered_pair, pair);
596}
597
598TEST_F(MapODhashTest, GetDataConstOverload)
599{
600 populate_map();
601 const auto & const_map = map;
602 const auto * pair = const_map.search("three");
603 ASSERT_NE(pair, nullptr);
604
605 const int & data = MapODhash<std::string, int>::get_data(pair->first);
606 EXPECT_EQ(data, 3);
607}
608
609TEST_F(MapODhashTest, GetKeyConstOverload)
610{
611 populate_map();
612 const auto & const_map = map;
613 const auto * pair = const_map.search("four");
614 ASSERT_NE(pair, nullptr);
615
616 const std::string & key = MapODhash<std::string, int>::get_key(&pair->second);
617 EXPECT_EQ(key, "four");
618}
619
620// =============================================================================
621// Iterator Tests
622// =============================================================================
623
624TEST_F(MapODhashTest, IteratorTraversal)
625{
626 populate_map();
627
628 size_t count = 0;
629 for (MapODhash<std::string, int>::Iterator it(map); it.has_curr(); it.next_ne())
630 {
631 auto & pair = it.get_curr();
632 EXPECT_FALSE(pair.first.empty());
633 ++count;
634 }
635
636 EXPECT_EQ(count, 5u);
637}
638
639TEST_F(MapODhashTest, IteratorOnEmptyMap)
640{
642 EXPECT_FALSE(it.has_curr());
643}
644
645TEST_F(MapODhashTest, TraverseFunctional)
646{
647 populate_map();
648
649 int sum = 0;
650 map.traverse([&sum](const auto & pair) {
651 sum += pair.second;
652 return true;
653 });
654
655 EXPECT_EQ(sum, 15);
656}
657
658TEST_F(MapODhashTest, TraverseEarlyTermination)
659{
660 populate_map();
661
662 int count = 0;
663 bool result = map.traverse([&count](const auto &) {
664 ++count;
665 return count < 3; // Stop after 3 elements
666 });
667
668 EXPECT_FALSE(result); // Stopped early
669 EXPECT_EQ(count, 3);
670}
671
672// =============================================================================
673// MapOLhash Specific Tests
674// =============================================================================
675
676TEST_F(MapOLhashTest, BasicOperations)
677{
678 populate_map();
679
680 EXPECT_TRUE(map.has("alpha"));
681 EXPECT_TRUE(map.has("beta"));
682 EXPECT_TRUE(map.has("gamma"));
683 EXPECT_EQ(map.size(), 3u);
684}
685
686TEST_F(MapOLhashTest, InsertAndSearch)
687{
688 map.insert("test", 42);
689 auto * pair = map.search("test");
690 ASSERT_NE(pair, nullptr);
691 EXPECT_EQ(pair->second, 42);
692}
693
694TEST_F(MapOLhashTest, RemoveOperation)
695{
696 populate_map();
697 map.remove("beta");
698
699 EXPECT_FALSE(map.has("beta"));
700 EXPECT_TRUE(map.has("alpha"));
701 EXPECT_TRUE(map.has("gamma"));
702}
703
704// =============================================================================
705// Large Scale Tests
706// =============================================================================
707
708TEST(MapOpenHashScaling, InsertManyElements)
709{
711
712 constexpr int N = 10000;
713 for (int i = 0; i < N; ++i)
714 map.insert(i, i * 2);
715
716 EXPECT_EQ(map.size(), static_cast<size_t>(N));
717
718 // Verify all elements
719 for (int i = 0; i < N; ++i)
720 {
721 ASSERT_TRUE(map.has(i)) << "Missing key: " << i;
722 EXPECT_EQ(map[i], i * 2);
723 }
724}
725
726TEST(MapOpenHashScaling, RemoveManyElements)
727{
729
730 constexpr int N = 1000;
731 for (int i = 0; i < N; ++i)
732 map.insert(i, i);
733
734 // Remove even numbers
735 for (int i = 0; i < N; i += 2)
736 map.remove(i);
737
738 EXPECT_EQ(map.size(), static_cast<size_t>(N / 2));
739
740 // Verify only odd numbers remain
741 for (int i = 0; i < N; ++i)
742 {
743 if (i % 2 == 0)
744 EXPECT_FALSE(map.has(i));
745 else
746 EXPECT_TRUE(map.has(i));
747 }
748}
749
750TEST(MapOpenHashScaling, InsertRemoveInterleaved)
751{
753
754 for (int i = 0; i < 500; ++i)
755 {
756 map.insert(i, i);
757 if (i > 100)
758 map.remove(i - 100);
759 }
760
761 // Remaining keys are 0 plus the last 100 inserted keys (400-499).
762 EXPECT_EQ(map.size(), 101u);
763 EXPECT_TRUE(map.has(0));
764 for (int i = 400; i < 500; ++i)
765 EXPECT_TRUE(map.has(i));
766}
767
768// =============================================================================
769// Edge Cases
770// =============================================================================
771
772TEST_F(MapODhashTest, EmptyStringKey)
773{
774 map.insert("", 0);
775 EXPECT_TRUE(map.has(""));
776 EXPECT_EQ(map[""], 0);
777}
778
779TEST_F(MapODhashTest, NegativeValues)
780{
781 map.insert("neg", -100);
782 EXPECT_EQ(map["neg"], -100);
783}
784
785TEST(MapOpenHashEdgeCases, ZeroKey)
786{
788 map.insert(0, "zero");
789 EXPECT_TRUE(map.has(0));
790 EXPECT_EQ(map[0], "zero");
791}
792
793TEST(MapOpenHashEdgeCases, NegativeKey)
794{
796 map.insert(-5, 50);
797 map.insert(-100, 1000);
798
799 EXPECT_TRUE(map.has(-5));
800 EXPECT_TRUE(map.has(-100));
801 EXPECT_EQ(map[-5], 50);
802 EXPECT_EQ(map[-100], 1000);
803}
804
805// =============================================================================
806// Assignment Operators
807// =============================================================================
808
809TEST(MapOpenHashAssignment, CopyAssignment)
810{
812 original.insert("a", 1);
813 original.insert("b", 2);
814
816 copy.insert("x", 100); // Some existing content
817
818 copy = original;
819
820 EXPECT_EQ(copy.size(), 2u);
821 EXPECT_TRUE(copy.has("a"));
822 EXPECT_TRUE(copy.has("b"));
823 EXPECT_FALSE(copy.has("x")); // Old content gone
824}
825
826TEST(MapOpenHashAssignment, MoveAssignment)
827{
829 original.insert("m", 10);
830 original.insert("n", 20);
831
833 target = std::move(original);
834
835 EXPECT_EQ(target.size(), 2u);
836 EXPECT_TRUE(target.has("m"));
837 EXPECT_TRUE(target.has("n"));
838}
839
840TEST(MapOpenHashAssignment, SelfAssignment)
841{
843 map.insert("self", 42);
844
845 map = map; // Self-assignment
846
847 EXPECT_EQ(map.size(), 1u);
848 EXPECT_EQ(map["self"], 42);
849}
850
851// =============================================================================
852// Type Alias Tests
853// =============================================================================
854
856{
857 // Verify type aliases exist and are correct
858 static_assert(std::is_same_v<MapODhash<std::string, int>::Key_Type, std::string>);
859 static_assert(std::is_same_v<MapODhash<std::string, int>::Data_Type, int>);
860 static_assert(std::is_same_v<MapODhash<std::string, int>::Value_Type, int>);
861 static_assert(std::is_same_v<MapODhash<std::string, int>::Item_Type,
862 std::pair<std::string, int>>);
863}
864
865// =============================================================================
866// Comparison with Linear Probing
867// =============================================================================
868
869TEST(MapVariantComparison, BothVariantsProduceSameResults)
870{
873
874 // Insert same data
875 for (int i = 0; i < 100; ++i)
876 {
877 std::string val = std::to_string(i * 10);
878 od_map.insert(i, val);
879 ol_map.insert(i, val);
880 }
881
882 EXPECT_EQ(od_map.size(), ol_map.size());
883
884 // Verify same content
885 for (int i = 0; i < 100; ++i)
886 {
887 EXPECT_TRUE(od_map.has(i));
888 EXPECT_TRUE(ol_map.has(i));
889 EXPECT_EQ(od_map[i], ol_map[i]);
890 }
891}
892
893// =============================================================================
894// Functional Methods (inherited)
895// =============================================================================
896
897TEST_F(MapODhashTest, FilterMethod)
898{
899 populate_map();
900
901 auto filtered = map.filter([](const auto & pair) {
902 return pair.second > 2;
903 });
904
905 EXPECT_EQ(filtered.size(), 3u); // three, four, five
906}
907
909{
910 populate_map();
911
912 auto doubled = map.template maps<int>([](const auto & pair) {
913 return pair.second * 2;
914 });
915
916 int sum = 0;
917 doubled.traverse([&sum](int v) {
918 sum += v;
919 return true;
920 });
921
922 EXPECT_EQ(sum, 30); // (1+2+3+4+5) * 2 = 30
923}
924
926{
927 populate_map();
928
929 int sum = map.template foldl<int>(0, [](int acc, const auto & pair) {
930 return acc + pair.second;
931 });
932
933 EXPECT_EQ(sum, 15);
934}
935
937{
938 populate_map();
939
940 bool all_positive = map.all([](const auto & pair) {
941 return pair.second > 0;
942 });
943
944 EXPECT_TRUE(all_positive);
945
946 map["zero"] = 0;
947
948 all_positive = map.all([](const auto & pair) {
949 return pair.second > 0;
950 });
951
952 EXPECT_FALSE(all_positive);
953}
954
955TEST_F(MapODhashTest, ExistsMethod)
956{
957 populate_map();
958
959 bool has_three = map.exists([](const auto & pair) {
960 return pair.second == 3;
961 });
962
963 EXPECT_TRUE(has_three);
964
965 bool has_ten = map.exists([](const auto & pair) {
966 return pair.second == 10;
967 });
968
969 EXPECT_FALSE(has_ten);
970}
971
972// =============================================================================
973// The hash function of the pairs
974// =============================================================================
975
976namespace
977{
978 size_t a_key_hash(const long & k) noexcept
979 {
980 return static_cast<size_t>(k) * 40503u + 7;
981 }
982} // namespace
983
984// The hash function of the pairs of a map holds only the pointer to that
985// of the keys, so that the std::function of the table keeps it in its
986// buffer and copies it without allocating. It was a std::bind of
987// map_hash_fct(), which did not fit: a move of a map, which copies its
988// functions, allocated, although it is noexcept
989TEST(MapHashOf, GivesAPairTheHashOfItsKey)
990{
991 const auto pair_hash = hash_fct_detail::map_hash_of<long, std::string>(a_key_hash);
992 static_assert(sizeof(pair_hash) == sizeof(void *));
993 static_assert(std::is_trivially_copyable_v<decltype(pair_hash)>);
994 static_assert(std::is_nothrow_copy_constructible_v<decltype(pair_hash)>);
995
996 for (long k = -5; k < 100; ++k)
997 EXPECT_EQ(pair_hash(std::pair<long, std::string>(k, "v")), a_key_hash(k));
998}
999
1000// A map with a hash function of its own finds its pairs by it, as before
1001TEST(MapHashOf, AMapFindsItsPairsWithAHashFunctionOfItsOwn)
1002{
1003 MapODhash<long, std::string> od(101, a_key_hash, a_key_hash);
1004 MapOLhash<long, std::string> ol(101, a_key_hash);
1005 for (long k = 0; k < 500; ++k)
1006 {
1007 od.insert(k, std::to_string(k));
1008 ol.insert(k, std::to_string(k));
1009 }
1010 for (long k = 0; k < 500; ++k)
1011 {
1012 ASSERT_NE(od.search(k), nullptr) << k;
1013 EXPECT_EQ(od.search(k)->second, std::to_string(k));
1014 ASSERT_NE(ol.search(k), nullptr) << k;
1015 EXPECT_EQ(ol.search(k)->second, std::to_string(k));
1016 }
1017 EXPECT_EQ(od.search(500), nullptr);
1018 EXPECT_EQ(ol.search(500), nullptr);
1019}
1020
1021// =============================================================================
1022// Main
1023// =============================================================================
1024
1025int main(int argc, char ** argv)
1026{
1027 ::testing::InitGoogleTest(&argc, argv);
1028 return RUN_ALL_TESTS();
1029}
int main()
size_t size_t int32_t value
Definition ca-c-api.h:116
Number Theoretic Transform over Z / MOD Z.
Definition ntt.H:115
void SetUp() override
MapODhash< std::string, int > map
MapODhash< int, std::string > int_map
MapOLhash< std::string, int > map
#define TEST(name)
TEST_F(MapODhashTest, DefaultConstruction)
#define N
Definition fib.C:294
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::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
Definition ahPair.H:89
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.
Open addressing hash map using double hashing.
Open addressing hash map using linear probing.
void remove(const Key &key)
Remove an entry by key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Search for a key in the map.
Pair * insert(const Key &key, const Data &data)
Insert a key-value pair (copy semantics).
bool has(const Key &key) const noexcept(nothrow_search< const Key & >)
Check if a key exists in the map.
static int * k
Dynamic map with open hashing.