forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnicodeTrie.swift.gyb
249 lines (204 loc) · 7.54 KB
/
UnicodeTrie.swift.gyb
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
//===--- UnicodeTrie.swift.gyb --------------------------------*- swift -*-===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
// RUN: rm -rf %t && mkdir -p %t && %S/../../utils/gyb -DunicodeGraphemeBreakPropertyFile=%S/../../utils/UnicodeData/GraphemeBreakProperty.txt -DunicodeGraphemeBreakTestFile=%S/../../utils/UnicodeData/GraphemeBreakTest.txt %s -o %t/UnicodeTrie.swift
// RUN: %S/../../utils/line-directive %t/UnicodeTrie.swift -- %target-build-swift %t/UnicodeTrie.swift -o %t/a.out -g -Xfrontend -disable-access-control
// RUN: %S/../../utils/line-directive %t/UnicodeTrie.swift -- %target-run %t/a.out
// REQUIRES: executable_test
// FIXME: rdar://problem/19648117 Needs splitting objc parts out
// XFAIL: linux
%{
from GYBUnicodeDataUtils import *
grapheme_cluster_break_property_table = \
GraphemeClusterBreakPropertyTable(unicodeGraphemeBreakPropertyFile)
}%
import SwiftPrivate
import StdlibUnittest
import Darwin
import Foundation
var graphemeBreakPropertyTable = [
// 'as Int' annotations are needed to help prevent the type-checker from
// blowing the stack. <rdar://problem/17539704>
% for start_code_point,end_code_point,value in grapheme_cluster_break_property_table.property_value_ranges:
(${start_code_point} as Int, ${end_code_point} as Int, _GraphemeClusterBreakPropertyValue.${value}),
% end
]
var UnicodeTrie = TestSuite("UnicodeTrie")
UnicodeTrie.test("_UnicodeGraphemeClusterBreakPropertyTrie") {
// Verify that the trie reports correct values of the property for every code
// point.
var trie = _UnicodeGraphemeClusterBreakPropertyTrie()
var expected = [_GraphemeClusterBreakPropertyValue](count: 0x110000,
repeatedValue: _GraphemeClusterBreakPropertyValue.Other)
for (startCodePoint, endCodePoint, value) in graphemeBreakPropertyTable {
for cp in startCodePoint...endCodePoint {
expected[cp] = value
}
}
for cp in UInt32(0)...UInt32(0x10ffff) {
if cp % 0x10000 == 0 {
print("\(cp)...")
}
expectEqual(
expected[Int(cp)], trie.getPropertyValue(cp), "code point \(cp)")
}
}
%{
grapheme_cluster_break_tests = \
get_grapheme_cluster_break_tests_as_unicode_scalars(
unicodeGraphemeBreakTestFile)
}%
// The most simple subclass of NSString that CoreFoundation does not know
// about.
class NonContiguousNSString : NSString {
override init() {
_value = []
super.init()
}
required init(coder aDecoder: NSCoder) {
fatalError("don't call this initializer")
}
init(_ value: [UInt16]) {
_value = value
super.init()
}
convenience init(_ scalars: [UInt32]) {
var encoded: [UInt16] = []
var g = scalars.generate()
let output: (UInt16) -> Void = { encoded.append($0) }
let hadError =
transcode(UTF32.self, UTF16.self, g, output, stopOnError: true)
expectFalse(hadError)
self.init(encoded)
}
@objc override func copyWithZone(zone: NSZone) -> AnyObject {
// Ensure that copying this string produces a class that CoreFoundation
// does not know about.
return self
}
@objc override var length: Int {
return _value.count
}
@objc override func characterAtIndex(index: Int) -> unichar {
return _value[index]
}
var _value: [UInt16]
}
/// Verify that extended grapheme cluster boundaries in `subject` occur at
/// positions specified in `expectedBoundaries`.
func checkGraphemeClusterSegmentation(
expectedBoundaries: [Int], _ subject: String, _ stackTrace: SourceLocStack
) {
var actualBoundaries: [Int] = [ 0 ]
var unicodeScalarCount = 0
for c in subject.characters {
let currentClusterSize = String(c).unicodeScalars.count
unicodeScalarCount += currentClusterSize
actualBoundaries += [ unicodeScalarCount ]
}
expectEqual(
expectedBoundaries, actualBoundaries,
"scalars: \(asHex(Array(subject.unicodeScalars.lazy.map { $0.value })))"
)
let expectedCharacters: [Character] = Array(subject.characters)
checkSliceableWithBidirectionalIndex(expectedCharacters, subject.characters)
}
func checkGraphemeClusterSegmentation(
expectedBoundaries: [Int], scalars: [UInt32], _ stackTrace: SourceLocStack
) {
let subject = NonContiguousNSString(scalars) as String
checkGraphemeClusterSegmentation(expectedBoundaries, subject,
stackTrace.withCurrentLoc())
}
func checkGraphemeClusterSegmentation(
expectedBoundaries: [Int], codeUnits: [UInt16], _ stackTrace: SourceLocStack
) {
let subject = NonContiguousNSString(codeUnits) as String
checkGraphemeClusterSegmentation(expectedBoundaries, subject,
stackTrace.withCurrentLoc())
}
UnicodeTrie.test("GraphemeClusterSegmentation/UnicodeSpec") {
// Test segmentation algorithm using test data from the Unicode
// specification.
% for code_points,expected_boundaries in grapheme_cluster_break_tests:
if true {
let scalars: [UInt32] =
[ ${", ".join([ str(cp) for cp in code_points ])} ]
let expectedBoundaries: [Int] =
[ ${", ".join([ str(x) for x in expected_boundaries ])} ]
checkGraphemeClusterSegmentation(expectedBoundaries, scalars: scalars,
SourceLocStack().withCurrentLoc())
}
% end
}
UnicodeTrie.test("GraphemeClusterSegmentation/Extra") {
// Extra tests for input Strings that contain ill-formed code unit sequences.
// U+D800 (high-surrogate)
checkGraphemeClusterSegmentation(
[ 0, 1 ],
codeUnits: [ 0xd800 ],
SourceLocStack().withCurrentLoc())
// U+D800 (high-surrogate)
// U+D800 (high-surrogate)
checkGraphemeClusterSegmentation(
[ 0, 1, 2 ],
codeUnits: [ 0xd800, 0xd800 ],
SourceLocStack().withCurrentLoc())
// U+0041 LATIN CAPITAL LETTER A
// U+D800 (high-surrogate)
checkGraphemeClusterSegmentation(
[ 0, 1, 2 ],
codeUnits: [ 0x0041, 0xd800 ],
SourceLocStack().withCurrentLoc())
// U+D800 (high-surrogate)
// U+0041 LATIN CAPITAL LETTER A
checkGraphemeClusterSegmentation(
[ 0, 1, 2 ],
codeUnits: [ 0xd800, 0x0041 ],
SourceLocStack().withCurrentLoc())
// U+0041 LATIN CAPITAL LETTER A
// U+0301 COMBINING ACUTE ACCENT
// U+D800 (high-surrogate)
checkGraphemeClusterSegmentation(
[ 0, 2, 3 ],
codeUnits: [ 0x0041, 0x0301, 0xd800 ],
SourceLocStack().withCurrentLoc())
// U+D800 (high-surrogate)
// U+0041 LATIN CAPITAL LETTER A
// U+0301 COMBINING ACUTE ACCENT
checkGraphemeClusterSegmentation(
[ 0, 1, 3 ],
codeUnits: [ 0xd800, 0x0041, 0x0301 ],
SourceLocStack().withCurrentLoc())
}
UnicodeTrie.test("GraphemeClusterSegmentation/Unicode_7_0_0") {
// Verify that we are using Unicode 7.0.0+ data tables.
// In Unicode 6.3.0, this sequence was segmented into two grapheme clusters.
//
// U+0041 LATIN CAPITAL LETTER A
// U+1122C KHOJKI VOWEL SIGN AA
checkGraphemeClusterSegmentation(
[ 0, 2 ],
scalars: [ 0x0041, 0x1122c ],
SourceLocStack().withCurrentLoc())
}
UnicodeTrie.test("GraphemeClusterSegmentation/Unicode_8_0_0") {
// Verify that we are using Unicode 8.0.0+ data tables.
// In Unicode 7.0.0, this sequence was segmented into two grapheme clusters.
//
// U+0041 LATIN CAPITAL LETTER A
// U+11720 AHOM VOWEL SIGN A
checkGraphemeClusterSegmentation(
[ 0, 2 ],
scalars: [ 0x0041, 0x11720 ],
SourceLocStack().withCurrentLoc())
}
runAllTests()