Skip to content

Commit c204224

Browse files
Seyi007gitster
authored andcommitted
t/unit-tests: convert strbuf test to clar framework
Adapt strbuf test script to clar framework by using clar assertions where necessary. Test functions are created as standalone to test different test cases. Mentored-by: Patrick Steinhardt <[email protected]> Signed-off-by: Seyi Kuforiji <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 57f6a64 commit c204224

File tree

4 files changed

+123
-124
lines changed

4 files changed

+123
-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: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
* Freshly-initialized strbufs may not have a dynamically allocated
38+
* buffer
39+
*/
40+
if (buf->len == 0 && buf->alloc == 0)
41+
return;
42+
/* alloc must be at least one byte larger than len */
43+
cl_assert(buf->len < buf->alloc);
44+
}
45+
46+
void test_strbuf__static_init(void)
47+
{
48+
struct strbuf buf = STRBUF_INIT;
49+
50+
cl_assert_equal_i(buf.len, 0);
51+
cl_assert_equal_i(buf.alloc, 0);
52+
cl_assert(buf.buf[0] == '\0');
53+
}
54+
55+
void test_strbuf__dynamic_init(void)
56+
{
57+
struct strbuf buf;
58+
59+
strbuf_init(&buf, 1024);
60+
assert_sane_strbuf(&buf);
61+
cl_assert_equal_i(buf.len, 0);
62+
cl_assert(buf.alloc >= 1024);
63+
cl_assert(buf.buf[0] == '\0');
64+
strbuf_release(&buf);
65+
}
66+
67+
static void t_addch(struct strbuf *buf, const void *data)
68+
{
69+
const char *p_ch = data;
70+
const char ch = *p_ch;
71+
size_t orig_alloc = buf->alloc;
72+
size_t orig_len = buf->len;
73+
74+
assert_sane_strbuf(buf);
75+
strbuf_addch(buf, ch);
76+
assert_sane_strbuf(buf);
77+
cl_assert_equal_i(buf->len, orig_len + 1);
78+
cl_assert(buf->alloc >= orig_alloc);
79+
cl_assert(buf->buf[buf->len] == '\0');
80+
}
81+
82+
static void t_addstr(struct strbuf *buf, const void *data)
83+
{
84+
const char *text = data;
85+
size_t len = strlen(text);
86+
size_t orig_alloc = buf->alloc;
87+
size_t orig_len = buf->len;
88+
89+
assert_sane_strbuf(buf);
90+
strbuf_addstr(buf, text);
91+
assert_sane_strbuf(buf);
92+
cl_assert_equal_i(buf->len, orig_len + len);
93+
cl_assert(buf->alloc >= orig_alloc);
94+
cl_assert(buf->buf[buf->len] == '\0');
95+
cl_assert_equal_s(buf->buf + orig_len, text);
96+
}
97+
98+
void test_strbuf__add_single_char(void)
99+
{
100+
setup(t_addch, "a");
101+
}
102+
103+
void test_strbuf__add_empty_char(void)
104+
{
105+
setup(t_addch, "");
106+
}
107+
108+
void test_strbuf__add_multi_char(void)
109+
{
110+
setup_populated(t_addch, "initial value", "a");
111+
}
112+
113+
void test_strbuf__add_single_str(void)
114+
{
115+
setup(t_addstr, "hello there");
116+
}
117+
118+
void test_strbuf__add_multi_str(void)
119+
{
120+
setup_populated(t_addstr, "initial value", "hello there");
121+
}

0 commit comments

Comments
 (0)