summaryrefslogtreecommitdiffstats
path: root/test/lib/test-fold.c
blob: 2aee39da4bb64084da808ba4a0e6a8a1a63918c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214

#define _GNU_SOURCE

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <locale.h>
#include <wchar.h>
#include <langinfo.h>

#include <fold/fold.h>
#include <list/list.h>
#include <talloc/talloc.h>

struct line {
	const char		*buf;
	unsigned int		len;
	struct list_item	list;
};

struct ctx {
	struct list		lines;
};

struct test {
	const char	*in;
	unsigned int	linelen;
	const char	*out[];
};

/* split on newline boundaries, no actual folding */
struct test test_split = {
	.in	= "Lorem ipsum dolor\nsit amet,\nconsectetuer\n",
	.linelen = 20,
	.out = {
		"Lorem ipsum dolor",
		"sit amet,",
		"consectetuer",
		"",
		NULL,
	},
};

/* fold a long line */
struct test test_fold_line = {
	.in = "Lorem ipsum dolor sit amet, consectetuer adipiscing "
		"elit, sed diam nonummy nibh euismod tincidunt ut "
		"laoreet dolore magna aliquam erat volutpat.",
	.linelen = 20,
	.out = {
		"Lorem ipsum dolor",
		"sit amet,",
		"consectetuer",
		"adipiscing elit,",
		"sed diam nonummy",
		"nibh euismod",
		"tincidunt ut",
		"laoreet dolore",
		"magna aliquam erat",
		"volutpat.",
		NULL
	},
};

/* break a word */
struct test test_break = {
	.in = "Lorem ipsum dolor sit amet, consectetuer",
	.linelen = 10,
	.out = {
		"Lorem",
		"ipsum",
		"dolor sit",
		"amet,",
		"consectetu",
		"er",
		NULL
	},
};

struct test test_mbs = {
	.in = "從主功能表畫面中,選取啟動選項。",
	.linelen = 15,
	.out = {
		"從主功能表畫面",
		"中,選取啟動選",
		"項。",
		NULL,
	},
};

struct test test_mbs_sep = {
	.in = "從主功 能表 畫面中,選 取啟 動選項。",
	.linelen = 15,
	.out = {
		"從主功 能表",
		"畫面中,選",
		"取啟 動選項。",
		NULL,
	},
};

static struct test *tests[] = {
	&test_split, &test_fold_line, &test_break, &test_mbs, &test_mbs_sep,
};

static void __attribute__((noreturn)) fail(struct ctx *ctx,
		struct test *test, const char *msg)
{
	struct line *line;
	int i;

	fprintf(stderr, "%s\n", msg);
	fprintf(stderr, "input, split at %d:\n%s\n", test->linelen, test->in);

	fprintf(stderr, "expected:\n");
	for (i = 0; test->out[i]; i++)
		fprintf(stderr, "  '%s'\n", test->out[i]);

	fprintf(stderr, "actual:\n");
	list_for_each_entry(&ctx->lines, line, list) {
		char *buf = talloc_strndup(ctx, line->buf, line->len);
		fprintf(stderr, "  '%s'\n", buf);
		talloc_free(buf);
	}

	exit(EXIT_FAILURE);
}

static int fold_line_cb(void *arg, const char *start, int len)
{
	struct ctx *ctx = arg;
	struct line *line;

	line = talloc(ctx, struct line);
	line->buf = start;
	line->len = len;
	list_add_tail(&ctx->lines, &line->list);

	return 0;
}

static void run_test(struct test *test)
{
	struct line *line;
	struct ctx *ctx;
	wchar_t *wcs;
	int i, n;

	ctx = talloc(NULL, struct ctx);
	n = strlen(test->in) + 1;
	list_init(&ctx->lines);
	fold_text(test->in, test->linelen, fold_line_cb, ctx);


	i = 0;
	list_for_each_entry(&ctx->lines, line, list) {
		size_t wcslen;
		char *buf;
		int width;

		if (!test->out[i])
			fail(ctx, test,
				"fold_text returned more lines than expected");

		buf = talloc_strndup(ctx, line->buf, line->len);
		wcslen = mbstowcs(NULL, buf, 0);

		if (wcslen == (size_t)-1)
			fail(ctx, test, "invalid mutlibyte sequence");

		wcs = talloc_array(ctx, wchar_t, wcslen + 1);
		wcslen = mbstowcs(wcs, buf, n);

		width = wcswidth(wcs, wcslen);
		if (width == -1)
			fail(ctx, test, "nonprintable characters present");

		if (width > (signed int)test->linelen)
			fail(ctx, test, "line too long");

		if (line->len != strlen(test->out[i]))
			fail(ctx, test, "line lengths differ");

		if (strncmp(line->buf, test->out[i], line->len))
			fail(ctx, test, "line data differs");

		i++;
	}

	if (test->out[i])
		fail(ctx, test, "fold_text returned fewer lines than expected");

	talloc_free(ctx);
}

int main(void)
{
	unsigned int i;
	char *charset;

	setlocale(LC_CTYPE, "");

	charset = nl_langinfo(CODESET);
	if (strcmp(charset, "UTF-8")) {
		fprintf(stderr, "Current charset is %s, tests require UTF-8\n",
				charset);
		return EXIT_FAILURE;
	}

	for (i = 0; i < ARRAY_SIZE(tests); i++)
		run_test(tests[i]);

	return EXIT_SUCCESS;
}
OpenPOWER on IntegriCloud