File: scanner_test%2Btest.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (252 lines) | stat: -rw-r--r-- 6,403 bytes parent folder | download
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;
use encoding::utf8;
use io;
use memio;
use strings;
use types;

@test fn read_byte() void = {
	let buf = memio::fixed([1, 3, 3, 7]);

	assert(read_byte(&buf) as u8 == 1);
	assert(read_byte(&buf) as u8 == 3);
	assert(read_byte(&buf) as u8 == 3);
	assert(read_byte(&buf) as u8 == 7);
	assert(read_byte(&buf) is io::EOF);
};

@test fn read_tok() void = {
	let buf = memio::fixed([1, 3, 4, 5, 3, 7]);

	let tok = read_tok(&buf, 4) as []u8;
	defer free(tok);
	assert(bytes::equal(tok, [1, 3]));

	let tok = read_tok(&buf, 7) as []u8;
	defer free(tok);
	assert(bytes::equal(tok, [5, 3]));

	assert(read_tok(&buf, 1) is io::EOF);
};

@test fn read_line() void = {
	let helloworld = strings::toutf8("hello\nworld");
	let buf = memio::fixed(helloworld);

	let line = read_line(&buf) as []u8;
	defer free(line);
	assert(bytes::equal(line, strings::toutf8("hello")));

	let line = read_line(&buf) as []u8;
	defer free(line);
	assert(bytes::equal(line, strings::toutf8("world")));

	assert(read_line(&buf) is io::EOF);
};

@test fn read_rune() void = {
	let in = memio::fixed([
		0xE3, 0x81, 0x93, 0xE3, 0x82, 0x93, 0xE3, 0x81,
		0xAB, 0xE3, 0x81, 0xA1, 0xE3, 0x81, 0xAF, 0x00,
	]);

	const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [
		'こ', 'ん', 'に', 'ち', 'は', '\0', io::EOF,
	];
	for (let i = 0z; i < len(expected); i += 1) {
		let want = expected[i];

		match (read_rune(&in)) {
		case let r: rune =>
			assert(want is rune && want as rune == r);
		case io::EOF =>
			assert(want is io::EOF);
		case =>
			abort();
		};
	};
};

@test fn scan_rune() void = {
	let in = memio::fixed(strings::toutf8("hello"));
	let scanner = newscanner(&in, 32);
	defer finish(&scanner);

	const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [
		'h', 'e', 'l', 'l', 'o', io::EOF,
	];
	for (let i = 0z; i < len(expected); i += 1) {
		let want = expected[i];

		match (scan_rune(&scanner)) {
		case let r: rune =>
			assert(want is rune && want as rune == r);
		case io::EOF =>
			assert(want is io::EOF);
		case =>
			abort();
		};
	};
};

@test fn scan_rune_cutoff() void = {
	let in = memio::fixed([
		'a', 0xE3,
	]);
	let scanner = newscanner(&in, 32);
	defer finish(&scanner);

	const expected: [_](rune | utf8::invalid | io::EOF | io::error) = [
		'a', utf8::invalid,
	];
	for (let i = 0z; i < len(expected); i += 1) {
		let want = expected[i];

		match (scan_rune(&scanner)) {
		case let r: rune =>
			assert(want is rune && want as rune == r);
		case io::EOF =>
			assert(want is io::EOF);
		case utf8::invalid =>
			assert(want is utf8::invalid);
		case =>
			abort();
		};
	};
};

@test fn scan_byte() void = {
	let in = memio::fixed([1, 2, 3]);
	let scanner = newscanner(&in, 3);
	defer finish(&scanner);

	assert(scan_byte(&scanner) as u8 == 1);
	assert(scan_byte(&scanner) as u8 == 2);
	assert(scan_byte(&scanner) as u8 == 3);
	assert(scan_byte(&scanner) is io::EOF);
};

@test fn scan_read() void = {
	const expected: [_]u8 = [
		0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
	];
	let in = memio::fixed(expected);

	let scanner = newscanner(&in, 2);
	defer finish(&scanner);
	let result = io::drain(&scanner)!;
	defer free(result);
	assert(bytes::equal(expected, result));
};

