Skip to content

Commit 165d80a

Browse files
committed
Merge branch 'sk/unit-tests' into seen
* 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
2 parents 2e4c216 + 50ba75f commit 165d80a

File tree

8 files changed

+137
-147
lines changed

8 files changed

+137
-147
lines changed

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,9 @@ THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
13531353

13541354
CLAR_TEST_SUITES += u-ctype
13551355
CLAR_TEST_SUITES += u-hash
1356+
CLAR_TEST_SUITES += u-mem-pool
1357+
CLAR_TEST_SUITES += u-prio-queue
1358+
CLAR_TEST_SUITES += u-reftable-tree
13561359
CLAR_TEST_SUITES += u-strvec
13571360
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13581361
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1361,11 +1364,9 @@ CLAR_TEST_OBJS += $(UNIT_TEST_DIR)/unit-test.o
13611364

13621365
UNIT_TEST_PROGRAMS += t-example-decorate
13631366
UNIT_TEST_PROGRAMS += t-hashmap
1364-
UNIT_TEST_PROGRAMS += t-mem-pool
13651367
UNIT_TEST_PROGRAMS += t-oid-array
13661368
UNIT_TEST_PROGRAMS += t-oidmap
13671369
UNIT_TEST_PROGRAMS += t-oidtree
1368-
UNIT_TEST_PROGRAMS += t-prio-queue
13691370
UNIT_TEST_PROGRAMS += t-reftable-basics
13701371
UNIT_TEST_PROGRAMS += t-reftable-block
13711372
UNIT_TEST_PROGRAMS += t-reftable-merged
@@ -1374,7 +1375,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
13741375
UNIT_TEST_PROGRAMS += t-reftable-readwrite
13751376
UNIT_TEST_PROGRAMS += t-reftable-record
13761377
UNIT_TEST_PROGRAMS += t-reftable-stack
1377-
UNIT_TEST_PROGRAMS += t-reftable-tree
13781378
UNIT_TEST_PROGRAMS += t-strbuf
13791379
UNIT_TEST_PROGRAMS += t-strcmp-offset
13801380
UNIT_TEST_PROGRAMS += t-trailer

t/meson.build

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
clar_test_suites = [
22
'unit-tests/u-ctype.c',
33
'unit-tests/u-hash.c',
4+
'unit-tests/u-mem-pool.c',
5+
'unit-tests/u-prio-queue.c',
6+
'unit-tests/u-reftable-tree.c',
47
'unit-tests/u-strvec.c',
58
]
69

