-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestUnicode.Normalization.pas
More file actions
222 lines (178 loc) · 6.21 KB
/
TestUnicode.Normalization.pas
File metadata and controls
222 lines (178 loc) · 6.21 KB
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
unit TestUnicode.Normalization;
interface
uses
Generics.Collections,
Windows, Classes, SysUtils,
PascalType.Unicode,
TestFramework;
type
TUnicodeTestCase = record
Row: integer;
Name: string;
Source: TPascalTypeCodePoints;
NFC: TPascalTypeCodePoints;
NFD: TPascalTypeCodePoints;
NFKC: TPascalTypeCodePoints;
NFKD: TPascalTypeCodePoints;
end;
TTestPascalTypeUnicodeNormalization = class(TTestCase)
strict private
FTestCases: TList<TUnicodeTestCase>;
public
procedure SetUp; override;
procedure TearDown; override;
published
procedure TestDecompose;
procedure TestCompose;
end;
implementation
uses
TestUnicode;
{ TTestPascalTypeUnicodeNormalization }
const
sUnicodeDataFolder = '..\..\Source\Unicode\UCD';
procedure TTestPascalTypeUnicodeNormalization.SetUp;
function HexToCardinal(const Hex: string): TPascalTypeCodePoint;
begin
Result := StrToInt('$'+Hex);
end;
function HexToCardinals(const Hex: string): TPascalTypeCodePoints;
begin
var Values := Hex.Split([' ']);
SetLength(Result, Length(Values));
for var i := 0 to High(Values) do
Result[i] := HexToCardinal(Values[i]);
end;
begin
inherited;
FTestCases := TList<TUnicodeTestCase>.Create;
var Reader := TStreamReader.Create(sUnicodeDataFolder + '\NormalizationTest.txt', TEncoding.UTF8);
try
var RowValues := TStringList.Create;
try
RowValues.Delimiter := ';';
RowValues.StrictDelimiter := True;
var RowIndex := 0;
while (not Reader.EndOfStream) do
begin
var Row := Reader.ReadLine;
Inc(RowIndex);
if (Row.StartsWith('#')) or (Row.StartsWith('@')) then
continue;
var UnicodeTestCase: TUnicodeTestCase;
UnicodeTestCase.Row := RowIndex;
var n := Pos(' ) ', Row);
if (n > 0) then
UnicodeTestCase.Name := Copy(Row, n+3, MaxInt);
n := Pos('; #', Row);
if (n > 0) then
SetLength(Row, n-1);
RowValues.DelimitedText := Row;
UnicodeTestCase.Source := HexToCardinals(RowValues[0]);
UnicodeTestCase.NFC := HexToCardinals(RowValues[1]);
UnicodeTestCase.NFD := HexToCardinals(RowValues[2]);
UnicodeTestCase.NFKC := HexToCardinals(RowValues[3]);
UnicodeTestCase.NFKD := HexToCardinals(RowValues[4]);
FTestCases.Add(UnicodeTestCase);
end;
finally
RowValues.Free;
end;
finally
Reader.Free;
end;
end;
procedure TTestPascalTypeUnicodeNormalization.TearDown;
begin
FTestCases.Free;
inherited;
end;
procedure TTestPascalTypeUnicodeNormalization.TestCompose;
function CodePointsToString(const CodePoints: TPascalTypeCodePoints): string;
begin
Result := '';
for var CodePoint in CodePoints do
begin
if (Result <> '') then
Result := Result + ' ';
Result := Result + IntToHex(CodePoint, 4);
end;
end;
begin
var Succeeded := 0;
var Failed := 0;
var Skipped := 0;
for var UnicodeTestCase in FTestCases do
begin
// Status(Format('Testing row %d: %s...', [UnicodeTestCase.Row, UnicodeTestCase.Name]));
var ThisFailed := False;
var Normalized := UnicodeTestCase.NFD;
Normalized := PascalTypeUnicode.Decompose(Normalized);
// PascalTypeUnicode.Normalize(Normalized);
var ComposedCodePoints := PascalTypeUnicode.Compose(Normalized);
// CheckEquals(Length(ComposedCodePoints), Length(UnicodeTestCase.NFC), Format('Incorrect Composed length in row %d', [UnicodeTestCase.Row]));
if (Length(UnicodeTestCase.NFC) <> Length(ComposedCodePoints)) then
begin
Status(Format('Incorrect composed length in row %d. Expected: %d, Actual: %d (%s)', [UnicodeTestCase.Row, Length(UnicodeTestCase.NFC), Length(ComposedCodePoints), UnicodeTestCase.Name]));
ThisFailed := True;
end;
for var i := 0 to High(ComposedCodePoints) do
if (UnicodeTestCase.NFC[i] <> ComposedCodePoints[i]) then
begin
Status(Format('Incorrect composed component in row %d. Decomposed: %s, Expected:%s, Actual: %s (%s)',
[UnicodeTestCase.Row, CodePointsToString(UnicodeTestCase.NFD), CodePointsToString(UnicodeTestCase.NFC), CodePointsToString(ComposedCodePoints), UnicodeTestCase.Name]));
ThisFailed := True;
break;
end;
if (ThisFailed) then
Inc(Failed)
else
Inc(Succeeded);
end;
Status(Format('%.0n tests succeeded, %.0n tests failed, %.0n skipped', [Succeeded * 1.0, Failed * 1.0, Skipped * 1.0]));
Check(Failed = 0);
end;
procedure TTestPascalTypeUnicodeNormalization.TestDecompose;
function CodePointsToString(const CodePoints: TPascalTypeCodePoints): string;
begin
Result := '';
for var CodePoint in CodePoints do
begin
if (Result <> '') then
Result := Result + ' ';
Result := Result + IntToHex(CodePoint, 4);
end;
end;
begin
var Succeeded := 0;
var Failed := 0;
var Skipped := 0;
for var UnicodeTestCase in FTestCases do
begin
// Status(Format('Testing row %d: %s...', [UnicodeTestCase.Row, UnicodeTestCase.Name]));
var DecomposedCodePoints := PascalTypeUnicode.Decompose(UnicodeTestCase.Source);
PascalTypeUnicode.Normalize(DecomposedCodePoints);
// CheckEquals(Length(UnicodeTestCase.NFD), Length(DecomposedCodePoints), Format('Incorrect decomposed length in row %d', [UnicodeTestCase.Row]));
var ThisFailed := False;
for var i := 0 to High(DecomposedCodePoints) do
if (UnicodeTestCase.NFD[i] <> DecomposedCodePoints[i]) then
begin
var Msg := '';
if (UnicodeTestCase.NFKD[i] = DecomposedCodePoints[i]) then
Msg := ' (*** matched NFKD ***)';
Status(Format('Incorrect decomposed component in row %d. Source: %s, Expected:%s, Actual: %s%s',
[UnicodeTestCase.Row, CodePointsToString(UnicodeTestCase.Source), CodePointsToString(UnicodeTestCase.NFD), CodePointsToString(DecomposedCodePoints), Msg]));
ThisFailed := True;
break;
end;
if (ThisFailed) then
Inc(Failed)
else
Inc(Succeeded);
end;
Status(Format('%.0n tests succeeded, %.0n tests failed, %.0n skipped', [Succeeded * 1.0, Failed * 1.0, Skipped * 1.0]));
Check(Failed = 0);
end;
initialization
TestSuiteUnicode.AddSuite(TTestPascalTypeUnicodeNormalization.Suite);
end.