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 <ps@pks.im> Signed-off-by: Seyi Kuforiji <kuforiji98@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Seyi Kuforiji committed Jan 31, 2025 at 23:14 UTC 4b995465b2ed97cb6df5d48bcb843f7f631627bf
4 files changed +121 -124
Makefile
+1 -1
@@ -1346,6 +1346,7 @@ CLAR_TEST_SUITES += u-hashmap
1346 CLAR_TEST_SUITES += u-mem-pool
1347 CLAR_TEST_SUITES += u-prio-queue
1348 CLAR_TEST_SUITES += u-reftable-tree
1349 +CLAR_TEST_SUITES += u-strbuf
1350 CLAR_TEST_SUITES += u-strvec
1351 CLAR_TEST_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1352 CLAR_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(CLAR_TEST_SUITES))
@@ -1363,7 +1364,6 @@ UNIT_TEST_PROGRAMS += t-reftable-reader
1364 UNIT_TEST_PROGRAMS += t-reftable-readwrite
1365 UNIT_TEST_PROGRAMS += t-reftable-record
1366 UNIT_TEST_PROGRAMS += t-reftable-stack
1366 -UNIT_TEST_PROGRAMS += t-strbuf
1367 UNIT_TEST_PROGRAMS += t-strcmp-offset
1368 UNIT_TEST_PROGRAMS += t-trailer
1369 UNIT_TEST_PROGRAMS += t-urlmatch-normalization
t/meson.build
+1 -1
@@ -6,6 +6,7 @@ clar_test_suites = [
6 'unit-tests/u-mem-pool.c',
7 'unit-tests/u-prio-queue.c',
8 'unit-tests/u-reftable-tree.c',
9 + 'unit-tests/u-strbuf.c',
10 'unit-tests/u-strvec.c',
11 ]
12
@@ -57,7 +58,6 @@ unit_test_programs = [
58 'unit-tests/t-reftable-readwrite.c',
59 'unit-tests/t-reftable-record.c',
60 'unit-tests/t-reftable-stack.c',
60 - 'unit-tests/t-strbuf.c',
61 'unit-tests/t-strcmp-offset.c',
62 'unit-tests/t-trailer.c',
63 'unit-tests/t-urlmatch-normalization.c',
t/unit-tests/t-strbuf.c deleted
-122
@@ -1,122 +0,0 @@
1 -#include "test-lib.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 - check_uint(buf.len, ==, 0);
13 - check_uint(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 - check_uint(buf.len, ==, strlen(init_str));
24 - f(&buf, data);
25 - strbuf_release(&buf);
26 - check_uint(buf.len, ==, 0);
27 - check_uint(buf.alloc, ==, 0);
28 -}
29 -
30 -static int assert_sane_strbuf(struct strbuf *buf)
31 -{
32 - /* Initialized strbufs should always have a non-NULL buffer */
33 - if (!check(!!buf->buf))
34 - return 0;
35 - /* Buffers should always be NUL-terminated */
36 - if (!check_char(buf->buf[buf->len], ==, '\0'))
37 - return 0;
38 - /*
39 - * Freshly-initialized strbufs may not have a dynamically allocated
40 - * buffer
41 - */
42 - if (buf->len == 0 && buf->alloc == 0)
43 - return 1;
44 - /* alloc must be at least one byte larger than len */
45 - return check_uint(buf->len, <, buf->alloc);
46 -}
47 -
48 -static void t_static_init(void)
49 -{
50 - struct strbuf buf = STRBUF_INIT;
51 -
52 - check_uint(buf.len, ==, 0);
53 - check_uint(buf.alloc, ==, 0);
54 - check_char(buf.buf[0], ==, '\0');
55 -}
56 -
57 -static void t_dynamic_init(void)
58 -{
59 - struct strbuf buf;
60 -
61 - strbuf_init(&buf, 1024);
62 - check(assert_sane_strbuf(&buf));
63 - check_uint(buf.len, ==, 0);
64 - check_uint(buf.alloc, >=, 1024);
65 - check_char(buf.buf[0], ==, '\0');
66 - strbuf_release(&buf);
67 -}
68 -
69 -static void t_addch(struct strbuf *buf, const void *data)
70 -{
71 - const char *p_ch = data;
72 - const char ch = *p_ch;
73 - size_t orig_alloc = buf->alloc;
74 - size_t orig_len = buf->len;
75 -
76 - if (!check(assert_sane_strbuf(buf)))
77 - return;
78 - strbuf_addch(buf, ch);
79 - if (!check(assert_sane_strbuf(buf)))
80 - return;
81 - if (!(check_uint(buf->len, ==, orig_len + 1) &&
82 - check_uint(buf->alloc, >=, orig_alloc)))
83 - return; /* avoid de-referencing buf->buf */
84 - check_char(buf->buf[buf->len - 1], ==, ch);
85 - check_char(buf->buf[buf->len], ==, '\0');
86 -}
87 -
88 -static void t_addstr(struct strbuf *buf, const void *data)
89 -{
90 - const char *text = data;
91 - size_t len = strlen(text);
92 - size_t orig_alloc = buf->alloc;
93 - size_t orig_len = buf->len;
94 -
95 - if (!check(assert_sane_strbuf(buf)))
96 - return;
97 - strbuf_addstr(buf, text);
98 - if (!check(assert_sane_strbuf(buf)))
99 - return;
100 - if (!(check_uint(buf->len, ==, orig_len + len) &&
101 - check_uint(buf->alloc, >=, orig_alloc) &&
102 - check_uint(buf->alloc, >, orig_len + len) &&
103 - check_char(buf->buf[orig_len + len], ==, '\0')))
104 - return;
105 - check_str(buf->buf + orig_len, text);
106 -}
107 -
108 -int cmd_main(int argc UNUSED, const char **argv UNUSED)
109 -{
110 - if (!TEST(t_static_init(), "static initialization works"))
111 - test_skip_all("STRBUF_INIT is broken");
112 - TEST(t_dynamic_init(), "dynamic initialization works");
113 - TEST(setup(t_addch, "a"), "strbuf_addch adds char");
114 - TEST(setup(t_addch, ""), "strbuf_addch adds NUL char");
115 - TEST(setup_populated(t_addch, "initial value", "a"),
116 - "strbuf_addch appends to initial value");
117 - TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string");
118 - TEST(setup_populated(t_addstr, "initial value", "hello there"),
119 - "strbuf_addstr appends string to initial value");
120 -
121 - return test_done();
122 -}
t/unit-tests/u-strbuf.c new
+119
@@ -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 +}