Skip to content

Commit b982aa9

Browse files
committed
Merge branch 'al/unit-test-ctype'
Move test-ctype helper to the unit-test framework. * al/unit-test-ctype: unit-tests: rewrite t/helper/test-ctype.c as a unit test
2 parents f3657b3 + e875d45 commit b982aa9

File tree

6 files changed

+81
-77
lines changed

6 files changed

+81
-77
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -792,7 +792,6 @@ TEST_BUILTINS_OBJS += test-chmtime.o
792792
TEST_BUILTINS_OBJS += test-config.o
793793
TEST_BUILTINS_OBJS += test-crontab.o
794794
TEST_BUILTINS_OBJS += test-csprng.o
795-
TEST_BUILTINS_OBJS += test-ctype.o
796795
TEST_BUILTINS_OBJS += test-date.o
797796
TEST_BUILTINS_OBJS += test-delta.o
798797
TEST_BUILTINS_OBJS += test-dir-iterator.o
@@ -1342,6 +1341,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
13421341
UNIT_TEST_PROGRAMS += t-basic
13431342
UNIT_TEST_PROGRAMS += t-mem-pool
13441343
UNIT_TEST_PROGRAMS += t-strbuf
1344+
UNIT_TEST_PROGRAMS += t-ctype
13451345
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
13461346
UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
13471347
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o

t/helper/test-ctype.c

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

t/helper/test-tool.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ static struct test_cmd cmds[] = {
1919
{ "config", cmd__config },
2020
{ "crontab", cmd__crontab },
2121
{ "csprng", cmd__csprng },
22-
{ "ctype", cmd__ctype },
2322
{ "date", cmd__date },
2423
{ "delta", cmd__delta },
2524
{ "dir-iterator", cmd__dir_iterator },

t/helper/test-tool.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ int cmd__chmtime(int argc, const char **argv);
1212
int cmd__config(int argc, const char **argv);
1313
int cmd__crontab(int argc, const char **argv);
1414
int cmd__csprng(int argc, const char **argv);
15-
int cmd__ctype(int argc, const char **argv);
1615
int cmd__date(int argc, const char **argv);
1716
int cmd__delta(int argc, const char **argv);
1817
int cmd__dir_iterator(int argc, const char **argv);

t/t0070-fundamental.sh

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ Verify wrappers and compatibility functions.
99
TEST_PASSES_SANITIZE_LEAK=true
1010
. ./test-lib.sh
1111

12-
test_expect_success 'character classes (isspace, isalpha etc.)' '
13-
test-tool ctype
14-
'
15-
1612
test_expect_success 'mktemp to nonexistent directory prints filename' '
1713
test_must_fail test-tool mktemp doesnotexist/testXXXXXX 2>err &&
1814
grep "doesnotexist/test" err

t/unit-tests/t-ctype.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "test-lib.h"
2+
3+
static int is_in(const char *s, int ch)
4+
{
5+
/*
6+
* We can't find NUL using strchr. Accept it as the first
7+
* character in the spec -- there are no empty classes.
8+
*/
9+
if (ch == '\0')
10+
return ch == *s;
11+
if (*s == '\0')
12+
s++;
13+
return !!strchr(s, ch);
14+
}
15+
16+
/* Macro to test a character type */
17+
#define TEST_CTYPE_FUNC(func, string) \
18+
static void test_ctype_##func(void) { \
19+
for (int i = 0; i < 256; i++) { \
20+
if (!check_int(func(i), ==, is_in(string, i))) \
21+
test_msg(" i: 0x%02x", i); \
22+
} \
23+
if (!check(!func(EOF))) \
24+
test_msg(" i: 0x%02x (EOF)", EOF); \
25+
}
26+
27+
#define TEST_CHAR_CLASS(class) TEST(test_ctype_##class(), #class " works")
28+
29+
#define DIGIT "0123456789"
30+
#define LOWER "abcdefghijklmnopqrstuvwxyz"
31+
#define UPPER "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
32+
#define PUNCT "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
33+
#define ASCII \
34+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
35+
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
36+
"\x20\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f" \
37+
"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
38+
"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
39+
"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f" \
40+
"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
41+
"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"
42+
#define CNTRL \
43+
"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f" \
44+
"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
45+
"\x7f"
46+
47+
TEST_CTYPE_FUNC(isdigit, DIGIT)
48+
TEST_CTYPE_FUNC(isspace, " \n\r\t")
49+
TEST_CTYPE_FUNC(isalpha, LOWER UPPER)
50+
TEST_CTYPE_FUNC(isalnum, LOWER UPPER DIGIT)
51+
TEST_CTYPE_FUNC(is_glob_special, "*?[\\")
52+
TEST_CTYPE_FUNC(is_regex_special, "$()*+.?[\\^{|")
53+
TEST_CTYPE_FUNC(is_pathspec_magic, "!\"#%&',-/:;<=>@_`~")
54+
TEST_CTYPE_FUNC(isascii, ASCII)
55+
TEST_CTYPE_FUNC(islower, LOWER)
56+
TEST_CTYPE_FUNC(isupper, UPPER)
57+
TEST_CTYPE_FUNC(iscntrl, CNTRL)
58+
TEST_CTYPE_FUNC(ispunct, PUNCT)
59+
TEST_CTYPE_FUNC(isxdigit, DIGIT "abcdefABCDEF")
60+
TEST_CTYPE_FUNC(isprint, LOWER UPPER DIGIT PUNCT " ")
61+
62+
int cmd_main(int argc, const char **argv) {
63+
/* Run all character type tests */
64+
TEST_CHAR_CLASS(isspace);
65+
TEST_CHAR_CLASS(isdigit);
66+
TEST_CHAR_CLASS(isalpha);
67+
TEST_CHAR_CLASS(isalnum);
68+
TEST_CHAR_CLASS(is_glob_special);
69+
TEST_CHAR_CLASS(is_regex_special);
70+
TEST_CHAR_CLASS(is_pathspec_magic);
71+
TEST_CHAR_CLASS(isascii);
72+
TEST_CHAR_CLASS(islower);
73+
TEST_CHAR_CLASS(isupper);
74+
TEST_CHAR_CLASS(iscntrl);
75+
TEST_CHAR_CLASS(ispunct);
76+
TEST_CHAR_CLASS(isxdigit);
77+
TEST_CHAR_CLASS(isprint);
78+
79+
return test_done();
80+
}

0 commit comments

Comments
 (0)