Add more tests

This commit is contained in:
jun 2025-02-25 15:06:53 +01:00
parent 6abe12c4c1
commit 7a990c810e

View File

@ -407,10 +407,93 @@ struct DbufCstringTest : public Test
TEST_CHECK(str_dbuf.size == 0 && str_dbuf.capacity == 0 && !str_dbuf.data);
}
void test_find(int n = 32)
{
if (n < 2) {
n = 2;
}
std::vector<std::string> str_vec {};
dbuf_heap_cstr str_dbuf = {};
dbuf_heap_cstr_init(&str_dbuf, 0, &guf_allocator_libc);
for (int i = 0; i < n; ++i) {
constexpr int BUF_SZ = 128;
char buf[BUF_SZ];
memset(buf, '\0', BUF_SZ);
snprintf(buf, BUF_SZ, "String number %d", i);
dbuf_heap_cstr_push_val_cpy(&str_dbuf, buf);
str_vec.push_back(buf);
}
char *move_me = strdup("Moved string");
dbuf_heap_cstr_push(&str_dbuf, &move_me, GUF_CPY_MOVE);
GUF_ASSERT_RELEASE(move_me == NULL);
str_vec.emplace_back("Moved string");
TEST_CHECK(std::ssize(str_vec) == str_dbuf.size);
for (ptrdiff_t i = 0; i < str_dbuf.size; ++i) {
char *needle = *dbuf_heap_cstr_at(&str_dbuf, i);
TEST_CHECK(str_vec.at(i) == needle);
TEST_CHECK(dbuf_heap_cstr_contains_val(&str_dbuf, needle));
dbuf_heap_cstr_iter fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), dbuf_heap_cstr_end(&str_dbuf), needle);
TEST_CHECK(!dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.cbegin(), str_vec.cend(), needle) != str_vec.end());
dbuf_heap_cstr_iter begin = dbuf_heap_cstr_iter_next(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), i);
dbuf_heap_cstr_iter end = dbuf_heap_cstr_end(&str_dbuf);
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, begin, end, needle);
TEST_CHECK(!dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.cbegin() + i, str_vec.cend(), needle) != str_vec.end());
begin = dbuf_heap_cstr_iter_next(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), i + 1);
end = dbuf_heap_cstr_end(&str_dbuf);
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, begin, end, needle);
TEST_CHECK(dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.cbegin() + i + 1, str_vec.cend(), needle) == str_vec.end());
// Reverse.
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_rbegin(&str_dbuf), dbuf_heap_cstr_rend(&str_dbuf), needle);
TEST_CHECK(!dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.crbegin(), str_vec.crend(), needle) != str_vec.rend());
}
char needle[] = "Definitely not inside";
dbuf_heap_cstr_iter fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), dbuf_heap_cstr_end(&str_dbuf), needle);
TEST_CHECK(dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.cbegin(), str_vec.cend(), needle) == str_vec.end());
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_rbegin(&str_dbuf), dbuf_heap_cstr_rend(&str_dbuf), needle);
TEST_CHECK(dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.crbegin(), str_vec.crend(), needle) == str_vec.rend());
char *needle2 = *dbuf_heap_cstr_at(&str_dbuf, 0);
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_iter_next(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), 1), dbuf_heap_cstr_end(&str_dbuf), needle2);
TEST_CHECK(dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.cbegin() + 1, str_vec.cend(), needle2) == str_vec.end());
needle2 = *dbuf_heap_cstr_back(&str_dbuf);
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_iter_next(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), 1), dbuf_heap_cstr_iter_next(&str_dbuf, dbuf_heap_cstr_end(&str_dbuf), -1), needle2);
TEST_CHECK(dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.begin(), str_vec.end() - 1, needle2) == (str_vec.end() - 1));
needle2 = *dbuf_heap_cstr_at(&str_dbuf, 0);
fnd_it = dbuf_heap_cstr_find_val(&str_dbuf, dbuf_heap_cstr_begin(&str_dbuf), dbuf_heap_cstr_begin(&str_dbuf), needle2);
TEST_CHECK(dbuf_heap_cstr_iter_is_end(&str_dbuf, fnd_it));
TEST_CHECK(std::find(str_vec.cbegin(), str_vec.cbegin(), needle2) == str_vec.cbegin());
dbuf_heap_cstr_free(&str_dbuf, NULL);
}
public:
bool run()
{
push_check_name("push_insert_erase");
for (int i = 1; i <= 32; ++i) {
test_push_insert_erase(i);
test_push_insert_erase(i, i - 1);
@ -440,6 +523,15 @@ struct DbufCstringTest : public Test
dbuf_heap_cstr_free(&str_dbuf, NULL);
TEST_CHECK(str_dbuf.size == 0 && str_dbuf.capacity == 0 && !str_dbuf.data);
pop_check_name();
push_check_name("find");
test_find();
test_find(3);
test_find(42);
test_find(129);
pop_check_name();
done = true;
passed = (num_failed_checks == 0);
return passed;