-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsparsevec_test.cpp
More file actions
86 lines (70 loc) · 2.29 KB
/
Copy pathsparsevec_test.cpp
File metadata and controls
86 lines (70 loc) · 2.29 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
#include <span>
#include <sstream>
#include <stdexcept>
#include <unordered_map>
#include <vector>
#include <pgvector/sparsevec.hpp>
#include "helper.hpp"
using pgvector::SparseVector;
namespace {
void test_constructor_vector() {
SparseVector vec{std::vector<float>{1, 0, 2, 0, 3, 0}};
assert_equal(vec.dimensions(), 6);
assert_equal(vec.indices() == std::vector<int>{0, 2, 4}, true);
assert_equal(vec.values() == std::vector<float>{1, 2, 3}, true);
}
void test_constructor_span() {
SparseVector vec{std::span<const float>{{1, 0, 2, 0, 3, 0}}};
assert_equal(vec.dimensions(), 6);
}
void test_constructor_map() {
std::unordered_map<int, float> map{{2, 2}, {4, 3}, {3, 0}, {0, 1}};
SparseVector vec{map, 6};
assert_equal(vec.dimensions(), 6);
assert_equal(vec.indices() == std::vector<int>{0, 2, 4}, true);
assert_equal(vec.values() == std::vector<float>{1, 2, 3}, true);
assert_exception<std::invalid_argument>(
[&] { SparseVector{map, -1}; }, "sparsevec cannot have negative dimensions"
);
assert_exception<std::invalid_argument>(
[&] { SparseVector{map, 4}; }, "sparsevec index out of bounds"
);
assert_exception<std::invalid_argument>(
[] { SparseVector{{{0, 1}}, 0}; }, "sparsevec index out of bounds"
);
}
void test_constructor_empty() {
SparseVector vec{std::vector<float>{}};
assert_equal(vec.dimensions(), 0);
SparseVector vec2{{}, 0};
assert_equal(vec2.dimensions(), 0);
}
void test_dimensions() {
SparseVector vec{std::vector<float>{1, 0, 2, 0, 3, 0}};
assert_equal(vec.dimensions(), 6);
}
void test_indices() {
SparseVector vec{std::vector<float>{1, 0, 2, 0, 3, 0}};
assert_equal(vec.indices() == std::vector<int>{0, 2, 4}, true);
}
void test_values() {
SparseVector vec{std::vector<float>{1, 0, 2, 0, 3, 0}};
assert_equal(vec.values() == std::vector<float>{1, 2, 3}, true);
}
void test_string() {
SparseVector vec{std::vector<float>{1, 0, 2, 0, 3, 0}};
std::ostringstream oss;
oss << vec;
assert_equal(oss.str(), "{1:1,3:2,5:3}/6");
}
} // namespace
void test_sparsevec() {
test_constructor_vector();
test_constructor_span();
test_constructor_empty();
test_constructor_map();
test_dimensions();
test_indices();
test_values();
test_string();
}