-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathvector2d.cpp
165 lines (160 loc) · 4.71 KB
/
vector2d.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
//
// vector2d.cpp
// boe_test
//
// Created by Celtic Minstrel on 2023-01-09.
//
#include "tools/vector2d.hpp"
#include "catch.hpp"
TEST_CASE("vector2d") {
vector2d<int> test_vector;
SECTION("default construct") {
CHECK(test_vector.width() == 0);
CHECK(test_vector.height() == 0);
CHECK(test_vector.size() == 0);
CHECK(test_vector.empty());
}
test_vector.resize(5, 5);
SECTION("resize changes size") {
CHECK(test_vector.width() == 5);
CHECK(test_vector.height() == 5);
CHECK(test_vector.size() == 25);
CHECK_FALSE(test_vector.empty());
}
SECTION("assignment") {
SECTION("by call") {
test_vector(3,4) = 12;
CHECK(test_vector(3,4) == 12);
}
SECTION("by index") {
test_vector[3][4] = 12;
CHECK(test_vector(3,4) == 12);
}
SECTION("by row and column") {
test_vector.row(4)[3] = 12;
CHECK(test_vector(3,4) == 12);
}
SECTION("by column and row") {
test_vector.col(3)[4] = 12;
CHECK(test_vector(3,4) == 12);
}
SECTION("by name") {
test_vector.top_left() = 1;
test_vector.top_right() = 2;
test_vector.bottom_left() = 3;
test_vector.bottom_right() = 4;
test_vector.centre() = 5;
CHECK(test_vector(0,0) == 1);
CHECK(test_vector(0,4) == 2);
CHECK(test_vector(4,0) == 3);
CHECK(test_vector(4,4) == 4);
CHECK(test_vector(2,2) == 5);
}
SECTION("by row") {
test_vector.row(4) = {1, 2, 3, 4, 5};
CHECK(test_vector(0,4) == 1);
CHECK(test_vector(1,4) == 2);
CHECK(test_vector(2,4) == 3);
CHECK(test_vector(3,4) == 4);
CHECK(test_vector(4,4) == 5);
}
SECTION("by column") {
test_vector.col(4) = {1, 2, 3, 4, 5};
CHECK(test_vector(4,0) == 1);
CHECK(test_vector(4,1) == 2);
CHECK(test_vector(4,2) == 3);
CHECK(test_vector(4,3) == 4);
CHECK(test_vector(4,4) == 5);
}
}
SECTION("access") {
test_vector(3,4) = 12;
test_vector(1,2) = 9;
SECTION("by call") {
CHECK(test_vector(3,4) == 12);
CHECK(test_vector(1,2) == 9);
}
SECTION("by index") {
CHECK(test_vector[3][4] == 12);
CHECK(test_vector[1][2] == 9);
}
SECTION("by row and colum") {
CHECK(test_vector.row(4)[3] == 12);
CHECK(test_vector.row(2)[1] == 9);
}
SECTION("by column and row") {
CHECK(test_vector.col(3)[4] == 12);
CHECK(test_vector.col(1)[2] == 9);
}
}
}
static void fill_some_values(vector2d<int>& tv) {
tv(1,3) = 1; tv(1,2) = 11; tv(1,4) = 21; tv(1,0) = 31; tv(1,1) = 41;
tv(2,4) = 2; tv(2,3) = 12; tv(2,2) = 22; tv(2,1) = 32; tv(2,0) = 42;
tv(0,4) = 3; tv(0,2) = 13; tv(0,1) = 23; tv(0,0) = 33; tv(0,3) = 43;
tv(3,1) = 4; tv(3,4) = 14; tv(3,0) = 24; tv(3,2) = 34; tv(3,3) = 44;
tv(4,2) = 5; tv(4,1) = 15; tv(4,4) = 25; tv(4,0) = 35; tv(4,3) = 45;
}
static void check_the_values(const vector2d<int>& tv) {
CHECK(tv(1,3) == 1); CHECK(tv(1,2) == 11); CHECK(tv(1,4) == 21); CHECK(tv(1,0) == 31); CHECK(tv(1,1) == 41);
CHECK(tv(2,4) == 2); CHECK(tv(2,3) == 12); CHECK(tv(2,2) == 22); CHECK(tv(2,1) == 32); CHECK(tv(2,0) == 42);
CHECK(tv(0,4) == 3); CHECK(tv(0,2) == 13); CHECK(tv(0,1) == 23); CHECK(tv(0,0) == 33); CHECK(tv(0,3) == 43);
CHECK(tv(3,1) == 4); CHECK(tv(3,4) == 14); CHECK(tv(3,0) == 24); CHECK(tv(3,2) == 34); CHECK(tv(3,3) == 44);
CHECK(tv(4,2) == 5); CHECK(tv(4,1) == 15); CHECK(tv(4,4) == 25); CHECK(tv(4,0) == 35); CHECK(tv(4,3) == 45);
}
TEST_CASE("vector2d resize") {
vector2d<int> test_vector;
SECTION("from empty") {
test_vector.resize(5, 5);
CHECK(test_vector.width() == 5);
CHECK(test_vector.height() == 5);
}
SECTION("grow width, leave height") {
test_vector.resize(5, 5);
fill_some_values(test_vector);
test_vector.resize(12, 5);
check_the_values(test_vector);
}
SECTION("shrink width, leave height") {
test_vector.resize(12, 5);
fill_some_values(test_vector);
test_vector.resize(5, 5);
check_the_values(test_vector);
}
SECTION("grow width, grow height") {
test_vector.resize(5, 5);
fill_some_values(test_vector);
test_vector.resize(12, 12);
check_the_values(test_vector);
}
SECTION("leave width, grow height") {
test_vector.resize(5, 5);
fill_some_values(test_vector);
test_vector.resize(5, 12);
check_the_values(test_vector);
}
SECTION("shrink width, grow height") {
test_vector.resize(12, 5);
fill_some_values(test_vector);
test_vector.resize(5, 12);
check_the_values(test_vector);
}
SECTION("grow width, shrink height") {
test_vector.resize(5, 12);
fill_some_values(test_vector);
test_vector.resize(12, 5);
check_the_values(test_vector);
}
SECTION("leave width, shrink height") {
test_vector.resize(5, 12);
fill_some_values(test_vector);
test_vector.resize(5, 5);
check_the_values(test_vector);
}
SECTION("shrink width, shrink height") {
test_vector.resize(12, 12);
fill_some_values(test_vector);
test_vector.resize(5, 5);
check_the_values(test_vector);
}
}