@@ -43,11 +46,9 @@ test('unit-tests', clar_unit_tests)
4346
unit_test_programs = [
4447
'unit-tests/t-example-decorate.c',
4548
'unit-tests/t-hashmap.c',
46-
'unit-tests/t-mem-pool.c',
4749
'unit-tests/t-oid-array.c',
4850
'unit-tests/t-oidmap.c',
4951
'unit-tests/t-oidtree.c',
50-
'unit-tests/t-prio-queue.c',
5152
'unit-tests/t-reftable-basics.c',
5253
'unit-tests/t-reftable-block.c',
5354
'unit-tests/t-reftable-merged.c',
@@ -56,7 +57,6 @@ unit_test_programs = [
5657
'unit-tests/t-reftable-readwrite.c',
5758
'unit-tests/t-reftable-record.c',
5859
'unit-tests/t-reftable-stack.c',
59-
'unit-tests/t-reftable-tree.c',
6060
'unit-tests/t-strbuf.c',
6161
'unit-tests/t-strcmp-offset.c',
6262
'unit-tests/t-trailer.c',

t/unit-tests/generate-clar-decls.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ do
1414
suite_name=$(basename "$suite")
1515
suite_name=${suite_name%.c}
1616
suite_name=${suite_name#u-}
17+
suite_name=$(echo "$suite_name" | tr '-' '_')
1718
sed -ne "s/^\(void test_${suite_name}__[a-zA-Z_0-9][a-zA-Z_0-9]*(void)\)$/extern \1;/p" "$suite" ||
1819
exit 1
1920
done >"$OUTPUT"

t/unit-tests/t-mem-pool.c

Lines changed: 0 additions & 31 deletions
This file was deleted.

t/unit-tests/t-prio-queue.c

Lines changed: 0 additions & 91 deletions
This file was deleted.

t/unit-tests/u-mem-pool.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "unit-test.h"
2+
#include "mem-pool.h"
3+
4+
static void test_many_pool_allocations(size_t block_alloc)
5+
{
6+
struct mem_pool pool = { .block_alloc = block_alloc };
7+
size_t size = 100;
8+
char *buffer = mem_pool_calloc(&pool, 1, size);
9+
for (size_t i = 0; i < size; i++)
10+
cl_assert_equal_i(0, buffer[i]);
11+
cl_assert(pool.mp_block != NULL);
12+
cl_assert(pool.mp_block->next_free != NULL);
13+
cl_assert(pool.mp_block->end != NULL);
14+
mem_pool_discard(&pool, 0);
15+
}
16+
17+
void test_mem_pool__big_block(void)
18+
{
19+
test_many_pool_allocations(1024 * 1024);
20+
}
21+
22+
void test_mem_pool__tiny_block(void)
23+
{
24+
test_many_pool_allocations(1);
25+
}

t/unit-tests/u-prio-queue.c

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#include "unit-test.h"
2+
#include "prio-queue.h"
3+
4+
static int intcmp(const void *va, const void *vb, void *data UNUSED)
5+
{
6+
const int *a = va, *b = vb;
7+
return *a - *b;
8+
}
9+
10+
11+
#define MISSING -1
12+
#define DUMP -2
13+
#define STACK -3
14+
#define GET -4
15+
#define REVERSE -5
16+
17+
static int show(int *v)
18+
{
19+
return v ? *v : MISSING;
20+
}
21+
22+
static void test_prio_queue(int *input, size_t input_size,
23+
int *result, size_t result_size)
24+
{
25+
struct prio_queue pq = { intcmp };
26+
size_t j = 0;
27+
28+
for (size_t i = 0; i < input_size; i++) {
29+
void *peek, *get;
30+
switch(input[i]) {
31+
case GET:
32+
peek = prio_queue_peek(&pq);
33+
get = prio_queue_get(&pq);
34+
cl_assert(peek == get);
35+
cl_assert(j < result_size);
36+
cl_assert_equal_i(result[j], show(get));
37+
j++;
38+
break;
39+
case DUMP:
40+
while ((peek = prio_queue_peek(&pq))) {
41+
get = prio_queue_get(&pq);
42+
cl_assert(peek == get);
43+
cl_assert(j < result_size);
44+
cl_assert_equal_i(result[j], show(get));
45+
j++;
46+
}
47+
break;
48+
case STACK:
49+
pq.compare = NULL;
50+
break;
51+
case REVERSE:
52+
prio_queue_reverse(&pq);
53+
break;
54+
default:
55+
prio_queue_put(&pq, &input[i]);
56+
break;
57+
}
58+
}
59+
cl_assert_equal_i(j, result_size);
60+
clear_prio_queue(&pq);
61+
}
62+
63+
#define TEST_INPUT(input, result) \
64+
test_prio_queue(input, ARRAY_SIZE(input), result, ARRAY_SIZE(result))
65+
66+
void test_prio_queue__basic(void)
67+
{
68+
TEST_INPUT(((int []){ 2, 6, 3, 10, 9, 5, 7, 4, 5, 8, 1, DUMP }),
69+
((int []){ 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 10 }));
70+
}
71+
72+
void test_prio_queue__mixed(void)
73+
{
74+
TEST_INPUT(((int []){ 6, 2, 4, GET, 5, 3, GET, GET, 1, DUMP }),
75+
((int []){ 2, 3, 4, 1, 5, 6 }));
76+
}
77+
78+
void test_prio_queue__empty(void)
79+
{
80+
TEST_INPUT(((int []){ 1, 2, GET, GET, GET, 1, 2, GET, GET, GET }),
81+
((int []){ 1, 2, MISSING, 1, 2, MISSING }));
82+
}
83+
84+
void test_prio_queue__stack(void)
85+
{
86+
TEST_INPUT(((int []){ STACK, 8, 1, 5, 4, 6, 2, 3, DUMP }),
87+
((int []){ 3, 2, 6, 4, 5, 1, 8 }));
88+
}
89+
90+
void test_prio_queue__reverse_stack(void)
91+
{
92+
TEST_INPUT(((int []){ STACK, 1, 2, 3, 4, 5, 6, REVERSE, DUMP }),
93+
((int []){ 1, 2, 3, 4, 5, 6 }));
94+
}

t/unit-tests/t-reftable-tree.c renamed to t/unit-tests/u-reftable-tree.c

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ license that can be found in the LICENSE file or at
66
https://developers.google.com/open-source/licenses/bsd
77
*/
88

9-
#include "test-lib.h"
9+
#include "unit-test.h"
1010
#include "reftable/tree.h"
1111

1212
static int t_compare(const void *a, const void *b)
@@ -25,7 +25,7 @@ static void store(void *arg, void *key)
2525
c->arr[c->len++] = key;
2626
}
2727

28-
static void t_tree_search(void)
28+
void test_reftable_tree__tree_search(void)
2929
{
3030
struct tree_node *root = NULL;
3131
void *values[11] = { 0 };
@@ -38,20 +38,20 @@ static void t_tree_search(void)
3838
*/
3939
do {
4040
nodes[i] = tree_insert(&root, &values[i], &t_compare);
41-
check(nodes[i] != NULL);
41+
cl_assert(nodes[i] != NULL);
4242
i = (i * 7) % 11;
4343
} while (i != 1);
4444

4545
for (i = 1; i < ARRAY_SIZE(nodes); i++) {
46-
check_pointer_eq(&values[i], nodes[i]->key);
47-
check_pointer_eq(nodes[i], tree_search(root, &values[i], &t_compare));
46+
cl_assert_equal_p(&values[i], nodes[i]->key);
47+
cl_assert_equal_p(nodes[i], tree_search(root, &values[i], &t_compare));
4848
}
4949

50-
check(!tree_search(root, values, t_compare));
50+
cl_assert(tree_search(root, values, t_compare) == NULL);
5151
tree_free(root);
5252
}
5353

54-
static void t_infix_walk(void)
54+
void test_reftable_tree__infix_walk(void)
5555
{
5656
struct tree_node *root = NULL;
5757
void *values[11] = { 0 };
@@ -64,23 +64,15 @@ static void t_infix_walk(void)
6464

6565
do {
6666
struct tree_node *node = tree_insert(&root, &values[i], t_compare);
67-
check(node != NULL);
67+
cl_assert(node != NULL);
6868
i = (i * 7) % 11;
6969
count++;
7070
} while (i != 1);
7171

7272
infix_walk(root, &store, &c);
7373
for (i = 1; i < ARRAY_SIZE(values); i++)
74-
check_pointer_eq(&values[i], out[i - 1]);
75-
check(!out[i - 1]);
76-
check_int(c.len, ==, count);
74+
cl_assert_equal_p(&values[i], out[i - 1]);
75+
cl_assert(out[i - 1] == NULL);
76+
cl_assert_equal_i(c.len, count);
7777
tree_free(root);
7878
}
79-
80-
int cmd_main(int argc UNUSED, const char *argv[] UNUSED)
81-
{
82-
TEST(t_tree_search(), "tree_search works");
83-
TEST(t_infix_walk(), "infix_walk works");
84-
85-
return test_done();
86-
}

0 commit comments

Comments
 (0)