Skip to content

Commit 91be2f7

Browse files
committed
Add tests
1 parent 7f267dc commit 91be2f7

File tree

2 files changed

+257
-0
lines changed

2 files changed

+257
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include "../voxec.h"
2+
3+
#include <gtest/gtest.h>
4+
5+
#include <chrono>
6+
7+
TEST(Dimensionality, Estimate) {
8+
using std::chrono::duration_cast;
9+
using std::chrono::duration;
10+
using std::chrono::high_resolution_clock;
11+
12+
auto storage = new chunked_voxel_storage<bit_t>(0., 0., 0., 0.1, 10, 10, 10, 10);
13+
14+
for (size_t i = 1; i < 8; ++i) {
15+
for (size_t j = 1; j < 8; ++j) {
16+
for (size_t k = 1; k < 8; ++k) {
17+
storage->Set(make_vec(i, j, k));
18+
}
19+
}
20+
}
21+
22+
std::array<double, 4> times_ms;
23+
24+
for (int i = 0; i < 4; ++i) {
25+
scope_map args;
26+
27+
args["input"] = storage;
28+
args["max_depth"] = 5;
29+
args["max_depth_2"] = 2;
30+
args["reposition"] = 1;
31+
32+
if (i == 1) {
33+
args["inward_distance_approximate"] = 1;
34+
args["subsampling_factor"] = 2;
35+
}
36+
if (i == 2) {
37+
args["inward_distance_skip"] = 1;
38+
}
39+
if (i == 3) {
40+
args["THREADS"] = 4;
41+
}
42+
43+
auto t1 = high_resolution_clock::now();
44+
auto v = op_dimensionality_estimate{}.invoke(args);
45+
auto t2 = high_resolution_clock::now();
46+
duration<double, std::milli> ms = t2 - t1;
47+
48+
std::cerr << "Time (ms) " << (times_ms[i] = ms.count()) << std::endl;
49+
50+
ASSERT_EQ(v.which(), 3);
51+
52+
auto vp = boost::get<abstract_voxel_storage*>(v);
53+
54+
ASSERT_EQ(vp->value_bits(), 4 * 2 * 8);
55+
56+
normal_and_curvature<int16_t> vi16;
57+
58+
static auto ONE_DIV_SQRT_THREE_F = 1.f / std::sqrt(3.f);
59+
60+
for (auto ijk : *storage) {
61+
vp->Get(ijk, &vi16);
62+
auto n = vi16.convert<float>().normal();
63+
ASSERT_TRUE(((n - ONE_DIV_SQRT_THREE_F) < 1.e-5).all());
64+
}
65+
}
66+
67+
ASSERT_LT(times_ms[1] * 1.5, times_ms[0]);
68+
ASSERT_LT(times_ms[2] * 1.1, times_ms[1]);
69+
ASSERT_LT(times_ms[3] * 3, times_ms[0]);
70+
}

