8 #include <talloc/talloc.h>
13 struct list_item list;
26 /* split on newline boundaries, no actual folding */
27 struct test test_split = {
28 .in = "Lorem ipsum dolor\nsit amet,\nconsectetuer\n",
39 /* fold a long line */
40 struct test test_fold_line = {
41 .in = "Lorem ipsum dolor sit amet, consectetuer adipiscing "
42 "elit, sed diam nonummy nibh euismod tincidunt ut "
43 "laoreet dolore magna aliquam erat volutpat.",
61 struct test test_break = {
62 .in = "Lorem ipsum dolor sit amet, consectetuer",
75 static struct test *tests[] = {
76 &test_split, &test_fold_line, &test_break,
79 static void __attribute__((noreturn)) fail(struct ctx *ctx,
80 struct test *test, const char *msg)
85 fprintf(stderr, "%s\n", msg);
86 fprintf(stderr, "input:\n%s\n", test->in);
88 fprintf(stderr, "expected:\n");
89 for (i = 0; test->out[i]; i++)
90 fprintf(stderr, " '%s'\n", test->out[i]);
92 fprintf(stderr, "actual:\n");
93 list_for_each_entry(&ctx->lines, line, list) {
94 char *buf = talloc_strndup(ctx, line->buf, line->len);
95 fprintf(stderr, " '%s'\n", buf);
102 static int fold_line_cb(void *arg, const char *start, int len)
104 struct ctx *ctx = arg;
107 line = talloc(ctx, struct line);
110 list_add_tail(&ctx->lines, &line->list);
115 static void run_test(struct test *test)
121 ctx = talloc(NULL, struct ctx);
122 list_init(&ctx->lines);
123 fold_text(test->in, test->linelen, fold_line_cb, ctx);
126 list_for_each_entry(&ctx->lines, line, list) {
129 "fold_text returned more lines than expected");
131 if (line->len > test->linelen)
132 fail(ctx, test, "line too long");
134 if (line->len != strlen(test->out[i]))
135 fail(ctx, test, "line lengths differ");
137 if (strncmp(line->buf, test->out[i], line->len))
138 fail(ctx, test, "line data differs");
144 fail(ctx, test, "fold_text returned fewer lines than expected");
153 for (i = 0; i < ARRAY_SIZE(tests); i++)