-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
NormalizationAll.cs
176 lines (151 loc) · 7.6 KB
/
NormalizationAll.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Reflection;
using System.Text;
using System.IO;
using Xunit;
namespace System.Globalization.Tests
{
public class StringNormalizationAllTests
{
private string ConvertToString(string codepoints)
{
StringBuilder sb = new StringBuilder();
string[] parts = codepoints.Split('-');
foreach (string part in parts)
{
sb.Append((char) int.Parse(part, NumberStyles.HexNumber | NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingWhite, CultureInfo.InvariantCulture));
}
return sb.ToString();
}
[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34577", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
public void Normalize()
{
// Windows 8 test data came from http://www.unicode.org/Public/UCD/latest/ucd/NormalizationTest.txt
// Windows 7 test came from http://www.unicode.org/Public/3.0-Update1/NormalizationTest-3.0.1.txt
using (Stream stream = typeof(StringNormalizationAllTests).GetTypeInfo().Assembly.GetManifestResourceStream(PlatformDetection.IsWindows7 ? "NormalizationDataWin7" : "NormalizationDataWin8"))
{
using (StreamReader sr = new StreamReader(stream))
{
int index = 0;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] parts = line.Split(',');
Assert.True(parts.Length == 5, $"Wrong data at the line {index}");
string part0 = ConvertToString(parts[0]);
string part1 = ConvertToString(parts[1]);
string part2 = ConvertToString(parts[2]);
string part3 = ConvertToString(parts[3]);
string part4 = ConvertToString(parts[4]);
// Form C
VerifyConformanceInvariant(NormalizationForm.FormC, part0, part1, part2, part3, part4);
// Form D
VerifyConformanceInvariant(NormalizationForm.FormD, part0, part1, part2, part3, part4);
// Mobile / Browser ICU doesn't support FormKC and FormKD
if (PlatformDetection.IsNotUsingLimitedCultures)
{
// Form KC
VerifyConformanceInvariant(NormalizationForm.FormKC, part0, part1, part2, part3, part4);
// Form KD
VerifyConformanceInvariant(NormalizationForm.FormKD, part0, part1, part2, part3, part4);
}
}
}
}
}
/// <summary>
/// Verifies the first normalization conformance invariant for the
/// specified normalization form, where the rule as defined for all
/// Unicode normalization forms is as follows:
/// 1. The following invariants must be true for all conformant
/// implementations:
/// NFC
/// c2 == NFC(c1) == NFC(c2) == NFC(c3)
/// c4 == NFC(c4) == NFC(c5)
///
/// NFD
/// c3 == NFD(c1) == NFD(c2) == NFD(c3)
/// c5 == NFD(c4) == NFD(c5)
///
/// NFKC
/// c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4) == NFKC(c5)
///
/// NFKD
/// c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4) == NFKD(c5)
/// </summary>
private static void VerifyConformanceInvariant(NormalizationForm normForm, string c1, string c2, string c3, string c4, string c5)
{
string normalized1 = c1.Normalize(normForm);
string normalized2 = c2.Normalize(normForm);
string normalized3 = c3.Normalize(normForm);
string normalized4 = c4.Normalize(normForm);
string normalized5 = c5.Normalize(normForm);
switch (normForm)
{
case NormalizationForm.FormC:
// c2 == NFC(c1) == NFC(c2) == NFC(c3)
AssertEqualsForm(c2, normalized1);
AssertEqualsForm(c2, normalized2);
AssertEqualsForm(c2, normalized3);
// c4 == NFC(c4) == NFC(c5)
AssertEqualsForm(c4, normalized4);
AssertEqualsForm(c4, normalized5);
// c2 is normalized to Form C
Assert.True(c2.IsNormalized(normForm), $"'{c2}' is marked as not normalized with form {normForm}");
// c4 is normalized to Form C
Assert.True(c4.IsNormalized(normForm), $"'{c4}' is marked as not normalized with form {normForm}");
break;
case NormalizationForm.FormD:
// c3 == NFD(c1) == NFD(c2) == NFD(c3)
AssertEqualsForm(c3, normalized1);
AssertEqualsForm(c3, normalized2);
AssertEqualsForm(c3, normalized3);
// c5 == NFD(c4) == NFD(c5)
AssertEqualsForm(c5, normalized4);
AssertEqualsForm(c5, normalized5);
// c3 is normalized to Form D
Assert.True(c3.IsNormalized(normForm), $"'{c3}' is marked as not normalized with form {normForm}");
// c5 is normalized to Form D
Assert.True(c5.IsNormalized(normForm), $"'{c5}' is marked as not normalized with form {normForm}");
break;
case NormalizationForm.FormKC:
// c4 == NFKC(c1) == NFKC(c2) == NFKC(c3) == NFKC(c4)
// == NFKC(c5)
AssertEqualsForm(c4, normalized1);
AssertEqualsForm(c4, normalized2);
AssertEqualsForm(c4, normalized3);
AssertEqualsForm(c4, normalized4);
AssertEqualsForm(c4, normalized5);
// c4 is normalized to Form KC
Assert.True(c4.IsNormalized(normForm), $"'{c4}' is marked as not normalized with form {normForm}");
break;
case NormalizationForm.FormKD:
// c5 == NFKD(c1) == NFKD(c2) == NFKD(c3) == NFKD(c4)
// == NFKD(c5)
AssertEqualsForm(c5, normalized1);
AssertEqualsForm(c5, normalized2);
AssertEqualsForm(c5, normalized3);
AssertEqualsForm(c5, normalized4);
AssertEqualsForm(c5, normalized5);
// c5 is normalized to Form KD
Assert.True(c5.IsNormalized(normForm), $"'{c5}' is marked as not normalized with form {normForm}");
break;
}
}
private static void AssertEqualsForm(string c, string cForm)
{
Assert.True(c.Equals(cForm), $"'{DumpStringAsCodepoints(c)}' is not matched with the normalized form '{DumpStringAsCodepoints(cForm)} with {cForm} normalization");
}
private static string DumpStringAsCodepoints(string s)
{
StringBuilder sb = new StringBuilder();
for (int i=0; i<s.Length; i++)
{
sb.Append($"\\x{(int)s[i]:X4}");
}
return sb.ToString();
}
}
}