tests/test_memoized_traversal.cpp

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
#include "../memoized_traversal.h"
2+
#include "dim3.h"
3+
4+
#include <chrono>
5+
#include <iostream>
6+
#include <unordered_set>
7+
8+
#include <gtest/gtest.h>
9+
10+
TEST(Vec, Hashing) {
11+
auto vec = make_vec(1, 2, 3);
12+
std::hash<vec_n<3, int>> hash_fn;
13+
auto vec2 = make_vec(1, 2, 3);
14+
ASSERT_EQ(hash_fn(vec), hash_fn(vec));
15+
}
16+
17+
TEST(Traversal, Comparison) {
18+
auto storage = new chunked_voxel_storage<bit_t>(0., 0., 0., 0.1, 10, 10, 10, 10);
19+
20+
for (size_t i = 2; i < 7; ++i) {
21+
for (size_t j = 2; j < 7; ++j) {
22+
for (size_t k = 2; k < 7; ++k) {
23+
if (((make_vec(4, 4, 4) - make_vec(i, j, k).as<int>()).abs() < 2).all()) {
24+
continue;
25+
}
26+
storage->Set(make_vec(i, j, k));
27+
}
28+
}
29+
}
30+
31+
ASSERT_EQ(storage->count(), 5 * 5 * 2 + 5 * 3 * 2 + 3 * 3 * 2);
32+
ASSERT_EQ(storage->count(), 5 * 5 * 5 - 3 * 3 * 3);
33+
34+
/*
35+
for (size_t k = 2; k < 4; ++k) {
36+
for (size_t j = 2; j < 4; ++j) {
37+
for (size_t i = 2; i < 9; ++i) {
38+
storage->Set(make_vec(i, j, k));
39+
}
40+
}
41+
}
42+
43+
ASSERT_EQ(storage->count(), 7 * 2 * 2);
44+
*/
45+
46+
double accum_1 = 0.;
47+
double accum_2 = 0.;
48+
49+
using std::chrono::duration_cast;
50+
using std::chrono::duration;
51+
using std::chrono::high_resolution_clock;
52+
53+
auto t1 = high_resolution_clock::now();
54+
memoized_traversal trav(storage, 1000);
55+
auto t2 = high_resolution_clock::now();
56+
duration<double, std::milli> ms = t2 - t1;
57+
accum_2 += ms.count();
58+
59+
for (int k = 2; k < 3; ++k) {
60+
for (int j = 2; j < 5; ++j) {
61+
auto seed = make_vec<size_t>(j, k, 2);
62+
std::cout << "From: " << seed.format() << std::endl;
63+
64+
visitor<> v;
65+
v.max_depth = 16;
66+
int count = 0;
67+
68+
std::vector<vec_n<3, size_t>> vecs;
69+
70+
t1 = high_resolution_clock::now();
71+
v([&count, &vecs](const tagged_index& v) {
72+
vecs.push_back(v.pos);
73+
count++;
74+
}, storage, seed);
75+
t2 = high_resolution_clock::now();
76+
ms = t2 - t1;
77+
accum_1 += ms.count();
78+
79+
std::sort(vecs.begin(), vecs.end(), [](auto& a, auto& b) { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); });
80+
for (auto& p : vecs) {
81+
std::cerr << p.format() << std::endl;
82+
}
83+
std::cerr << "vs" << std::endl;
84+
vecs.clear();
85+
86+
std::unordered_map<vec_n<3, size_t>, size_t> out;
87+
t1 = high_resolution_clock::now();
88+
trav(seed, (size_t)*v.max_depth, out);
89+
t2 = high_resolution_clock::now();
90+
ms = t2 - t1;
91+
accum_2 += ms.count();
92+
for (auto& p : out) {
93+
vecs.push_back(p.first);
94+
}
95+
std::sort(vecs.begin(), vecs.end(), [](auto& a, auto& b) { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); });
96+
for (auto& p : vecs) {
97+
std::cerr << p.format() << std::endl;
98+
}
99+
100+
ASSERT_EQ(out.size(), count);
101+
102+
std::cerr << "-------------------" << std::endl;
103+
}
104+
}
105+
std::cerr << accum_1 << " vs " << accum_2 << std::endl;
106+
std::cerr << "h: " << trav.cache_hits << " m: " << trav.cache_misses << std::endl;
107+
}
108+
109+
TEST(DISABLED_Traversal, Squaring) {
110+
auto storage = new chunked_voxel_storage<bit_t>(0., 0., 0., 0.1, 10, 10, 10, 10);
111+
112+
for (size_t i = 2; i < 7; ++i) {
113+
for (size_t j = 2; j < 7; ++j) {
114+
for (size_t k = 2; k < 7; ++k) {
115+
if (((make_vec(4, 4, 4) - make_vec(i, j, k).as<int>()).abs() < 2).all()) {
116+
continue;
117+
}
118+
storage->Set(make_vec(i, j, k));
119+
}
120+
}
121+
}
122+
123+
ASSERT_EQ(storage->count(), 5 * 5 * 2 + 5 * 3 * 2 + 3 * 3 * 2);
124+
ASSERT_EQ(storage->count(), 5 * 5 * 5 - 3 * 3 * 3);
125+
126+
double accum_1 = 0.;
127+
double accum_2 = 0.;
128+
129+
using std::chrono::duration_cast;
130+
using std::chrono::duration;
131+
using std::chrono::high_resolution_clock;
132+
133+
auto t1 = high_resolution_clock::now();
134+
squaring_traversal trav(storage);
135+
auto t2 = high_resolution_clock::now();
136+
duration<double, std::milli> ms = t2 - t1;
137+
accum_2 += ms.count();
138+
139+
for (int k = 2; k < 4; ++k) {
140+
for (int j = 2; j < 7; ++j) {
141+
auto seed = make_vec<size_t>(j, k, 2);
142+
143+
visitor<> v;
144+
v.max_depth = 32;
145+
int count = 0;
146+
147+
std::vector<vec_n<3, size_t>> vecs;
148+
149+
t1 = high_resolution_clock::now();
150+
v([&count, &vecs](const tagged_index& v) {
151+
vecs.push_back(v.pos);
152+
count++;
153+
}, storage, seed);
154+
t2 = high_resolution_clock::now();
155+
ms = t2 - t1;
156+
accum_1 += ms.count();
157+
158+
std::sort(vecs.begin(), vecs.end(), [](auto& a, auto& b) { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); });
159+
for (auto& p : vecs) {
160+
std::cerr << p.format() << std::endl;
161+
}
162+
std::cerr << std::endl << std::endl;
163+
vecs.clear();
164+
165+
squaring_traversal::set_type out;
166+
t1 = high_resolution_clock::now();
167+
trav(seed, out);
168+
t2 = high_resolution_clock::now();
169+
ms = t2 - t1;
170+
accum_2 += ms.count();
171+
172+
std::transform(out.begin(), out.end(), std::back_inserter(vecs), [](auto& v) {
173+
return make_vec<size_t>(v.v[0], v.v[1], v.v[2]);
174+
});
175+
std::sort(vecs.begin(), vecs.end(), [](auto& a, auto& b) { return std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end()); });
176+
for (auto& p : vecs) {
177+
std::cerr << p.format() << std::endl;
178+
}
179+
180+
ASSERT_EQ(out.size(), count);
181+
182+
std::cout << "-------------------" << std::endl;
183+
}
184+
}
185+
std::cerr << accum_1 << " vs " << accum_2 << std::endl;
186+
// std::cerr << "h: " << trav.cache_hits << " m: " << trav.cache_misses << std::endl;
187+
}

0 commit comments

Comments
 (0)