1- #include < cassert>
21#include < optional>
32#include < string>
43#include < string_view>
@@ -34,48 +33,46 @@ void test_vector(pqxx::connection &conn) {
3433
3534 pqxx::nontransaction tx (conn);
3635 auto embedding = pgvector::Vector ({1 , 2 , 3 });
37- assert (embedding.dimensions () == 3 );
3836 float arr[] = {4 , 5 , 6 };
3937 auto embedding2 = pgvector::Vector (std::span{arr, 3 });
4038 tx.exec (" INSERT INTO items (embedding) VALUES ($1), ($2), ($3)" , {embedding, embedding2, std::nullopt });
4139
4240 pqxx::result res = tx.exec (" SELECT embedding FROM items ORDER BY embedding <-> $1" , {embedding2});
43- assert (res.size () == 3 );
44- assert (res[0 ][0 ].as <pgvector::Vector>() == embedding2);
45- assert (res[1 ][0 ].as <pgvector::Vector>() == embedding);
46- assert (!res[2 ][0 ].as <std::optional<pgvector::Vector>>().has_value ());
41+ assert_equal (res.size (), 3 );
42+ assert_equal (res[0 ][0 ].as <pgvector::Vector>(), embedding2);
43+ assert_equal (res[1 ][0 ].as <pgvector::Vector>(), embedding);
44+ assert_true (!res[2 ][0 ].as <std::optional<pgvector::Vector>>().has_value ());
4745}
4846
4947void test_halfvec (pqxx::connection &conn) {
5048 before_each (conn);
5149
5250 pqxx::nontransaction tx (conn);
5351 auto embedding = pgvector::HalfVector ({1 , 2 , 3 });
54- assert (embedding.dimensions () == 3 );
5552 float arr[] = {4 , 5 , 6 };
5653 auto embedding2 = pgvector::HalfVector (std::span{arr, 3 });
5754 tx.exec (" INSERT INTO items (half_embedding) VALUES ($1), ($2), ($3)" , {embedding, embedding2, std::nullopt });
5855
5956 pqxx::result res = tx.exec (" SELECT half_embedding FROM items ORDER BY half_embedding <-> $1" , {embedding2});
60- assert (res.size () == 3 );
61- assert (res[0 ][0 ].as <pgvector::HalfVector>() == embedding2);
62- assert (res[1 ][0 ].as <pgvector::HalfVector>() == embedding);
63- assert (!res[2 ][0 ].as <std::optional<pgvector::HalfVector>>().has_value ());
57+ assert_equal (res.size (), 3 );
58+ assert_equal (res[0 ][0 ].as <pgvector::HalfVector>(), embedding2);
59+ assert_equal (res[1 ][0 ].as <pgvector::HalfVector>(), embedding);
60+ assert_true (!res[2 ][0 ].as <std::optional<pgvector::HalfVector>>().has_value ());
6461}
6562
6663void test_bit (pqxx::connection &conn) {
6764 before_each (conn);
6865
6966 pqxx::nontransaction tx (conn);
70- auto embedding = " 101" ;
71- auto embedding2 = " 111" ;
67+ std::string embedding = " 101" ;
68+ std::string embedding2 = " 111" ;
7269 tx.exec (" INSERT INTO items (binary_embedding) VALUES ($1), ($2), ($3)" , {embedding, embedding2, std::nullopt });
7370
7471 pqxx::result res = tx.exec (" SELECT binary_embedding FROM items ORDER BY binary_embedding <~> $1" , pqxx::params{embedding2});
75- assert (res.size () == 3 );
76- assert (res[0 ][0 ].as <std::string>() == embedding2);
77- assert (res[1 ][0 ].as <std::string>() == embedding);
78- assert (!res[2 ][0 ].as <std::optional<std::string>>().has_value ());
72+ assert_equal (res.size (), 3 );
73+ assert_equal (res[0 ][0 ].as <std::string>(), embedding2);
74+ assert_equal (res[1 ][0 ].as <std::string>(), embedding);
75+ assert_true (!res[2 ][0 ].as <std::optional<std::string>>().has_value ());
7976}
8077
8178void test_sparsevec (pqxx::connection &conn) {
@@ -87,10 +84,10 @@ void test_sparsevec(pqxx::connection &conn) {
8784 tx.exec (" INSERT INTO items (sparse_embedding) VALUES ($1), ($2), ($3)" , {embedding, embedding2, std::nullopt });
8885
8986 pqxx::result res = tx.exec (" SELECT sparse_embedding FROM items ORDER BY sparse_embedding <-> $1" , {embedding2});
90- assert (res.size () == 3 );
91- assert (res[0 ][0 ].as <pgvector::SparseVector>() == embedding2);
92- assert (res[1 ][0 ].as <pgvector::SparseVector>() == embedding);
93- assert (!res[2 ][0 ].as <std::optional<pgvector::SparseVector>>().has_value ());
87+ assert_equal (res.size (), 3 );
88+ assert_equal (res[0 ][0 ].as <pgvector::SparseVector>(), embedding2);
89+ assert_equal (res[1 ][0 ].as <pgvector::SparseVector>(), embedding);
90+ assert_true (!res[2 ][0 ].as <std::optional<pgvector::SparseVector>>().has_value ());
9491}
9592
9693void test_sparsevec_nnz (pqxx::connection &conn) {
@@ -111,11 +108,11 @@ void test_stream(pqxx::connection &conn) {
111108 auto embedding = pgvector::Vector ({1 , 2 , 3 });
112109 tx.exec (" INSERT INTO items (embedding) VALUES ($1)" , {embedding});
113110 int count = 0 ;
114- for (auto [id, embedding ] : tx.stream <int , pgvector::Vector>(" SELECT id, embedding FROM items WHERE embedding IS NOT NULL" )) {
115- assert (embedding. dimensions () == 3 );
111+ for (auto [id, embedding2 ] : tx.stream <int , pgvector::Vector>(" SELECT id, embedding FROM items WHERE embedding IS NOT NULL" )) {
112+ assert_equal (embedding2, embedding );
116113 count++;
117114 }
118- assert (count == 1 );
115+ assert_equal (count, 1 );
119116}
120117
121118void test_stream_to (pqxx::connection &conn) {
@@ -127,8 +124,8 @@ void test_stream_to(pqxx::connection &conn) {
127124 stream.write_values (pgvector::Vector ({4 , 5 , 6 }));
128125 stream.complete ();
129126 pqxx::result res = tx.exec (" SELECT embedding FROM items ORDER BY id" );
130- assert (res[0 ][0 ].as <std::string>() == " [1,2,3]" );
131- assert (res[1 ][0 ].as <std::string>() == " [4,5,6]" );
127+ assert_true (res[0 ][0 ].as <std::string>() == " [1,2,3]" );
128+ assert_true (res[1 ][0 ].as <std::string>() == " [4,5,6]" );
132129}
133130
134131void test_precision (pqxx::connection &conn) {
@@ -139,21 +136,21 @@ void test_precision(pqxx::connection &conn) {
139136 tx.exec (" INSERT INTO items (embedding) VALUES ($1)" , {embedding});
140137 tx.exec (" SET extra_float_digits = 3" );
141138 pqxx::result res = tx.exec (" SELECT embedding FROM items ORDER BY id DESC LIMIT 1" );
142- assert (res[0 ][0 ].as <pgvector::Vector>() == embedding);
139+ assert_equal (res[0 ][0 ].as <pgvector::Vector>(), embedding);
143140}
144141
145142void test_vector_to_string () {
146- assert (pqxx::to_string (pgvector::Vector ({1 , 2 , 3 })) == " [1,2,3]" );
147- assert (pqxx::to_string (pgvector::Vector ({-1.234567890123 })) == " [-1.2345679]" );
143+ assert_true (pqxx::to_string (pgvector::Vector ({1 , 2 , 3 })) == " [1,2,3]" );
144+ assert_true (pqxx::to_string (pgvector::Vector ({-1.234567890123 })) == " [-1.2345679]" );
148145
149146 assert_exception<pqxx::conversion_overrun>([] {
150147 auto unused = pqxx::to_string (pgvector::Vector (std::vector<float >(16001 )));
151148 }, " vector cannot have more than 16000 dimensions" );
152149}
153150
154151void test_vector_from_string () {
155- assert (pqxx::from_string<pgvector::Vector>(" [1,2,3]" ) == pgvector::Vector ({1 , 2 , 3 }));
156- assert (pqxx::from_string<pgvector::Vector>(" []" ) == pgvector::Vector (std::vector<float >{}));
152+ assert_equal (pqxx::from_string<pgvector::Vector>(" [1,2,3]" ), pgvector::Vector ({1 , 2 , 3 }));
153+ assert_equal (pqxx::from_string<pgvector::Vector>(" []" ), pgvector::Vector (std::vector<float >{}));
157154
158155 assert_exception<pqxx::conversion_error>([] {
159156 auto unused = pqxx::from_string<pgvector::Vector>(" " );
@@ -181,17 +178,17 @@ void test_vector_from_string() {
181178}
182179
183180void test_halfvec_to_string () {
184- assert (pqxx::to_string (pgvector::HalfVector ({1 , 2 , 3 })) == " [1,2,3]" );
185- assert (pqxx::to_string (pgvector::HalfVector ({-1.234567890123 })) == " [-1.2345679]" );
181+ assert_true (pqxx::to_string (pgvector::HalfVector ({1 , 2 , 3 })) == " [1,2,3]" );
182+ assert_true (pqxx::to_string (pgvector::HalfVector ({-1.234567890123 })) == " [-1.2345679]" );
186183
187184 assert_exception<pqxx::conversion_overrun>([] {
188185 auto unused = pqxx::to_string (pgvector::HalfVector (std::vector<float >(16001 )));
189186 }, " halfvec cannot have more than 16000 dimensions" );
190187}
191188
192189void test_halfvec_from_string () {
193- assert (pqxx::from_string<pgvector::HalfVector>(" [1,2,3]" ) == pgvector::HalfVector ({1 , 2 , 3 }));
194- assert (pqxx::from_string<pgvector::HalfVector>(" []" ) == pgvector::HalfVector (std::vector<float >{}));
190+ assert_equal (pqxx::from_string<pgvector::HalfVector>(" [1,2,3]" ), pgvector::HalfVector ({1 , 2 , 3 }));
191+ assert_equal (pqxx::from_string<pgvector::HalfVector>(" []" ), pgvector::HalfVector (std::vector<float >{}));
195192
196193 assert_exception<pqxx::conversion_error>([] {
197194 auto unused = pqxx::from_string<pgvector::HalfVector>(" " );
@@ -219,14 +216,14 @@ void test_halfvec_from_string() {
219216}
220217
221218void test_sparsevec_to_string () {
222- assert (pqxx::to_string (pgvector::SparseVector ({1 , 0 , 2 , 0 , 3 , 0 })) == " {1:1,3:2,5:3}/6" );
219+ assert_true (pqxx::to_string (pgvector::SparseVector ({1 , 0 , 2 , 0 , 3 , 0 })) == " {1:1,3:2,5:3}/6" );
223220 std::unordered_map<int , float > map = {{999999999 , -1.234567890123 }};
224- assert (pqxx::to_string (pgvector::SparseVector (map, 1000000000 )) == " {1000000000:-1.2345679}/1000000000" );
221+ assert_true (pqxx::to_string (pgvector::SparseVector (map, 1000000000 )) == " {1000000000:-1.2345679}/1000000000" );
225222}
226223
227224void test_sparsevec_from_string () {
228- assert (pqxx::from_string<pgvector::SparseVector>(" {1:1,3:2,5:3}/6" ) == pgvector::SparseVector ({1 , 0 , 2 , 0 , 3 , 0 }));
229- assert (pqxx::from_string<pgvector::SparseVector>(" {}/6" ) == pgvector::SparseVector ({0 , 0 , 0 , 0 , 0 , 0 }));
225+ assert_equal (pqxx::from_string<pgvector::SparseVector>(" {1:1,3:2,5:3}/6" ), pgvector::SparseVector ({1 , 0 , 2 , 0 , 3 , 0 }));
226+ assert_equal (pqxx::from_string<pgvector::SparseVector>(" {}/6" ), pgvector::SparseVector ({0 , 0 , 0 , 0 , 0 , 0 }));
230227
231228 assert_exception<pqxx::conversion_error>([] {
232229 auto unused = pqxx::from_string<pgvector::SparseVector>(" " );
0 commit comments