Skip to content

Commit 4b99546

Browse files
Seyi007gitster
authored andcommitted
t/unit-tests: convert strbuf test to use clar test framework
Adapt strbuf test script to clar framework by using clar assertions where necessary. Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e0f807b commit 4b99546

File tree

4 files changed

+121
-124
lines changed

4 files changed

+121
-124
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,6 +1346,7 @@ CLAR_TEST_SUITES += u-hashmap
13461346
CLAR_TEST_SUITES += u-mem-pool
13471347
CLAR_TEST_SUITES += u-prio-queue
13481348
CLAR_TEST_SUITES += u-reftable-tree
1349+
CLAR_TEST_SUITES += u-strbuf
13491350
CLAR_TEST_SUITES += u-strvec
13501351
CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
13511352
CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1363,7 +1364,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
13631364
UNIT_TEST_PROGRAMS += t-reftable-readwrite
13641365
UNIT_TEST_PROGRAMS += t-reftable-record
13651366
UNIT_TEST_PROGRAMS += t-reftable-stack
1366-
UNIT_TEST_PROGRAMS += t-strbuf
13671367
UNIT_TEST_PROGRAMS += t-strcmp-offset
13681368
UNIT_TEST_PROGRAMS += t-trailer
13691369
UNIT_TEST_PROGRAMS += t-urlmatch-normalization

t/meson.build

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ clar_test_suites = [
66
'unit-tests/u-mem-pool.c',
77
'unit-tests/u-prio-queue.c',
88
'unit-tests/u-reftable-tree.c',
9+
'unit-tests/u-strbuf.c',
910
'unit-tests/u-strvec.c',
1011
]
1112

@@ -57,7 +58,6 @@ unit_test_programs = [
5758
'unit-tests/t-reftable-readwrite.c',
5859
'unit-tests/t-reftable-record.c',
5960
'unit-tests/t-reftable-stack.c',
60-
'unit-tests/t-strbuf.c',
6161
'unit-tests/t-strcmp-offset.c',
6262
'unit-tests/t-trailer.c',
6363
'unit-tests/t-urlmatch-normalization.c',

t/unit-tests/t-strbuf.c

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

t/unit-tests/u-strbuf.c

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#include "unit-test.h"
2+
#include "strbuf.h"
3+
4+
/* wrapper that supplies tests with an empty, initialized strbuf */
5+
static void setup(void (*f)(struct strbuf*, const void*),
6+
const void *data)
7+
{
8+
struct strbuf buf = STRBUF_INIT;
9+
10+
f(&buf, data);
11+
strbuf_release(&buf);
12+
cl_assert_equal_i(buf.len, 0);
13+
cl_assert_equal_i(buf.alloc, 0);
14+
}
15+
16+
/* wrapper that supplies tests with a populated, initialized strbuf */
17+
static void setup_populated(void (*f)(struct strbuf*, const void*),
18+
const char *init_str, const void *data)
19+
{
20+
struct strbuf buf = STRBUF_INIT;
21+
22+
strbuf_addstr(&buf, init_str);
23+
cl_assert_equal_i(buf.len, strlen(init_str));
24+
f(&buf, data);
25+
strbuf_release(&buf);
26+
cl_assert_equal_i(buf.len, 0);
27+
cl_assert_equal_i(buf.alloc, 0);
28+
}
29+
30+
static void assert_sane_strbuf(struct strbuf *buf)
31+
{
32+
/* Initialized strbufs should always have a non-NULL buffer */
33+
cl_assert(buf->buf != NULL);
34+
/* Buffers should always be NUL-terminated */
35+
cl_assert(buf->buf[buf->len] == '\0');
36+
/*
37+
* In case the buffer contains anything, `alloc` must alloc must
38+
* be at least one byte larger than `len`.
39+
*/
40+
if (buf->len)
41+
cl_assert(buf->len < buf->alloc);
42+
}
43+
44+
void test_strbuf__static_init(void)
45+
{
46+
struct strbuf buf = STRBUF_INIT;
47+
48+
cl_assert_equal_i(buf.len, 0);
49+
cl_assert_equal_i(buf.alloc, 0);
50+
cl_assert(buf.buf[0] == '\0');
51+
}
52+
53+
void test_strbuf__dynamic_init(void)
54+
{
55+
struct strbuf buf;
56+
57+
strbuf_init(&buf, 1024);
58+
assert_sane_strbuf(&buf);
59+
cl_assert_equal_i(buf.len, 0);
60+
cl_assert(buf.alloc >= 1024);
61+
cl_assert(buf.buf[0] == '\0');
62+
strbuf_release(&buf);
63+
}
64+
65+
static void t_addch(struct strbuf *buf, const void *data)
66+
{
67+
const char *p_ch = data;
68+
const char ch = *p_ch;
69+
size_t orig_alloc = buf->alloc;
70+
size_t orig_len = buf->len;
71+
72+
assert_sane_strbuf(buf);
73+
strbuf_addch(buf, ch);
74+
assert_sane_strbuf(buf);
75+
cl_assert_equal_i(buf->len, orig_len + 1);
76+
cl_assert(buf->alloc >= orig_alloc);
77+
cl_assert(buf->buf[buf->len] == '\0');
78+
}
79+
80+
static void t_addstr(struct strbuf *buf, const void *data)
81+
{
82+
const char *text = data;
83+
size_t len = strlen(text);
84+
size_t orig_alloc = buf->alloc;
85+
size_t orig_len = buf->len;
86+
87+
assert_sane_strbuf(buf);
88+
strbuf_addstr(buf, text);
89+
assert_sane_strbuf(buf);
90+
cl_assert_equal_i(buf->len, orig_len + len);
91+
cl_assert(buf->alloc >= orig_alloc);
92+
cl_assert(buf->buf[buf->len] == '\0');
93+
cl_assert_equal_s(buf->buf + orig_len, text);
94+
}
95+
96+
void test_strbuf__add_single_char(void)
97+
{
98+
setup(t_addch, "a");
99+
}
100+
101+
void test_strbuf__add_empty_char(void)
102+
{
103+
setup(t_addch, "");
104+
}
105+
106+
void test_strbuf__add_append_char(void)
107+
{
108+
setup_populated(t_addch, "initial value", "a");
109+
}
110+
111+
void test_strbuf__add_single_str(void)
112+
{
113+
setup(t_addstr, "hello there");
114+
}
115+
116+
void test_strbuf__add_append_str(void)
117+
{
118+
setup_populated(t_addstr, "initial value", "hello there");
119+
}

0 commit comments

Comments
 (0)