Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
identifier_tests.cpp
Go to the documentation of this file.
1
#include "gtest/gtest.h"
2
#include <cstddef>
3
#include <cstdint>
4
#include <iostream>
5
#include <iterator>
6
#include <random>
7
8
#include "
idna.h
"
9
10
// Pull in the range tables (decompressed on first use) as ground truth for
11
// valid_name_code_point().
12
#include "
../src/table_store.hpp
"
13
14
class
IdnaTest
:
public
testing::Test {
15
protected
:
16
static
std::u32string
to_utf32
(std::string_view ut8_string) {
17
size_t
utf32_length =
18
ada::idna::utf32_length_from_utf8
(ut8_string.data(), ut8_string.size());
19
std::u32string utf32(utf32_length,
'\0'
);
20
ada::idna::utf8_to_utf32
(ut8_string.data(), ut8_string.size(),
21
utf32.data());
22
return
utf32;
23
}
24
25
// Helper method to verify code points
26
static
void
VerifyCodePoint
(std::string_view input,
bool
first,
27
bool
expected) {
28
std::u32string code_points =
to_utf32
(input);
29
ASSERT_FALSE(code_points.empty()) <<
"Failed to convert: "
<< input;
30
ASSERT_EQ(
ada::idna::valid_name_code_point
(code_points[0], first), expected)
31
<<
"Test failed for input: "
<< input <<
", first: "
<< std::boolalpha
32
<< first;
33
}
34
};
35
36
TEST_F
(
IdnaTest
, FirstPositionCodePoints) {
37
VerifyCodePoint(
"a"
,
true
,
true
);
38
VerifyCodePoint(
"é"
,
true
,
true
);
39
VerifyCodePoint(
"A"
,
true
,
true
);
40
VerifyCodePoint(
"0"
,
true
,
false
);
41
}
42
43
TEST_F
(
IdnaTest
, OtherPositionCodePoints) {
44
VerifyCodePoint(
"a"
,
false
,
true
);
45
VerifyCodePoint(
"A"
,
false
,
true
);
46
VerifyCodePoint(
"À"
,
false
,
true
);
47
VerifyCodePoint(
"0"
,
false
,
true
);
48
VerifyCodePoint(
" "
,
false
,
false
);
49
}
50
51
// Disabled by default: exhaustive sweep over all 1.1M code points is slow.
52
// Run explicitly with `--gtest_also_run_disabled_tests`.
53
TEST_F
(
IdnaTest
, DISABLED_ExhaustiveAgainstIdTables) {
54
// Walk the entire Unicode code-point space [0, 0x10FFFF] and confirm that
55
// valid_name_code_point() agrees with a direct scan of id_start /
56
// id_continue.
57
//
58
// The function adds two JS-identifier extensions on top of the Unicode
59
// ID_Start / ID_Continue properties (per
60
// https://tc39.es/ecma262/#prod-IdentifierStart):
61
// - '$' and '_' are valid in IdentifierStart
62
// - '$' is valid in IdentifierPart
63
// Surrogates (U+D800..U+DFFF) are rejected outright.
64
//
65
// The tables are sorted by range, so we walk them with rolling indices
66
// (si, ci) that only ever advance — O(N + table_size) total work.
67
ASSERT_TRUE(
ada::idna::ensure_tables
());
68
const
std::size_t id_start_n =
ada::idna::id_start_count
;
69
const
std::size_t id_continue_n =
ada::idna::id_continue_count
;
70
71
std::size_t si = 0, ci = 0;
72
for
(uint32_t cp = 0; cp <= 0x10FFFF; ++cp) {
73
while
(si < id_start_n &&
ada::idna::id_start
[si][1] < cp) ++si;
74
while
(ci < id_continue_n &&
ada::idna::id_continue
[ci][1] < cp) ++ci;
75
76
const
bool
is_surrogate = (cp >= 0xD800 && cp <= 0xDFFF);
77
78
const
bool
expected_first =
79
!is_surrogate &&
80
(cp == U
'$'
|| cp == U
'_'
||
81
(si < id_start_n && cp >=
ada::idna::id_start
[si][0]));
82
83
const
bool
expected_continue =
84
!is_surrogate && (cp == U
'$'
|| (ci < id_continue_n &&
85
cp >=
ada::idna::id_continue
[ci][0]));
86
87
ASSERT_EQ(
ada::idna::valid_name_code_point
(
static_cast<
char32_t
>
(cp),
true
),
88
expected_first)
89
<<
"Mismatch at U+"
<< std::hex << cp <<
" (first=true)"
;
90
ASSERT_EQ(
91
ada::idna::valid_name_code_point
(
static_cast<
char32_t
>
(cp),
false
),
92
expected_continue)
93
<<
"Mismatch at U+"
<< std::hex << cp <<
" (first=false)"
;
94
}
95
}
96
97
TEST_F
(
IdnaTest
, SampledAgainstIdTables) {
98
// Same check as DISABLED_ExhaustiveAgainstIdTables, but samples roughly
99
// 1 in 100 code points using a fixed-seed RNG so the run is deterministic
100
// and fast enough for the default test suite. The rolling table indices
101
// still advance for every code point so they remain consistent at the
102
// sampled positions.
103
ASSERT_TRUE(
ada::idna::ensure_tables
());
104
const
std::size_t id_start_n =
ada::idna::id_start_count
;
105
const
std::size_t id_continue_n =
ada::idna::id_continue_count
;
106
107
std::mt19937 rng(0xC0DE);
108
std::uniform_int_distribution<int>
pick
(0, 99);
109
110
std::size_t si = 0, ci = 0;
111
for
(uint32_t cp = 0; cp <= 0x10FFFF; ++cp) {
112
while
(si < id_start_n &&
ada::idna::id_start
[si][1] < cp) ++si;
113
while
(ci < id_continue_n &&
ada::idna::id_continue
[ci][1] < cp) ++ci;
114
115
if
(
pick
(rng) != 0)
continue
;
116
117
const
bool
is_surrogate = (cp >= 0xD800 && cp <= 0xDFFF);
118
119
const
bool
expected_first =
120
!is_surrogate &&
121
(cp == U
'$'
|| cp == U
'_'
||
122
(si < id_start_n && cp >=
ada::idna::id_start
[si][0]));
123
124
const
bool
expected_continue =
125
!is_surrogate && (cp == U
'$'
|| (ci < id_continue_n &&
126
cp >=
ada::idna::id_continue
[ci][0]));
127
128
ASSERT_EQ(
ada::idna::valid_name_code_point
(
static_cast<
char32_t
>
(cp),
true
),
129
expected_first)
130
<<
"Mismatch at U+"
<< std::hex << cp <<
" (first=true)"
;
131
ASSERT_EQ(
132
ada::idna::valid_name_code_point
(
static_cast<
char32_t
>
(cp),
false
),
133
expected_continue)
134
<<
"Mismatch at U+"
<< std::hex << cp <<
" (first=false)"
;
135
}
136
}
IdnaTest
Definition
identifier_tests.cpp:14
IdnaTest::to_utf32
static std::u32string to_utf32(std::string_view ut8_string)
Definition
identifier_tests.cpp:16
IdnaTest::VerifyCodePoint
static void VerifyCodePoint(std::string_view input, bool first, bool expected)
Definition
identifier_tests.cpp:26
TEST_F
TEST_F(IdnaTest, FirstPositionCodePoints)
Definition
identifier_tests.cpp:36
idna.h
ada::idna::id_continue_count
constexpr size_t id_continue_count
Definition
ada_idna.cpp:4900
ada::idna::utf32_length_from_utf8
size_t utf32_length_from_utf8(const char *buf, size_t len)
Definition
ada_idna.cpp:142
ada::idna::id_continue
range_pair_ptr id_continue
Definition
ada_idna.cpp:4888
ada::idna::ensure_tables
bool ensure_tables() noexcept
Definition
ada_idna.cpp:4956
ada::idna::valid_name_code_point
bool valid_name_code_point(char32_t code_point, bool first)
Definition
ada_idna.cpp:6972
ada::idna::id_start
range_pair_ptr id_start
Definition
ada_idna.cpp:4889
ada::idna::utf8_to_utf32
size_t utf8_to_utf32(const char *buf, size_t len, char32_t *utf32_output)
Definition
ada_idna.cpp:32
ada::idna::id_start_count
constexpr size_t id_start_count
Definition
ada_idna.cpp:4901
pick
static const char * pick(FuzzedDataProvider &fdp, const char *const (&arr)[N])
Definition
simple_absolute.cc:70
table_store.hpp