File: testUniConversion.cxx

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (361 lines) | stat: -rw-r--r-- 9,821 bytes parent folder | download | duplicates (2)
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/** @file testUniConversion.cxx
 ** Unit Tests for Scintilla internal data structures
 **/

#include <cstring>

#include <string>
#include <string_view>
#include <vector>
#include <optional>
#include <algorithm>
#include <memory>

#include "Debugging.h"

#include "UniConversion.h"

#include "catch.hpp"

using namespace Scintilla::Internal;

// Test UniConversion.
// Use examples from Wikipedia:
// https://en.wikipedia.org/wiki/UTF-8

TEST_CASE("UTF16Length") {

	SECTION("UTF16Length ASCII") {
		// Latin Small Letter A
		const char *s = "a";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 1U);
	}

	SECTION("UTF16Length Example1") {
		// Dollar Sign
		const char *s = "\x24";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 1U);
	}

	SECTION("UTF16Length Example2") {
		// Cent Sign
		const char *s = "\xC2\xA2";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 1U);
	}

	SECTION("UTF16Length Example3") {
		// Euro Sign
		const char *s = "\xE2\x82\xAC";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 1U);
	}

	SECTION("UTF16Length Example4") {
		// Gothic Letter Hwair
		const char *s = "\xF0\x90\x8D\x88";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 2U);
	}

	SECTION("UTF16Length Invalid Trail byte in lead position") {
		const char *s = "a\xB5yz";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 4U);
	}

	SECTION("UTF16Length Invalid Lead byte at end") {
		const char *s = "a\xC2";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 2U);
	}

	SECTION("UTF16Length Invalid Lead byte implies 3 trails but only 2") {
		const char *s = "a\xF1yz";
		const size_t len = UTF16Length(s);
		REQUIRE(len == 2U);
	}
}

TEST_CASE("UniConversion") {

	// UnicodeFromUTF8

	SECTION("UnicodeFromUTF8 ASCII") {
		const unsigned char s[]={'a', 0, 0, 0};
		REQUIRE(UnicodeFromUTF8(s) == 'a');
	}

	SECTION("UnicodeFromUTF8 Example1") {
		const unsigned char s[]={0x24, 0, 0, 0};
		REQUIRE(UnicodeFromUTF8(s) == 0x24);
	}

	SECTION("UnicodeFromUTF8 Example2") {
		const unsigned char s[]={0xC2, 0xA2, 0, 0};
		REQUIRE(UnicodeFromUTF8(s) == 0xA2);
	}

	SECTION("UnicodeFromUTF8 Example3") {
		const unsigned char s[]={0xE2, 0x82, 0xAC, 0};
		REQUIRE(UnicodeFromUTF8(s) == 0x20AC);
	}

	SECTION("UnicodeFromUTF8 Example4") {
		const unsigned char s[]={0xF0, 0x90, 0x8D, 0x88, 0};
		REQUIRE(UnicodeFromUTF8(s) == 0x10348);
	}

	// UTF16FromUTF8

	SECTION("UTF16FromUTF8 ASCII") {
		const char s[] = {'a', 0};
		wchar_t tbuf[1] = {0};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 'a');
	}

	SECTION("UTF16FromUTF8 Example1") {
		const char s[] = {'\x24', 0};
		wchar_t tbuf[1] = {0};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0x24);
	}

	SECTION("UTF16FromUTF8 Example2") {
		const char s[] = {'\xC2', '\xA2', 0};
		wchar_t tbuf[1] = {0};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0xA2);
	}

	SECTION("UTF16FromUTF8 Example3") {
		const char s[] = {'\xE2', '\x82', '\xAC', 0};
		wchar_t tbuf[1] = {0};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 1);;
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0x20AC);
	}

	SECTION("UTF16FromUTF8 Example4") {
		const char s[] = {'\xF0', '\x90', '\x8D', '\x88', 0};
		wchar_t tbuf[2] = {0, 0};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 2);
		REQUIRE(tlen == 2U);
		REQUIRE(tbuf[0] == 0xD800);
		REQUIRE(tbuf[1] == 0xDF48);
	}

	SECTION("UTF16FromUTF8 Invalid Trail byte in lead position") {
		const char s[] = "a\xB5yz";
		wchar_t tbuf[4] = {};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 4);
		REQUIRE(tlen == 4U);
		REQUIRE(tbuf[0] == 'a');
		REQUIRE(tbuf[1] == 0xB5);
		REQUIRE(tbuf[2] == 'y');
		REQUIRE(tbuf[3] == 'z');
	}

	SECTION("UTF16FromUTF8 Invalid Lead byte at end") {
		const char s[] = "a\xC2";
		wchar_t tbuf[2] = {};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 2);
		REQUIRE(tlen == 2U);
		REQUIRE(tbuf[0] == 'a');
		REQUIRE(tbuf[1] == 0xC2);
	}

	SECTION("UTF16FromUTF8 Invalid Lead byte implies 3 trails but only 2") {
		const char *s = "a\xF1yz";
		wchar_t tbuf[4] = {};
		const size_t tlen = UTF16FromUTF8(s, tbuf, 4);
		REQUIRE(tlen == 2U);
		REQUIRE(tbuf[0] == 'a');
		REQUIRE(tbuf[1] == 0xF1);
	}

	// UTF32FromUTF8

	SECTION("UTF32FromUTF8 ASCII") {
		const char s[] = {'a', 0};
		unsigned int tbuf[1] = {0};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == static_cast<unsigned int>('a'));
	}

	SECTION("UTF32FromUTF8 Example1") {
		const char s[] = {'\x24', 0};
		unsigned int tbuf[1] = {0};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0x24);
	}

	SECTION("UTF32FromUTF8 Example2") {
		const char s[] = {'\xC2', '\xA2', 0};
		unsigned int tbuf[1] = {0};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0xA2);
	}

	SECTION("UTF32FromUTF8 Example3") {
		const char s[] = {'\xE2', '\x82', '\xAC', 0};
		unsigned int tbuf[1] = {0};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0x20AC);
	}

	SECTION("UTF32FromUTF8 Example4") {
		const char s[] = {'\xF0', '\x90', '\x8D', '\x88', 0};
		unsigned int tbuf[1] = {0};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0x10348);
	}

	SECTION("UTF32FromUTF8 Invalid Trail byte in lead position") {
		const char s[] = "a\xB5yz";
		unsigned int tbuf[4] = {};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 4);
		REQUIRE(tlen == 4U);
		REQUIRE(tbuf[0] == static_cast<unsigned int>('a'));
		REQUIRE(tbuf[1] == 0xB5);
		REQUIRE(tbuf[2] == static_cast<unsigned int>('y'));
		REQUIRE(tbuf[3] == static_cast<unsigned int>('z'));
	}

	SECTION("UTF32FromUTF8 Invalid Lead byte at end") {
		const char s[] = "a\xC2";
		unsigned int tbuf[2] = {};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 2);
		REQUIRE(tlen == 2U);
		REQUIRE(tbuf[0] == static_cast<unsigned int>('a'));
		REQUIRE(tbuf[1] == 0xC2);
	}

	SECTION("UTF32FromUTF8 Invalid Lead byte implies 3 trails but only 2") {
		const char *s = "a\xF1yz";
		unsigned int tbuf[4] = {};
		const size_t tlen = UTF32FromUTF8(s, tbuf, 4);
		REQUIRE(tlen == 2U);
		REQUIRE(tbuf[0] == static_cast<unsigned int>('a'));
		REQUIRE(tbuf[1] == 0xF1);
	}
}

