-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.h
225 lines (199 loc) · 5.79 KB
/
tests.h
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
#pragma once
#include <cassert>
#include <stdexcept>
#include <iostream>
#include "simple_vector.h"
inline void Test1() {
// Default constructor initialization
{
SimpleVector<int> v;
assert(v.GetSize() == 0u);
assert(v.IsEmpty());
assert(v.GetCapacity() == 0u);
}
// Initialization of vector with required size
{
SimpleVector<int> v(5);
assert(v.GetSize() == 5u);
assert(v.GetCapacity() == 5u);
assert(!v.IsEmpty());
for (size_t i = 0; i < v.GetSize(); ++i) {
assert(v[i] == 0);
}
}
// Initialization of vector filled with some values
{
SimpleVector<int> v(3, 42);
assert(v.GetSize() == 3);
assert(v.GetCapacity() == 3);
for (size_t i = 0; i < v.GetSize(); ++i) {
assert(v[i] == 42);
}
}
// Initialization of vector with initializer_list
{
SimpleVector<int> v{ 1, 2, 3 };
assert(v.GetSize() == 3);
assert(v.GetCapacity() == 3);
assert(v[2] == 3);
}
// Using vector.At[]
{
SimpleVector<int> v(3);
assert(&v.At(2) == &v[2]);
try {
v.At(3);
assert(false);
} catch (const std::out_of_range&) {
} catch (...) {
assert(false);
}
}
// Clear vector
{
SimpleVector<int> v(10);
const size_t old_capacity = v.GetCapacity();
v.Clear();
assert(v.GetSize() == 0);
assert(v.GetCapacity() == old_capacity);
}
// Size changing
{
SimpleVector<int> v(3);
v[2] = 17;
v.Resize(7);
assert(v.GetSize() == 7);
assert(v.GetCapacity() >= v.GetSize());
assert(v[2] == 17);
assert(v[3] == 0);
}
{
SimpleVector<int> v(3);
v[0] = 42;
v[1] = 55;
const size_t old_capacity = v.GetCapacity();
v.Resize(2);
assert(v.GetSize() == 2);
assert(v.GetCapacity() == old_capacity);
assert(v[0] == 42);
assert(v[1] == 55);
}
{
const size_t old_size = 3;
SimpleVector<int> v(3);
v.Resize(old_size + 5);
v[3] = 42;
v.Resize(old_size);
v.Resize(old_size + 2);
assert(v[3] == 0);
}
// Iterating over SimpleVector
{
// Empty vector
{
SimpleVector<int> v;
assert(v.begin() == nullptr);
assert(v.end() == nullptr);
}
// Not empty vector
{
SimpleVector<int> v(10, 42);
assert(v.begin());
assert(*v.begin() == 42);
assert(v.end() == v.begin() + v.GetSize());
}
}
}
inline void Test2() {
// PushBack
{
SimpleVector<int> v(1);
v.PushBack(42);
assert(v.GetSize() == 2);
assert(v.GetCapacity() >= v.GetSize());
assert(v[0] == 0);
assert(v[1] == 42);
}
// PushBack increases capacity if it needs to
{
SimpleVector<int> v(2);
v.Resize(1);
const size_t old_capacity = v.GetCapacity();
v.PushBack(123);
assert(v.GetSize() == 2);
assert(v.GetCapacity() == old_capacity);
}
// PopBack
{
SimpleVector<int> v{ 0, 1, 2, 3 };
const size_t old_capacity = v.GetCapacity();
const auto old_begin = v.begin();
v.PopBack();
assert(v.GetCapacity() == old_capacity);
assert(v.begin() == old_begin);
assert((v == SimpleVector<int>{0, 1, 2}));
}
// Copy constructor
{
SimpleVector<int> numbers{ 1, 2 };
auto numbers_copy(numbers);
assert(&numbers_copy[0] != &numbers[0]);
assert(numbers_copy.GetSize() == numbers.GetSize());
for (size_t i = 0; i < numbers.GetSize(); ++i) {
assert(numbers_copy[i] == numbers[i]);
assert(&numbers_copy[i] != &numbers[i]);
}
}
// Comparison
{
assert((SimpleVector{ 1, 2, 3 } == SimpleVector{ 1, 2, 3 }));
assert((SimpleVector{ 1, 2, 3 } != SimpleVector{ 1, 2, 2 }));
assert((SimpleVector{ 1, 2, 3 } < SimpleVector{ 1, 2, 3, 1 }));
assert((SimpleVector{ 1, 2, 3 } > SimpleVector{ 1, 2, 2, 1 }));
assert((SimpleVector{ 1, 2, 3 } >= SimpleVector{ 1, 2, 3 }));
assert((SimpleVector{ 1, 2, 4 } >= SimpleVector{ 1, 2, 3 }));
assert((SimpleVector{ 1, 2, 3 } <= SimpleVector{ 1, 2, 3 }));
assert((SimpleVector{ 1, 2, 3 } <= SimpleVector{ 1, 2, 4 }));
}
// Swap of vectors' values
{
SimpleVector<int> v1{ 42, 666 };
SimpleVector<int> v2;
v2.PushBack(0);
v2.PushBack(1);
v2.PushBack(2);
const int* const begin1 = &v1[0];
const int* const begin2 = &v2[0];
const size_t capacity1 = v1.GetCapacity();
const size_t capacity2 = v2.GetCapacity();
const size_t size1 = v1.GetSize();
const size_t size2 = v2.GetSize();
static_assert(noexcept(v1.Swap(v2)));
v1.Swap(v2);
assert(&v2[0] == begin1);
assert(&v1[0] == begin2);
assert(v1.GetSize() == size2);
assert(v2.GetSize() == size1);
assert(v1.GetCapacity() == capacity2);
assert(v2.GetCapacity() == capacity1);
}
// Assignment
{
SimpleVector<int> src_vector{ 1, 2, 3, 4 };
SimpleVector<int> dst_vector{ 1, 2, 3, 4, 5, 6 };
dst_vector = src_vector;
assert(dst_vector == src_vector);
}
// Insert
{
SimpleVector<int> v{ 1, 2, 3, 4 };
auto it = v.Insert(v.begin() + 2, 42);
assert((v == SimpleVector<int>{1, 2, 42, 3, 4}));
}
// Deleting
{
SimpleVector<int> v{ 1, 2, 3, 4 };
auto it = v.Erase(v.cbegin() + 2);
assert((v == SimpleVector<int>{1, 2, 4}));
}
}