11 #include <fold/fold.h>
12 #include <list/list.h>
13 #include <talloc/talloc.h>
18 struct list_item list;
31 /* split on newline boundaries, no actual folding */
32 struct test test_split = {
33 .in = "Lorem ipsum dolor\nsit amet,\nconsectetuer\n",
44 /* fold a long line */
45 struct test test_fold_line = {
46 .in = "Lorem ipsum dolor sit amet, consectetuer adipiscing "
47 "elit, sed diam nonummy nibh euismod tincidunt ut "
48 "laoreet dolore magna aliquam erat volutpat.",
66 struct test test_break = {
67 .in = "Lorem ipsum dolor sit amet, consectetuer",
80 struct test test_mbs = {
81 .in = "從主功能表畫面中,選取啟動選項。",
91 struct test test_mbs_sep = {
92 .in = "從主功 能表 畫面中,選 取啟 動選項。",
102 static struct test *tests[] = {
103 &test_split, &test_fold_line, &test_break, &test_mbs, &test_mbs_sep,
106 static void __attribute__((noreturn)) fail(struct ctx *ctx,
107 struct test *test, const char *msg)
112 fprintf(stderr, "%s\n", msg);
113 fprintf(stderr, "input, split at %d:\n%s\n", test->linelen, test->in);
115 fprintf(stderr, "expected:\n");
116 for (i = 0; test->out[i]; i++)
117 fprintf(stderr, " '%s'\n", test->out[i]);
119 fprintf(stderr, "actual:\n");
120 list_for_each_entry(&ctx->lines, line, list) {
121 char *buf = talloc_strndup(ctx, line->buf, line->len);
122 fprintf(stderr, " '%s'\n", buf);
129 static int fold_line_cb(void *arg, const char *start, int len)
131 struct ctx *ctx = arg;
134 line = talloc(ctx, struct line);
137 list_add_tail(&ctx->lines, &line->list);
142 static void run_test(struct test *test)
149 ctx = talloc(NULL, struct ctx);
150 n = strlen(test->in) + 1;
151 list_init(&ctx->lines);
152 fold_text(test->in, test->linelen, fold_line_cb, ctx);
156 list_for_each_entry(&ctx->lines, line, list) {
163 "fold_text returned more lines than expected");
165 buf = talloc_strndup(ctx, line->buf, line->len);
166 wcslen = mbstowcs(NULL, buf, 0);
168 if (wcslen == (size_t)-1)
169 fail(ctx, test, "invalid mutlibyte sequence");
171 wcs = talloc_array(ctx, wchar_t, wcslen + 1);
172 wcslen = mbstowcs(wcs, buf, n);
174 width = wcswidth(wcs, wcslen);
176 fail(ctx, test, "nonprintable characters present");
178 if (width > (signed int)test->linelen)
179 fail(ctx, test, "line too long");
181 if (line->len != strlen(test->out[i]))
182 fail(ctx, test, "line lengths differ");
184 if (strncmp(line->buf, test->out[i], line->len))
185 fail(ctx, test, "line data differs");
191 fail(ctx, test, "fold_text returned fewer lines than expected");
201 setlocale(LC_CTYPE, "");
203 charset = nl_langinfo(CODESET);
204 if (strcmp(charset, "UTF-8")) {
205 fprintf(stderr, "Current charset is %s, tests require UTF-8\n",
210 for (i = 0; i < ARRAY_SIZE(tests); i++)