Merge branch 'sk/unit-tests'

Move a few more unit tests to the clar test framework.

* sk/unit-tests:
  t/unit-tests: convert reftable tree test to use clar test framework
  t/unit-tests: adapt priority queue test to use clar test framework
  t/unit-tests: convert mem-pool test to use clar test framework
  t/unit-tests: handle dashes in test suite filenames
This commit is contained in:
Junio C Hamano
2025-01-28 13:02:22 -08:00
8 changed files with 137 additions and 147 deletions

View File

@ -1339,6 +1339,9 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
CLAR_TEST_SUITES += u-ctype
CLAR_TEST_SUITES += u-hash
CLAR_TEST_SUITES += u-mem-pool
CLAR_TEST_SUITES += u-prio-queue
CLAR_TEST_SUITES += u-reftable-tree
CLAR_TEST_SUITES += u-strvec
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@ -1347,11 +1350,9 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
UNIT_TEST_PROGRAMS += t-example-decorate
UNIT_TEST_PROGRAMS += t-hashmap
UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-oid-array
UNIT_TEST_PROGRAMS += t-oidmap
UNIT_TEST_PROGRAMS += t-oidtree
UNIT_TEST_PROGRAMS += t-prio-queue
UNIT_TEST_PROGRAMS += t-reftable-basics
UNIT_TEST_PROGRAMS += t-reftable-block
UNIT_TEST_PROGRAMS += t-reftable-merged
@ -1360,7 +1361,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
UNIT_TEST_PROGRAMS += t-reftable-readwrite
UNIT_TEST_PROGRAMS += t-reftable-record
UNIT_TEST_PROGRAMS += t-reftable-stack
UNIT_TEST_PROGRAMS += t-reftable-tree
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-strcmp-offset
UNIT_TEST_PROGRAMS += t-trailer

View File

