Skip to content

Commit 321185e

Browse files
committed
Added tests for to_buf and into_buf functions
1 parent 7c51883 commit 321185e

1 file changed

Lines changed: 67 additions & 0 deletions

File tree

test/pqxx_test.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,66 @@ void test_sparsevec_from_string() {
280280
}, "Could not convert 'a' to int: Invalid argument.");
281281
}
282282

283+
void test_vector_to_buf() {
284+
char buf[10];
285+
assert_equal(pqxx::to_buf(std::span<char>{buf}, pgvector::Vector{{1, 2, 3}}), "[1,2,3]");
286+
287+
assert_exception<pqxx::conversion_overrun>([] {
288+
return pqxx::to_buf(std::span<char>{}, pgvector::Vector{{1, 2, 3}});
289+
});
290+
}
291+
292+
void test_vector_into_buf() {
293+
char buf[10];
294+
size_t size = pqxx::into_buf(std::span<char>{buf}, pgvector::Vector{{1, 2, 3}});
295+
assert_equal(size, 7u);
296+
assert_equal(std::string_view{buf, size}, "[1,2,3]");
297+
298+
assert_exception<pqxx::conversion_overrun>([] {
299+
return pqxx::into_buf(std::span<char>{}, pgvector::Vector{{1, 2, 3}});
300+
});
301+
}
302+
303+
void test_halfvec_to_buf() {
304+
char buf[10];
305+
assert_equal(pqxx::to_buf(std::span<char>{buf}, pgvector::HalfVector{{1, 2, 3}}), "[1,2,3]");
306+
307+
assert_exception<pqxx::conversion_overrun>([] {
308+
return pqxx::to_buf(std::span<char>{}, pgvector::HalfVector{{1, 2, 3}});
309+
});
310+
}
311+
312+
void test_halfvec_into_buf() {
313+
char buf[10];
314+
size_t size = pqxx::into_buf(std::span<char>{buf}, pgvector::HalfVector{{1, 2, 3}});
315+
assert_equal(size, 7u);
316+
assert_equal(std::string_view{buf, size}, "[1,2,3]");
317+
318+
assert_exception<pqxx::conversion_overrun>([] {
319+
return pqxx::into_buf(std::span<char>{}, pgvector::HalfVector{{1, 2, 3}});
320+
});
321+
}
322+
323+
void test_sparsevec_to_buf() {
324+
char buf[40];
325+
assert_equal(pqxx::to_buf(std::span<char>{buf}, pgvector::SparseVector{{1, 2, 3}}), "{1:1,2:2,3:3}/3");
326+
327+
assert_exception<pqxx::conversion_overrun>([] {
328+
return pqxx::to_buf(std::span<char>{}, pgvector::SparseVector{{1, 2, 3}});
329+
});
330+
}
331+
332+
void test_sparsevec_into_buf() {
333+
char buf[40];
334+
size_t size = pqxx::into_buf(std::span<char>{buf}, pgvector::SparseVector{{1, 2, 3}});
335+
assert_equal(size, 15u);
336+
assert_equal(std::string_view{buf, size}, "{1:1,2:2,3:3}/3");
337+
338+
assert_exception<pqxx::conversion_overrun>([] {
339+
return pqxx::into_buf(std::span<char>{}, pgvector::SparseVector{{1, 2, 3}});
340+
});
341+
}
342+
283343
void test_pqxx() {
284344
pqxx::connection conn{"dbname=pgvector_cpp_test"};
285345
setup(conn);
@@ -299,4 +359,11 @@ void test_pqxx() {
299359
test_halfvec_from_string();
300360
test_sparsevec_to_string();
301361
test_sparsevec_from_string();
362+
363+
test_vector_to_buf();
364+
test_vector_into_buf();
365+
test_halfvec_to_buf();
366+
test_halfvec_into_buf();
367+
test_sparsevec_to_buf();
368+
test_sparsevec_into_buf();
302369
}

0 commit comments

Comments
 (0)