aboutsummaryrefslogtreecommitdiff
path: root/lib/util/test/get_line.c
blob: 6189d31c885bb890383482ff5b4cf4e132df60a4 (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
/* SPDX-License-Identifier: GPL-3.0-or-later */
/*
 * get_line.c
 *
 * Copyright (C) 2019 David Oberhollenzer <goliath@infraroot.at>
 */
#include "util/parse.h"
#include "io/mem.h"
#include "util/test.h"

typedef struct {
	size_t line_num;
	const char *str;
} line_t;

static void run_test_case(const char *raw, const line_t *lines, size_t count,
			  int flags)
{
	size_t i, line_num, old_line_num;
	sqfs_istream_t *fp;
	char *line;
	int ret;

	fp = istream_memory_create("lines.txt", 2, raw, strlen(raw));
	TEST_NOT_NULL(fp);

	line_num = 1;
	line = NULL;

	for (i = 0; i < count; ++i) {
		old_line_num = line_num;
		ret = istream_get_line(fp, &line, &line_num, flags);

		TEST_ASSERT(line_num >= old_line_num);
		TEST_EQUAL_I(ret, 0);
		TEST_NOT_NULL(line);

		TEST_EQUAL_UI(line_num, lines[i].line_num);
		TEST_STR_EQUAL(line, lines[i].str);

		free(line);
		line = NULL;
		line_num += 1;
	}

	ret = istream_get_line(fp, &line, &line_num, flags);
	TEST_ASSERT(ret > 0);

	sqfs_drop(fp);
}

static const char *file =
"\r\n"
"The quick\r\n"
"  \r\n"
"  brown fox  \r\n"
"\r\n"
"jumps over\r\n"
"the\r\n"
"lazy\r\n"
"\r\n"
"dog\r\n"
"\r\n";

static const line_t lines_raw[] = {
	{ 1, "" },
	{ 2, "The quick" },
	{ 3, "  " },
	{ 4, "  brown fox  " },
	{ 5, "" },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 9, "" },
	{ 10, "dog" },
	{ 11, "" },
};

static const line_t lines_ltrim[] = {
	{ 1, "" },
	{ 2, "The quick" },
	{ 3, "" },
	{ 4, "brown fox  " },
	{ 5, "" },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 9, "" },
	{ 10, "dog" },
	{ 11, "" },
};

static const line_t lines_rtrim[] = {
	{ 1, "" },
	{ 2, "The quick" },
	{ 3, "" },
	{ 4, "  brown fox" },
	{ 5, "" },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 9, "" },
	{ 10, "dog" },
	{ 11, "" },
};

static const line_t lines_trim[] = {
	{ 1, "" },
	{ 2, "The quick" },
	{ 3, "" },
	{ 4, "brown fox" },
	{ 5, "" },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 9, "" },
	{ 10, "dog" },
	{ 11, "" },
};

static const line_t lines_no_empty[] = {
	{ 2, "The quick" },
	{ 3, "  " },
	{ 4, "  brown fox  " },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 10, "dog" },
};

static const line_t lines_no_empty_ltrim[] = {
	{ 2, "The quick" },
	{ 4, "brown fox  " },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 10, "dog" },
};

static const line_t lines_no_empty_rtrim[] = {
	{ 2, "The quick" },
	{ 4, "  brown fox" },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 10, "dog" },
};

static const line_t lines_no_empty_trim[] = {
	{ 2, "The quick" },
	{ 4, "brown fox" },
	{ 6, "jumps over" },
	{ 7, "the" },
	{ 8, "lazy" },
	{ 10, "dog" },
};

int main(int argc, char **argv)
{
	(void)argc; (void)argv;

	run_test_case(file, lines_raw, 11, 0);
	run_test_case(file, lines_ltrim, 11, ISTREAM_LINE_LTRIM);
	run_test_case(file, lines_rtrim, 11, ISTREAM_LINE_RTRIM);
	run_test_case(file, lines_trim, 11,
		      ISTREAM_LINE_LTRIM | ISTREAM_LINE_RTRIM);

	run_test_case(file, lines_no_empty, 7, ISTREAM_LINE_SKIP_EMPTY);
	run_test_case(file, lines_no_empty_ltrim, 6,
		      ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_LTRIM);
	run_test_case(file, lines_no_empty_rtrim, 6,
		      ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_RTRIM);
	run_test_case(file, lines_no_empty_trim, 6,
		      ISTREAM_LINE_SKIP_EMPTY | ISTREAM_LINE_LTRIM |
		      ISTREAM_LINE_RTRIM);

	return EXIT_SUCCESS;
}