@ -1,6 +1,9 @@
clar_test_suites = [
'unit-tests/u-ctype.c',
'unit-tests/u-hash.c',
'unit-tests/u-mem-pool.c',
'unit-tests/u-prio-queue.c',
'unit-tests/u-reftable-tree.c',
'unit-tests/u-strvec.c',
]
@ -43,11 +46,9 @@ test('unit-tests', clar_unit_tests)
unit_test_programs = [
'unit-tests/t-example-decorate.c',
'unit-tests/t-hashmap.c',
'unit-tests/t-mem-pool.c',
'unit-tests/t-oid-array.c',
'unit-tests/t-oidmap.c',
'unit-tests/t-oidtree.c',
'unit-tests/t-prio-queue.c',
'unit-tests/t-reftable-basics.c',
'unit-tests/t-reftable-block.c',
'unit-tests/t-reftable-merged.c',
@ -56,7 +57,6 @@ unit_test_programs = [
'unit-tests/t-reftable-readwrite.c',
'unit-tests/t-reftable-record.c',
'unit-tests/t-reftable-stack.c',
'unit-tests/t-reftable-tree.c',
'unit-tests/t-strbuf.c',
'unit-tests/t-strcmp-offset.c',
'unit-tests/t-trailer.c',

View File

@ -14,6 +14,7 @@ do
suite_name=$(basename "$suite")
suite_name=${suite_name%.c}
suite_name=${suite_name#u-}
suite_name=$(echo "$suite_name" | tr '-' '_')
sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" ||
exit 1
done >"$OUTPUT"

View File

@ -1,31 +0,0 @@
#include "test-lib.h"
#include "mem-pool.h"
static void setup_static(void (*f)(struct mem_pool *), size_t block_alloc)
{
struct mem_pool pool = { .block_alloc = block_alloc };
f(&pool);
mem_pool_discard(&pool, 0);
}
static void t_calloc_100(struct mem_pool *pool)
{
size_t size = 100;
char *buffer = mem_pool_calloc(pool, 1, size);
for (size_t i = 0; i < size; i++)
check_int(buffer[i], ==, 0);
if (!check(pool->mp_block != NULL))
return;
check(pool->mp_block->next_free != NULL);
check(pool->mp_block->end != NULL);
}
int cmd_main(int argc UNUSED, const char **argv UNUSED)
{
TEST(setup_static(t_calloc_100, 1024 * 1024),
"mem_pool_calloc returns 100 zeroed bytes with big block");
TEST(setup_static(t_calloc_100, 1),
"mem_pool_calloc returns 100 zeroed bytes with tiny block");
return test_done();
}

View File

@ -1,91 +0,0 @@
#include "test-lib.h"
#include "prio-queue.h"
static int intcmp(const void *va, const void *vb, void *data UNUSED)
{
const int *a = va, *b = vb;
return *a - *b;
}
#define MISSING -1
#define DUMP -2
#define STACK -3
#define GET -4
#define REVERSE -5
static int show(int *v)
{
return v ? *v : MISSING;
}
static void test_prio_queue(int *input, size_t input_size,
int *result, size_t result_size)
{
struct prio_queue pq = { intcmp };
int j = 0;
for (size_t i = 0; i < input_size; i++) {
void *peek, *get;
switch(input[i]) {
case GET:
peek = prio_queue_peek(&pq);
get = prio_queue_get(&pq);
if (!check(peek == get))
return;
if (!check_uint(j, <, result_size))
break;
if (!check_int(result[j], ==, show(get)))
test_msg(" j: %d", j);
j++;
break;
case DUMP:
while ((peek = prio_queue_peek(&pq))) {
get = prio_queue_get(&pq);
if (!check(peek == get))
return;
if (!check_uint(j, <, result_size))
break;
if (!check_int(result[j], ==, show(get)))
test_msg(" j: %d", j);
j++;
}
break;
case STACK:
pq.compare = NULL;
break;
case REVERSE:
prio_queue_reverse(&pq);
break;
default:
prio_queue_put(&pq, &input[i]);
break;
}
}
check_uint(j, ==, result_size);
clear_prio_queue(&pq);
}
#define TEST_INPUT(input, result) \
test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
int cmd_main(int argc UNUSED, const char **argv UNUSED)
{
TEST(TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 })),
"prio-queue works for basic input");
TEST(TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
((int []){ 2, 3, 4, 1, 5, 6 })),
"prio-queue works for mixed put & get commands");
TEST(TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
((int []){ 1, 2, MISSING, 1, 2, MISSING })),
"prio-queue works when queue is empty");
TEST(TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
((int []){ 3, 2, 6, 4, 5, 1, 8 })),
"prio-queue works when used as a LIFO stack");
TEST(TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
((int []){ 1, 2, 3, 4, 5, 6 })),
"prio-queue works when LIFO stack is reversed");
return test_done();
}

25
t/unit-tests/u-mem-pool.c Normal file
View File

@ -0,0 +1,25 @@
#include "unit-test.h"
#include "mem-pool.h"
static void test_many_pool_allocations(size_t block_alloc)
{
struct mem_pool pool = { .block_alloc = block_alloc };
size_t size = 100;
char *buffer = mem_pool_calloc(&pool, 1, size);
for (size_t i = 0; i < size; i++)
cl_assert_equal_i(0, buffer[i]);
cl_assert(pool.mp_block != NULL);
cl_assert(pool.mp_block->next_free != NULL);
cl_assert(pool.mp_block->end != NULL);
mem_pool_discard(&pool, 0);
}
void test_mem_pool__big_block(void)
{
test_many_pool_allocations(1024 * 1024);
}
void test_mem_pool__tiny_block(void)
{
test_many_pool_allocations(1);
}

View File

