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 static struct test *tests[] = {
92 &test_split, &test_fold_line, &test_break, &test_mbs,
95 static void __attribute__((noreturn)) fail(struct ctx *ctx,
96 struct test *test, const char *msg)
101 fprintf(stderr, "%s\n", msg);
102 fprintf(stderr, "input, split at %d:\n%s\n", test->linelen, test->in);
104 fprintf(stderr, "expected:\n");
105 for (i = 0; test->out[i]; i++)
106 fprintf(stderr, " '%s'\n", test->out[i]);
108 fprintf(stderr, "actual:\n");
109 list_for_each_entry(&ctx->lines, line, list) {
110 char *buf = talloc_strndup(ctx, line->buf, line->len);
111 fprintf(stderr, " '%s'\n", buf);
118 static int fold_line_cb(void *arg, const char *start, int len)
120 struct ctx *ctx = arg;
123 line = talloc(ctx, struct line);
126 list_add_tail(&ctx->lines, &line->list);
131 static void run_test(struct test *test)
138 ctx = talloc(NULL, struct ctx);
139 n = strlen(test->in) + 1;
140 list_init(&ctx->lines);
141 fold_text(test->in, test->linelen, fold_line_cb, ctx);
145 list_for_each_entry(&ctx->lines, line, list) {
152 "fold_text returned more lines than expected");
154 buf = talloc_strndup(ctx, line->buf, line->len);
155 wcslen = mbstowcs(NULL, buf, 0);
157 if (wcslen == (size_t)-1)
158 fail(ctx, test, "invalid mutlibyte sequence");
160 wcs = talloc_array(ctx, wchar_t, wcslen + 1);
161 wcslen = mbstowcs(wcs, buf, n);
163 width = wcswidth(wcs, wcslen);
165 fail(ctx, test, "nonprintable characters present");
167 if (width > (signed int)test->linelen)
168 fail(ctx, test, "line too long");
170 if (line->len != strlen(test->out[i]))
171 fail(ctx, test, "line lengths differ");
173 if (strncmp(line->buf, test->out[i], line->len))
174 fail(ctx, test, "line data differs");
180 fail(ctx, test, "fold_text returned fewer lines than expected");
190 setlocale(LC_CTYPE, "");
192 charset = nl_langinfo(CODESET);
193 if (strcmp(charset, "UTF-8")) {
194 fprintf(stderr, "Current charset is %s, tests require UTF-8\n",
199 for (i = 0; i < ARRAY_SIZE(tests); i++)