-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhypervector_test.cpp
More file actions
178 lines (154 loc) · 5.45 KB
/
hypervector_test.cpp
File metadata and controls
178 lines (154 loc) · 5.45 KB
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
#include "hypervector.h"
#include <iostream>
#include <string>
hypervector<std::string, 3> reference(
std::initializer_list<
std::initializer_list<
std::initializer_list<std::string>>> init) {
auto hvec = hypervector<std::string, 3>(std::move(init));
return hvec;
}
hypervector<std::string, 4> reference_iota(
size_t size0,
size_t size1,
size_t size2,
size_t size3) {
auto hvec = hypervector<std::string, 4>(size0, size1, size2, size3);
int i = 0;
for(auto& v : hvec) {
v = std::to_string(i++);
}
return hvec;
}
hypervector<std::string, 4> reference_iota_reverse(
size_t size0,
size_t size1,
size_t size2,
size_t size3) {
auto hvec = hypervector<std::string, 4>(size0, size1, size2, size3);
int i = size0 * size1* size2 * size3;
for(auto& v : hvec) {
v = std::to_string(--i);
}
return hvec;
}
int main(int /*argc*/, char** /*argv*/) {
bool success = true;
{ // test the different constructors, assign() and resize()
{
hypervector<std::string, 3> hvec;
hvec.reserve(3, 3, 3);
success &= (hvec.size() == 0);
std::cout << "construct():\n" << hvec << "\n\n";
}
{
hypervector<std::string, 3> hvec(1, 2, 3);
hvec.reserve(3 * 3 * 3);
success &= (hvec.sizeOf<0>() == 1);
success &= (hvec.sizeOf<1>() == 2);
success &= (hvec.sizeOf<2>() == 3);
success &= (hvec.size() == 1 * 2 * 3);
std::cout << "construct(1, 2, 3):\n" << hvec << "\n\n";
}
hypervector<std::string, 3> hvec;
hvec.reserve(3, 3, 3);
hvec.assign(1, 2, 3, "ho");
success &= (hvec.size() == 1 * 2 * 3);
success &= (hvec == reference({{
{"ho", "ho", "ho"},
{"ho", "ho", "ho"}}}));
std::cout << "construct(1, 2, 3, \"ho\"):\n" << hvec << "\n\n";
hvec.assign(3, 2, 1, "hi");
success &= (hvec.size() == 3 * 2 * 1);
success &= (hvec == reference({
{{"hi"}, {"hi"}},
{{"hi"}, {"hi"}},
{{"hi"}, {"hi"}}}));
std::cout << "assign(3, 2, 1, \"hi\"):\n" << hvec << "\n\n";
hvec.resize(1, 1, 1, "he");
success &= (hvec.size() == 1 * 1 * 1);
success &= (hvec == reference({{{"hi"}}}));
std::cout << "resize(1, 1, 1, \"he\"):\n" << hvec << "\n\n";
hvec.resize(2, 2, 2, "ha");
success &= (hvec.size() == 2 * 2 * 2);
success &= (hvec == reference({
{{"hi", "ha"}, {"ha", "ha"}},
{{"ha", "ha"}, {"ha", "ha"}}}));
std::cout << "resize(2, 2, 2, \"ha\"):\n" << hvec << "\n\n";
hvec.resize(3, 3, 3);
success &= (hvec.size() == 3 * 3 * 3);
success &= (hvec == reference({
{{"hi", "ha", "ha"},
{"ha", "ha", "ha"},
{"ha", "ha", ""}},
{{"", "", ""},
{"", "", ""},
{"", "", ""}},
{{"", "", ""},
{"", "", ""},
{"", "", ""}}}));
std::cout << "resize(3, 3, 3):\n" << hvec << "\n\n";
hvec.resize(3, 3, 0);
success &= (hvec.size() == 3 * 3 * 0);
std::cout << "resize(3, 3, 0):\n" << hvec << "\n\n";
hvec.resize(3, 0, 3);
success &= (hvec.size() == 3 * 0 * 3);
std::cout << "resize(3, 0, 3):\n" << hvec << "\n\n";
hvec.assign(0, 3, 3, "oh");
success &= (hvec.size() == 0 * 3 * 3);
std::cout << "assign(0, 3, 3, \"oh\"):\n" << hvec << "\n\n";
}
{
int test[4][3][2];
hypervector<int, 3> hvec(4, 3, 2);
int i = 0;
for (size_t x = 0; x < 4; ++x) {
for (size_t y = 0; y < 3; ++y) {
for (size_t z = 0; z < 2; ++z) {
test[x][y][z] = i;
hvec[x][y][z] = i++;
// test operator[] and at()
success &= (test[x][y][z] == hvec[x][y][z]);
success &= (test[x][y][z] == hvec.at(x, y, z));
}
}
}
{ // test begin() and end() globally
auto firstArray = &test[0][0][0];
const auto lastArray = firstArray + 4 * 3 * 2;
auto firstHvec = std::begin(hvec);
const auto lastHvec = std::end(hvec);
for (; firstArray != lastArray && firstHvec != lastHvec; ++firstArray, ++firstHvec)
success &= (*firstArray == *firstHvec);
}
{ // test begin() and end() on (const) subdimension
auto const subd = const_cast<const hypervector<int, 3>&>(hvec)[3];
static_assert(std::is_convertible<decltype(hvec[3]), decltype(subd)>::value, "const type mismatch");
auto firstArray = &test[3][0][0];
const auto lastArray = firstArray + 3 * 2;
auto firstHvec = std::begin(subd);
const auto lastHvec = std::end(subd);
for (; firstArray != lastArray && firstHvec != lastHvec; ++firstArray, ++firstHvec)
success &= (*firstArray == *firstHvec);
}
}
{ // fool around with operator[] and at()
hypervector<std::string, 4> hvec(5, 4, 3, 2, "0");
int i = 0;
for (size_t w = 0; w < hvec.sizeOf<0>(); ++w)
for (size_t x = 0; x < hvec.sizeOf<1>(); ++x)
for (size_t y = 0; y < hvec.sizeOf<2>(); ++y)
for (size_t z = 0; z < hvec.sizeOf<3>(); ++z)
hvec.at(w, x, y, z) = std::to_string(i++);
success &= (hvec == reference_iota(5, 4, 3, 2));
std::cout << "at(w, x, y, z):\n" << hvec << "\n\n";
for (size_t w = 0; w < hvec.sizeOf<0>(); ++w)
for (size_t x = 0; x < hvec.sizeOf<1>(); ++x)
for (size_t y = 0; y < hvec.sizeOf<2>(); ++y)
for (size_t z = 0; z < hvec.sizeOf<3>(); ++z)
hvec[w][x][y][z] = std::to_string(--i);
success &= (hvec == reference_iota_reverse(5, 4, 3, 2));
std::cout << "operator[w][x][y][z]:\n" << hvec << "\n\n";
}
return (success ? EXIT_SUCCESS : EXIT_FAILURE);
}