- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector-test.cpp
183 lines (147 loc) · 5.26 KB
/
vector-test.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
/// @file test/vector-test.cpp
/// @copyright 2022 Thomas E. Vaughan, all rights reserved.
/// @brief Tests for gsl::vector.
#include"gslcpp/vector-view.hpp"
#include"gslcpp/vector.hpp"
#include<catch.hpp>
using gsl::CALLOC;
using gsl::complex;
using gsl::v_iface;
using gsl::vector;
using gsl::vector_view;
template<typename E> voidverify_static_default_constructor() {
vector<E, 3> v;
REQUIRE(v.size() == 3);
}
TEST_CASE("Static vector's default constructor works.", "[vector]") {
verify_static_default_constructor<double>();
verify_static_default_constructor<float>();
verify_static_default_constructor<longdouble>();
verify_static_default_constructor<int>();
verify_static_default_constructor<unsigned>();
verify_static_default_constructor<long>();
verify_static_default_constructor<unsignedlong>();
verify_static_default_constructor<short>();
verify_static_default_constructor<unsignedshort>();
verify_static_default_constructor<char>();
verify_static_default_constructor<unsignedchar>();
verify_static_default_constructor<complex<double>>();
verify_static_default_constructor<complex<float>>();
verify_static_default_constructor<complex<longdouble>>();
}
template<typename E> voidverify_dynamic_base_constructor() {
vector<E> v(15);
REQUIRE(v.size() == 15);
vector<E> w(15, CALLOC);
v.set_zero();
REQUIRE(v == w);
}
TEST_CASE("Dynamic vector's base-constructor works.", "[vector]") {
verify_dynamic_base_constructor<double>();
verify_dynamic_base_constructor<float>();
verify_dynamic_base_constructor<longdouble>();
verify_dynamic_base_constructor<int>();
verify_dynamic_base_constructor<unsigned>();
verify_dynamic_base_constructor<long>();
verify_dynamic_base_constructor<unsignedlong>();
verify_dynamic_base_constructor<short>();
verify_dynamic_base_constructor<unsignedshort>();
verify_dynamic_base_constructor<char>();
verify_dynamic_base_constructor<unsignedchar>();
verify_dynamic_base_constructor<complex<double>>();
verify_dynamic_base_constructor<complex<float>>();
verify_dynamic_base_constructor<complex<longdouble>>();
}
template<typename E> voidverify_dynamic_movement() {
vector<E> r(15);
for(int i= 0; i < 15; ++i) r[i]= E(15 - i);
vector s= std::move(r); // Move-construction.
REQUIRE(r.valid() == false); // Because s was invalid before move.
for(int i= 0; i < 15; ++i) REQUIRE(s[i] == E(15 - i));
}
TEST_CASE("Dynamic vector's move-constructor works.", "[vector]") {
verify_dynamic_movement<double>();
verify_dynamic_movement<float>();
verify_dynamic_movement<longdouble>();
verify_dynamic_movement<int>();
verify_dynamic_movement<unsigned>();
verify_dynamic_movement<long>();
verify_dynamic_movement<unsignedlong>();
verify_dynamic_movement<short>();
verify_dynamic_movement<unsignedshort>();
verify_dynamic_movement<char>();
verify_dynamic_movement<unsignedchar>();
verify_dynamic_movement<complex<double>>();
verify_dynamic_movement<complex<float>>();
verify_dynamic_movement<complex<longdouble>>();
}
template<typename E> voidverify_static_base_constructor() {
REQUIRE_THROWS((vector<E, 3>(4)));
}
TEST_CASE("Static vector's base-constructor works.", "[vector]") {
verify_static_base_constructor<double>();
verify_static_base_constructor<float>();
verify_static_base_constructor<longdouble>();
verify_static_base_constructor<int>();
verify_static_base_constructor<unsigned>();
verify_static_base_constructor<long>();
verify_static_base_constructor<unsignedlong>();
verify_static_base_constructor<short>();
verify_static_base_constructor<unsignedshort>();
verify_static_base_constructor<char>();
verify_static_base_constructor<unsignedchar>();
verify_static_base_constructor<complex<double>>();
verify_static_base_constructor<complex<float>>();
verify_static_base_constructor<complex<longdouble>>();
}
TEST_CASE("Static vector's constructor from array works.", "[vector]") {
double d[]= {2.0, 4.0, 6.0, 8.0};
vector v(d); // No template-parameter required!
REQUIRE(v.size() == 4);
REQUIRE(v[0] == 2.0);
REQUIRE(v[1] == 4.0);
REQUIRE(v[2] == 6.0);
REQUIRE(v[3] == 8.0);
}
TEST_CASE("Dynamic vector's constructor from array works.", "[vector]") {
double d[]= {2.0, 4.0, 6.0, 8.0};
doubleconst *e= d;
vector v(e, 4); // No template-parameter required!
REQUIRE(v.size() == 4);
REQUIRE(v[0] == 2.0);
REQUIRE(v[1] == 4.0);
REQUIRE(v[2] == 6.0);
REQUIRE(v[3] == 8.0);
}
TEST_CASE("Static vector's assignment from array works.", "[vector]") {
double d[]= {2.0, 4.0, 6.0, 8.0};
vector v(d); // No template-parameter required!
REQUIRE(v[0] == 2.0);
REQUIRE(v[1] == 4.0);
REQUIRE(v[2] == 6.0);
REQUIRE(v[3] == 8.0);
v.set_zero();
REQUIRE(v[0] == 0.0);
REQUIRE(v[1] == 0.0);
REQUIRE(v[2] == 0.0);
REQUIRE(v[3] == 0.0);
v= d;
REQUIRE(v[0] == 2.0);
REQUIRE(v[1] == 4.0);
REQUIRE(v[2] == 6.0);
REQUIRE(v[3] == 8.0);
}
TEST_CASE("Static vector's copy-constructor works.", "[vector]") {
vector v({1.0, 2.0, 3.0});
vector w= v; // Copy.
REQUIRE(v == w);
auto view= v.view();
vector x= view; // Copy from other kind of vector.
REQUIRE(x == v);
}
TEST_CASE("Static vector's copy-assignment works.", "[vector]") {
vector v({1.0, 2.0, 3.0});
vector<double, 3> w;
w= v; // Assign.
}
// EOF