t/unit-tests: convert ctype tests to use clar

Convert the ctype tests to use the new clar unit testing framework. Introduce a new function `cl_failf()` that allows us to print a formatted error message, which we can use to point out which of the characters was classified incorrectly. This results in output like this on failure: # start of suite 1: ctype not ok 1 - ctype::isspace --- reason: | Test failed. 0x0d is classified incorrectly: expected 0, got 1 at: file: 't/unit-tests/ctype.c' line: 36 function: 'test_ctype__isspace' --- ok 2 - ctype::isdigit ok 3 - ctype::isalpha ok 4 - ctype::isalnum ok 5 - ctype::is_glob_special ok 6 - ctype::is_regex_special ok 7 - ctype::is_pathspec_magic ok 8 - ctype::isascii ok 9 - ctype::islower ok 10 - ctype::isupper ok 11 - ctype::iscntrl ok 12 - ctype::ispunct ok 13 - ctype::isxdigit ok 14 - ctype::isprint Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Sep 4, 2024 at 16:17 UTC c9763684ea1da13fc511eebeea3bc0b80c6c9c0e
3 files changed +69 -11
Makefile
+1 -1
@@ -1336,13 +1336,13 @@ THIRD_PARTY_SOURCES += sha1dc/%
1336 THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/%
1337 THIRD_PARTY_SOURCES += $(UNIT_TEST_DIR)/clar/clar/%
1338
1339 +UNIT_TESTS_SUITES += ctype
1340 UNIT_TESTS_SUITES += strvec
1341 UNIT_TESTS_PROG = $(UNIT_TEST_BIN)/unit-tests$(X)
1342 UNIT_TESTS_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TESTS_SUITES))
1343 UNIT_TESTS_OBJS += $(UNIT_TEST_DIR)/clar/clar.o
1344 UNIT_TESTS_OBJS += $(UNIT_TEST_DIR)/unit-test.o
1345
1345 -UNIT_TEST_PROGRAMS += t-ctype
1346 UNIT_TEST_PROGRAMS += t-example-decorate
1347 UNIT_TEST_PROGRAMS += t-hash
1348 UNIT_TEST_PROGRAMS += t-hashmap
t/unit-tests/ctype.c renamed
+61 -10
@@ -1,16 +1,16 @@
1 -#include "test-lib.h"
1 +#include "unit-test.h"
2
3 #define TEST_CHAR_CLASS(class, string) do { \
4 size_t len = ARRAY_SIZE(string) - 1 + \
5 BUILD_ASSERT_OR_ZERO(ARRAY_SIZE(string) > 0) + \
6 BUILD_ASSERT_OR_ZERO(sizeof(string[0]) == sizeof(char)); \
7 - if_test (#class " works") { \
8 - for (int i = 0; i < 256; i++) { \
9 - if (!check_int(class(i), ==, !!memchr(string, i, len)))\
10 - test_msg(" i: 0x%02x", i); \
11 - } \
12 - check(!class(EOF)); \
7 + for (int i = 0; i < 256; i++) { \
8 + int actual = class(i), expect = !!memchr(string, i, len); \
9 + if (actual != expect) \
10 + cl_failf("0x%02x is classified incorrectly: expected %d, got %d", \
11 + i, expect, actual); \
12 } \
13 + cl_assert(!class(EOF)); \
14 } while (0)
15
16 #define DIGIT "0123456789"
@@ -31,21 +31,72 @@
31 "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
32 "\x7f"
33
34 -int cmd_main(int argc, const char **argv) {
34 +void test_ctype__isspace(void)
35 +{
36 TEST_CHAR_CLASS(isspace, " \n\r\t");
37 +}
38 +
39 +void test_ctype__isdigit(void)
40 +{
41 TEST_CHAR_CLASS(isdigit, DIGIT);
42 +}
43 +
44 +void test_ctype__isalpha(void)
45 +{
46 TEST_CHAR_CLASS(isalpha, LOWER UPPER);
47 +}
48 +
49 +void test_ctype__isalnum(void)
50 +{
51 TEST_CHAR_CLASS(isalnum, LOWER UPPER DIGIT);
52 +}
53 +
54 +void test_ctype__is_glob_special(void)
55 +{
56 TEST_CHAR_CLASS(is_glob_special, "*?[\\");
57 +}
58 +
59 +void test_ctype__is_regex_special(void)
60 +{
61 TEST_CHAR_CLASS(is_regex_special, "$()*+.?[\\^{|");
62 +}
63 +
64 +void test_ctype__is_pathspec_magic(void)
65 +{
66 TEST_CHAR_CLASS(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~");
67 +}
68 +
69 +void test_ctype__isascii(void)
70 +{
71 TEST_CHAR_CLASS(isascii, ASCII);
72 +}
73 +
74 +void test_ctype__islower(void)
75 +{
76 TEST_CHAR_CLASS(islower, LOWER);
77 +}
78 +
79 +void test_ctype__isupper(void)
80 +{
81 TEST_CHAR_CLASS(isupper, UPPER);
82 +}
83 +
84 +void test_ctype__iscntrl(void)
85 +{
86 TEST_CHAR_CLASS(iscntrl, CNTRL);
87 +}
88 +
89 +void test_ctype__ispunct(void)
90 +{
91 TEST_CHAR_CLASS(ispunct, PUNCT);
92 +}
93 +
94 +void test_ctype__isxdigit(void)
95 +{
96 TEST_CHAR_CLASS(isxdigit, DIGIT "abcdefABCDEF");
48 - TEST_CHAR_CLASS(isprint, LOWER UPPER DIGIT PUNCT " ");
97 +}
98
50 - return test_done();
99 +void test_ctype__isprint(void)
100 +{
101 + TEST_CHAR_CLASS(isprint, LOWER UPPER DIGIT PUNCT " ");
102 }
t/unit-tests/unit-test.h
+7
@@ -1,3 +1,10 @@
1 #include "git-compat-util.h"
2 #include "clar/clar.h"
3 #include "clar-decls.h"
4 +#include "strbuf.h"
5 +
6 +#define cl_failf(fmt, ...) do { \
7 + char desc[4096]; \
8 + snprintf(desc, sizeof(desc), fmt, __VA_ARGS__); \
9 + clar__fail(__FILE__, __func__, __LINE__, "Test failed.", desc, 1); \
10 +} while (0)