-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
330 lines (322 loc) · 11.9 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
321
322
323
324
325
326
327
328
329
330
#include <iostream>
#include <memory>
#include <vector>
#include "containers/vector.hpp"
#include "containers/map.hpp"
#include "containers/stack.hpp"
#include <algorithm>
#include <map>
#include <list>
#include <string>
#include <stack>
#include <time.h>
#include <sys/time.h>
#define NAMESPACE ft // change to STD if you want to compare with the STL
time_t timer() {
struct timeval start = {};
gettimeofday(&start, NULL);
time_t msecs_time = (start.tv_sec * 1000) + (start.tv_usec / 1000);
return msecs_time;
}
void print_time(time_t start)
{
time_t second = timer();
std::cout << "Time = " << second - start << "ms" << std::endl;
}
int main()
{
/* ---------------------------------------------------- */
/* */
/* VECTOR */
/* */
/* ---------------------------------------------------- */
std::cout << "VECTOR" << std::endl << std::endl;
{
time_t start = timer();
std::cout << "~~~~TEST WITH INT~~~~~" << std::endl;
NAMESPACE::vector<int> a;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----CAPACITY----" << std::endl;
std::cout << a.capacity() << std::endl;
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----PUSH_BACK + operator[] + RESERVE----" << std::endl;
int j = 1;
for (int i = 0; i < 30; i++)
{
a.push_back((j += 3) % 2 ? j : -j);
std::cout << "Index " << i << " : value = " << a[i] << std::endl;
}
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----CAPACITY----" << std::endl;
std::cout << a.capacity() << std::endl;
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----RESERVE----" << std::endl;
a.reserve(50);
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----CAPACITY----" << std::endl;
std::cout << a.capacity() << std::endl;
std::cout << "----AT----" << std::endl;
std::cout << a.at(5) << std::endl;
std::cout << a.at(15) << std::endl;
std::cout << "----FRONT----" << std::endl;
std::cout << a.front() << std::endl;
std::cout << "----BACK----" << std::endl;
std::cout << a.back() << std::endl;
std::cout << "----COPY_CONSTRUCTOR----" << std::endl;
NAMESPACE::vector<int> b(a);
std::cout << "----ASSIGN----" << std::endl;
std::cout << "before : size = " << a.size() << " && capacity = " << a.capacity() << std::endl;
a.assign(10, 400);
std::cout << "after : size = " << a.size() << " && capacity = " << a.capacity() << std::endl;
std::cout << "----CLEAR----" << std::endl;
a.clear();
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----OPERATOR=----" << std::endl;
a = b;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----POP_BACK + RESIZE----" << std::endl;
for (int i = 0; i < 20; i++)
{
a.pop_back();
}
for (unsigned int i = 0; i < a.size(); i++)
{
std::cout << "Index " << i << " : value = " << a.at(i) << std::endl;
}
std::cout << "----INSERT + BEGIN + END----" << std::endl;
std::cout << "lets remplace the value of the index 0 by 900" << std::endl;
a.insert(a.begin(), 900);
std::cout << "Index " << 0 << " : value = " << a[0] << std::endl;
std::cout << "lets copy b in the end of a by using insert" << std::endl;
a.insert(a.end(), b.begin(), b.end());
std::cout << "lets add 5 x 200 to the result" << std::endl;
a.insert(a.end(), 5, 200);
for (unsigned int i = 0; i < a.size(); i++)
{
std::cout << "Index " << i << " : value = " << a[i] << std::endl;
}
std::cout << "----ERASE + BEGIN + END----" << std::endl;
std::cout << "lets remove the end element" << std::endl;
a.erase(a.end() - 1);
std::cout << "lets remove the two first elements" << std::endl;
NAMESPACE::vector<int>::iterator it = a.begin();
it = it + 2;
a.erase(a.begin(), it);
for (unsigned int i = 0; i < a.size(); i++)
{
std::cout << "Index " << i << " : value = " << a.at(i) << std::endl;
}
std::cout << "----SWAP----" << std::endl;
a.swap(b);
for (unsigned int i = 0; i < a.size(); i++)
{
std::cout << "Index " << i << " : value = " << a.at(i) << std::endl;
}
std::cout << "----OPERATORS----" << std::endl;
std::cout << "a == b : "<< (a == b) << std::endl;
std::cout << "a != b : "<< (a != b) << std::endl;
std::cout << "a > b : "<< (a > b) << std::endl;
std::cout << "a < b : "<< (a < b) << std::endl;
std::cout << "~~~~MINI TEST WITH CONST CHAR *~~~~" << std::endl;
std::cout << "----CONSTRUCTOR WITH VALUE----" << std::endl;
NAMESPACE::vector<const char *> c(4, "coucou");
std::cout << "----CONSTRUCTOR WITH ITERATORS----" << std::endl;
NAMESPACE::vector<const char *> d(c.begin(), c.end());
for (unsigned int i = 0; i < d.size(); i++)
{
std::cout << "Index " << i << " : value = " << d[i] << std::endl;
}
std::cout << "----TIME----" << std::endl;
print_time(start);
}
/* ---------------------------------------------------- */
/* */
/* STACK */
/* */
/* ---------------------------------------------------- */
std::cout << "STACK" << std::endl << std::endl;
{
time_t start = timer();
NAMESPACE::stack<int> a;
std::cout << "~~~TEST WITH INT :~~~" << std::endl;
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----PUSH----" << std::endl;
for (int i = 0; i < 30; i++)
{
a.push(i * 3);
std::cout << "at index " << i << " : value = " << i * 3 << std::endl;
}
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----TOP----" << std::endl;
std::cout << a.top() << std::endl;
std::cout << "----POP----" << std::endl;
for (int i = 0; i < 5; i++)
{
a.pop();
}
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----TOP----" << std::endl;
std::cout << a.top() << std::endl;
NAMESPACE::stack<float> b;
std::cout << std::endl;
std::cout << "~~~TEST WITH FLOAT :~~~" << std::endl;
std::cout << "----EMPTY----" << std::endl;
std::cout << b.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << b.size() << std::endl;
std::cout << "----PUSH----" << std::endl;
float f = -0.5f;
for (int i = 0; i < 30; i++)
{
b.push(f);
std::cout << "at index " << i << " : value = " << f << std::endl;
f += 0.6f;
}
std::cout << "----EMPTY----" << std::endl;
std::cout << b.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << b.size() << std::endl;
std::cout << "----TOP----" << std::endl;
std::cout << b.top() << std::endl;
std::cout << "----POP----" << std::endl;
for (int i = 0; i < 5; i++)
{
b.pop();
}
std::cout << "----SIZE----" << std::endl;
std::cout << b.size() << std::endl;
std::cout << "----TOP----" << std::endl;
std::cout << b.top() << std::endl;
std::cout << "----TIME----" << std::endl;
print_time(start);
}
/* ---------------------------------------------------- */
/* */
/* MAP */
/* */
/* ---------------------------------------------------- */
std::cout << "MAP" << std::endl << std::endl;
{
time_t start = timer();
std::cout << "----DEFAULT CONSTRUCTOR----" << std::endl;
NAMESPACE::map<int, float> a;
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----OPERATOR[] + INSERT----" << std::endl;
for (int i = 0; i < 5; i++)
{
std::cout << a[i] << std::endl;
}
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----OPERATOR[] + INSERT----" << std::endl;
a.insert(NAMESPACE::make_pair(6, 0.35f));
std::cout << "at the index 6, there is the value " << a[6] << std::endl;
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----COPY CONSTRUCTOR----" << std::endl;
NAMESPACE::map<int, float> b(a);
std::cout << "----CLEAR----" << std::endl;
a.clear();
std::cout << "----EMPTY----" << std::endl;
std::cout << a.empty() << std::endl;
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----SWAP----" << std::endl;
a.swap(b);
std::cout << "----SIZE----" << std::endl;
std::cout << a.size() << std::endl;
std::cout << "----COUNT----" << std::endl;
std::cout << "is 6th index existing ? " << a.count(6) << std::endl;
std::cout << "is 98th index existing ? " << a.count(98) << std::endl;
std::cout << "----CLEAR----" << std::endl;
a.clear();
std::cout << "----INSERT----" << std::endl;
float f = 1.0f;
int ran;
srand(8500);
for (int i = 0; i < 20; i++)
{
ran = rand() % 10000;
a.insert(a.begin(), NAMESPACE::make_pair(ran, f));
f += 1.5f;
std::cout << "at index " << ran << " : value = " << a[ran] << std::endl;
}
std::cout << "----CONSTRUCTOR WITH ITERATORS + BEGIN + END----" << std::endl;
NAMESPACE::map<int, float> c(a.begin(), a.end());
NAMESPACE::map<int, float>::iterator it = c.begin();
for (unsigned int i = 0; i < c.size(); i++)
{
std::cout << "at index " << it->first << " : value = " << c[it->first] << std::endl;
it++;
}
std::cout << "----CLEAR----" << std::endl;
a.clear();
std::cout << "----INSERT----" << std::endl;
a.insert(c.begin(), c.end());
it = a.begin();
for (unsigned int i = 0; i < a.size(); i++)
{
std::cout << "at index " << it->first << " : value = " << a[it->first] << std::endl;
it++;
}
std::cout << "----ERASE + FIND----" << std::endl;
std::cout << "lets erase first element" << std::endl;
a.erase(a.begin());
std::cout << "lets erase element 6706" << std::endl;
a.erase(6706);
std::cout << "lets erase element 9445 to end" << std::endl;
a.erase(a.find(9445), a.end());
it = a.begin();
for (unsigned int i = 0; i < a.size(); i++)
{
std::cout << "at index " << it->first << " : value = " << a[it->first] << std::endl;
it++;
}
std::cout << "----UPPER_BOUND----" << std::endl;
it = a.upper_bound(8429);
std::cout << "at index " << it->first << " : value = " << a[it->first] << std::endl;
std::cout << "----LOWER_BOUND----" << std::endl;
it = a.lower_bound(4427);
std::cout << "at index " << it->first << " : value = " << a[it->first] << std::endl;
std::cout << "----EQUAL_RANGE----" << std::endl;
NAMESPACE::pair<NAMESPACE::map<int, float>::iterator, NAMESPACE::map<int, float>::iterator> eq = a.equal_range(4427);
std::cout << "lower_bound at index " << eq.first->first << " : value = " << a[eq.first->first] << std::endl;
std::cout << "upper_bound at index " << eq.second->first << " : value = " << a[eq.second->first] << std::endl;
std::cout << "----RBEGIN + REND----" << std::endl;
NAMESPACE::map<int, float>::reverse_iterator rit = a.rbegin(), rite = a.rend();
while (rit != rite)
{
std::cout << "at index " << rit->first << " : value = " << a[rit->first] << std::endl;
rit++;
}
std::cout << "----OPERATORS----" << std::endl;
std::cout << "a == c : "<< (a == c) << std::endl;
std::cout << "a != c : "<< (a != c) << std::endl;
std::cout << "a >= c : "<< (a >= c) << std::endl;
std::cout << "a < c : "<< (a < c) << std::endl;
std::cout << "----TIME----" << std::endl;
print_time(start);
}
system("leaks ft_containers");
}