diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-02-13 13:02:28 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-02-13 13:11:47 +0100 |
commit | 79e6af86ca526d5fb56af6f6ca3da713e3a5e9f9 (patch) | |
tree | 752f1c26d1f122dcf58374ac78db109c9578be45 /vendor/librune/test |
Genesis commit
Diffstat (limited to 'vendor/librune/test')
-rw-r--r-- | vendor/librune/test/gbrk.c | 85 | ||||
-rwxr-xr-x | vendor/librune/test/tests | 39 |
2 files changed, 124 insertions, 0 deletions
diff --git a/vendor/librune/test/gbrk.c b/vendor/librune/test/gbrk.c new file mode 100644 index 0000000..bfac3a3 --- /dev/null +++ b/vendor/librune/test/gbrk.c @@ -0,0 +1,85 @@ +#define _POSIX_C_SOURCE 200809L +#include <err.h> +#include <locale.h> +#include <stdio.h> +#include <stdlib.h> + +#include <gbrk.h> +#include <mbstring.h> +#include <rune.h> + +#define die(...) err(EXIT_FAILURE, __VA_ARGS__) + +static void test(char *); + +int +main(int argc, char **argv) +{ + char *line = NULL; + size_t n; + ssize_t nr; + FILE *fp; + + setlocale(LC_ALL, ""); + + if (argc != 2) { + fprintf(stderr, "%s: file\n", *argv); + exit(EXIT_FAILURE); + } + + if (!(fp = fopen(argv[1], "r"))) + die("fopen"); + + while ((nr = getline(&line, &n, fp)) > 0) { + line[nr - 1] = 0; + test(line); + } + + if (nr == -1 && ferror(fp)) + die("getline"); + + fclose(fp); + free(line); + return EXIT_SUCCESS; +} + +void +test(char *raw) +{ + int n; + rune ch; + char8_t *p, *buf; + const char8_t *s; + size_t bufsiz = 4096; + struct u8view graph; + + if (!(buf = malloc(bufsiz))) + die("malloc"); + + p = buf; + while (sscanf(raw, "%" SCNxRUNE "%n", &ch, &n)) { + rune sep; + p += rtou8(p, ch, bufsiz - (p - buf)); + raw += n; + raw += u8tor(&sep, (char8_t *)raw); + if (!sep) + break; + } + *p = 0; + + s = buf; + while (u8gnext(&graph, &s, &bufsiz) && *graph.p) { + rune ch; + const char8_t *p; + + while (u8next(&ch, &graph.p, &graph.len) && ch) { + printf("%04" PRIXRUNE "%s", ch, graph.len > 0 ? "×" : ""); + p = graph.p; + } + if (bufsiz && *p) + fputs("÷", stdout); + } + + putchar('\n'); + free(buf); +} diff --git a/vendor/librune/test/tests b/vendor/librune/test/tests new file mode 100755 index 0000000..5affe3a --- /dev/null +++ b/vendor/librune/test/tests @@ -0,0 +1,39 @@ +#!/bin/sh + +report() +{ + case $1 in + 0) printf 'All tests passed\n' >&2 ;; + 1) printf '1 test failed\n' >&2 ;; + *) printf '%d tests failed\n' $1 >&2 + esac +} + +readonly src=../data/GraphemeBreakTest.txt + +set -e +cd "${0%/*}" +find ../lib -name '*.c' -exec \ + cc -std=c2x -Wno-attributes -I../include -o gbrk gbrk.c {} + +trap 'rm -f gbrk' EXIT + +n=$( + ./gbrk $src \ + | diff -y --suppress-common-lines $src - \ + | tee failures \ + | wc -l +) +test $n -eq 0 && rm failures + +if test -t 2 +then + case $n in + 0) printf '\033[0;32m' ;; + *) printf '\033[0;31m' + esac + + report $n + printf '\033[0m' >&2 +else + report $n +fi |