Skip to content

Commit 834a725

Browse files
committed
Moved tests to separate files
1 parent 2a5bac6 commit 834a725

File tree

6 files changed

+105
-72
lines changed

6 files changed

+105
-72
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ To get started with development:
162162
git clone https://github.com/pgvector/pgvector-cpp.git
163163
cd pgvector-cpp
164164
createdb pgvector_cpp_test
165-
g++ -std=c++17 -Wall -Wextra -Wno-unknown-attributes -Werror -o test/main test/main.cpp test/pqxx_test.cpp -lpqxx -lpq
165+
g++ -std=c++17 -Wall -Wextra -Wno-unknown-attributes -Werror -o test/main test/main.cpp test/vector_test.cpp test/halfvec_test.cpp test/sparsevec_test.cpp test/pqxx_test.cpp -lpqxx -lpq
166166
test/main
167167
```
168168

include/pgvector/sparsevec.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include <unordered_map>
1111
#include <vector>
1212

13+
#if __cplusplus >= 202002L
14+
#include <span>
15+
#endif
16+
1317
namespace pgvector {
1418
/// A sparse vector.
1519
class SparseVector {

test/halfvec_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <cassert>
2+
3+
#include "../include/pgvector/halfvec.hpp"
4+
5+
#if __cplusplus >= 202002L
6+
#include <span>
7+
#endif
8+
9+
using pgvector::HalfVector;
10+
11+
static void test_constructor_vector() {
12+
auto vec = HalfVector({1, 2, 3});
13+
assert(vec.dimensions() == 3);
14+
}
15+
16+
#if __cplusplus >= 202002L
17+
static void test_constructor_span() {
18+
auto vec = HalfVector(std::span<const float>({1, 2, 3}));
19+
assert(vec.dimensions() == 3);
20+
}
21+
#endif
22+
23+
void test_halfvec() {
24+
test_constructor_vector();
25+
#if __cplusplus >= 202002L
26+
test_constructor_span();
27+
#endif
28+
}

test/main.cpp

Lines changed: 4 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,79 +1,12 @@
1-
#include <cassert>
2-
#include <unordered_map>
3-
4-
#include "../include/pgvector/halfvec.hpp"
5-
#include "../include/pgvector/sparsevec.hpp"
6-
#include "../include/pgvector/vector.hpp"
7-
8-
#if __cplusplus >= 202002L
9-
#include <span>
10-
#endif
11-
12-
using pgvector::HalfVector;
13-
using pgvector::SparseVector;
14-
using pgvector::Vector;
15-
1+
void test_vector();
2+
void test_halfvec();
3+
void test_sparsevec();
164
void test_pqxx();
175

18-
void test_vector() {
19-
auto vec = Vector({1, 2, 3});
20-
assert(vec.dimensions() == 3);
21-
}
22-
23-
#if __cplusplus >= 202002L
24-
void test_vector_span() {
25-
auto vec = Vector(std::span<const float>({1, 2, 3}));
26-
assert(vec.dimensions() == 3);
27-
}
28-
#endif
29-
30-
void test_halfvec() {
31-
auto vec = HalfVector({1, 2, 3});
32-
assert(vec.dimensions() == 3);
33-
}
34-
35-
#if __cplusplus >= 202002L
36-
void test_halfvec_span() {
37-
auto vec = HalfVector(std::span<const float>({1, 2, 3}));
38-
assert(vec.dimensions() == 3);
39-
}
40-
#endif
41-
42-
void test_sparsevec() {
43-
auto vec = SparseVector({1, 0, 2, 0, 3, 0});
44-
assert(vec.dimensions() == 6);
45-
assert(vec.indices() == (std::vector<int>{0, 2, 4}));
46-
assert(vec.values() == (std::vector<float>{1, 2, 3}));
47-
}
48-
49-
#if __cplusplus >= 202002L
50-
void test_sparsevec_span() {
51-
auto vec = SparseVector(std::span<const float>({1, 0, 2, 0, 3, 0}));
52-
assert(vec.dimensions() == 6);
53-
}
54-
#endif
55-
56-
void test_sparsevec_map() {
57-
std::unordered_map<int, float> map = {{2, 2}, {4, 3}, {3, 0}, {0, 1}};
58-
auto vec = SparseVector(map, 6);
59-
assert(vec.dimensions() == 6);
60-
assert(vec.indices() == (std::vector<int>{0, 2, 4}));
61-
assert(vec.values() == (std::vector<float>{1, 2, 3}));
62-
}
63-
646
int main() {
65-
test_pqxx();
667
test_vector();
67-
#if __cplusplus >= 202002L
68-
test_vector_span();
69-
#endif
708
test_halfvec();
71-
#if __cplusplus >= 202002L
72-
test_halfvec_span();
73-
#endif
749
test_sparsevec();
75-
#if __cplusplus >= 202002L
76-
test_sparsevec_span();
77-
#endif
10+
test_pqxx();
7811
return 0;
7912
}

test/sparsevec_test.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <cassert>
2+
#include <unordered_map>
3+
4+
#include "../include/pgvector/sparsevec.hpp"
5+
6+
#if __cplusplus >= 202002L
7+
#include <span>
8+
#endif
9+
10+
using pgvector::SparseVector;
11+
12+
static void test_constructor_vector() {
13+
auto vec = SparseVector({1, 0, 2, 0, 3, 0});
14+
assert(vec.dimensions() == 6);
15+
assert(vec.indices() == (std::vector<int>{0, 2, 4}));
16+
assert(vec.values() == (std::vector<float>{1, 2, 3}));
17+
}
18+
19+
#if __cplusplus >= 202002L
20+
static void test_constructor_span() {
21+
auto vec = SparseVector(std::span<const float>({1, 0, 2, 0, 3, 0}));
22+
assert(vec.dimensions() == 6);
23+
}
24+
#endif
25+
26+
static void test_constructor_map() {
27+
std::unordered_map<int, float> map = {{2, 2}, {4, 3}, {3, 0}, {0, 1}};
28+
auto vec = SparseVector(map, 6);
29+
assert(vec.dimensions() == 6);
30+
assert(vec.indices() == (std::vector<int>{0, 2, 4}));
31+
assert(vec.values() == (std::vector<float>{1, 2, 3}));
32+
}
33+
34+
void test_sparsevec() {
35+
test_constructor_vector();
36+
#if __cplusplus >= 202002L
37+
test_constructor_span();
38+
#endif
39+
test_constructor_map();
40+
}

test/vector_test.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <cassert>
2+
3+
#include "../include/pgvector/vector.hpp"
4+
5+
#if __cplusplus >= 202002L
6+
#include <span>
7+
#endif
8+
9+
using pgvector::Vector;
10+
11+
static void test_constructor_vector() {
12+
auto vec = Vector({1, 2, 3});
13+
assert(vec.dimensions() == 3);
14+
}
15+
16+
#if __cplusplus >= 202002L
17+
static void test_constructor_span() {
18+
auto vec = Vector(std::span<const float>({1, 2, 3}));
19+
assert(vec.dimensions() == 3);
20+
}
21+
#endif
22+
23+
void test_vector() {
24+
test_constructor_vector();
25+
#if __cplusplus >= 202002L
26+
test_constructor_span();
27+
#endif
28+
}

0 commit comments

Comments
 (0)