Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
805 lines (737 loc) · 26.8 KB
/
Copy pathmain.cpp
File metadata and controls
805 lines (737 loc) · 26.8 KB
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.cpp :+: :+: */
/* +:+ */
/* By: rpet <marvin@codam.nl> +#+ */
/* +#+ */
/* Created: 2021/07/22 13:58:38 by rpet #+# #+# */
/* Updated: 2021/08/31 09:04:44 by rpet ######## odam.nl */
/* */
/* ************************************************************************** */
#include <iostream>
#include <fstream>
#include <string>
#include <deque>
#include <list>
#include <cstdlib>
#include <ctime>
#include <cstddef>
#include <exception>
#ifdef USE_STD
#include <map>
#include <stack>
#include <vector>
#define OFILE_NAME "std_times.txt"
#define LEAKS_CONTAINER "leaks std_containers"
namespace ft = std;
#else
#include "map.hpp"
#include "stack.hpp"
#include "vector.hpp"
#define OFILE_NAME "ft_times.txt"
#define LEAKS_CONTAINER "leaks ft_containers"
#endif
// Class where all the calculating times are noted in a txt file
class TimeLog
{
public:
TimeLog() : _currentTime(clock()), _newTime(0), _totalTime(0)
{
std::string name;
this->_outputFile.open(OFILE_NAME);
{
name = "====== FT TIMES: ======";
if (OFILE_NAME == "std_times.txt")
name = "====== STD TIMES: ======";
this->_outputFile << name << std::endl;
}
}
void calculateTime(std::string testName)
{
this->_newTime = clock();
this->_outputFile << testName << this->_newTime - this->_currentTime << std::endl;
this->_totalTime += this->_newTime - this->_currentTime;
this->_currentTime = this->_newTime;
std::cout << std::endl << "==============================================" << std::endl;
}
void totalTime()
{
this->_outputFile << "Total time elapsed: " << this->_totalTime << std::endl << std::endl;
}
private:
clock_t _currentTime;
clock_t _newTime;
clock_t _totalTime;
std::ofstream _outputFile;
};
// Check for leaks
void checkLeaks()
{
system(LEAKS_CONTAINER);
}
// Print all the content in a stack
template <typename T>
void printStack(ft::stack<T> &stack)
{
for (ft::stack<T> dump = stack; !stack.empty(); stack.pop())
std::cout << stack.top() << std::endl;
}
// Print all the content in a vector
template <typename T>
void printVector(ft::vector<T> &vector)
{
typename ft::vector<T>::iterator cur = vector.begin();
std::cout << "Current vector has a size of: " << vector.size() <<
" and a capacity of: " << vector.capacity() << std::endl;
for (size_t i = 0; cur != vector.end(); i++, cur++)
std::cout << "Index [" << i << "]: " << *cur << std::endl;
}
// Print the size and capacity of a vector
template <typename T>
void printVectorCapacity(ft::vector<T> &vector)
{
std::cout << "Current vector has a size of: " << vector.size() <<
" and a capacity of: " << vector.capacity() << std::endl;
}
template <typename T1, typename T2>
void printMap(ft::map<T1, T2> &map)
{
std::cout << "Map has a size of: " << map.size() << std::endl;
for (typename ft::map<T1, T2>::iterator cur = map.begin(); cur != map.end(); cur++)
std::cout << "Map key content: " << cur->first << " & mapped content: " <<
cur->second << std::endl;
}
// VECTOR CONSTRUCTORS
void vectorTest_constructors()
{
std::cout << "\t======== Vector constructors ========" << std::endl << std::endl;
std::cout << "Default constructor called." << std::endl;
ft::vector<int> defaultConstructor;
std::cout << "Fill constructor called." << std::endl;
ft::vector<int> fillConstructor(5, rand());
std::cout << "Range constructor called." << std::endl;
ft::vector<int> rangeConstructor(fillConstructor.begin(), fillConstructor.end());
std::cout << "Copy constructor called." << std::endl;
ft::vector<int> copyConstructor(fillConstructor);
std::cout << "Default constructor:" << std::endl;
printVector(defaultConstructor);
std::cout << "Fill constructor:" << std::endl;
printVector(fillConstructor);
std::cout << "Range constructor:" << std::endl;
printVector(rangeConstructor);
std::cout << "Copy constructor:" << std::endl;
printVector(copyConstructor);
}
// VECTOR ITERATOR FUNCTIONS
void vectorTest_iteratorFunctions()
{
std::cout << "\t======== Iterator functions ========" << std::endl << std::endl;
ft::vector<int> vec;
ft::vector<int>::iterator it;
ft::vector<int>::const_iterator cit;
ft::vector<int>::reverse_iterator rit;
ft::vector<int>::const_reverse_iterator crit;
for (size_t i = 0; i < 15; i++)
vec.push_back(rand());
it = vec.begin();
cit = vec.begin();
rit = vec.rbegin();
crit = vec.rbegin();
std::cout << "Begin: " << *it << std::endl;
std::cout << "Const begin: " << *cit << std::endl;
std::cout << "Reverse begin: " << *rit << std::endl;
std::cout << "Const reverse begin: " << *crit << std::endl;
it = vec.end() - 1;
cit = vec.end() - 1;
rit = vec.rend() - 1;
crit = vec.rend() - 1;
std::cout << "End: " << *it << std::endl;
std::cout << "Const end: " << *cit << std::endl;
std::cout << "Reverse end: " << *rit << std::endl;
std::cout << "Const reverse end: " << *crit << std::endl;
printVector(vec);
}
// VECTOR ITERATOR OPERATIONS
void vectorTest_iteratorOperators()
{
std::cout << "\t===== Iterator operators =====" << std::endl;
ft::vector<std::string> vec;
ft::vector<std::string>::iterator it;
ft::vector<std::string>::const_iterator cit;
ft::vector<std::string>::reverse_iterator rit;
ft::vector<std::string>::const_reverse_iterator crit;
vec.push_back("Remco");
vec.push_back("Mark");
vec.push_back("Jonas");
vec.push_back("Lars");
vec.push_back("Ksenia");
vec.push_back("Laura");
it = vec.begin();
cit = vec.begin();
rit = vec.rend() - 1;
crit = vec.rend() - 1;
// *it
std::cout << "*it: " << *it << " | *cit: " << *cit << " | *rit: " << *rit << " | *crit: " << *crit << std::endl;
// it->
std::cout << "->it: " << it->append("->op1") << " | ->cit: " << cit->data() << " | ->rit: " << rit->append("->op2") << " | ->crit: " << crit->data() << std::endl;
// ++it & --rit
std::cout << "++it: " << *++it << " | ++cit: " << *++cit << " | --rit: " << *--rit << " | --crit: " << *--crit << std::endl;
// it++ & rit--
std::cout << "it++: " << *it++ << " | cit++: " << *cit++ << " | rit--: " << *rit-- << " | crit--: " << *crit-- << std::endl;
// --it & ++rit
std::cout << "--it: " << *--it << " | --cit: " << *--cit << " | ++rit: " << *++rit << " | ++crit: " << *++crit << std::endl;
// it-- & rit++
std::cout << "it--: " << *it-- << " | cit--: " << *cit-- << " | rit++: " << *rit++ << " | crit++: " << *crit++ << std::endl;
std::cout << *it << *rit << std::endl;
// it + n & rit - n
it = it + 3;
rit = rit - 3;
std::cout << "it + n: " << *it << " | rit - n: " << *rit << std::endl;
// n + it & n + rit
it = 1 + it;
rit = 1 + rit;
std::cout << "n + it: " << *it << " | n + rit: " << *rit << std::endl;
// it - n & rit + n
it = it - 2;
rit = rit + 2;
std::cout << "it - n: " << *it << " | rit + n: " << *rit << std::endl;
// it - cit & rit - crit
std::cout << "it - cit: " << it - cit << " | rit - crit: " << rit - crit << std::endl;
// it < cit
std::cout << "it < cit: " << (it < cit) << std::endl;
// it > cit
std::cout << "it > cit: " << (it > cit) << std::endl;
// it <= cit
std::cout << "it <= cit: " << (it <= cit) << std::endl;
// it >= cit
std::cout << "it >= cit: " << (it >= cit) << std::endl;
// it += n
it += 1;
rit -= 1;
std::cout << "it += n: " << *it << " | rit -= n: " << *rit << std::endl;
// it -= n
it -= 1;
rit += 1;
std::cout << "it -= n: " << *it << " | rit += n: " << *rit << std::endl;
// it[n]
std::cout << "it[n]: " << it[2] << " | cit[n]: " << cit[2] << " | rit[n]: " << rit[-2] << " | crit[n]: " << crit[-2] << std::endl;
printVector(vec);
}
// VECTOR CAPACITY FUNCTIONS
void vectorTest_capacityFunctions()
{
std::cout << "\t===== Capacity functions =====" << std::endl << std::endl;
ft::vector<int> vec;
for (size_t i = 0; i < 15; i++)
vec.push_back(rand());
std::cout << "Size: " << vec.size() << std::endl;
std::cout << "Resize:" << std::endl;
vec.resize(1);
printVectorCapacity(vec);
vec.resize(33);
printVectorCapacity(vec);
vec.resize(38);
printVectorCapacity(vec);
vec.resize(4);
printVectorCapacity(vec);
vec.resize(89);
printVectorCapacity(vec);
std::cout << "Reserve:" << std::endl;
vec.reserve(7);
printVectorCapacity(vec);
vec.reserve(139);
printVectorCapacity(vec);
vec.reserve(147);
printVectorCapacity(vec);
std::cout << "Empty before clear: " << vec.empty() << std::endl;
vec.clear();
std::cout << "Empty after clear: " << vec.empty() << std::endl;
printVector(vec);
}
// VECTOR ELEMENT ACCESS FUCNTIONS
void vectorTest_elementFunctions()
{
std::cout << "\t===== Element access functions =====" << std::endl << std::endl;
ft::vector<int> vec;
for (size_t i = 0; i < 15; i++)
vec.push_back(rand());
std::cout << "Operator[] (subscript):" << std::endl;
std::cout << "Vector[5]: " << vec[5] << std::endl;
if (1337 < vec.size())
std::cout << "Vector[1337]: " << vec[1337] << std::endl;
try
{
std::cout << "At(5): " << vec.at(5) << std::endl;
}
catch (std::exception &e)
{
std::cout << "Exception error" << std::endl;
}
try
{
std::cout << "At(1337): " << vec.at(1337) << std::endl;
}
catch (std::exception &e)
{
std::cout << "Exception error" << std::endl;
}
std::cout << "Front: " << vec.front() << std::endl;
std::cout << "Back: " << vec.back() << std::endl;
printVector(vec);
}
// VECTOR MODIFIER FUNCTIONS
void vectorTest_modifierFunctions()
{
std::cout << "\t===== Modifier functions =====" << std::endl << std::endl;
ft::vector<int> vec1, vec2;
ft::vector<int>::iterator it;
for (size_t i = 0; i < 15; i++)
vec1.push_back(rand());
for (size_t i = 0; i < 35; i++)
vec2.push_back(rand());
it = vec1.begin() + 4;
std::cout << "Assign:" << std::endl;
vec1.assign(it, it + 5);
printVector(vec1);
it = vec2.begin();
vec1.assign(it, it + 33);
printVector(vec1);
vec1.assign(it, it);
printVector(vec1);
vec1.assign(30, 25);
printVector(vec1);
std::cout << "Insert:" << std::endl;
it = vec1.begin() + 6;
it = vec1.insert(it, 1337);
std::cout << "Return value insert single element: " << *it << std::endl;
vec1.insert(it + 20, 5, 420);
it = vec2.begin() + 17;
vec1.insert(vec1.begin() + 10, it, it + 4);
std::cout << "Erase:" << std::endl;
printVector(vec1);
it = vec1.erase(vec1.begin() + 11);
std::cout << "Return value erase single element: " << *it << std::endl;
it = vec1.erase(vec1.begin() + 11, vec1.begin() + 31);
std::cout << "Return value erase range: " << *it << std::endl;
printVector(vec1);
printVector(vec2);
std::cout << "Swap:" << std::endl;
it = vec1.begin() + 5;
std::cout << "Iterator value before swap: " << *it << std::endl;
vec1.swap(vec2);
std::cout << "Iterator value after swap: " << *it << std::endl;
printVector(vec1);
printVector(vec2);
std::cout << "Clear:" << std::endl;
vec1.clear();
// Stress test
std::cout << "Performing the stress test.." << std::endl;
ft::vector<int> stressVec;
ft::vector<int>::iterator stressIt;
for (size_t i = 0; i < 50000; i++)
stressVec.insert(stressVec.begin() + i, 1, i * 3);
printVector(stressVec);
stressVec.clear();
vec1.clear();
}
// VECTOR NON-MEMBER FUNCTIONS
void vectorTest_nonMemberFunctions()
{
std::cout << "\t===== Non-member functions =====" << std::endl << std::endl;
ft::vector<int> vec1, vec2;
std::string result;
for (size_t i = 0; i < 8; i++)
vec1.push_back(rand());
std::cout << "\tRandom numbers vector (vec1) and an empty vector (vec2)" << std::endl;
std::cout << "vec1 == vec2: " << std::boolalpha << (vec1 == vec2) << std::endl;
std::cout << "vec1 != vec2: " << std::boolalpha << (vec1 != vec2) << std::endl;
std::cout << "vec1 <= vec2: " << std::boolalpha << (vec1 <= vec2) << std::endl;
std::cout << "vec1 >= vec2: " << std::boolalpha << (vec1 >= vec2) << std::endl;
std::cout << std::endl << "\tExecuting vec2 = vec1" << std::endl;
vec2 = vec1;
std::cout << "vec1 == vec2: " << std::boolalpha << (vec1 == vec2) << std::endl;
std::cout << "vec1 != vec2: " << std::boolalpha << (vec1 != vec2) << std::endl;
std::cout << "vec1 <= vec2: " << std::boolalpha << (vec1 <= vec2) << std::endl;
std::cout << "vec1 >= vec2: " << std::boolalpha << (vec1 >= vec2) << std::endl;
std::cout << std::endl << "\tExecuting a push_back for vec1" << std::endl;
vec1.push_back(1337);
std::cout << "vec1 < vec2: " << std::boolalpha << (vec1 < vec2) << std::endl;
std::cout << "vec1 > vec2: " << std::boolalpha << (vec1 > vec2) << std::endl;
std::cout << std::endl << "\tChanging the first index without changing the size" << std::endl;
vec1[0] = 1;
vec2[0] = 2;
std::cout << "vec1 < vec2: " << std::boolalpha << (vec1 < vec2) << std::endl;
std::cout << "vec1 > vec2: " << std::boolalpha << (vec1 > vec2) << std::endl;
std::cout << std::endl << "Swap:" << std::endl;
printVector(vec1);
printVector(vec2);
ft::swap(vec1, vec2);
printVector(vec1);
printVector(vec2);
}
// STACK TESTS
void stackTests()
{
std::cout << "\t======== Stack tests ========" << std::endl << std::endl;
std::deque<int> deque(5, 69);
std::list<int> list(6, 42);
ft::vector<int> vector(7, 1337);
std::cout << "Default constructor called." << std::endl;
ft::stack<int> stack1;
std::cout << "Copy constructor called." << std::endl;
ft::stack<int> stack2 = stack1;
std::cout << "Assign operator called." << std::endl;
ft::stack<int> stack3 = stack1;
std::cout << "Copy constructor deque called." << std::endl;
ft::stack<int> copyConstructorDeque(deque);
std::cout << "Copy constructor list called." << std::endl;
ft::stack<int, std::list<int> > copyConstructorList(list);
std::cout << "Copy constructor vector called." << std::endl;
ft::stack<int, ft::vector<int> > copyConstructorVector(vector);
std::cout << "Empty: " << std::boolalpha << stack1.empty() << std::endl;
std::cout << "Empty: " << std::boolalpha << copyConstructorDeque.empty() << std::endl;
std::cout << "Size: " << copyConstructorList.size() << std::endl;
std::cout << "Top: " << copyConstructorDeque.top() << std::endl;
std::cout << "Calling push..." << std::endl;
copyConstructorDeque.push(99999);
std::cout << "Top: " << copyConstructorDeque.top() << std::endl;
std::cout << "Calling pop..." << std::endl;
copyConstructorDeque.pop();
std::cout << "Top: " << copyConstructorDeque.top() << std::endl;
std::cout << "stack1 == stack2: " << std::boolalpha << (stack1 == stack2) << std::endl;
std::cout << "stack1 != stack2: " << std::boolalpha << (stack1 != stack2) << std::endl;
std::cout << "stack1 < stack2: " << std::boolalpha << (stack1 < stack2) << std::endl;
std::cout << "stack1 <= stack2: " << std::boolalpha << (stack1 <= stack2) << std::endl;
std::cout << "stack1 > stack2: " << std::boolalpha << (stack1 > stack2) << std::endl;
std::cout << "stack1 >= stack2: " << std::boolalpha << (stack1 >= stack2) << std::endl;
}
// MAP CONSTRUCTORS
void mapTest_constructors()
{
std::cout << "\t======== Map constructors ========" << std::endl << std::endl;
std::cout << "Default constructor called." << std::endl;
ft::map<char, int> defaultConstructor;
for (size_t i = 0; i < 12; i++)
defaultConstructor['a' + rand() % 26] = rand();
std::cout << "Range constructor called." << std::endl;
ft::map<char, int> rangeConstructor(defaultConstructor.begin(), defaultConstructor.end());
std::cout << "Copy constructor called." << std::endl;
ft::map<char, int> copyConstructor(rangeConstructor);
}
// MAP ITERATOR FUNCTIONS
void mapTest_iteratorFunctions()
{
std::cout << "\t======== Iterator functions ========" << std::endl << std::endl;
ft::map<char, int> map;
ft::map<char, int>::iterator it;
ft::map<char, int>::const_iterator cit;
ft::map<char, int>::reverse_iterator rit;
ft::map<char, int>::const_reverse_iterator crit;
for (size_t i = 0; i < 15; i++)
map['a' + rand() % 26] = rand();
it = map.begin();
cit = map.begin();
rit = map.rbegin();
crit = map.rbegin();
std::cout << "Begin: " << it->first << std::endl;
std::cout << "Const begin: " << cit->first << std::endl;
std::cout << "Reverse begin: " << rit->first << std::endl;
std::cout << "Const reverse begin: " << crit->first << std::endl;
it = --map.end()--;
cit = --map.end()--;
rit = --map.rend()--;
crit = --map.rend()--;
std::cout << "End: [" << it->first << "]" << std::endl;
std::cout << "Const end: [" << cit->first << "]" << std::endl;
std::cout << "Reverse end: [" << rit->first << "]" << std::endl;
std::cout << "Const reverse end: [" << crit->first << "]" << std::endl;
printMap(map);
}
// MAP ITERATOR OPERATORS
void mapTest_iteratorOperators()
{
std::cout << "\t======== Iterator operators ========" << std::endl << std::endl;
ft::map<char, int> map;
ft::map<char, int>::iterator it;
ft::map<char, int>::const_iterator cit;
ft::map<char, int>::reverse_iterator rit;
ft::map<char, int>::const_reverse_iterator crit;
ft::map<char, int>::iterator itCopy(it);
for (size_t i = 0; i < 15; i++)
map['a' + rand() % 26] = rand();
it = map.begin();
cit = map.begin();
rit = --map.rend()--;
crit = --map.rend()--;
// *it
std::cout << "*it|*cit|*rit|*crit: " << (*it).first << (*cit).first << (*rit).first << (*crit).first << std::endl;
// it->
std::cout << "it->|cit->|rit->|crit->: " << it->first << cit->first << rit->first << crit->first << std::endl;
// ++it & --rit
std::cout << "++it|++cit|--rit|--crit: " << (++it)->first << (++cit)->first << (--rit)->first << (--crit)->first << std::endl;
// it++ & rit--
std::cout << "it++|cit++|rit--|crit--: " << (it++)->first << (cit++)->first << (rit--)->first << (crit--)->first << std::endl;
// == & != & =
std::cout << (itCopy == it) << std::endl;
std::cout << (itCopy != it) << std::endl;
itCopy = it;
std::cout << (itCopy == it) << std::endl;
std::cout << (itCopy != it) << std::endl;
}
// MAP CAPACITY FUNCTIONS
void mapTest_capacityFunctions()
{
std::cout << "\t======== Capacity functions ========" << std::endl << std::endl;
ft::map<char, int> map;
for (size_t i = 0; i < 15; i++)
map['a' + rand() % 26] = rand();
std::cout << "Size" << map.size() << std::endl;
std::cout << "Empty before clear: " << map.empty() << std::endl;
map.clear();
std::cout << "Empty after clear: " << map.empty() << std::endl;
}
// MAP ELEMENT ACCESS FUNCTIONS
void mapTest_elementFunctions()
{
std::cout << "\t======== Element access functions ========" << std::endl << std::endl;
ft::map<char, int> map;
std::cout << (map['a'] = 1337) << std::endl;
std::cout << (map['b'] = 69) << std::endl;
std::cout << (map['c'] = 42) << std::endl;
std::cout << (map['d'] = 9000) << std::endl;
std::cout << (map['a'] = 5050) << std::endl;
std::cout << map['e'] << std::endl;
std::cout << (map['e'] = 242)<< std::endl;
}
// MAP MODIFIER FUNCTIONS
void mapTest_modifierFunctions()
{
std::cout << "\t======== Modifier functions ========" << std::endl << std::endl;
ft::map<char, int> map, map2;
ft::map<int, int> stressMap;
ft::map<char, int>::iterator it, it2;
// Insert a pair
for (size_t i = 0; i < 8; i++)
map.insert(ft::make_pair('a' + rand() % 26, rand()));
it = map.begin();
// Insert a pair on a certain 'position'
for (size_t i = 0; i < 8; i++)
{
it = map.insert(it, ft::make_pair('a' + rand() % 26, rand()));
std::cout << "it: " << it->first << std::endl;
}
// Insert a range in a new map
map2.insert(map.begin(), map.end());
printMap(map2);
it = map.begin();
it++;
it++;
// Erases a node and tests if the iterator still points to the correct node.
std::cout << "Iterator points to before erase: " << it->first << std::endl;
map.erase(map.begin());
std::cout << "Iterator points to after erase: " << it->first << std::endl;
// Erases a given key
char eraseKey = it->first;
map.erase(eraseKey);
// Tries to erase a non-existing key
map.erase('.');
printMap(map);
map.clear();
printMap(map);
// Stress test
for (size_t i = 0; i < 500000; i++)
stressMap.insert(ft::make_pair(i, rand()));
stressMap.clear();
for (size_t i = 0; i < 37; i++)
map.insert(ft::make_pair('a' + rand() % 26, rand()));
for (size_t i = 0; i < 10; i++)
map2.insert(ft::make_pair('a' + rand() % 26, rand()));
std::cout << "Before swap" << std::endl;
printMap(map);
printMap(map2);
map.swap(map2);
std::cout << "After swap" << std::endl;
printMap(map);
printMap(map2);
}
// MAP OPERATION FUNCTIONS
void mapTest_operationFunctions()
{
std::cout << "\t======== Operation functions ========" << std::endl << std::endl;
ft::map<char, int> map;
ft::map<char, int>::iterator it1, it2;
ft::map<char, int>::const_iterator cit1, cit2;
for (size_t i = 0; i < 15; i++)
map['a' + i] = rand();
printMap(map);
// Find
it1 = map.find('h');
it2 = map.find('x');
cit1 = map.find('h');
cit2 = map.find('x');
if (map.end() == it1)
std::cout << "Didn't find the key in it1" << std::endl;
else
std::cout << "Key found in it1: " << it1->first << std::endl;
if (map.end() == cit1)
std::cout << "Didn't find the key in cit1"<< std::endl;
else
std::cout << "Key found in cit1: " << cit1->first << std::endl;
if (map.end() == it2)
std::cout << "Didn't find the key in it2" << std::endl;
else
std::cout << "Key found in it2: " << it2->first << std::endl;
if (map.end() == cit2)
std::cout << "Didn't find the key in cit2"<< std::endl;
else
std::cout << "Key found in cit2: " << cit2->first << std::endl;
// Count
std::cout << "Count: " << map.count('h') << std::endl;
std::cout << "Count: " << map.count('x') << std::endl;
std::cout << "Count: " << map.count('o') << std::endl;
std::cout << "Count: " << map.count('p') << std::endl;
// Lower bound
ft::map<char, int> boundMap;
for (size_t i = 0; i < 25; i++)
boundMap['a' + rand() % 26] = rand();
printMap(boundMap);
for (size_t i = 0; i < 26; i++)
{
if (boundMap.lower_bound('a' + i) != boundMap.end())
std::cout << "Lower bound key: " << boundMap.lower_bound('a' + i)->first << std::endl;
}
std::cout << std::endl;
// Upper bound
for (size_t i = 0; i < 26; i++)
{
if (boundMap.upper_bound('a' + i) != boundMap.end())
std::cout << "Upper bound key: " << boundMap.upper_bound('a' + i)->first << std::endl;
}
std::cout << std::endl;
// Equal range
ft::pair<ft::map<char,int>::iterator, ft::map<char,int>::iterator> equalRange;
for (size_t i = 0; i < 26; i++)
{
equalRange = boundMap.equal_range('a' + i);
if (equalRange.first != boundMap.end() && equalRange.second != boundMap.end())
std::cout << "Equal range lower bound: " << equalRange.first->first <<
" & upper bound: " << equalRange.second->first << std::endl;
}
}
// MAP NON-MEMBER FUNCTIONS
void mapTest_nonMemberFunctions()
{
std::cout << "\t===== Non-member functions =====" << std::endl << std::endl;
ft::map<int, int> m1, m2;
m1[3] = 4;
m2[4] = 3;
std::cout << "< " << (m1 < m2) << std::endl;
std::cout << "< " << (m2 < m1) << std::endl;
m2[1] = 5;
std::cout << "< " << (m1 < m2) << std::endl;
m1[1] = 5;
m1[4] = 3;
m2[3] = 4;
std::cout << "Print map1" << std::endl;
printMap(m1);
std::cout << "Print map2" << std::endl;
printMap(m2);
std::cout << "== " << (m1 == m2) << std::endl;
std::cout << "!= " << (m1 != m2) << std::endl;
std::cout << "< " << (m1 < m2) << std::endl;
std::cout << "<= " << (m1 <= m2) << std::endl;
std::cout << "> " << (m1 > m2) << std::endl;
std::cout << ">= " << (m1 >= m2) << std::endl;
m2[3] = 3;
std::cout << "== " << (m1 == m2) << std::endl;
std::cout << "!= " << (m1 != m2) << std::endl;
std::cout << "< " << (m1 < m2) << std::endl;
std::cout << "<= " << (m1 <= m2) << std::endl;
std::cout << "> " << (m1 > m2) << std::endl;
std::cout << ">= " << (m1 >= m2) << std::endl;
}
void test_isIntegral()
{
if (ft::is_integral<bool>::value)
std::cout << "Bool is an integral type" << std::endl;
if (ft::is_integral<char>::value)
std::cout << "Char is an integral type" << std::endl;
if (ft::is_integral<char16_t>::value)
std::cout << "Char16_t is an integral type" << std::endl;
if (ft::is_integral<char32_t>::value)
std::cout << "Char32_t is an integral type" << std::endl;
if (ft::is_integral<wchar_t>::value)
std::cout << "Wchar_t is an integral type" << std::endl;
if (ft::is_integral<signed char>::value)
std::cout << "Signed char is an integral type" << std::endl;
if (ft::is_integral<short int>::value)
std::cout << "Short int is an integral type" << std::endl;
if (ft::is_integral<int>::value)
std::cout << "Int is an integral type" << std::endl;
if (ft::is_integral<long int>::value)
std::cout << "Long int is an integral type" << std::endl;
if (ft::is_integral<unsigned char>::value)
std::cout << "Unsigned char is an integral type" << std::endl;
if (ft::is_integral<unsigned short int>::value)
std::cout << "Unsigned short int is an integral type" << std::endl;
if (ft::is_integral<unsigned int>::value)
std::cout << "Unsigned int is an integral type" << std::endl;
if (ft::is_integral<unsigned long int>::value)
std::cout << "Unsigned long int is an integral type" << std::endl;
if (ft::is_integral<float>::value)
std::cout << "Float is an integral type" << std::endl;
else
std::cout << "Float is not an integral type" << std::endl;
if (ft::is_integral<double>::value)
std::cout << "Double is an integral type" << std::endl;
else
std::cout << "Double is not an integral type" << std::endl;
}
int main()
{
// Turn on or off to check for leaks
//atexit(checkLeaks);
TimeLog time;
// Vector tests
vectorTest_constructors();
time.calculateTime("Vector constructors: ");
vectorTest_iteratorFunctions();
time.calculateTime("Vector iterator functions: ");
vectorTest_iteratorOperators();
time.calculateTime("Vector iterator operators: ");
vectorTest_capacityFunctions();
time.calculateTime("Vector capcity functions: ");
vectorTest_elementFunctions();
time.calculateTime("Vector element access functions: ");
vectorTest_modifierFunctions();
time.calculateTime("Vector modifier functions: ");
vectorTest_nonMemberFunctions();
time.calculateTime("Vector non-member functions: ");
// Stack tests
stackTests();
time.calculateTime("Stack tests: ");
// Map tests
mapTest_constructors();
time.calculateTime("Map constructors: ");
mapTest_iteratorFunctions();
time.calculateTime("Map iterator functions: ");
mapTest_iteratorOperators();
time.calculateTime("Map iterator operators: ");
mapTest_capacityFunctions();
time.calculateTime("Map capacity functions: ");
mapTest_elementFunctions();
time.calculateTime("Map element access functions: ");
mapTest_modifierFunctions();
time.calculateTime("Map modifier functions: ");
mapTest_operationFunctions();
time.calculateTime("Map operation functions: ");
mapTest_nonMemberFunctions();
time.calculateTime("Map non-member functions: ");
// Is integral
test_isIntegral();
time.calculateTime("Is integral tests: ");
// Total time
time.totalTime();
return (0);
}