namespace {

// Simple adapter to avoid casting
int UTFClass(std::string_view sv) noexcept {
	return UTF8Classify(sv);
}

}

TEST_CASE("UTF8Classify") {

	// These tests are supposed to hit every return statement in UTF8Classify in order
	// with some hit multiple times.

	// Single byte

	SECTION("UTF8Classify Simple ASCII") {
		REQUIRE(UTFClass("a") == 1);
	}
	SECTION("UTF8Classify Invalid Too large lead") {
		REQUIRE(UTFClass("\xF5") == (1|UTF8MaskInvalid));
	}
	SECTION("UTF8Classify Overlong") {
		REQUIRE(UTFClass("\xC0\x80") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify single trail byte") {
		REQUIRE(UTFClass("\x80") == (1 | UTF8MaskInvalid));
	}

	// Invalid length tests

	SECTION("UTF8Classify 2 byte lead, string less than 2 long") {
		REQUIRE(UTFClass("\xD0") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 3 byte lead, string less than 3 long") {
		REQUIRE(UTFClass("\xEF") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 4 byte lead, string less than 4 long") {
		REQUIRE(UTFClass("\xF0") == (1 | UTF8MaskInvalid));
	}

	// Invalid first trail byte tests

	SECTION("UTF8Classify 2 byte lead trail is invalid") {
		REQUIRE(UTFClass("\xD0q") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 3 byte lead invalid trails") {
		REQUIRE(UTFClass("\xE2qq") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 4 byte bad trails") {
		REQUIRE(UTFClass("\xF0xyz") == (1 | UTF8MaskInvalid));
	}

	// 2 byte lead

	SECTION("UTF8Classify 2 byte valid character") {
		REQUIRE(UTFClass("\xD0\x80") == 2);
	}

	// 3 byte lead

	SECTION("UTF8Classify 3 byte lead, overlong") {
		REQUIRE(UTFClass("\xE0\x80\xAF") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 3 byte lead, surrogate") {
		REQUIRE(UTFClass("\xED\xA0\x80") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify FFFE non-character") {
		REQUIRE(UTFClass("\xEF\xBF\xBE") == (3 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify FFFF non-character") {
		REQUIRE(UTFClass("\xEF\xBF\xBF") == (3 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify FDD0 non-character") {
		REQUIRE(UTFClass("\xEF\xB7\x90") == (3 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 3 byte valid character") {
		REQUIRE(UTFClass("\xE2\x82\xAC") == 3);
	}

	// 4 byte lead

	SECTION("UTF8Classify 1FFFF non-character") {
		REQUIRE(UTFClass("\xF0\x9F\xBF\xBF") == (4 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 1 Greater than max Unicode 110000") {
		// Maximum Unicode value is 10FFFF so 110000 is out of range
		REQUIRE(UTFClass("\xF4\x90\x80\x80") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 4 byte overlong") {
		REQUIRE(UTFClass("\xF0\x80\x80\x80") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 4 byte valid character") {
		REQUIRE(UTFClass("\xF0\x9F\x8C\x90") == 4);
	}

	// Invalid 2nd or 3rd continuation bytes
	SECTION("UTF8Classify 3 byte lead invalid 2nd trail") {
		REQUIRE(UTFClass("\xE2\x82q") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 4 byte lead invalid 2nd trail") {
		REQUIRE(UTFClass("\xF0\x9Fq\x9F") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 4 byte lead invalid 3rd trail") {
		REQUIRE(UTFClass("\xF0\x9F\x9Fq") == (1 | UTF8MaskInvalid));
	}
}