1 /* See LICENSE file for copyright and license details. */
8 #include "../grapheme.h"
9 #include "../gen/types.h"
13 run_break_tests(size_t (*next_break)(const uint_least32_t *, size_t),
14 const struct break_test *test, size_t testlen, const char *argv0)
16 size_t i, j, off, res, failed;
18 /* character break test */
19 for (i = 0, failed = 0; i < testlen; i++) {
20 for (j = 0, off = 0; off < test[i].cplen; off += res) {
21 res = next_break(test[i].cp + off, test[i].cplen - off);
23 /* check if our resulting offset matches */
24 if (j == test[i].lenlen ||
25 res != test[i].len[j++]) {
26 fprintf(stderr, "%s: Failed conformance test %zu \"%s\".\n",
27 argv0, i, test[i].descr);
28 fprintf(stderr, "J=%zu: EXPECTED len %zu, got %zu\n", j-1, test[i].len[j-1], res);
34 printf("%s: %zu/%zu conformance tests passed.\n", argv0,
35 testlen - failed, testlen);
37 return (failed > 0) ? 1 : 0;
41 run_unit_tests(int (*unit_test_callback)(const void *, size_t, const char *,
42 const char *), const void *test, size_t testlen, const char *name,
47 for (i = 0, failed = 0; i < testlen; i++) {
48 failed += (unit_test_callback(test, i, name, argv0) == 0) ? 0 : 1;
51 printf("%s: %s: %zu/%zu unit tests passed.\n", argv0, name,
52 testlen - failed, testlen);
54 return (failed > 0) ? 1 : 0;
58 unit_test_callback_next_break(const struct unit_test_next_break *t, size_t off,
59 size_t (*next_break)(const uint_least32_t *, size_t),
60 const char *name, const char *argv0)
62 const struct unit_test_next_break *test = t + off;
64 size_t ret = next_break(test->input.src, test->input.srclen);
66 if (ret != test->output.ret) {
72 fprintf(stderr, "%s: %s: Failed unit test %zu \"%s\" "
73 "(returned %zu instead of %zu).\n", argv0,
74 name, off, test->description, ret, test->output.ret);
79 unit_test_callback_next_break_utf8(const struct unit_test_next_break_utf8 *t,
81 size_t (*next_break_utf8)(const char *, size_t),
82 const char *name, const char *argv0)
84 const struct unit_test_next_break_utf8 *test = t + off;
86 size_t ret = next_break_utf8(test->input.src, test->input.srclen);
88 if (ret != test->output.ret) {
94 fprintf(stderr, "%s: %s: Failed unit test %zu \"%s\" "
95 "(returned %zu instead of %zu).\n", argv0,
96 name, off, test->description, ret, test->output.ret);