Skip to content

[swift-3.0-branch] stdlib: fix incorrect distance measurement between UTF-16 indices #4081

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 8, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/private/StdlibUnittest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ add_swift_library(swiftStdlibUnittest ${SWIFT_STDLIB_LIBRARY_BUILD_TYPES} IS_STD
# filename.
StdlibUnittest.swift.gyb

CheckStrideable.swift.gyb
InspectValue.cpp
InspectValue.swift
InterceptTraps.cpp
Expand Down
32 changes: 32 additions & 0 deletions stdlib/private/StdlibUnittest/CheckStrideable.swift.gyb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

%{
from gyb_stdlib_unittest_support import TRACE, stackTrace, trace
}%

public func checkStrideable<S : Strideable>(
instances: [S],
distances: [S.Stride],
distanceOracle: (Int, Int) -> S.Stride,
${TRACE}
) {
for i in instances.indices {
let first = instances[i]
for j in instances.indices {
let second = instances[j]
expectEqual(distanceOracle(i, j), first.distance(to: second))
expectEqual(second, first.advanced(by: distanceOracle(i, j)))
}
}
}

2 changes: 1 addition & 1 deletion stdlib/public/SDK/Foundation/ExtraStringAPIs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ extension String.UTF16View.Index : Strideable {
}

public func distance(to other: String.UTF16View.Index) -> Int {
return other._offset.distance(to: _offset)
return _offset.distance(to: other._offset)
}

public func advanced(by n: Int) -> String.UTF16View.Index {
Expand Down
119 changes: 119 additions & 0 deletions validation-test/stdlib/StringViews.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import Swift
import StdlibUnittest
import StdlibCollectionUnittest

#if _runtime(_ObjC)
// FIXME: Foundation leaks through StdlibUnittest. It adds some conformances
Expand Down Expand Up @@ -740,5 +741,123 @@ tests.test("UnicodeScalars->String") {
}
}

struct StringViewTest {
var string: String
var utf8: [UInt8]
var utf16: [UInt16]
var unicodeScalars: [UnicodeScalar]

init(string: String, utf8: [UInt8], utf16: [UInt16], utf32: [UInt32]) {
self.string = string
self.utf8 = utf8
self.utf16 = utf16
self.unicodeScalars = utf32.map { UnicodeScalar($0)! }
}
}

var stringViewTests: [StringViewTest] = [
StringViewTest(
string: "",
utf8: [],
utf16: [],
utf32: []),
StringViewTest(
string: "\u{0000}",
utf8: [0x00],
utf16: [0x00],
utf32: [0x00]),
StringViewTest(
string: "a",
utf8: [0x61],
utf16: [0x61],
utf32: [0x61]),
StringViewTest(
string: "aa",
utf8: [0x61, 0x61],
utf16: [0x61, 0x61],
utf32: [0x61, 0x61]),
StringViewTest(
string: "ab",
utf8: [0x61, 0x62],
utf16: [0x61, 0x62],
utf32: [0x61, 0x62]),
StringViewTest(
string: "abc",
utf8: [0x61, 0x62, 0x63],
utf16: [0x61, 0x62, 0x63],
utf32: [0x61, 0x62, 0x63]),
StringViewTest(
string: "\u{007f}",
utf8: [0x7f],
utf16: [0x7f],
utf32: [0x7f]),
StringViewTest(
string: "\u{0430}",
utf8: [0xd0, 0xb0],
utf16: [0x0430],
utf32: [0x0430]),
StringViewTest(
string: "\u{0430}\u{0431}\u{0432}",
utf8: [0xd0, 0xb0, 0xd0, 0xb1, 0xd0, 0xb2],
utf16: [0x0430, 0x0431, 0x0432],
utf32: [0x0430, 0x0431, 0x0432]),
StringViewTest(
string: "\u{1f425}",
utf8: [0xf0, 0x9f, 0x90, 0xa5],
utf16: [0xd83d, 0xdc25],
utf32: [0x1f425]),
]

#if _runtime(_ObjC)
tests.test("String.UTF16View.Index/Strideable")
.forEach(in: stringViewTests) {
test in

func allIndices<C : Collection>(of c: C) -> [C.Index]
where C.Indices.Iterator.Element == C.Index
{
var result = Array(c.indices)
result.append(c.endIndex)
return result
}

checkStrideable(
instances: allIndices(of: test.string.utf16),
distances: Array(0..<test.string.utf16.count),
distanceOracle: { $1 - $0 })
}
#endif

tests.test("String.UTF8View/Collection")
.forEach(in: stringViewTests) {
test in

// FIXME(ABI): should be `checkBidirectionalCollection`.
checkForwardCollection(test.utf8, test.string.utf8) { $0 == $1 }
}

#if _runtime(_Native)
tests.test("String.UTF16View/BidirectionalCollection")
.forEach(in: stringViewTests) {
test in

checkBidirectionalCollection(test.utf16, test.string.utf16) { $0 == $1 }
}
#else
tests.test("String.UTF16View/RandomAccessCollection")
.forEach(in: stringViewTests) {
test in

checkRandomAccessCollection(test.utf16, test.string.utf16) { $0 == $1 }
}
#endif

tests.test("String.UTF32View/BidirectionalCollection")
.forEach(in: stringViewTests) {
test in

checkBidirectionalCollection(
test.unicodeScalars, test.string.unicodeScalars) { $0 == $1 }
}

runAllTests()