]> git.ozlabs.org Git - petitboot/blob - test/lib/test-fold.c
grub2: fix empty file handling
[petitboot] / test / lib / test-fold.c
1
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5
6 #include <fold/fold.h>
7 #include <list/list.h>
8 #include <talloc/talloc.h>
9
10 struct line {
11         const char              *buf;
12         unsigned int            len;
13         struct list_item        list;
14 };
15
16 struct ctx {
17         struct list             lines;
18 };
19
20 struct test {
21         const char      *in;
22         unsigned int    linelen;
23         const char      *out[];
24 };
25
26 /* split on newline boundaries, no actual folding */
27 struct test test_split = {
28         .in     = "Lorem ipsum dolor\nsit amet,\nconsectetuer\n",
29         .linelen = 20,
30         .out = {
31                 "Lorem ipsum dolor",
32                 "sit amet,",
33                 "consectetuer",
34                 "",
35                 NULL,
36         },
37 };
38
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.",
44         .linelen = 20,
45         .out = {
46                 "Lorem ipsum dolor",
47                 "sit amet,",
48                 "consectetuer",
49                 "adipiscing elit,",
50                 "sed diam nonummy",
51                 "nibh euismod",
52                 "tincidunt ut",
53                 "laoreet dolore",
54                 "magna aliquam erat",
55                 "volutpat.",
56                 NULL
57         },
58 };
59
60 /* break a word */
61 struct test test_break = {
62         .in = "Lorem ipsum dolor sit amet, consectetuer",
63         .linelen = 10,
64         .out = {
65                 "Lorem",
66                 "ipsum",
67                 "dolor sit",
68                 "amet,",
69                 "consectetu",
70                 "er",
71                 NULL
72         },
73 };
74
75 static struct test *tests[] = {
76         &test_split, &test_fold_line, &test_break,
77 };
78
79 static void __attribute__((noreturn)) fail(struct ctx *ctx,
80                 struct test *test, const char *msg)
81 {
82         struct line *line;
83         int i;
84
85         fprintf(stderr, "%s\n", msg);
86         fprintf(stderr, "input:\n%s\n", test->in);
87
88         fprintf(stderr, "expected:\n");
89         for (i = 0; test->out[i]; i++)
90                 fprintf(stderr, "  '%s'\n", test->out[i]);
91
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);
96                 talloc_free(buf);
97         }
98
99         exit(EXIT_FAILURE);
100 }
101
102 static int fold_line_cb(void *arg, const char *start, int len)
103 {
104         struct ctx *ctx = arg;
105         struct line *line;
106
107         line = talloc(ctx, struct line);
108         line->buf = start;
109         line->len = len;
110         list_add_tail(&ctx->lines, &line->list);
111
112         return 0;
113 }
114
115 static void run_test(struct test *test)
116 {
117         struct line *line;
118         struct ctx *ctx;
119         int i;
120
121         ctx = talloc(NULL, struct ctx);
122         list_init(&ctx->lines);
123         fold_text(test->in, test->linelen, fold_line_cb, ctx);
124
125         i = 0;
126         list_for_each_entry(&ctx->lines, line, list) {
127                 if (!test->out[i])
128                         fail(ctx, test,
129                                 "fold_text returned more lines than expected");
130
131                 if (line->len > test->linelen)
132                         fail(ctx, test, "line too long");
133
134                 if (line->len != strlen(test->out[i]))
135                         fail(ctx, test, "line lengths differ");
136
137                 if (strncmp(line->buf, test->out[i], line->len))
138                         fail(ctx, test, "line data differs");
139
140                 i++;
141         }
142
143         if (test->out[i])
144                 fail(ctx, test, "fold_text returned fewer lines than expected");
145
146         talloc_free(ctx);
147 }
148
149 int main(void)
150 {
151         unsigned int i;
152
153         for (i = 0; i < ARRAY_SIZE(tests); i++)
154                 run_test(tests[i]);
155
156         return EXIT_SUCCESS;
157 }