From 81a5b4a3c8519557107be3d3169e825fb0f7c1a4 Mon Sep 17 00:00:00 2001 From: Robear Selwans Date: Sun, 3 May 2026 20:21:33 +0300 Subject: [PATCH] Added more tests --- tests/ev_hash/meson.build | 8 +++ tests/ev_hash/seed_high_bits.c | 22 ++++++++ tests/ev_str/find_first_empty_query.c | 17 ++++++ tests/ev_str/meson.build | 1 + tests/ev_vec/meson.build | 1 + .../ev_vec/pop_out_frees_vector_owned_elem.c | 55 +++++++++++++++++++ tests/meson.build | 1 + 7 files changed, 105 insertions(+) create mode 100644 tests/ev_hash/meson.build create mode 100644 tests/ev_hash/seed_high_bits.c create mode 100644 tests/ev_str/find_first_empty_query.c create mode 100644 tests/ev_vec/pop_out_frees_vector_owned_elem.c diff --git a/tests/ev_hash/meson.build b/tests/ev_hash/meson.build new file mode 100644 index 0000000..74b1e17 --- /dev/null +++ b/tests/ev_hash/meson.build @@ -0,0 +1,8 @@ +tests = [ + 'seed_high_bits', +] + +foreach t : tests + exec = executable(t, t+'.c', include_directories: headers_include, c_args: evh_c_args) + test(t, exec, suite: 'hash') +endforeach diff --git a/tests/ev_hash/seed_high_bits.c b/tests/ev_hash/seed_high_bits.c new file mode 100644 index 0000000..cda3488 --- /dev/null +++ b/tests/ev_hash/seed_high_bits.c @@ -0,0 +1,22 @@ +#define EV_HASH_IMPLEMENTATION +#include "ev_macros.h" +#include "ev_hash.h" + +#include +#include + +int main(void) +{ + const char *data = "same input"; + + u64 low_seed = 1; + u64 high_seed = (1ull << 32) | 1ull; + + u64 low_hash = ev_hash_murmur3(data, (u32)strlen(data), low_seed); + u64 high_hash = ev_hash_murmur3(data, (u32)strlen(data), high_seed); + + assert(low_seed != high_seed); + assert(low_hash != high_hash); + + return 0; +} diff --git a/tests/ev_str/find_first_empty_query.c b/tests/ev_str/find_first_empty_query.c new file mode 100644 index 0000000..07c57f0 --- /dev/null +++ b/tests/ev_str/find_first_empty_query.c @@ -0,0 +1,17 @@ +#define EV_STR_IMPLEMENTATION +#include "ev_str.h" + +#include + +int main(void) +{ + evstring text = evstr("abc"); + evstring query = evstr(""); + + evstring_view match = evstring_findFirst(text, query); + + assert(match.len == 0); + assert(match.offset == ~0ull); + + return 0; +} diff --git a/tests/ev_str/meson.build b/tests/ev_str/meson.build index caf0826..3e95197 100644 --- a/tests/ev_str/meson.build +++ b/tests/ev_str/meson.build @@ -10,6 +10,7 @@ tests = [ 'find_first_overlapping_prefix', 'stack_get_space', 'overlapping_push', + 'find_first_empty_query', ] foreach t : tests diff --git a/tests/ev_vec/meson.build b/tests/ev_vec/meson.build index 31f1526..75f46fa 100644 --- a/tests/ev_vec/meson.build +++ b/tests/ev_vec/meson.build @@ -3,6 +3,7 @@ tests = [ 'reducecap_updatelen', 'reducecap_free_elems', 'reducelen_free_elems', + 'pop_out_frees_vector_owned_elem', ] foreach t : tests diff --git a/tests/ev_vec/pop_out_frees_vector_owned_elem.c b/tests/ev_vec/pop_out_frees_vector_owned_elem.c new file mode 100644 index 0000000..a001393 --- /dev/null +++ b/tests/ev_vec/pop_out_frees_vector_owned_elem.c @@ -0,0 +1,55 @@ +#define EV_VEC_IMPLEMENTATION +#include "ev_vec.h" + +#include +#include + +static int free_calls = 0; + +typedef struct OwningInt { + int *ptr; +} OwningInt; + +DEFINE_COPY_FUNCTION(OwningInt, Default) +{ + dst->ptr = malloc(sizeof(*dst->ptr)); + assert(dst->ptr != NULL); + *dst->ptr = *src->ptr; +} + +DEFINE_FREE_FUNCTION(OwningInt, Default) +{ + assert(self->ptr != NULL); + free(self->ptr); + self->ptr = NULL; + free_calls++; +} + +TYPEDATA_GEN(OwningInt, COPY(Default), FREE(Default)); + +int main(void) +{ + ev_vec(OwningInt) v = ev_vec_init(OwningInt); + assert(v != NULL); + + OwningInt original = { .ptr = malloc(sizeof(*original.ptr)) }; + assert(original.ptr != NULL); + *original.ptr = 42; + + assert(ev_vec_push_impl(&v, &original) == 0); + free(original.ptr); + + OwningInt out = {0}; + assert(ev_vec_pop(&v, &out) == EV_VEC_ERR_NONE); + assert(out.ptr != NULL); + assert(*out.ptr == 42); + + FREE_FUNCTION(OwningInt, Default)(&out); + + /* Popping with an output copy transfers no pointer from the vector element. + The vector's own deep-copied element must still be destroyed. */ + assert(free_calls == 2); + + ev_vec_fini(&v); + return 0; +} diff --git a/tests/meson.build b/tests/meson.build index ef46572..250d6eb 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -1,3 +1,4 @@ +subdir('ev_hash') subdir('ev_log') subdir('ev_str') subdir('ev_types')