1+ #include " ../Hashmap.hpp"
2+
3+ #include < iostream>
4+ #include < cassert>
5+ #include < string>
6+ #include < thread>
7+
8+ /* *
9+ * @file advanced.cpp
10+ * @brief Advanced tests for the functionality of the hashmap.
11+ */
12+ int main ([[maybe_unused]] int _argc, [[maybe_unused]] char * _argv[]) {
13+
14+ LouiEriksson::Hashmap<int , std::string> hashmap;
15+
16+ std::cout << " ~ ADVANCED TESTS ~\n " ;
17+
18+ // Test 1: Large volume of data
19+ {
20+ std::cout << " Test 1: Large volume of data..." << std::flush;
21+
22+ static constexpr int iterations = 2000000 ;
23+
24+ for (int i = 0 ; i < iterations; ++i) {
25+ hashmap.Add (i, std::to_string (i));
26+ }
27+ assert (hashmap.size () == iterations);
28+
29+ std::cout << " Done.\n " ;
30+ }
31+
32+ // Test 2: Duplicate keys
33+ {
34+ std::cout << " Test 2: Duplicate keys..." << std::flush;
35+
36+ const auto controlSize = hashmap.size ();
37+
38+ for (int i = 0 ; i < controlSize; ++i) {
39+ hashmap.Add (i, " Duplicate" );
40+
41+ assert ((hashmap.Get (i).value () != " Duplicate" ) && " Duplicate found." );
42+ assert ((hashmap.size () == controlSize) && " Erroneous insertion." );
43+ }
44+
45+ std::cout << " Done.\n " ;
46+ }
47+
48+ // Test 3: Key collision
49+ {
50+ std::cout << " Test 3: Key collision..." << std::flush;
51+
52+ static constexpr int iterations = 2000000 ;
53+
54+ for (int i = 1 ; i < iterations; ++i) {
55+ hashmap.Add (-i, " Negative" );
56+
57+ assert (hashmap.Get (-i).value () == " Negative" );
58+ }
59+
60+ std::cout << " Done.\n " ;
61+ }
62+
63+ // Test 4: High churn
64+ {
65+ std::cout << " Test 4: High churn..." << std::flush;
66+
67+ hashmap.Clear ();
68+
69+ static constexpr int iterations = 2000000 ;
70+
71+ for (int i = iterations; i < iterations * 2 ; ++i) {
72+ hashmap.Add (i, std::to_string (i));
73+ hashmap.Remove (i - iterations);
74+ }
75+ assert (hashmap.size () == iterations);
76+
77+ std::cout << " Done.\n " ;
78+ }
79+
80+ // Test 5: Concurrency
81+ {
82+ std::cout << " Test 5: Concurrency..." << std::flush;
83+
84+ hashmap.Clear ();
85+
86+ static constexpr int iterations = 2000000 ;
87+
88+ std::exception_ptr writerException = nullptr ;
89+ std::exception_ptr readerException = nullptr ;
90+
91+ std::thread writer ([&hashmap, &writerException]() {
92+
93+ try {
94+ for (int i = 0 ; i < iterations; ++i) {
95+ hashmap.Add (i, std::to_string (i));
96+ }
97+ }
98+ catch (...) {
99+ writerException = std::current_exception ();
100+ }
101+ });
102+
103+ std::thread reader ([&hashmap, &readerException]() {
104+
105+ try {
106+ int last_size = 0 ;
107+
108+ while (last_size < iterations) {
109+ if (hashmap.size () > last_size) {
110+ for (int i = last_size; i < hashmap.size (); ++i) {
111+ assert ((hashmap.Get (i).value () == std::to_string (i)));
112+ }
113+ last_size = hashmap.size ();
114+ }
115+ }
116+ }
117+ catch (...) {
118+ readerException = std::current_exception ();
119+ }
120+ });
121+
122+ writer.join ();
123+ reader.join ();
124+
125+ if (writerException) { std::rethrow_exception (writerException); }
126+ if (readerException) { std::rethrow_exception (readerException); }
127+
128+ assert (hashmap.size () == iterations);
129+
130+ std::cout << " Done.\n " ;
131+ }
132+
133+ std::cout << " All tests passed!" << std::endl;
134+
135+ return 0 ;
136+ }
0 commit comments