Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
mapping_tests.cpp
Go to the documentation of this file.
1
#include "gtest/gtest.h"
2
#include "
ada/idna/mapping.h
"
3
4
// ── original smoke tests ───────────────────────────────────────────────────
5
TEST
(mapping_tests, verify) {
6
ASSERT_EQ(
ada::idna::map
(U
"asciitwontchange"
), U
"asciitwontchange"
);
7
ASSERT_EQ(
ada::idna::map
(U
"hasomit\u00adted"
), U
"hasomitted"
);
8
ASSERT_EQ(
ada::idna::map
(U
"\u00aalla"
), U
"alla"
);
9
}
10
11
// ── ASCII fast-path ────────────────────────────────────────────────────────
12
// All 26 capital letters must map to their lowercase equivalents.
13
TEST
(mapping_tests, ascii_uppercase_to_lowercase) {
14
for
(
char32_t
cp = U
'A'
; cp <= U
'Z'
; ++cp) {
15
std::u32string input(1, cp);
16
std::u32string expected(1, cp + 32);
17
EXPECT_EQ(
ada::idna::map
(input), expected)
18
<<
"Failed for U+"
<< std::hex << (unsigned)cp;
19
}
20
}
21
22
// Digits and hyphen must be valid.
23
TEST
(mapping_tests, ascii_digits_and_hyphen_valid) {
24
for
(
char32_t
cp = U
'0'
; cp <= U
'9'
; ++cp) {
25
std::u32string input(1, cp);
26
EXPECT_EQ(
ada::idna::map
(input), input)
27
<<
"Digit U+"
<< std::hex << (unsigned)cp <<
" should be valid"
;
28
}
29
EXPECT_EQ(
ada::idna::map
(U
"-"
), U
"-"
);
// hyphen-minus
30
}
31
32
// ── Ignored code points -> empty output ────────────────────────────────────
33
TEST
(mapping_tests, ignored_code_points) {
34
// U+00AD SOFT HYPHEN
35
EXPECT_EQ(
ada::idna::map
(U
"\U000000AD"
), U
""
);
36
// U+034F COMBINING GRAPHEME JOINER
37
EXPECT_EQ(
ada::idna::map
(U
"\U0000034F"
), U
""
);
38
// U+200B ZERO WIDTH SPACE
39
EXPECT_EQ(
ada::idna::map
(U
"\U0000200B"
), U
""
);
40
// U+2060 WORD JOINER
41
EXPECT_EQ(
ada::idna::map
(U
"\U00002060"
), U
""
);
42
// U+FEFF ZERO WIDTH NO-BREAK SPACE (BOM)
43
EXPECT_EQ(
ada::idna::map
(U
"\U0000FEFF"
), U
""
);
44
}
45
46
// Ignored code points embedded in a valid string vanish.
47
TEST
(mapping_tests, ignored_embedded_in_valid) {
48
// "ab\u034Fcd" -> "abcd" (CGJ between valid chars)
49
EXPECT_EQ(
ada::idna::map
(U
"ab\U0000034Fcd"
), U
"abcd"
);
50
// Soft-hyphen inside a word
51
EXPECT_EQ(
ada::idna::map
(U
"foo\U000000ADbar"
), U
"foobar"
);
52
}
53
54
// ── Variation selector supplement U+E0100–U+E01EF (all ignored) ───────────
55
TEST
(mapping_tests, variation_selector_supplement_ignored) {
56
// First in range
57
EXPECT_EQ(
ada::idna::map
(U
"\U000E0100"
), U
""
);
58
// Middle of range
59
EXPECT_EQ(
ada::idna::map
(U
"\U000E0150"
), U
""
);
60
// Last in range
61
EXPECT_EQ(
ada::idna::map
(U
"\U000E01EF"
), U
""
);
62
}
63
64
// ── Disallowed code points -> empty string returned from map() ─────────────
65
// Note: U+007F (DEL) is actually VALID in IDNA (range 007B..007F valid NV8).
66
// The first disallowed code point after ASCII printables is U+0080.
67
TEST
(mapping_tests, disallowed_returns_empty) {
68
// U+0080 first C1 control (UCN banned for < 0xA0; use char32_t cast)
69
EXPECT_TRUE(
ada::idna::map
(std::u32string(1,
char32_t
(0x80))).empty());
70
// U+0085 NEXT LINE (another C1 control)
71
EXPECT_TRUE(
ada::idna::map
(std::u32string(1,
char32_t
(0x85))).empty());
72
// Private-use area (U+E000)
73
EXPECT_TRUE(
ada::idna::map
(U
"\U0000E000"
).empty());
74
// Surrogates (UCN not allowed for D800-DFFF; use char32_t cast)
75
EXPECT_TRUE(
ada::idna::map
(std::u32string(1,
char32_t
(0xD800))).empty());
76
EXPECT_TRUE(
ada::idna::map
(std::u32string(1,
char32_t
(0xDFFF))).empty());
77
// Noncharacters
78
EXPECT_TRUE(
ada::idna::map
(U
"\U0000FFFE"
).empty());
79
EXPECT_TRUE(
ada::idna::map
(U
"\U0000FFFF"
).empty());
80
// Just after the mid-range valid area
81
EXPECT_TRUE(
ada::idna::map
(U
"\U0003347A"
).empty());
82
// Just after the ignored high range
83
EXPECT_TRUE(
ada::idna::map
(U
"\U000E01F0"
).empty());
84
}
85
86
// ── Single-code-point mappings ─────────────────────────────────────────────
87
TEST
(mapping_tests, single_codepoint_mappings) {
88
// U+00AA FEMININE ORDINAL INDICATOR -> 'a'
89
EXPECT_EQ(
ada::idna::map
(U
"\U000000AA"
), U
"a"
);
90
// U+00BA MASCULINE ORDINAL INDICATOR -> 'o'
91
EXPECT_EQ(
ada::idna::map
(U
"\U000000BA"
), U
"o"
);
92
// U+00B5 MICRO SIGN -> U+03BC (Greek small mu)
93
EXPECT_EQ(
ada::idna::map
(U
"\U000000B5"
), U
"\U000003BC"
);
94
// U+03A3 GREEK CAPITAL SIGMA -> U+03C3
95
EXPECT_EQ(
ada::idna::map
(U
"\U000003A3"
), U
"\U000003C3"
);
96
// U+0410 CYRILLIC CAPITAL A -> U+0430
97
EXPECT_EQ(
ada::idna::map
(U
"\U00000410"
), U
"\U00000430"
);
98
// U+FF21 FULLWIDTH LATIN CAPITAL LETTER A -> 'a'
99
EXPECT_EQ(
ada::idna::map
(U
"\U0000FF21"
), U
"a"
);
100
// U+FF3A FULLWIDTH LATIN CAPITAL LETTER Z -> 'z'
101
EXPECT_EQ(
ada::idna::map
(U
"\U0000FF3A"
), U
"z"
);
102
// U+A7DD LATIN CAPITAL LETTER CLOSED OMEGA (Unicode 18) -> U+0277
103
EXPECT_EQ(
ada::idna::map
(U
"\U0000A7DD"
), U
"\U00000277"
);
104
// U+1DF40 LATIN CAPITAL LETTER BARRED A (Unicode 18) -> U+1DF41
105
EXPECT_EQ(
ada::idna::map
(U
"\U0001DF40"
), U
"\U0001DF41"
);
106
}
107
108
// ── Multi-code-point mappings ──────────────────────────────────────────────
109
TEST
(mapping_tests, multi_codepoint_mappings) {
110
// U+00B2 SUPERSCRIPT TWO -> "2"
111
EXPECT_EQ(
ada::idna::map
(U
"\U000000B2"
), U
"2"
);
112
// U+00B3 SUPERSCRIPT THREE -> "3"
113
EXPECT_EQ(
ada::idna::map
(U
"\U000000B3"
), U
"3"
);
114
// U+00BC VULGAR FRACTION ONE QUARTER -> "1/4" (U+0031 U+2044 U+0034)
115
EXPECT_EQ(
ada::idna::map
(U
"\U000000BC"
),
116
(std::u32string{0x31, 0x2044, 0x34}));
117
// U+FB00 LATIN SMALL LIGATURE FF -> "ff"
118
EXPECT_EQ(
ada::idna::map
(U
"\U0000FB00"
), U
"ff"
);
119
// U+FB01 LATIN SMALL LIGATURE FI -> "fi"
120
EXPECT_EQ(
ada::idna::map
(U
"\U0000FB01"
), U
"fi"
);
121
// U+FB03 LATIN SMALL LIGATURE FFI -> "ffi"
122
EXPECT_EQ(
ada::idna::map
(U
"\U0000FB03"
), U
"ffi"
);
123
// U+0132 LATIN CAPITAL LIGATURE IJ -> "ij"
124
EXPECT_EQ(
ada::idna::map
(U
"\U00000132"
), U
"ij"
);
125
// U+01F1 LATIN CAPITAL LETTER DZ -> "dz"
126
EXPECT_EQ(
ada::idna::map
(U
"\U000001F1"
), U
"dz"
);
127
// U+1E9E LATIN CAPITAL LETTER SHARP S -> U+00DF
128
EXPECT_EQ(
ada::idna::map
(U
"\U00001E9E"
), U
"\U000000DF"
);
129
}
130
131
// ── Valid non-ASCII code points pass through unchanged ─────────────────────
132
TEST
(mapping_tests, valid_non_ascii_unchanged) {
133
// U+00DF LATIN SMALL LETTER SHARP S (valid in IDNA, not mapped to "ss")
134
EXPECT_EQ(
ada::idna::map
(U
"\U000000DF"
), U
"\U000000DF"
);
135
// U+00E0 LATIN SMALL LETTER A WITH GRAVE
136
EXPECT_EQ(
ada::idna::map
(U
"\U000000E0"
), U
"\U000000E0"
);
137
// U+03B1 GREEK SMALL LETTER ALPHA
138
EXPECT_EQ(
ada::idna::map
(U
"\U000003B1"
), U
"\U000003B1"
);
139
// U+4E2D CJK UNIFIED IDEOGRAPH
140
EXPECT_EQ(
ada::idna::map
(U
"\U00004E2D"
), U
"\U00004E2D"
);
141
}
142
143
// ── Mid-range boundaries (0x30000–0x3FC40) ────────────────────────────────
144
// These code points are inside the two-level table (LOW_RANGE_END = 0x3FC40).
145
// U+3134B–U+3134F is a small disallowed gap within an otherwise-valid range.
146
TEST
(mapping_tests, mid_range_boundaries) {
147
// U+2FFFF: last code point inside the low-range two-level table
148
// (It is disallowed – just verify we don't crash or return wrong result)
149
EXPECT_TRUE(
ada::idna::map
(U
"\U0002FFFF"
).empty());
// disallowed
150
151
// U+30000: first valid code point of mid range (CJK Extension I start)
152
EXPECT_EQ(
ada::idna::map
(U
"\U00030000"
), (std::u32string{0x30000}));
153
154
// U+3134A: last valid of mid-range part 1
155
EXPECT_EQ(
ada::idna::map
(U
"\U0003134A"
), (std::u32string{0x3134A}));
156
157
// U+3134B: first disallowed in the small mid-range gap
158
EXPECT_TRUE(
ada::idna::map
(U
"\U0003134B"
).empty());
159
160
// U+3134F: last disallowed in the small mid-range gap
161
EXPECT_TRUE(
ada::idna::map
(U
"\U0003134F"
).empty());
162
163
// U+31350: first valid of mid-range part 2 (CJK Extension J start)
164
EXPECT_EQ(
ada::idna::map
(U
"\U00031350"
), (std::u32string{0x31350}));
165
166
// U+33479: last valid of CJK Extension J
167
EXPECT_EQ(
ada::idna::map
(U
"\U00033479"
), (std::u32string{0x33479}));
168
169
// U+3347A: first disallowed after CJK Extension J
170
EXPECT_TRUE(
ada::idna::map
(U
"\U0003347A"
).empty());
171
172
// U+3CFFF: last disallowed before the Small Seal block
173
EXPECT_TRUE(
ada::idna::map
(U
"\U0003CFFF"
).empty());
174
175
// U+3D000: first Small Seal character (Unicode 18)
176
EXPECT_EQ(
ada::idna::map
(U
"\U0003D000"
), (std::u32string{0x3D000}));
177
178
// U+3FC3F: last Small Seal character, the last valid code point of the table
179
EXPECT_EQ(
ada::idna::map
(U
"\U0003FC3F"
), (std::u32string{0x3FC3F}));
180
181
// U+3FC40: first code point past the two-level table -> disallowed
182
EXPECT_TRUE(
ada::idna::map
(U
"\U0003FC40"
).empty());
183
}
184
185
// ── High ignored range boundaries (0xE0100–0xE01EF) ───────────────────────
186
TEST
(mapping_tests, high_ignored_range_boundaries) {
187
// U+E00FF: just before the ignored range -> disallowed
188
EXPECT_TRUE(
ada::idna::map
(U
"\U000E00FF"
).empty());
189
190
// U+E0100: first in the ignored range
191
EXPECT_EQ(
ada::idna::map
(U
"\U000E0100"
), U
""
);
192
193
// U+E01EF: last in the ignored range
194
EXPECT_EQ(
ada::idna::map
(U
"\U000E01EF"
), U
""
);
195
196
// U+E01F0: just after the ignored range -> disallowed
197
EXPECT_TRUE(
ada::idna::map
(U
"\U000E01F0"
).empty());
198
}
199
200
// ── Block (64-cp) boundary conditions ─────────────────────────────────────
201
// Test the last code point of a block and the first of the next block
202
// to guard against off-by-one errors in the stage1/stage2 indexing.
203
TEST
(mapping_tests, block_boundary_conditions) {
204
// Block 0 ends at cp 63 (U+003F '?') – valid
205
EXPECT_EQ(
ada::idna::map
(U
"\U0000003F"
), U
"\U0000003F"
);
206
// Block 1 starts at cp 64 (U+0040 '@') – valid
207
EXPECT_EQ(
ada::idna::map
(U
"\U00000040"
), U
"\U00000040"
);
208
209
// Block 1 ends at cp 127 (U+007F DEL) – valid per IDNA (007B..007F valid NV8)
210
EXPECT_EQ(
ada::idna::map
(std::u32string(1,
char32_t
(0x7F))),
211
std::u32string(1,
char32_t
(0x7F)));
212
// Block 2 starts at cp 128 (U+0080 first C1 control) – disallowed
213
EXPECT_TRUE(
ada::idna::map
(std::u32string(1,
char32_t
(0x80))).empty());
214
215
// Block 3: cp 192 (U+00C0 'À') -> mapped to 'à' (U+00E0)
216
EXPECT_EQ(
ada::idna::map
(U
"\U000000C0"
), U
"\U000000E0"
);
217
// Block 3: cp 223 (U+00DF 'ß') -> valid
218
EXPECT_EQ(
ada::idna::map
(U
"\U000000DF"
), U
"\U000000DF"
);
219
// Block 4: cp 224 (U+00E0 'à') -> valid
220
EXPECT_EQ(
ada::idna::map
(U
"\U000000E0"
), U
"\U000000E0"
);
221
222
// Block at 0x640 (cp 1600 = Arabic tatweel, U+0640) -> valid
223
EXPECT_EQ(
ada::idna::map
(U
"\U00000640"
), U
"\U00000640"
);
224
225
// cp 0x3FC (1020 = U+03FC GREEK SMALL LETTER RHO WITH STROKE) -> valid
226
EXPECT_EQ(
ada::idna::map
(U
"\U000003FC"
), U
"\U000003FC"
);
227
}
228
229
// ── String-level tests ──────────────────────────────────────────────────────
230
// Mapping operates on strings, not just individual code points.
231
232
TEST
(mapping_tests, string_with_all_valid) {
233
EXPECT_EQ(
ada::idna::map
(U
"hello"
), U
"hello"
);
234
EXPECT_EQ(
ada::idna::map
(U
"hello123"
), U
"hello123"
);
235
}
236
237
TEST
(mapping_tests, string_uppercase_folded) {
238
EXPECT_EQ(
ada::idna::map
(U
"Hello"
), U
"hello"
);
239
EXPECT_EQ(
ada::idna::map
(U
"EXAMPLE"
), U
"example"
);
240
EXPECT_EQ(
ada::idna::map
(U
"MiXeD"
), U
"mixed"
);
241
}
242
243
TEST
(mapping_tests, string_ignored_chars_removed) {
244
// Multiple soft hyphens removed
245
EXPECT_EQ(
ada::idna::map
(U
"a\U000000AD\U000000ADb"
), U
"ab"
);
246
// Ignored variation selector removed, leaving base char
247
EXPECT_EQ(
ada::idna::map
(U
"\U00004E2D\U000E0100"
), U
"\U00004E2D"
);
248
}
249
250
TEST
(mapping_tests, string_disallowed_anywhere_fails) {
251
// Disallowed at the start
252
EXPECT_TRUE(
ada::idna::map
(U
"\U0000FFFEhello"
).empty());
253
// Disallowed in the middle (U+0080, first C1 control)
254
EXPECT_TRUE(
ada::idna::map
(std::u32string(U
"hel"
) +
255
std::u32string(1,
char32_t
(0x80)) +
256
std::u32string(U
"lo"
))
257
.empty());
258
// Disallowed at the end
259
EXPECT_TRUE(
ada::idna::map
(std::u32string(U
"hello"
) +
260
std::u32string(1,
char32_t
(0x80)))
261
.empty());
262
}
263
264
TEST
(mapping_tests, string_multi_cp_mappings_in_context) {
265
// "file" (fi ligature + "le") -> "file"
266
EXPECT_EQ(
ada::idna::map
(U
"\U0000FB01le"
), U
"file"
);
267
// Mix of uppercase, ligature, and valid
268
EXPECT_EQ(
ada::idna::map
(U
"A\U0000FB00B"
), U
"affb"
);
269
}
270
271
TEST
(mapping_tests, empty_string) { EXPECT_EQ(
ada::idna::map
(U
""
), U
""
); }
272
273
// ── CJK compatibility ideographs in the mapping range ─────────────────────
274
// These are in the high area of the two-level table (0x2F800 range).
275
TEST
(mapping_tests, cjk_compatibility_ideographs) {
276
// U+2F800 maps to U+4E3D
277
EXPECT_EQ(
ada::idna::map
(U
"\U0002F800"
), (std::u32string{0x4E3D}));
278
// U+2F801 maps to U+4E38
279
EXPECT_EQ(
ada::idna::map
(U
"\U0002F801"
), (std::u32string{0x4E38}));
280
}
mapping.h
TEST
TEST(mapping_tests, verify)
Definition
mapping_tests.cpp:5
ada::idna::map
std::u32string map(std::u32string_view input)
Definition
ada_idna.cpp:5288