Skip to content

Commit a2fae30

Browse files
committed
Added more tests for SparseVector
1 parent 3b36e47 commit a2fae30

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

test/helper.hpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include <cassert>
4+
#include <functional>
5+
#include <optional>
6+
#include <string_view>
7+
8+
template<typename T>
9+
void assert_exception(std::function<void(void)> code, std::optional<std::string_view> message = std::nullopt) {
10+
bool exception = false;
11+
try {
12+
code();
13+
} catch (const T& e) {
14+
exception = true;
15+
if (message) {
16+
assert(std::string_view(e.what()) == *message);
17+
}
18+
}
19+
assert(exception);
20+
}

test/pqxx_test.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include <cassert>
2-
#include <functional>
32
#include <optional>
43
#include <string>
54
#include <string_view>
65
#include <vector>
76

87
#include <pqxx/pqxx>
9-
108
#include <pgvector/pqxx.hpp>
119

10+
#include "helper.hpp"
11+
1212
void setup(pqxx::connection &conn) {
1313
pqxx::nontransaction tx(conn);
1414
tx.exec("CREATE EXTENSION IF NOT EXISTS vector");
@@ -21,20 +21,6 @@ void before_each(pqxx::connection &conn) {
2121
tx.exec("TRUNCATE items");
2222
}
2323

24-
template<typename T>
25-
void assert_exception(std::function<void(void)> code, std::optional<std::string_view> message = std::nullopt) {
26-
bool exception = false;
27-
try {
28-
code();
29-
} catch (const T& e) {
30-
exception = true;
31-
if (message) {
32-
assert(std::string_view(e.what()) == *message);
33-
}
34-
}
35-
assert(exception);
36-
}
37-
3824
std::optional<std::string_view> float_error([[maybe_unused]] std::string_view message) {
3925
#ifdef __linux__
4026
return message;

test/sparsevec_test.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
#include <pgvector/sparsevec.hpp>
66

7+
#include "helper.hpp"
8+
79
using pgvector::SparseVector;
810

911
static void test_constructor_vector() {
@@ -24,6 +26,14 @@ static void test_constructor_map() {
2426
assert(vec.dimensions() == 6);
2527
assert(vec.indices() == (std::vector<int>{0, 2, 4}));
2628
assert(vec.values() == (std::vector<float>{1, 2, 3}));
29+
30+
assert_exception<std::invalid_argument>([&]{
31+
auto unused = SparseVector(map, 0);
32+
}, "sparsevec must have at least 1 dimension");
33+
34+
assert_exception<std::invalid_argument>([&]{
35+
auto unused = SparseVector(map, 4);
36+
}, "sparsevec index out of bounds");
2737
}
2838

2939
void test_sparsevec() {

0 commit comments

Comments
 (0)