aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/unit/testUniConversion.cxx
blob: 4d34abd602a9c11e9b3b7c2c331399571b8f5641 (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
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
// Unit Tests for Scintilla internal data structures

#include <cstring>

#include <string>
#include <algorithm>

#include "Platform.h"

#include "UniConversion.h"

#include "catch.hpp"

using namespace Scintilla;

// 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";
		size_t len = UTF16Length(s, strlen(s));
		REQUIRE(len == 1U);
	}

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

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

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

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

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

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

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

TEST_CASE("UniConversion") {

	// UTF16FromUTF8

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

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

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

	SECTION("UTF16FromUTF8 Example3") {
		const char s[] = {'\xE2', '\x82', '\xAC', 0};
		wchar_t tbuf[1] = {0};
		size_t tlen = UTF16FromUTF8(s, 3, 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};
		size_t tlen = UTF16FromUTF8(s, 4, 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] = {};
		size_t tlen = UTF16FromUTF8(s, 4, 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] = {};
		size_t tlen = UTF16FromUTF8(s, 2, 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] = {};
		size_t tlen = UTF16FromUTF8(s, 4, 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};
		size_t tlen = UTF32FromUTF8(s, 1, 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};
		size_t tlen = UTF32FromUTF8(s, 1, tbuf, 1);
		REQUIRE(tlen == 1U);
		REQUIRE(tbuf[0] == 0x24);
	}

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

	SECTION("UTF32FromUTF8 Example3") {
		const char s[] = {'\xE2', '\x82', '\xAC', 0};
		unsigned int tbuf[1] = {0};
		size_t tlen = UTF32FromUTF8(s, 3, 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};
		size_t tlen = UTF32FromUTF8(s, 4, 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] = {};
		size_t tlen = UTF32FromUTF8(s, 4, 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] = {};
		size_t tlen = UTF32FromUTF8(s, 2, 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] = {};
		size_t tlen = UTF32FromUTF8(s, 4, 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(const char *s) {
	return UTF8Classify(reinterpret_cast<const unsigned char *>(s), static_cast<int>(strlen(s)));
}

}

TEST_CASE("UTF8Classify") {

	// These tests are supposed to hit every return statement in UTF8Classify once in order
	// except the last which is hit twice.

	// Single byte

	SECTION("UTF8Classify Simple ASCII") {
		REQUIRE(UTFClass("a") == 1);
	}
	SECTION("UTF8Classify Invalid Too large lead") {
		REQUIRE(UTFClass("\xF5") == (1|UTF8MaskInvalid));
	}

	// 4 byte lead

	SECTION("UTF8Classify 4 byte lead, string less than 4 long") {
		REQUIRE(UTFClass("\xF0") == (1 | UTF8MaskInvalid));
	}
	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);
	}
	SECTION("UTF8Classify 4 byte bad trails") {
		REQUIRE(UTFClass("\xF0xyz") == (1 | UTF8MaskInvalid));
	}

	// 3 byte lead

	SECTION("UTF8Classify 3 byte lead, string less than 3 long") {
		REQUIRE(UTFClass("\xEF") == (1 | UTF8MaskInvalid));
	}
	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);
	}
	SECTION("UTF8Classify 3 byte bad trails") {
		REQUIRE(UTFClass("\xE2qq") == (1 | UTF8MaskInvalid));
	}

	// 2 byte lead

	SECTION("UTF8Classify 2 byte lead, string less than 2 long") {
		REQUIRE(UTFClass("\xD0") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify 2 byte valid character") {
		REQUIRE(UTFClass("\xD0\x80") == 2);
	}
	SECTION("UTF8Classify 2 byte lead trail is invalid") {
		REQUIRE(UTFClass("\xD0q") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify Overlong") {
		REQUIRE(UTFClass("\xC0") == (1 | UTF8MaskInvalid));
	}
	SECTION("UTF8Classify single trail byte") {
		REQUIRE(UTFClass("\x80") == (1 | UTF8MaskInvalid));
	}
}