libgrapheme

unicode string library
git clone git://git.suckless.org/libgrapheme
Log | Files | Refs | README | LICENSE

commit 52e94b51d3dab06e9e467f1074422a48843779e3
parent 9b3187980729ca574233ceee0cedcf597e523600
Author: Laslo Hunhold <dev@frign.de>
Date:   Sun,  9 Jan 2022 12:22:42 +0100

Add a general break-test-check-function in test/util

Signed-off-by: Laslo Hunhold <dev@frign.de>

Diffstat:
Mtest/character.c | 42+++++++++---------------------------------
Mtest/util.c | 42+++++++++++++++++++++++++++++++++++++++++-
Mtest/util.h | 7++++++-
3 files changed, 56 insertions(+), 35 deletions(-)

diff --git a/test/character.c b/test/character.c @@ -1,45 +1,21 @@ /* See LICENSE file for copyright and license details. */ -#include <stddef.h> +#include <stdbool.h> #include <stdint.h> -#include <stdio.h> -#include <string.h> -#include "../grapheme.h" #include "../gen/character-test.h" #include "util.h" +static bool +is_break(uint_least32_t cp0, uint_least32_t cp1, GRAPHEME_STATE *state) +{ + return grapheme_is_character_break(cp0, cp1, state); +} + int main(int argc, char *argv[]) { - GRAPHEME_STATE state; - size_t i, j, k, len, failed; - (void)argc; - /* character break test */ - for (i = 0, failed = 0; i < LEN(character_test); i++) { - memset(&state, 0, sizeof(state)); - for (j = 0, k = 0, len = 1; j < character_test[i].cplen; j++) { - if ((j + 1) == character_test[i].cplen || - grapheme_is_character_break(character_test[i].cp[j], - character_test[i].cp[j + 1], - &state)) { - /* check if our resulting length matches */ - if (k == character_test[i].lenlen || - len != character_test[i].len[k++]) { - fprintf(stderr, "%s: Failed test \"%s\".\n", - argv[0], character_test[i].descr); - failed++; - break; - } - len = 1; - } else { - len++; - } - } - } - printf("%s: %zu/%zu tests passed.\n", argv[0], - LEN(character_test) - failed, LEN(character_test)); - - return (failed > 0) ? 1 : 0; + return run_break_tests(is_break, character_test, + LEN(character_test), argv[0]); } diff --git a/test/util.c b/test/util.c @@ -1,4 +1,44 @@ /* See LICENSE file for copyright and license details. */ -#include <time.h> +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> +#include "../grapheme.h" +#include "../gen/types.h" #include "util.h" + +int +run_break_tests(bool (*is_break)(uint_least32_t, uint_least32_t, GRAPHEME_STATE *), + const struct test *test, size_t testlen, const char *argv0) +{ + GRAPHEME_STATE state; + size_t i, j, k, len, failed; + + /* character break test */ + for (i = 0, failed = 0; i < testlen; i++) { + memset(&state, 0, sizeof(state)); + for (j = 0, k = 0, len = 1; j < test[i].cplen; j++) { + if ((j + 1) == test[i].cplen || + is_break(test[i].cp[j], test[i].cp[j + 1], + &state)) { + /* check if our resulting length matches */ + if (k == test[i].lenlen || + len != test[i].len[k++]) { + fprintf(stderr, "%s: Failed test \"%s\".\n", + argv0, test[i].descr); + failed++; + break; + } + len = 1; + } else { + len++; + } + } + } + printf("%s: %zu/%zu tests passed.\n", argv0, + testlen - failed, testlen); + + return (failed > 0) ? 1 : 0; +} diff --git a/test/util.h b/test/util.h @@ -2,8 +2,13 @@ #ifndef UTIL_H #define UTIL_H -#include <time.h> +#include "../gen/types.h" +#include "../grapheme.h" #define LEN(x) (sizeof(x) / sizeof(*(x))) +int run_break_tests(bool (*is_break)(uint_least32_t, uint_least32_t, + GRAPHEME_STATE *), const struct test *test, + size_t testlen, const char *); + #endif /* UTIL_H */