Blame


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>
7 3448adb0 2022-11-02 op
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"
11 3448adb0 2022-11-02 op
12 3448adb0 2022-11-02 op int
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)
15 3448adb0 2022-11-02 op {
16 3448adb0 2022-11-02 op size_t i, j, off, res, failed;
17 3448adb0 2022-11-02 op
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);
22 3448adb0 2022-11-02 op
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);
29 3448adb0 2022-11-02 op failed++;
30 3448adb0 2022-11-02 op break;
31 3448adb0 2022-11-02 op }
32 3448adb0 2022-11-02 op }
33 3448adb0 2022-11-02 op }
34 3448adb0 2022-11-02 op printf("%s: %zu/%zu conformance tests passed.\n", argv0,
35 3448adb0 2022-11-02 op testlen - failed, testlen);
36 3448adb0 2022-11-02 op
37 3448adb0 2022-11-02 op return (failed > 0) ? 1 : 0;
38 3448adb0 2022-11-02 op }
39 3448adb0 2022-11-02 op
40 3448adb0 2022-11-02 op int
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)
44 3448adb0 2022-11-02 op {
45 3448adb0 2022-11-02 op size_t i, failed;
46 3448adb0 2022-11-02 op
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;
49 3448adb0 2022-11-02 op }
50 3448adb0 2022-11-02 op
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);
53 3448adb0 2022-11-02 op
54 3448adb0 2022-11-02 op return (failed > 0) ? 1 : 0;
55 3448adb0 2022-11-02 op }
56 3448adb0 2022-11-02 op
57 3448adb0 2022-11-02 op int
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)
61 3448adb0 2022-11-02 op {
62 3448adb0 2022-11-02 op const struct unit_test_next_break *test = t + off;
63 3448adb0 2022-11-02 op
64 3448adb0 2022-11-02 op size_t ret = next_break(test->input.src, test->input.srclen);
65 3448adb0 2022-11-02 op
66 3448adb0 2022-11-02 op if (ret != test->output.ret) {
67 3448adb0 2022-11-02 op goto err;
68 3448adb0 2022-11-02 op }
69 3448adb0 2022-11-02 op
70 3448adb0 2022-11-02 op return 0;
71 3448adb0 2022-11-02 op err:
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);
75 3448adb0 2022-11-02 op return 1;
76 3448adb0 2022-11-02 op }
77 3448adb0 2022-11-02 op
78 3448adb0 2022-11-02 op int
79 3448adb0 2022-11-02 op unit_test_callback_next_break_utf8(const struct unit_test_next_break_utf8 *t,
80 3448adb0 2022-11-02 op size_t off,
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)
83 3448adb0 2022-11-02 op {
84 3448adb0 2022-11-02 op const struct unit_test_next_break_utf8 *test = t + off;
85 3448adb0 2022-11-02 op
86 3448adb0 2022-11-02 op size_t ret = next_break_utf8(test->input.src, test->input.srclen);
87 3448adb0 2022-11-02 op
88 3448adb0 2022-11-02 op if (ret != test->output.ret) {
89 3448adb0 2022-11-02 op goto err;
90 3448adb0 2022-11-02 op }
91 3448adb0 2022-11-02 op
92 3448adb0 2022-11-02 op return 0;
93 3448adb0 2022-11-02 op err:
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);
97 3448adb0 2022-11-02 op return 1;
98 3448adb0 2022-11-02 op }