-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_module_tester.cpp
257 lines (212 loc) · 7.79 KB
/
vector_module_tester.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
#include "vector_module_tester.hpp"
template <typename T>
void print_vector(ft::vector<T>& vec) {
std::cout << "[";
for (size_t i = 0; i < vec.size(); i++) {
std::cout << vec[i] << " ";
}
std::cout << "]" << std::endl;
}
void vec_constructors() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<int> default_construct;
ft::vector<int> fill_construct((size_t)10);
ft::vector<int> fill_construct_2((size_t)10, 42);
ft::vector<int> range_construct(fill_construct.begin(), fill_construct.end());
ft::vector<int> copy_construct(range_construct);
print_vector(default_construct);
print_vector(fill_construct);
print_vector(fill_construct_2);
print_vector(range_construct);
print_vector(copy_construct);
}
void vec_assign_operator() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<int> fill_construct((size_t)10, 42);
ft::vector<int> assign_op_target;
assign_op_target = fill_construct;
print_vector(assign_op_target);
print_vector(fill_construct);
}
void vec_iterators() {
ft::vector<char> vec;
for (char c = 'a'; c < 'z'; c++) {
vec.push_back(c);
}
// begin() / end() / * operator
for (ft::vector<char>::iterator iter = vec.begin(); iter != vec.end(); iter++) {
std::cout << *iter;
}
std::cout << std::endl;
// const operator check / +, - operator
for (ft::vector<char>::const_iterator iter = vec.begin() + 1; iter != vec.end() - 1; iter++) {
std::cout << *iter;
}
std::cout << std::endl;
// +, -, ++, --, operation between iterator and iterator
ft::vector<char>::iterator a = vec.begin();
ft::vector<char>::const_iterator b = vec.begin();
a++;
a--;
a = a + 5;
a = 1 + a;
a = a - 4;
std::cout << *(a += 2) << std::endl;
std::cout << *(a -= 1) << std::endl;
*(a -= 2) = 'Z';
*(a += 2) = 'W';
std::cout << "const_iterator +=: " << *(b += 2) << std::endl;
std::cout << "const_iterator -=: " << *(b -= 2) << std::endl;
std::cout << "(iterator == const_iterator): " << (a == b) << std::endl;
std::cout << "(const_iterator - iterator): " << (b - a) << std::endl;
std::cout << "(iterator + 3 != iterator): " << (a + 3 == a) << std::endl;
ft::vector<char>::difference_type diff = b - a;
ft::vector<char>::difference_type diff1 = a - b;
std::cout << "diff : " << diff << ", " << diff1 << std::endl;
// reverse operator test
for (ft::vector<char>::reverse_iterator iter = vec.rbegin(); iter != vec.rend(); iter++) {
std::cout << *iter;
}
std::cout << std::endl;
}
void vec_capacity_functions() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<char> vec;
// size(), capacity(), empty() test
std::cout << "1 : " << vec.size() << ", " << vec.capacity() << ", " << vec.empty() << std::endl;
vec.reserve(5); // reserve() test
// size(), capacity(), empty() for reserve()
std::cout << "2 : " << vec.size() << ", " << vec.capacity() << ", " << vec.empty() << std::endl;
for (char c = 'a'; c < 'z'; c++) {
vec.push_back(c);
}
// size(), capacity(), empty() for size change
std::cout << "3 : " << vec.size() << ", " << vec.capacity() << ", " << vec.empty() << std::endl;
vec.resize(10); // resize() test
// size(), capacity(), empty() for resize()
std::cout << "3 : " << vec.size() << ", " << vec.capacity() << ", " << vec.empty() << std::endl;
print_vector(vec); // print vec
}
void vec_element_access_functions() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<char> vec;
for (char c = 'a'; c < 'z'; c++) {
vec.push_back(c);
}
// [] test
std::cout << vec[0];
std::cout << vec[21];
std::cout << vec[9];
std::cout << vec[8];
std::cout << vec[7];
std::cout << vec[10];
std::cout<< std::endl;
// at() test
std::cout << vec.at(0);
std::cout << vec.at(21);
std::cout << vec.at(9);
std::cout << vec.at(8);
std::cout << vec.at(7);
std::cout << vec.at(10);
std::cout<< std::endl;
// front test
std::cout << vec.front();
std::cout<< std::endl;
// back test
std::cout << vec.back();
std::cout<< std::endl;
}
void vec_assigns() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<char> vec_origin;
for (char c = 'a'; c < 'z'; c++) {
vec_origin.push_back(c);
}
ft::vector<char> vec_range;
ft::vector<char> vec_fill;
for (char c = 'a'; c < 'z'; c++) {
vec_range.push_back(c);
vec_fill.push_back(c);
}
vec_range.assign(vec_origin.begin() + 10, vec_origin.end() - 5);
vec_fill.assign(12, 'X');
print_vector(vec_range); // print vec_range
print_vector(vec_fill); // print vec_fill
}
void vec_push_pop_check() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<char> vec;
for (char c = 'a'; c < 'z'; c++) {
vec.push_back(c);
}
// size(), capacity(), empty() test
std::cout << "1 : " << vec.size() << ", " << vec.capacity() << ", " << vec.empty() << std::endl;
for (char c = 'a'; c < 'g'; c++) {
vec.pop_back();
}
// size(), capacity(), empty() test
std::cout << "1 : " << vec.size() << ", " << vec.capacity() << ", " << vec.empty() << std::endl;
}
void vec_inserts() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<char> vec_origin;
for (char c = 'a'; c < 'z'; c++) {
vec_origin.push_back(c);
}
ft::vector<char> vec_single_element;
ft::vector<char> vec_fill;
ft::vector<char> vec_range;
for (char c = 'a'; c < 'z'; c++) {
vec_single_element.push_back(c);
vec_fill.push_back(c);
vec_range.push_back(c);
}
vec_single_element.insert(vec_single_element.begin() + 10, 'X');
vec_fill.insert(vec_fill.begin() + 10, 10, 'X');
vec_range.insert(vec_range.begin() + 10, vec_origin.begin(), vec_origin.end());
print_vector(vec_single_element); // print vec_single_element
print_vector(vec_fill); // print vec_fill
print_vector(vec_range); // print vec_range
}
void vec_erases() {
std::cout << "[" << __func__ << "]" << std::endl;
ft::vector<char> vec_pos;
ft::vector<char> vec_range;
for (char c = 'a'; c < 'z'; c++) {
vec_pos.push_back(c);
vec_range.push_back(c);
}
// iterator erase (iterator position); test
std::cout << *(vec_pos.erase(vec_pos.begin())) << std::endl;
std::cout << *(vec_pos.erase(vec_pos.begin() + 1)) << std::endl;
std::cout << *(vec_pos.erase(vec_pos.begin() + 3)) << std::endl;
// iterator erase (iterator first, iterator last); test
std::cout << *(vec_range.erase(vec_range.begin() + 10, vec_range.end() - 5)) << std::endl;
std::cout << *(vec_range.erase(vec_range.begin(), vec_range.begin() + 2)) << std::endl;
print_vector(vec_pos); // print vec_pos
print_vector(vec_range); // print vec_range
}
void vec_swap_clear() {
ft::vector<int> v1((size_t)10, 42);
ft::vector<int> v2;
ft::vector<int> v3((size_t)10, 42);
v2.swap(v1);
print_vector(v1); // print v1
print_vector(v2); // print v2
print_vector(v3); // print v3
v2.clear();
ft::vector<int>().swap(v3);
print_vector(v1); // print v1
print_vector(v2); // print v2
print_vector(v3); // print v3
}
void vec_relational_operators() {
ft::vector<int> foo (3,100); // three ints with a value of 100
ft::vector<int> bar (2,200); // two ints with a value of 200
if (foo==bar) std::cout << "foo and bar are equal\n";
if (foo!=bar) std::cout << "foo and bar are not equal\n";
if (foo< bar) std::cout << "foo is less than bar\n";
if (foo> bar) std::cout << "foo is greater than bar\n";
if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";
}