forked from swiftlang/swift-corelibs-foundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestNSRange.swift
195 lines (171 loc) · 9.96 KB
/
TestNSRange.swift
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
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 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
//
class TestNSRange : XCTestCase {
static var allTests: [(String, (TestNSRange) -> () throws -> Void)] {
return [
("test_NSRangeFromString", test_NSRangeFromString ),
("test_NSRangeBridging", test_NSRangeBridging),
("test_NSMaxRange", test_NSMaxRange),
("test_NSLocationInRange", test_NSLocationInRange),
("test_NSEqualRanges", test_NSEqualRanges),
("test_NSUnionRange", test_NSUnionRange),
("test_NSIntersectionRange", test_NSIntersectionRange),
("test_NSStringFromRange", test_NSStringFromRange),
("test_init_region_in_ascii_string", test_init_region_in_ascii_string),
("test_init_region_in_unicode_string", test_init_region_in_unicode_string),
("test_hashing", test_hashing),
]
}
func test_NSRangeFromString() {
let emptyRangeStrings = [
"",
"{}",
"{a, b}",
]
let emptyRange = NSRange(location: 0, length: 0)
for string in emptyRangeStrings {
XCTAssert(NSEqualRanges(NSRangeFromString(string), emptyRange))
}
let partialRangeStrings = [
"12",
"[12]",
"{12",
"{12,",
]
let partialRange = NSRange(location: 12, length: 0)
for string in partialRangeStrings {
XCTAssert(NSEqualRanges(NSRangeFromString(string), partialRange))
}
let fullRangeStrings = [
"{12, 34}",
"[12, 34]",
"12.34",
]
let fullRange = NSRange(location: 12, length: 34)
for string in fullRangeStrings {
XCTAssert(NSEqualRanges(NSRangeFromString(string), fullRange))
}
}
func test_NSRangeBridging() {
let swiftRange: Range<Int> = 1..<7
let range = NSRange(swiftRange)
let swiftRange2 = Range(range)
XCTAssertEqual(swiftRange, swiftRange2)
}
func test_NSMaxRange() {
let ranges = [(NSRange(location: 0, length: 3), 3),
(NSRange(location: 7, length: 8), 15),
(NSRange(location: 56, length: 1), 57)]
for (range, result) in ranges {
XCTAssertEqual(NSMaxRange(range), result)
}
}
func test_NSLocationInRange() {
let ranges = [(3, NSRange(location: 0, length: 5), true),
(10, NSRange(location: 2, length: 9), true),
(7, NSRange(location: 2, length: 5), false),
(5, NSRange(location: 5, length: 1), true)];
for (location, range, result) in ranges {
XCTAssertEqual(NSLocationInRange(location, range), result);
}
}
func test_NSEqualRanges() {
let ranges = [(NSRange(location: 0, length: 3), NSRange(location: 0, length: 3), true),
(NSRange(location: 0, length: 4), NSRange(location: 0, length: 8), false),
(NSRange(location: 3, length: 6), NSRange(location: 3, length: 10), false),
(NSRange(location: 0, length: 5), NSRange(location: 7, length: 8), false)]
for (first, second, result) in ranges {
XCTAssertEqual(NSEqualRanges(first, second), result)
}
}
func test_NSUnionRange() {
let ranges = [(NSRange(location: 0, length: 5), NSRange(location: 3, length: 8), NSRange(location: 0, length: 11)),
(NSRange(location: 6, length: 10), NSRange(location: 3, length: 8), NSRange(location: 3, length: 13)),
(NSRange(location: 3, length: 8), NSRange(location: 6, length: 10), NSRange(location: 3, length: 13)),
(NSRange(location: 0, length: 5), NSRange(location: 7, length: 8), NSRange(location: 0, length: 15)),
(NSRange(location: 0, length: 3), NSRange(location: 1, length: 2), NSRange(location: 0, length: 3))]
for (first, second, result) in ranges {
XCTAssert(NSEqualRanges(NSUnionRange(first, second), result))
}
}
func test_NSIntersectionRange() {
let ranges = [(NSRange(location: 0, length: 5), NSRange(location: 3, length: 8), NSRange(location: 3, length: 2)),
(NSRange(location: 6, length: 10), NSRange(location: 3, length: 8), NSRange(location: 6, length: 5)),
(NSRange(location: 3, length: 8), NSRange(location: 6, length: 10), NSRange(location: 6, length: 5)),
(NSRange(location: 0, length: 5), NSRange(location: 7, length: 8), NSRange(location: 0, length: 0)),
(NSRange(location: 0, length: 3), NSRange(location: 1, length: 2), NSRange(location: 1, length: 2))]
for (first, second, result) in ranges {
XCTAssert(NSEqualRanges(NSIntersectionRange(first, second), result))
}
}
func test_NSStringFromRange() {
let ranges = ["{0, 0}": NSRange(location: 0, length: 0),
"{6, 4}": NSRange(location: 6, length: 4),
"{0, 10}": NSRange(location: 0, length: 10),
"{10, 200}": NSRange(location: 10, length: 200),
"{100, 10}": NSRange(location: 100, length: 10),
"{1000, 100000}": NSRange(location: 1000, length: 100_000)];
for (string, range) in ranges {
XCTAssertEqual(NSStringFromRange(range), string)
}
}
/// Specialized for the below tests.
private func _assertNSRangeInit<S: StringProtocol, R: RangeExpression>(
_ region: R, in target: S, is rangeString: String
) where R.Bound == S.Index {
XCTAssert(NSEqualRanges(NSRangeFromString(rangeString), NSRange(region, in: target)))
}
func test_init_region_in_ascii_string() {
// all count = 18
let normalString = "1;DROP TABLE users"
_assertNSRangeInit(normalString.index(normalString.startIndex, offsetBy: 2)..<normalString.index(normalString.endIndex, offsetBy: -6), in: normalString, is: "{2, 10}")
_assertNSRangeInit(normalString.index(after: normalString.startIndex)...normalString.index(before: normalString.endIndex), in: normalString, is: "{1, 17}")
_assertNSRangeInit(normalString.startIndex..., in: normalString, is: "{0, 18}")
_assertNSRangeInit(...normalString.firstIndex(of: " ")!, in: normalString, is: "{0, 7}")
_assertNSRangeInit(..<normalString.lastIndex(of: " ")!, in: normalString, is: "{0, 12}")
let normalSubstring: Substring = normalString.split(separator: ";")[1]
_assertNSRangeInit(normalSubstring.range(of: "TABLE")!, in: normalSubstring, is: "{5, 5}")
_assertNSRangeInit(normalSubstring.index(after: normalSubstring.firstIndex(of: " ")!)..<normalSubstring.lastIndex(of: " ")!, in: normalString, is: "{7, 5}")
_assertNSRangeInit(normalSubstring.firstIndex(of: "u")!...normalSubstring.lastIndex(of: "u")!, in: normalSubstring, is: "{11, 1}")
_assertNSRangeInit(normalSubstring.startIndex..., in: normalSubstring, is: "{0, 16}")
_assertNSRangeInit(normalSubstring.startIndex..., in: normalString, is: "{2, 16}")
_assertNSRangeInit(...normalSubstring.lastIndex(of: " ")!, in: normalSubstring, is: "{0, 11}")
_assertNSRangeInit(..<normalSubstring.lastIndex(of: " ")!, in: normalString, is: "{0, 12}")
}
func test_init_region_in_unicode_string() {
// count: 46, utf8: 90, utf16: 54
let unicodeString = "This is a #naughty👻 string (╯°□°)╯︵ ┻━┻👨👩👧👦)"
_assertNSRangeInit(unicodeString.index(unicodeString.startIndex, offsetBy: 10)..<unicodeString.index(unicodeString.startIndex, offsetBy: 28), in: unicodeString, is: "{10, 19}")
_assertNSRangeInit(unicodeString.index(after: unicodeString.startIndex)...unicodeString.index(before: unicodeString.endIndex), in: unicodeString, is: "{1, 53}")
_assertNSRangeInit(unicodeString.startIndex..., in: unicodeString, is: "{0, 54}")
_assertNSRangeInit(...unicodeString.firstIndex(of: "👻")!, in: unicodeString, is: "{0, 22}")
_assertNSRangeInit(..<unicodeString.range(of: "👨👩👧👦")!.lowerBound, in: unicodeString, is: "{0, 42}")
let unicodeSubstring: Substring = unicodeString[unicodeString.firstIndex(of: "👻")!...]
_assertNSRangeInit(unicodeSubstring.range(of: "👨👩👧👦")!, in: unicodeSubstring, is: "{22, 11}")
_assertNSRangeInit(unicodeSubstring.range(of: "👨")!.lowerBound..<unicodeSubstring.range(of: "👦")!.upperBound, in: unicodeString, is: "{42, 11}")
_assertNSRangeInit(unicodeSubstring.index(after: unicodeSubstring.startIndex)...unicodeSubstring.index(before: unicodeSubstring.endIndex), in: unicodeSubstring, is: "{2, 32}")
_assertNSRangeInit(unicodeSubstring.startIndex..., in: unicodeSubstring, is: "{0, 34}")
_assertNSRangeInit(unicodeSubstring.startIndex..., in: unicodeString, is: "{20, 34}")
_assertNSRangeInit(...unicodeSubstring.firstIndex(of: "╯")!, in: unicodeSubstring, is: "{0, 12}")
_assertNSRangeInit(..<unicodeSubstring.firstIndex(of: "╯")!, in: unicodeString, is: "{0, 31}")
}
func test_hashing() {
let large = Int.max >> 2
let samples: [NSRange] = [
NSRange(location: 1, length: 1),
NSRange(location: 1, length: 2),
NSRange(location: 2, length: 1),
NSRange(location: 2, length: 2),
NSRange(location: large, length: large),
NSRange(location: 0, length: large),
NSRange(location: large, length: 0),
]
checkHashable(samples, equalityOracle: { $0 == $1 })
}
}