-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
320 lines (276 loc) · 12.3 KB
/
main.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tblaase <tblaase@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/25 17:32:13 by tblaase #+# #+# */
/* Updated: 2022/05/26 15:31:35 by tblaase ### ########.fr */
/* */
/* ************************************************************************** */
#include <iostream>
#include <cstdlib>
#include <sys/time.h>
// #include <chrono> // can be used instead of sys/time.h, but is not c++98 compliant
#define TESTED_NAMESPACE ft
#ifdef STD
# undef TESTED_NAMESPACE
# define TESTED_NAMESPACE std
#include <vector>
#include <map>
#include <stack>
#else
#include "../include/vector.hpp"
#include "../include/stack.hpp"
#include "../include/map.hpp"
#endif
int main()
{
// # SET_TEST_START #
size_t test_val; // uncomment all the commented lines until '# SET_TEST_END #' to enable custom testing for the vector
// std::cout << "test value: ";
// std::string test_str;
// std::getline(std::cin, test_str);
// if (test_str.length() == 0)
test_val = 50000000;
// else
// test_val = std::atoll(test_str.c_str());
// std::cout << "test value set: " << test_val << std::endl;
// std::cout << "resize value: ";
size_t resize_val;
// std::string resize_str;
// std::getline(std::cin, resize_str);
// if (resize_str.length() == 0)
resize_val = 500000050;
// else
// resize_val = std::atoll(resize_str.c_str());
// std::cout << "resize value set: " << resize_val << std::endl;
// # SET_TEST_END #
struct timeval start, end;
long seconds;
long microseconds;
std::cout << std::endl << "----------------------------------------" << std::endl << std::endl;
{
// ##### Test of TESTED_NAMESPACE::vector #####
#ifndef STD
std::cout << "#####Test of my ft::vector#####" << std::endl;
#else
std::cout << "#####Test of the std::vector#####" << std::endl;
#endif
gettimeofday(&start, NULL);
// std::chrono::steady_clock::time_point end;
// std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
TESTED_NAMESPACE::vector< int > *vct_1;
try
{
std::cout << "\tcreate vct_1 now with size " << test_val << std::endl;
vct_1 = new TESTED_NAMESPACE::vector< int >(test_val);
}
catch (std::exception &e)
{
std::cerr << "\tvct_1 exception caught!!!! " << e.what() << std::endl;
goto next_vct;
}
std::cout << "\tmax_size: " << vct_1->max_size() << std::endl;
std::cout << "\tvct_1 size: " << vct_1->size() << std::endl;
std::cout << "\tvct_1 capacity: " << vct_1->capacity() << std::endl;
try
{
std::cout << "\tresizing vct_1 now to " << resize_val << std::endl;
vct_1->resize(resize_val);
}
catch (std::exception &e)
{
std::cerr << "\tvct_1 exception caught!!!! " << e.what() << std::endl;
}
std::cout << "\tvct_1 size: " << vct_1->size() << std::endl;
std::cout << "\tvct_1 capacity: " << vct_1->capacity() << std::endl;
std::cout << "\tdeleting vct_1 now" << std::endl;
delete vct_1;
vct_1 = NULL;
next_vct:
TESTED_NAMESPACE::vector<int> vct_2;
TESTED_NAMESPACE::vector< int >::iterator it;
std::cout << "\n\tfilling vct_2 now" << std::endl;
for (size_t i = 0; i < test_val * 2; ++i)
{
try
{
vct_2.push_back(i);
}
catch (std::exception &e)
{
std::cerr << "\tvct_2 exception caught!!!! " << e.what() << std::endl;
break ;
}
}
std::cout << "\tvct_2 size: " << vct_2.size() << std::endl;
std::cout << "\tvct_2 capacity: " << vct_2.capacity() << std::endl;
it = vct_2.begin();
std::cout << "\tprinting the first 5 elements of vct_1" << std::endl;
for (size_t i = 0; i < 5 && it != vct_2.end(); ++i)
std::cout << "\telem " << i << ": " << *it++ << std::endl;
std::cout << "\terasing vct_2 now" << std::endl;
vct_2.erase(vct_2.begin(), vct_2.end());;
std::cout << "\tvct_2 size: " << vct_2.size() << std::endl;
std::cout << "\tvct_2 capacity: " << vct_2.capacity() << std::endl;
gettimeofday(&end, NULL);
// end = std::chrono::steady_clock::now();
seconds = (end.tv_sec - start.tv_sec);
microseconds = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
#ifndef STD
std::cout << std::endl << "#####ft::vector: " << (microseconds / 1000000) << "," << (microseconds % 1000000) << " seconds#####" << std::endl;
// std::cout << std::endl << "\tft::vector took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " milliseconds to run." << std::endl;
#else
std::cout << std::endl << "#####std::vector: " << (microseconds / 1000000) << "," << (microseconds % 1000000) << " seconds#####" << std::endl;
// std::cout << std::endl << "\tstd::vector took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " milliseconds to run." << std::endl;
#endif
// # LEAK_CHECK_START # //when checking, make sure fsanitize is disabled in the makefile CXXFLAGS
#ifdef LEAK
#ifndef STD
system("leaks ft_containers | tail -3");
#else
system("leaks std_containers | tail -3");
#endif
#endif
// # LEAK_CHECK_END #
}
std::cout << std::endl << "----------------------------------------" << std::endl << std::endl;
{
// ##### Test of TESTED_NAMESPACE::stack #####
#ifndef STD
std::cout << "#####Test of my ft::stack#####" << std::endl;
#else
std::cout << "#####Test of the std::stack#####" << std::endl;
#endif
gettimeofday(&start, NULL);
// std::chrono::steady_clock::time_point end;
// std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
std::cout << "\tcreate st_1" << std::endl;
TESTED_NAMESPACE::stack<int>st_1;
std::cout << "\tst_1 size: " << st_1.size() << std::endl;
std::cout << "\tfill with " << test_val << " elements" << std::endl;
for (size_t i = 0; i < test_val; i++)
st_1.push(i + 42);
std::cout << "\tst_1 size: " << st_1.size() << std::endl;
std::cout << "\tpopping all elements" << std::endl;
for (size_t i = 0; i < test_val; i++)
st_1.pop();
std::cout << "\tst_1 size: " << st_1.size() << std::endl << std::endl;
std::cout << "\tcreate st_2" << std::endl;
TESTED_NAMESPACE::stack<int> *st_2 = new TESTED_NAMESPACE::stack<int>;
std::cout << "\tst_2 size: " << st_2->size() << std::endl;
std::cout << "\tfill with " << test_val * 2 << "elements" << std::endl;
for (size_t i = 0; i < test_val * 2 ; i++)
st_2->push(i + 42);
std::cout << "\tst_2 size: " << st_2->size() << std::endl;
std::cout << "\tpopping half of the elements" << std::endl;
for (size_t i = 0; i < test_val; i++)
st_2->pop();
std::cout << "\tst_2 size: " << st_2->size() << std::endl;
std::cout << "\tremaining elements should be handled by the destructor" << std::endl;
delete st_2;
st_2 = NULL;
gettimeofday(&end, NULL);
// end = std::chrono::steady_clock::now();
seconds = (end.tv_sec - start.tv_sec);
microseconds = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
#ifndef STD
std::cout << std::endl << "#####ft::stack: " << (microseconds / 1000000) << "," << (microseconds % 1000000) << " seconds#####" << std::endl;
// std::cout << std::endl << "\tft::vector took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " milliseconds to run." << std::endl;
#else
std::cout << std::endl << "#####std::stack: " << (microseconds / 1000000) << "," << (microseconds % 1000000) << " seconds#####" << std::endl;
// std::cout << std::endl << "\tstd::vector took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " milliseconds to run." << std::endl;
#endif
// # LEAK_CHECK_START # //when checking, make sure fsanitize is disabled in the makefile CXXFLAGS
#ifdef LEAK
#ifndef STD
system("leaks ft_containers | tail -3");
#else
system("leaks std_containers | tail -3");
#endif
#endif
// # LEAK_CHECK_END #
}
std::cout << std::endl << "----------------------------------------" << std::endl << std::endl;
{
// ##### Test of TESTED_NAMESPACE::map #####
#ifndef STD
std::cout << "#####Test of my ft::map#####" << std::endl;
#else
std::cout << "#####Test of the std::map#####" << std::endl;
#endif
gettimeofday(&start, NULL);
// std::chrono::steady_clock::time_point end;
// std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
TESTED_NAMESPACE::map<int, int> *map_1 = new TESTED_NAMESPACE::map<int, int>;
std::cout << "\tinserting " << test_val / 100 << " elements into map_1" << std::endl;
for (size_t i = 0; i < test_val / 100; ++i)
{
map_1->insert(TESTED_NAMESPACE::make_pair(i, i + 1));
}
std::cout << "\tmax_size: " << map_1->max_size() << std::endl;
std::cout << "\tmap size: " << map_1->size() << std::endl;
std::cout << "\titerating over map using the iterator" << std::endl;
TESTED_NAMESPACE::map<int, int>::const_iterator it = map_1->begin();
for (size_t i = 0; i < 15 && it != map_1->end(); ++i)
{
std::cout << "\tkey: " << it->first << "\tvalue: " << it->second << std::endl;
++it;
}
std::cout << "\toutput for adding `make_pair(0, 55)`, a duplicate element(this output is a bool): " << map_1->insert(TESTED_NAMESPACE::make_pair(0, 55)).second << std::endl;
std::cout << "\tthe insertion of duplicate elements should not change the value of the key:" << std::endl;
it = map_1->begin();
std::cout << "\tkey: " << it->first << "\tvalue: " << it->second << std::endl;
std::cout << "\tmap size: " << map_1->size() << std::endl;
std::cout << "\tthe deletion of the elements is now up to the destructor" << std::endl;
delete map_1;
map_1 = NULL;
TESTED_NAMESPACE::map<int, int> map_2;
std::cout << "\tinserting " << test_val / 100 << " elements into map_2" << std::endl;
for (size_t i = 0; i < test_val / 100; ++i)
{
map_2.insert(TESTED_NAMESPACE::make_pair(i, i + 1));
}
std::cout << "\tmax_size: " << map_2.max_size() << std::endl;
std::cout << "\tmap size: " << map_2.size() << std::endl;
std::cout << "\titerating over map using the iterator" << std::endl;
TESTED_NAMESPACE::map<int, int>::const_iterator it_2 = map_2.begin();
for (size_t i = 0; i < 15 && it_2 != map_2.end(); ++i)
{
std::cout << "\tkey: " << it_2->first << "\tvalue: " << it_2->second << std::endl;
++it_2;
}
std::cout << "\toutput for adding `make_pair(0, 55)`, a duplicate element(this output is a bool): " << map_2.insert(TESTED_NAMESPACE::make_pair(0, 55)).second << std::endl;
std::cout << "\tthe insertion of duplicate elements should not change the value of the key" << std::endl;
it_2 = map_2.begin();
std::cout << "\tkey: " << it_2->first << "\tvalue: " << it_2->second << std::endl;
std::cout << "\tmap size: " << map_2.size() << std::endl;
std::cout << "\tclearing map_2" << std::endl;
map_2.clear();
std::cout << "\tmap size: " << map_2.size();
gettimeofday(&end, NULL);
// end = std::chrono::steady_clock::now();
seconds = (end.tv_sec - start.tv_sec);
microseconds = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
#ifndef STD
std::cout << std::endl << "#####ft::map: " << (microseconds / 1000000) << "," << (microseconds % 1000000) << " seconds#####" << std::endl;
// std::cout << std::endl << "\tft::vector took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " milliseconds to run." << std::endl;
#else
std::cout << std::endl << "#####std::map: " << (microseconds / 1000000) << "," << (microseconds % 1000000) << " seconds#####" << std::endl;
// std::cout << std::endl << "\tstd::vector took " << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " milliseconds to run." << std::endl;
#endif
// # LEAK_CHECK_START # //when checking, make sure fsanitize is disabled in the makefile CXXFLAGS
#ifdef LEAK
#ifndef STD
system("leaks ft_containers | tail -3");
#else
system("leaks std_containers | tail -3");
#endif
#endif
// # LEAK_CHECK_END #
}
std::cout << std::endl << "----------------------------------------" << std::endl << std::endl;
return (0);
}