-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinuxTests.cpp
155 lines (130 loc) · 5.2 KB
/
LinuxTests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <map>
#include <iomanip>
#include "AVLTree.h"
#include "EmployeeInfo.h"
#include "timer.h"
AVL tree; // Assuming global AVL tree for ease of use in this example
std::map<int, EmployeeInfo> stdMap; // Global std::map for similar reason
// Include your test functions here
void testMaximumSize();
void testSpeedOfSearch();
void testMaximumSizeStdMap();
void testSearchSpeedStdMap();
int main() {
// Run your tests here
testMaximumSize();
testSpeedOfSearch();
testMaximumSizeStdMap();
testSearchSpeedStdMap();
return 0;
}
// Define your test functions after main or in a separate file that you include
void testMaximumSize() {
Timer timer;
std::cout << "Testing Maximum Size..." << std::endl;
size_t count = 0;
const size_t maxIterations = 1500000000;
timer.start();
while (count < maxIterations) {
try {
EmployeeInfo empl;
empl.sin = static_cast<int>(count); // Use count as the SIN
empl.age = 30;
empl.salary = 50000;
empl.emplNumber = static_cast<int>(count);
tree.insert(empl);
++count;
}
catch (const std::bad_alloc& e) {
std::cout << "Maximum elements before std::bad_alloc: " << count << std::endl;
break;
}
catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << " after inserting " << count << " elements.\n";
}
}
timer.stop();
// tree.clear();
// not clearing the tree and using the same tree to perform speed of search!
std::cout << "Test completed with " << count << " elements." << std::endl;
double timeForMaximumSize = timer.currtime();
std::cout << "Time to attempt loading " << maxIterations << " elements on the std::map: "
<< std::fixed << std::setprecision(6) << timeForMaximumSize << " seconds" << std::endl;
}
void testSpeedOfSearch() {
std::cout << "Testing Speed of Search..." << std::endl;
Timer timer;
// Search for the minimumnode
timer.start();
node* minNode = tree.findMinPublic();
timer.stop();
double timeForMinSearch = timer.currtime();
std::cout << "Time to find minimum node: " << std::fixed << std::setprecision(8) << timeForMinSearch << " seconds" << std::endl;
timer.reset();
// Search for the maximum node
timer.start();
node* maxNode = tree.findMaxPublic();
timer.stop();
double timeForMaxSearch = timer.currtime();
std::cout << "Time to find maximum node: " << std::fixed << std::setprecision(8) << timeForMaxSearch << " seconds" << std::endl;
// Displaying the nodes found
if (minNode != NULL) {
std::cout << "Minimum node SIN: " << minNode->empl.sin << std::endl;
}
if (maxNode != NULL) {
std::cout << "Maximum node SIN: " << maxNode->empl.sin << std::endl;
}
}
void testMaximumSizeStdMap() {
Timer timer;
std::cout << "Testing Maximum Size for std::map..." << std::endl;
std::map<int, EmployeeInfo> stdMap;
size_t count = 0;
const size_t maxIterations = 1500000000;
timer.start();
while (count < maxIterations) {
try {
EmployeeInfo empl;
empl.sin = static_cast<int>(count); // Use count as the SIN
empl.age = 30;
empl.salary = 50000;
empl.emplNumber = static_cast<int>(count);
stdMap[count] = empl; // Inserting by count as key
++count;
}
catch (const std::bad_alloc& e) {
std::cout << "Maximum elements before std::bad_alloc: " << count << std::endl;
break; // Break on memory allocation failure
}
catch (const std::exception& e) {
std::cout << "Caught exception: " << e.what() << " after inserting " << count << " elements.\n";
}
}
timer.stop();
// stdMap.clear(); // Clearing the std::map after finishing the test for maximum size
// not clearing the map and using the same map to perform speed of search
std::cout << "Test completed with " << count << " elements in std::map." << std::endl;
double timeForMaximumSize = timer.currtime();
std::cout << "Time to attempt loading " << maxIterations << " elements on the std::map: "
<< std::fixed << std::setprecision(6) << timeForMaximumSize << " seconds" << std::endl;
}
void testSearchSpeedStdMap(){
std::cout << "Testing Speed of search Size..." << std::endl;
Timer timer;
size_t minKey = 0; // Assuming keys start at 0 for simplicity
size_t maxKey = stdMap.rbegin()->first; // Last element's key
timer.start();
auto minIt = stdMap.find(minKey);
timer.stop();
std::cout << "Time to find minimum key in std::map: "
<< std::fixed << std::setprecision(8) << timer.currtime() << " seconds." << std::endl;
timer.reset();
timer.start();
auto maxIt = stdMap.find(maxKey);
timer.stop();
std::cout << "Time to find maximum key in std::map: "
<< std::fixed << std::setprecision(8) << timer.currtime() << " seconds." << std::endl;
std::cout << "Minimum node SIN: " << minIt->second.sin << std::endl;
std::cout << "Maximum node SIN: " << maxIt->second.sin << std::endl;
}