Skip to content

Commit 3460568

Browse files
authored
test code refactoring (#318)
1 parent ae2f2fb commit 3460568

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
lines changed

test/custom.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ int main(void)
1919
utf8proc_uint8_t *output;
2020
utf8proc_map_custom(input, 0, &output, UTF8PROC_CASEFOLD | UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_NULLTERM,
2121
custom, &thunk_test);
22-
printf("mapped \"%s\" -> \"%s\"\n", (char*)input, (char*)output);
23-
check(strlen((char*) output) == 6, "incorrect output length");
24-
check(!memcmp(correct, output, 7), "incorrect output data");
25-
free(output);
22+
check_compare("map_custom", input, correct, output, 1);
2623
printf("map_custom tests SUCCEEDED.\n");
2724
return 0;
2825
}

test/misc.c

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,22 @@ static void issue128(void) /* #128 */
77
utf8proc_uint8_t input[] = {0x72, 0xcc, 0x87, 0xcc, 0xa3, 0x00}; /* "r\u0307\u0323" */
88
utf8proc_uint8_t nfc[] = {0xe1, 0xb9, 0x9b, 0xcc, 0x87, 0x00}; /* "\u1E5B\u0307" */
99
utf8proc_uint8_t nfd[] = {0x72, 0xcc, 0xa3, 0xcc, 0x87, 0x00}; /* "r\u0323\u0307" */
10-
utf8proc_uint8_t *nfc_out, *nfd_out;
11-
nfc_out = utf8proc_NFC(input);
12-
printf("NFC \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)nfc_out, (char*)nfc);
13-
check(strlen((char*) nfc_out) == 5, "incorrect nfc length");
14-
check(!memcmp(nfc, nfc_out, 6), "incorrect nfc data");
15-
nfd_out = utf8proc_NFD(input);
16-
printf("NFD \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)nfd_out, (char*)nfd);
17-
check(strlen((char*) nfd_out) == 5, "incorrect nfd length");
18-
check(!memcmp(nfd, nfd_out, 6), "incorrect nfd data");
19-
free(nfd_out); free(nfc_out);
10+
11+
check_compare("NFC", input, nfc, utf8proc_NFC(input), 1);
12+
check_compare("NFD", input, nfd, utf8proc_NFD(input), 1);
2013
}
2114

22-
static void issue102(void) /* #128 */
15+
static void issue102(void) /* #102 */
2316
{
2417
utf8proc_uint8_t input[] = {0x58, 0xe2, 0x81, 0xa5, 0x45, 0xcc, 0x80, 0xc2, 0xad, 0xe1, 0xb4, 0xac, 0x00}; /* "X\u2065E\u0300\u00ad\u1d2c" */
2518
utf8proc_uint8_t stripna[] = {0x78, 0xc3, 0xa8, 0x61, 0x00}; /* "x\u00e8a" */
2619
utf8proc_uint8_t correct[] = {0x78, 0xe2, 0x81, 0xa5, 0xc3, 0xa8, 0x61, 0x00}; /* "x\u2065\u00e8a" */
2720
utf8proc_uint8_t *output;
21+
2822
utf8proc_map(input, 0, &output, UTF8PROC_NULLTERM | UTF8PROC_STABLE |
2923
UTF8PROC_COMPOSE | UTF8PROC_COMPAT | UTF8PROC_CASEFOLD | UTF8PROC_IGNORE | UTF8PROC_STRIPNA);
30-
printf("NFKC_Casefold \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)output, (char*)stripna);
31-
check(strlen((char*) output) == 4, "incorrect NFKC_Casefold+stripna length");
32-
check(!memcmp(stripna, output, 5), "incorrect NFKC_Casefold+stripna data");
33-
free(output);
34-
output = utf8proc_NFKC_Casefold(input);
35-
printf("NFKC_Casefold \"%s\" -> \"%s\" vs. \"%s\"\n", (char*)input, (char*)output, (char*)correct);
36-
check(strlen((char*) output) == 7, "incorrect NFKC_Casefold length");
37-
check(!memcmp(correct, output, 8), "incorrect NFKC_Casefold data");
38-
free(output);
24+
check_compare("NFKC_Casefold+stripna", input, stripna, output, 1);
25+
check_compare("NFKC_Casefold", input, correct, utf8proc_NFKC_Casefold(input), 1);
3926
}
4027

4128
int main(void)

test/tests.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ void check(int cond, const char *format, ...)
88
{
99
if (!cond) {
1010
va_list args;
11-
fprintf(stderr, "line %zd: ", lineno);
11+
if (lineno)
12+
fprintf(stderr, "FAILED at line %zd: ", lineno);
13+
else
14+
fprintf(stderr, "FAILED: ");
1215
va_start(args, format);
1316
vfprintf(stderr, format, args);
1417
va_end(args);
@@ -58,3 +61,40 @@ size_t simple_getline(unsigned char buf[8192], FILE *f) {
5861
buf[i] = 0;
5962
return i;
6063
}
64+
65+
void print_escaped(FILE* f, const utf8proc_uint8_t *utf8) {
66+
fprintf(f, "\"");
67+
while (*utf8) {
68+
utf8proc_int32_t codepoint;
69+
utf8 += utf8proc_iterate(utf8, -1, &codepoint);
70+
if (codepoint < 0x10000)
71+
fprintf(f, "\\u%04x", codepoint);
72+
else
73+
fprintf(f, "\\U%06x", codepoint);
74+
}
75+
fprintf(f, "\"");
76+
}
77+
78+
void print_string_and_escaped(FILE* f, const utf8proc_uint8_t *utf8) {
79+
fprintf(f, "\"%s\" (", (const char *) utf8);
80+
print_escaped(f, utf8);
81+
fprintf(f, ")");
82+
}
83+
84+
void check_compare(const char *transformation,
85+
const utf8proc_uint8_t *input, const utf8proc_uint8_t *expected,
86+
utf8proc_uint8_t *received, int free_received) {
87+
int passed = !strcmp((const char *) received, (const char *) expected);
88+
FILE *f = passed ? stdout : stderr;
89+
fprintf(f, "%s: %s ", passed ? "PASSED" : "FAILED", transformation);
90+
print_string_and_escaped(f, input);
91+
fprintf(f, " -> ");
92+
print_string_and_escaped(f, received);
93+
if (!passed) {
94+
fprintf(f, " != expected ");
95+
print_string_and_escaped(f, expected);
96+
}
97+
fprintf(f, "\n");
98+
if (free_received) free(received);
99+
if (!passed) exit(1);
100+
}

test/tests.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ void check(int cond, const char *format, ...);
2525
size_t skipspaces(const unsigned char *buf, size_t i);
2626
size_t encode(unsigned char *dest, size_t *dest_len, const unsigned char *buf);
2727
size_t simple_getline(unsigned char buf[8192], FILE *f);
28+
void print_escaped(FILE* f, const utf8proc_uint8_t *utf8);
29+
void print_string_and_escaped(FILE* f, const utf8proc_uint8_t *utf8);
30+
void check_compare(const char *transformation,
31+
const utf8proc_uint8_t *input, const utf8proc_uint8_t *expected,
32+
utf8proc_uint8_t *received, int free_received);

0 commit comments

Comments
 (0)