|
| 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