aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-05-04 02:02:47 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-05-04 02:02:47 +0200
commit2030a059606ec4895f61828761da3b51a17963e7 (patch)
treeabffa81e0aab2c9e3c3786584f1b9db1f4bd293b /test
parent3ec5c9cbfc26bf715451c83026832f1fdfd25092 (diff)
Generalize case-mapping tests
Diffstat (limited to 'test')
-rw-r--r--test/_case-test.h98
-rw-r--r--test/lower-test.c91
-rw-r--r--test/title-test.c95
-rw-r--r--test/upper-test.c93
4 files changed, 104 insertions, 273 deletions
diff --git a/test/_case-test.h b/test/_case-test.h
new file mode 100644
index 0000000..997eecd
--- /dev/null
+++ b/test/_case-test.h
@@ -0,0 +1,98 @@
+#ifndef CASETYPE
+# error "CASETYPE is not defined!"
+#endif
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <alloc.h>
+#include <errors.h>
+#include <macros.h>
+#include <mbstring.h>
+#include <unicode/string.h>
+
+#define TESTFILE STR(CASETYPE) ".in"
+#define FUNC CONCAT(u8, CASETYPE)
+
+static bool test(const char8_t *, int);
+
+int
+main(int, char **argv)
+{
+ int rv;
+ size_t n;
+ ssize_t nr;
+ char *line;
+ FILE *fp;
+
+ rv = EXIT_SUCCESS;
+ line = nullptr;
+ mlib_setprogname(argv[0]);
+
+ if ((fp = fopen(TESTFILE, "r")) == nullptr)
+ err("fopen: %s:", TESTFILE);
+
+ for (int id = 1; (nr = getline(&line, &n, fp)) > 0; id++) {
+ if (line[nr - 1] == '\n')
+ line[--nr] = '\0';
+
+ if (!test(line, id))
+ rv = EXIT_FAILURE;
+ }
+ if (ferror(fp))
+ err("getline: %s:", TESTFILE);
+
+ free(line);
+ fclose(fp);
+ return rv;
+}
+
+bool
+test(const char8_t *line, int id)
+{
+ struct u8view before, after, flags;
+ before.p = line;
+ after.p = strchr(line, ';') + 1;
+ before.len = after.p - before.p - 1;
+ flags.p = strchr(after.p, ';') + 1;
+ after.len = flags.p - after.p - 1;
+ flags.len = strlen(flags.p);
+
+ enum caseflags cf = 0;
+ if (u8eq(U8_ARGS(flags), U8_ARGS(U8("ẞ"))))
+ cf |= CF_ẞ;
+ else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("AZ"))))
+ cf |= CF_LANG_AZ;
+ else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("LT"))))
+ cf |= CF_LANG_LT;
+ else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("NL"))))
+ cf |= CF_LANG_NL;
+
+ char8_t *buf = bufalloc(nullptr, 1, after.len);
+ size_t bufsz = FUNC(nullptr, 0, U8_ARGS(before), cf);
+ if (bufsz != after.len) {
+ warn("case %d: expected %scased buffer size of %zu but got %zu "
+ "(flags=‘%.*s’)",
+ id, STR(CASETYPE), after.len, bufsz, U8_PRI_ARGS(flags));
+ return false;
+ }
+
+ bufsz = FUNC(buf, bufsz, U8_ARGS(before), cf);
+ if (bufsz != after.len) {
+ warn("case %d: expected %scased length of %zu but got %zu "
+ "(flags=‘%.*s’)",
+ id, STR(CASETYPE), after.len, bufsz, U8_PRI_ARGS(flags));
+ return false;
+ }
+
+ if (!memeq(buf, after.p, bufsz)) {
+ warn("case %d: expected ‘%.*s’ but got ‘%.*s’ (flags=‘%.*s’)", id,
+ U8_PRI_ARGS(after), (int)bufsz, buf, U8_PRI_ARGS(flags));
+ return false;
+ }
+
+ free(buf);
+ return true;
+}
diff --git a/test/lower-test.c b/test/lower-test.c
index b8e7228..a9a56ca 100644
--- a/test/lower-test.c
+++ b/test/lower-test.c
@@ -1,89 +1,2 @@
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <alloc.h>
-#include <errors.h>
-#include <macros.h>
-#include <mbstring.h>
-#include <unicode/string.h>
-
-#define TESTFILE "lower.in"
-
-static bool test(const char8_t *, int);
-
-int
-main(int, char **argv)
-{
- int rv;
- size_t n;
- ssize_t nr;
- char *line;
- FILE *fp;
-
- rv = EXIT_SUCCESS;
- line = nullptr;
- mlib_setprogname(argv[0]);
-
- if ((fp = fopen(TESTFILE, "r")) == nullptr)
- err("fopen: %s:", TESTFILE);
-
- for (int id = 1; (nr = getline(&line, &n, fp)) > 0; id++) {
- if (line[nr - 1] == '\n')
- line[--nr] = '\0';
-
- if (!test(line, id))
- rv = EXIT_FAILURE;
- }
- if (ferror(fp))
- err("getline: %s:", TESTFILE);
-
- free(line);
- fclose(fp);
- return rv;
-}
-
-bool
-test(const char8_t *line, int id)
-{
- struct u8view before, after, flags;
- before.p = line;
- after.p = strchr(line, ';') + 1;
- before.len = after.p - before.p - 1;
- flags.p = strchr(after.p, ';') + 1;
- after.len = flags.p - after.p - 1;
- flags.len = strlen(flags.p);
-
- enum caseflags cf = 0;
- if (u8eq(U8_ARGS(flags), U8_ARGS(U8("AZ"))))
- cf |= CF_LANG_AZ;
- else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("LT"))))
- cf |= CF_LANG_LT;
-
- char8_t *buf = bufalloc(nullptr, 1, after.len);
- size_t bufsz = u8lower(nullptr, 0, U8_ARGS(before), cf);
- if (bufsz != after.len) {
- warn("case %d: expected lowercased buffer size of %zu but got %zu "
- "(flags=‘%.*s’)",
- id, after.len, bufsz, U8_PRI_ARGS(flags));
- return false;
- }
-
- bufsz = u8lower(buf, bufsz, U8_ARGS(before), cf);
- if (bufsz != after.len) {
- warn("case %d: expected lowercased length of %zu but got %zu "
- "(flags=‘%.*s’)",
- id, after.len, bufsz, U8_PRI_ARGS(flags));
- return false;
- }
-
- if (!memeq(buf, after.p, bufsz)) {
- warn("case %d: expected ‘%.*s’ but got ‘%.*s’ (flags=‘%.*s’)", id,
- U8_PRI_ARGS(after), (int)bufsz, buf, U8_PRI_ARGS(flags));
- return false;
- }
-
- free(buf);
- return true;
-}
+#define CASETYPE lower
+#include "_case-test.h"
diff --git a/test/title-test.c b/test/title-test.c
index bea1ce7..8f66fcd 100644
--- a/test/title-test.c
+++ b/test/title-test.c
@@ -1,93 +1,2 @@
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <alloc.h>
-#include <errors.h>
-#include <macros.h>
-#include <mbstring.h>
-#include <unicode/string.h>
-
-#define TESTFILE "title.in"
-
-static bool test(const char8_t *, int);
-
-int
-main(int, char **argv)
-{
- int rv;
- size_t n;
- ssize_t nr;
- char *line;
- FILE *fp;
-
- rv = EXIT_SUCCESS;
- line = nullptr;
- mlib_setprogname(argv[0]);
-
- if ((fp = fopen(TESTFILE, "r")) == nullptr)
- err("fopen: %s:", TESTFILE);
-
- for (int id = 1; (nr = getline(&line, &n, fp)) > 0; id++) {
- if (line[nr - 1] == '\n')
- line[--nr] = '\0';
-
- if (!test(line, id))
- rv = EXIT_FAILURE;
- }
- if (ferror(fp))
- err("getline: %s:", TESTFILE);
-
- free(line);
- fclose(fp);
- return rv;
-}
-
-bool
-test(const char8_t *line, int id)
-{
- struct u8view before, after, flags;
- before.p = line;
- after.p = strchr(line, ';') + 1;
- before.len = after.p - before.p - 1;
- flags.p = strchr(after.p, ';') + 1;
- after.len = flags.p - after.p - 1;
- flags.len = strlen(flags.p);
-
- enum caseflags cf = 0;
- if (u8eq(U8_ARGS(flags), U8_ARGS(U8("ẞ"))))
- cf |= CF_ẞ;
- else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("AZ"))))
- cf |= CF_LANG_AZ;
- else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("LT"))))
- cf |= CF_LANG_LT;
- else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("NL"))))
- cf |= CF_LANG_NL;
-
- char8_t *buf = bufalloc(nullptr, 1, after.len);
- size_t bufsz = u8title(nullptr, 0, U8_ARGS(before), cf);
- if (bufsz != after.len) {
- warn("case %d: expected titlecased buffer size of %zu but got %zu "
- "(flags=‘%.*s’)",
- id, after.len, bufsz, U8_PRI_ARGS(flags));
- return false;
- }
-
- bufsz = u8title(buf, bufsz, U8_ARGS(before), cf);
- if (bufsz != after.len) {
- warn("case %d: expected titlecased length of %zu but got %zu "
- "(flags=‘%.*s’)",
- id, after.len, bufsz, U8_PRI_ARGS(flags));
- return false;
- }
-
- if (!memeq(buf, after.p, bufsz)) {
- warn("case %d: expected ‘%.*s’ but got ‘%.*s’ (flags=‘%.*s’)", id,
- U8_PRI_ARGS(after), (int)bufsz, buf, U8_PRI_ARGS(flags));
- return false;
- }
-
- free(buf);
- return true;
-}
+#define CASETYPE title
+#include "_case-test.h"
diff --git a/test/upper-test.c b/test/upper-test.c
index c1a8ead..81db918 100644
--- a/test/upper-test.c
+++ b/test/upper-test.c
@@ -1,91 +1,2 @@
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include <alloc.h>
-#include <errors.h>
-#include <macros.h>
-#include <mbstring.h>
-#include <unicode/string.h>
-
-#define TESTFILE "upper.in"
-
-static bool test(const char8_t *, int);
-
-int
-main(int, char **argv)
-{
- int rv;
- size_t n;
- ssize_t nr;
- char *line;
- FILE *fp;
-
- rv = EXIT_SUCCESS;
- line = nullptr;
- mlib_setprogname(argv[0]);
-
- if ((fp = fopen(TESTFILE, "r")) == nullptr)
- err("fopen: %s:", TESTFILE);
-
- for (int id = 1; (nr = getline(&line, &n, fp)) > 0; id++) {
- if (line[nr - 1] == '\n')
- line[--nr] = '\0';
-
- if (!test(line, id))
- rv = EXIT_FAILURE;
- }
- if (ferror(fp))
- err("getline: %s:", TESTFILE);
-
- free(line);
- fclose(fp);
- return rv;
-}
-
-bool
-test(const char8_t *line, int id)
-{
- struct u8view before, after, flags;
- before.p = line;
- after.p = strchr(line, ';') + 1;
- before.len = after.p - before.p - 1;
- flags.p = strchr(after.p, ';') + 1;
- after.len = flags.p - after.p - 1;
- flags.len = strlen(flags.p);
-
- enum caseflags cf = 0;
- if (u8eq(U8_ARGS(flags), U8_ARGS(U8("ẞ"))))
- cf |= CF_ẞ;
- else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("AZ"))))
- cf |= CF_LANG_AZ;
- else if (u8eq(U8_ARGS(flags), U8_ARGS(U8("LT"))))
- cf |= CF_LANG_LT;
-
- char8_t *buf = bufalloc(nullptr, 1, after.len);
- size_t bufsz = u8upper(nullptr, 0, U8_ARGS(before), cf);
- if (bufsz != after.len) {
- warn("case %d: expected uppercased buffer size of %zu but got %zu "
- "(flags=‘%.*s’)",
- id, after.len, bufsz, U8_PRI_ARGS(flags));
- return false;
- }
-
- bufsz = u8upper(buf, bufsz, U8_ARGS(before), cf);
- if (bufsz != after.len) {
- warn("case %d: expected uppercased length of %zu but got %zu "
- "(flags=‘%.*s’)",
- id, after.len, bufsz, U8_PRI_ARGS(flags));
- return false;
- }
-
- if (!memeq(buf, after.p, bufsz)) {
- warn("case %d: expected ‘%.*s’ but got ‘%.*s’ (flags=‘%.*s’)", id,
- U8_PRI_ARGS(after), (int)bufsz, buf, U8_PRI_ARGS(flags));
- return false;
- }
-
- free(buf);
- return true;
-}
+#define CASETYPE upper
+#include "_case-test.h"