Skip to content

Commit 2eeb88c

Browse files
committed
thorough testing code from Swift lib to match
1 parent c519e4b commit 2eeb88c

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

TestFoundation/TestNSString.swift

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class TestNSString : XCTestCase {
8787
("test_ExternalRepresentation", test_ExternalRepresentation),
8888
("test_mutableStringConstructor", test_mutableStringConstructor),
8989
("test_PrefixSuffix", test_PrefixSuffix),
90-
("test_utf16StringRangeCount", test_utf16StringRangeCount),
90+
("test_utf16StringRangeCount", test_StringUTF16ViewIndexStrideableRange),
9191
("test_reflection", { _ in test_reflection }),
9292
]
9393
}
@@ -939,19 +939,31 @@ class TestNSString : XCTestCase {
939939
}
940940

941941
//[SR-1988] Ranges of String.UTF16View.Index have negative count
942-
func test_utf16StringRangeCount(){
943-
let str = "How many elements do I contain?".utf16
944-
let indices: Range = str.startIndex ..< str.endIndex
945-
XCTAssertEqual(indices.count , 31, "Range should be 31")
946-
947-
let blank = "".utf16
948-
let blankindices: Range = blank.startIndex ..< blank.endIndex
949-
XCTAssertTrue(blankindices.count == 0, "Range should be 0")
950-
951-
let one = "1".utf16
952-
let oneindices: Range = one.startIndex ..< one.endIndex
953-
XCTAssertTrue(oneindices.count == 1, "Range should be 1")
942+
func test_StringUTF16ViewIndexStrideableRange(){
943+
let testStrings = ["", "\u{0000}", "a", "aa", "ab", "\u{007f}", "\u{0430}", "\u{0430}\u{0431}\u{0432}","\u{1f425}"]
954944

945+
func checkStrideable<S : Strideable>(
946+
instances: [S],
947+
distances: [S.Stride],
948+
distanceOracle: (Int, Int) -> S.Stride
949+
) {
950+
for i in instances.indices {
951+
let first = instances[i]
952+
for j in instances.indices {
953+
let second = instances[j]
954+
XCTAssertTrue(distanceOracle(i, j) == first.distance(to: second))
955+
XCTAssertTrue(second == first.advanced(by: distanceOracle(i, j)))
956+
}
957+
}
958+
}
959+
testStrings.forEach{
960+
let utf16 = $0.utf16
961+
var indicies = Array(utf16.indices)
962+
indicies.append(utf16.indices.endIndex)
963+
checkStrideable(instances: indicies,
964+
distances: Array(0..<utf16.count),
965+
distanceOracle: {$1 - $0})
966+
}
955967
}
956968

957969
func test_mutableStringConstructor() {

0 commit comments

Comments
 (0)