@ -0,0 +1,94 @@
#include "unit-test.h"
#include "prio-queue.h"
static int intcmp(const void *va, const void *vb, void *data UNUSED)
{
const int *a = va, *b = vb;
return *a - *b;
}
#define MISSING -1
#define DUMP -2
#define STACK -3
#define GET -4
#define REVERSE -5
static int show(int *v)
{
return v ? *v : MISSING;
}
static void test_prio_queue(int *input, size_t input_size,
int *result, size_t result_size)
{
struct prio_queue pq = { intcmp };
size_t j = 0;
for (size_t i = 0; i < input_size; i++) {
void *peek, *get;
switch(input[i]) {
case GET:
peek = prio_queue_peek(&pq);
get = prio_queue_get(&pq);
cl_assert(peek == get);
cl_assert(j < result_size);
cl_assert_equal_i(result[j], show(get));
j++;
break;
case DUMP:
while ((peek = prio_queue_peek(&pq))) {
get = prio_queue_get(&pq);
cl_assert(peek == get);
cl_assert(j < result_size);
cl_assert_equal_i(result[j], show(get));
j++;
}
break;
case STACK:
pq.compare = NULL;
break;
case REVERSE:
prio_queue_reverse(&pq);
break;
default:
prio_queue_put(&pq, &input[i]);
break;
}
}
cl_assert_equal_i(j, result_size);
clear_prio_queue(&pq);
}
#define TEST_INPUT(input, result) \
test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
void test_prio_queue__basic(void)
{
TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 }));
}
void test_prio_queue__mixed(void)
{
TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
((int []){ 2, 3, 4, 1, 5, 6 }));
}
void test_prio_queue__empty(void)
{
TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
((int []){ 1, 2, MISSING, 1, 2, MISSING }));
}
void test_prio_queue__stack(void)
{
TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
((int []){ 3, 2, 6, 4, 5, 1, 8 }));
}
void test_prio_queue__reverse_stack(void)
{
TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
((int []){ 1, 2, 3, 4, 5, 6 }));
}

View File

@ -6,7 +6,7 @@ license that can be found in the LICENSE file or at
https://developers.google.com/open-source/licenses/bsd
*/
#include "test-lib.h"
#include "unit-test.h"
#include "reftable/tree.h"
static int t_compare(const void *a, const void *b)
@ -25,7 +25,7 @@ static void store(void *arg, void *key)
c->arr[c->len++] = key;
}
static void t_tree_search(void)
void test_reftable_tree__tree_search(void)
{
struct tree_node *root = NULL;
void *values[11] = { 0 };
@ -38,20 +38,20 @@ static void t_tree_search(void)
*/
do {
nodes[i] = tree_insert(&root, &values[i], &t_compare);
check(nodes[i] != NULL);
cl_assert(nodes[i] != NULL);
i = (i * 7) % 11;
} while (i != 1);
for (i = 1; i < ARRAY_SIZE(nodes); i++) {
check_pointer_eq(&values[i], nodes[i]->key);
check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare));
cl_assert_equal_p(&values[i], nodes[i]->key);
cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare));
}
check(!tree_search(root, values, t_compare));
cl_assert(tree_search(root, values, t_compare) == NULL);
tree_free(root);
}
static void t_infix_walk(void)
void test_reftable_tree__infix_walk(void)
{
struct tree_node *root = NULL;
void *values[11] = { 0 };
@ -64,23 +64,15 @@ static void t_infix_walk(void)
do {
struct tree_node *node = tree_insert(&root, &values[i], t_compare);
check(node != NULL);
cl_assert(node != NULL);
i = (i * 7) % 11;
count++;
} while (i != 1);
infix_walk(root, &store, &c);
for (i = 1; i < ARRAY_SIZE(values); i++)
check_pointer_eq(&values[i], out[i - 1]);
check(!out[i - 1]);
check_int(c.len, ==, count);
cl_assert_equal_p(&values[i], out[i - 1]);
cl_assert(out[i - 1] == NULL);
cl_assert_equal_i(c.len, count);
tree_free(root);
}
int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
{
TEST(t_tree_search(), "tree_search works");
TEST(t_infix_walk(), "infix_walk works");
return test_done();
}