@test fn scan_unread() void = {
	const expected: str = " I will not repeat  \nDone!\n";
	let in = memio::fixed(strings::toutf8(expected));

	let scanner = newscanner(&in, 32);
	defer finish(&scanner);

	let b = scan_byte(&scanner) as u8;
	unread(&scanner, [b]);

	let b = scan_rune(&scanner) as rune;
	unread(&scanner, utf8::encoderune(b));

	let l = scan_line(&scanner)! as const str;
	assert(l == " I will not repeat  ");

	unread(&scanner, strings::toutf8("\n"));
	unread(&scanner, strings::toutf8(l));
	let l = scan_line(&scanner)! as const str;
	assert(l == " I will not repeat  ");

	unread(&scanner, strings::toutf8("\n"));
	unread(&scanner, strings::toutf8(strings::trim(l)));
	let l = scan_line(&scanner)! as const str;
	assert(l == "I will not repeat");

	unread(&scanner, strings::toutf8("See?\n"));
	let l = scan_line(&scanner)! as const str;
	assert(l == "See?");

	let b = scan_rune(&scanner) as rune;
	unreadrune(&scanner, b);
	unreadrune(&scanner, ' ');
	unread(&scanner, strings::toutf8("I'm"));
	let l = scan_line(&scanner)! as const str;
	assert(l == "I'm Done!");

	assert(scan_line(&scanner) is io::EOF);
};

@test fn scan_uncomplete_line() void = {
	let buf = memio::dynamic();
	defer io::close(&buf)!;
	let scan = newscanner(&buf);
	defer finish(&scan);

	assert(scan_line(&scan) is io::EOF);

	io::write(&buf, strings::toutf8("hello"))!;
	io::seek(&buf, 0, io::whence::SET)!;

	assert(scan_line(&scan) is io::EOF);

	io::write(&buf, strings::toutf8("\n"))!;
	io::seek(&buf, -1, io::whence::CUR)!;

	let line = scan_line(&scan) as const str;
	assert(strings::compare(line, "hello") == 0);
};

@test fn greedy_scan_uncomplete_line() void = {
	let buf = memio::dynamic();
	defer io::close(&buf)!;
	let scan = newscanner(&buf, types::SIZE_MAX, scan_options::EOF_GREEDY);
	defer finish(&scan);

	assert(scan_line(&scan) is io::EOF);

	io::write(&buf, strings::toutf8("hello"))!;
	io::seek(&buf, 0, io::whence::SET)!;

	let line = scan_line(&scan) as const str;
	assert(strings::compare(line, "hello") == 0);
};

@test fn scan_seek() void = {
	let buf = memio::fixed(strings::toutf8(`Line 1
Line 2
Line 3
`));
	let scan = newscanner(&buf);
	defer finish(&scan);
	assert(scan_line(&scan) as const str == "Line 1");

	// Test whence::SET / io::tell
	const line2 = io::tell(&scan)!;
	io::seek(&scan, 0, io::whence::SET)!;

	assert(scan_line(&scan) as const str == "Line 1");
	assert(scan_line(&scan) as const str == "Line 2");
	assert(scan_line(&scan) as const str == "Line 3");

	io::seek(&scan, line2, io::whence::SET)!;
	assert(scan_line(&scan) as const str == "Line 2");
	assert(scan_line(&scan) as const str == "Line 3");

	// Test whence::END
	io::seek(&scan, -(len("Line 3\n"): io::off), io::whence::END)!;
	assert(scan_line(&scan) as const str == "Line 3");

	// whence::CUR
	io::seek(&scan, 0, io::whence::SET)!;
	assert(scan_line(&scan) as const str == "Line 1");
	assert(scan_line(&scan) as const str == "Line 2");
	io::seek(&scan, -(len("Line 2\n"): io::off), io::whence::CUR)!;
	assert(scan_line(&scan) as const str == "Line 2");
	assert(scan_line(&scan) as const str == "Line 3");
};