-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathmain.cpp
130 lines (115 loc) · 3.02 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
#include <functional>
#include <iostream>
#include <memory>
#include <vector>
#include "stack.hpp"
#include "utils/test.hpp"
static std::vector<void*> allocations_;
void* operator new[](std::size_t size) {
auto memory = malloc(size);
allocations_.push_back(memory);
return memory;
}
void operator delete[](void *memory) noexcept {
allocations_.erase(std::remove(allocations_.begin(), allocations_.end(), memory), allocations_.end());
free(memory);
}
int main() {
std::cout << "\n\n"
<< "*******************************************************\n"
<< "* Running unit tests... *\n"
<< "*******************************************************\n";
test(" -> Stack functionality", []() {
Stack<int> stack(3);
try {
stack.push(1);
stack.push(2);
stack.push(3);
} catch (std::out_of_range) {
return false;
}
if (stack.pop() != 3) {
return false;
}
if (stack.pop() != 2) {
return false;
}
if (stack.pop() != 1) {
return false;
}
return true;
});
test(" -> Push to full stack with move semantics", []() {
Stack<int> stack(3);
try {
stack.push(0);
stack.push(0);
stack.push(0);
} catch (std::out_of_range) {
return false;
}
try {
stack.push(0);
} catch (std::out_of_range) {
return true;
}
return false;
});
test(" -> Push to full stack with copy semantics", []() {
int a = 0;
Stack<int> stack(3);
try {
stack.push(a);
stack.push(a);
stack.push(a);
} catch (std::out_of_range) {
return false;
}
try {
stack.push(a);
} catch (std::out_of_range) {
return true;
}
return false;
});
test(" -> Pop from empty stack", []() {
Stack<int> stack(3);
try {
stack.push(0);
stack.push(0);
stack.pop();
stack.pop();
} catch (std::out_of_range) {
return false;
}
try {
stack.pop();
} catch (std::out_of_range) {
return true;
}
return false;
});
test(" -> Push to stack with 0 capacity", []() {
Stack<int> stack(0);
try {
stack.push(0);
} catch (std::out_of_range) {
return true;
}
return false;
});
test(" -> Pop from stack with 0 capacity", []() {
Stack<int> stack(0);
try {
stack.pop();
} catch (std::out_of_range) {
return true;
}
return false;
});
test(" -> Memory leaks", []() {
return allocations_.size() == 0;
});
std::cout << "\n** \033[32mALL TESTS PASSED, congrats!\033[0m **\n\n";
return 0;
}