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