mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
New indexing model: WIP fixed compile issues in various stdlib tests
This commit is contained in:
@@ -12,7 +12,7 @@
|
||||
|
||||
// TODO: swift-3-indexing-model - this conformed to ForwardIndex but not sure why it did.
|
||||
|
||||
public final class LifetimeTracked : Equatable, CustomStringConvertible {
|
||||
public final class LifetimeTracked {
|
||||
public init(_ value: Int, identity: Int = 0) {
|
||||
LifetimeTracked.instances += 1
|
||||
LifetimeTracked._nextSerialNumber += 1
|
||||
@@ -27,11 +27,6 @@ public final class LifetimeTracked : Equatable, CustomStringConvertible {
|
||||
serialNumber = -serialNumber
|
||||
}
|
||||
|
||||
public var description: String {
|
||||
assert(serialNumber > 0, "dead Tracked!")
|
||||
return value.description
|
||||
}
|
||||
|
||||
public static var instances: Int = 0
|
||||
internal static var _nextSerialNumber = 0
|
||||
|
||||
@@ -40,6 +35,30 @@ public final class LifetimeTracked : Equatable, CustomStringConvertible {
|
||||
public var serialNumber: Int = 0
|
||||
}
|
||||
|
||||
|
||||
extension LifetimeTracked : Strideable {
|
||||
@warn_unused_result
|
||||
public func distance(to other: LifetimeTracked) -> Int {
|
||||
return self.value.distance(to: other.value)
|
||||
}
|
||||
|
||||
@warn_unused_result
|
||||
public func advanced(by n: Int) -> LifetimeTracked {
|
||||
return LifetimeTracked(self.value.advanced(by: n))
|
||||
}
|
||||
}
|
||||
|
||||
extension LifetimeTracked : CustomStringConvertible {
|
||||
public var description: String {
|
||||
assert(serialNumber > 0, "dead Tracked!")
|
||||
return value.description
|
||||
}
|
||||
}
|
||||
|
||||
public func == (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
|
||||
return x.value == y.value
|
||||
}
|
||||
|
||||
public func < (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
|
||||
return x.value < y.value
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
public struct RangeOfStrideable<
|
||||
Element : Strideable
|
||||
> : Equatable, Collection,
|
||||
> : Equatable, RandomAccessCollection,
|
||||
CustomStringConvertible, CustomDebugStringConvertible {
|
||||
|
||||
/// Construct a copy of `x`.
|
||||
|
||||
@@ -64,7 +64,7 @@ class Tracked : NSObject, Fooable {
|
||||
}
|
||||
|
||||
func successor() -> Self {
|
||||
return self.dynamicType.init(self.value.successor())
|
||||
return self.dynamicType.init(self.value + 1)
|
||||
}
|
||||
|
||||
var value: Int
|
||||
@@ -142,7 +142,7 @@ struct BridgedSwift : CustomStringConvertible, _ObjectiveCBridgeable {
|
||||
}
|
||||
|
||||
func successor() -> BridgedSwift {
|
||||
return BridgedSwift(trak.value.successor())
|
||||
return BridgedSwift(trak.value + 1)
|
||||
}
|
||||
|
||||
static func printStats() {
|
||||
|
||||
@@ -13,48 +13,11 @@
|
||||
// REQUIRES: executable_test
|
||||
|
||||
import Swift
|
||||
|
||||
//===--- class Tracked ----------------------------------------------------===//
|
||||
// Instead of testing with Int elements, we use this wrapper class
|
||||
// that can help us track allocations and find issues with object
|
||||
// lifetime inside Array implementations.
|
||||
var trackedCount = 0
|
||||
var nextTrackedSerialNumber = 0
|
||||
|
||||
final class Tracked : ForwardIndex, CustomStringConvertible {
|
||||
required init(_ value: Int) {
|
||||
trackedCount += 1
|
||||
nextTrackedSerialNumber += 1
|
||||
serialNumber = nextTrackedSerialNumber
|
||||
self.value = value
|
||||
}
|
||||
|
||||
deinit {
|
||||
assert(serialNumber > 0, "double destruction!")
|
||||
trackedCount -= 1
|
||||
serialNumber = -serialNumber
|
||||
}
|
||||
|
||||
var description: String {
|
||||
assert(serialNumber > 0, "dead Tracked!")
|
||||
return value.description
|
||||
}
|
||||
|
||||
func successor() -> Self {
|
||||
return self.dynamicType.init(self.value.successor())
|
||||
}
|
||||
|
||||
var value: Int
|
||||
var serialNumber: Int
|
||||
}
|
||||
|
||||
func == (x: Tracked, y: Tracked) -> Bool {
|
||||
return x.value == y.value
|
||||
}
|
||||
import StdlibUnittest
|
||||
|
||||
//===--- struct MrMcRange -------------------------------------------------===//
|
||||
// A wrapper around Range<Tracked> that allows us to detect when it is
|
||||
// being treated as a Collection rather than merely a Sequence, which
|
||||
// A wrapper around Range<LifetimeTracked> that allows us to detect when
|
||||
// it is being treated as a Collection rather than merely a Sequence, which
|
||||
// helps us to prove that an optimization is being used. In
|
||||
// particular, when constructing a _ContiguousArrayBuffer from a
|
||||
// Collection, the necessary storage should be pre-allocated.
|
||||
@@ -74,8 +37,8 @@ struct MrMcRange : Collection {
|
||||
return base.endIndex
|
||||
}
|
||||
|
||||
subscript(i: Int) -> Tracked {
|
||||
return Tracked(i)
|
||||
subscript(i: Int) -> LifetimeTracked {
|
||||
return LifetimeTracked(i)
|
||||
}
|
||||
|
||||
var base: Base
|
||||
@@ -92,15 +55,15 @@ func printSequence<T : Sequence>(x: T) {
|
||||
print(">")
|
||||
}
|
||||
|
||||
|
||||
|
||||
// CHECK: testing...
|
||||
print("testing...")
|
||||
|
||||
func test() {
|
||||
//===--- Sequences can be converted -------------------------------------===//
|
||||
|
||||
let n0 = ((Tracked(10)..<Tracked(27)).makeIterator())._copyToNativeArrayBuffer()
|
||||
let n0 = (
|
||||
LifetimeTracked(10)..<LifetimeTracked(27)
|
||||
).makeIterator()._copyToNativeArrayBuffer()
|
||||
// CHECK-NEXT: <10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26>
|
||||
printSequence(n0)
|
||||
|
||||
@@ -114,7 +77,7 @@ func test() {
|
||||
test()
|
||||
|
||||
// CHECK-NEXT: trackedCount = 0
|
||||
print("trackedCount = \(trackedCount)")
|
||||
print("trackedCount = \(LifetimeTracked.instances)")
|
||||
|
||||
// CHECK-NEXT: done.
|
||||
print("done.")
|
||||
|
||||
@@ -25,44 +25,7 @@
|
||||
import Swift
|
||||
import SwiftShims
|
||||
import ObjectiveC
|
||||
|
||||
//===--- class Tracked ----------------------------------------------------===//
|
||||
// Instead of testing with Int elements, we use this wrapper class
|
||||
// that can help us track allocations and find issues with object
|
||||
// lifetime inside Array implementations.
|
||||
var trackedCount = 0
|
||||
var nextTrackedSerialNumber = 0
|
||||
|
||||
final class Tracked : ForwardIndex, CustomStringConvertible {
|
||||
required init(_ value: Int) {
|
||||
trackedCount += 1
|
||||
nextTrackedSerialNumber += 1
|
||||
serialNumber = nextTrackedSerialNumber
|
||||
self.value = value
|
||||
}
|
||||
|
||||
deinit {
|
||||
assert(serialNumber > 0, "double destruction!")
|
||||
trackedCount -= 1
|
||||
serialNumber = -serialNumber
|
||||
}
|
||||
|
||||
var description: String {
|
||||
assert(serialNumber > 0, "dead Tracked!")
|
||||
return value.description
|
||||
}
|
||||
|
||||
func successor() -> Self {
|
||||
return self.dynamicType.init(self.value.successor())
|
||||
}
|
||||
|
||||
var value: Int
|
||||
var serialNumber: Int
|
||||
}
|
||||
|
||||
func == (x: Tracked, y: Tracked) -> Bool {
|
||||
return x.value == y.value
|
||||
}
|
||||
import StdlibUnittest
|
||||
|
||||
struct X : _ObjectiveCBridgeable {
|
||||
static func _isBridgedToObjectiveC() -> Bool {
|
||||
@@ -74,22 +37,22 @@ struct X : _ObjectiveCBridgeable {
|
||||
}
|
||||
|
||||
static func _getObjectiveCType() -> Any.Type {
|
||||
return Tracked.self
|
||||
return LifetimeTracked.self
|
||||
}
|
||||
|
||||
func _bridgeToObjectiveC() -> Tracked {
|
||||
return Tracked(value)
|
||||
func _bridgeToObjectiveC() -> LifetimeTracked {
|
||||
return LifetimeTracked(value)
|
||||
}
|
||||
|
||||
static func _forceBridgeFromObjectiveC(
|
||||
x: Tracked,
|
||||
x: LifetimeTracked,
|
||||
result: inout X?
|
||||
) {
|
||||
result = X(x.value)
|
||||
}
|
||||
|
||||
static func _conditionallyBridgeFromObjectiveC(
|
||||
x: Tracked,
|
||||
x: LifetimeTracked,
|
||||
result: inout X?
|
||||
) -> Bool {
|
||||
result = X(x.value)
|
||||
@@ -108,16 +71,16 @@ func testScope() {
|
||||
|
||||
// construction of these tracked objects is lazy
|
||||
// CHECK-NEXT: trackedCount = 0 .
|
||||
print("trackedCount = \(trackedCount) .")
|
||||
print("trackedCount = \(LifetimeTracked.instances) .")
|
||||
|
||||
// We can get a single element out
|
||||
// CHECK-NEXT: nsx[0]: 1 .
|
||||
let one = nsx.objectAt(0) as! Tracked
|
||||
let one = nsx.objectAt(0) as! LifetimeTracked
|
||||
print("nsx[0]: \(one.value) .")
|
||||
|
||||
// We can get the element again, but it may not have the same identity
|
||||
// CHECK-NEXT: object identity matches?
|
||||
let anotherOne = nsx.objectAt(0) as! Tracked
|
||||
let anotherOne = nsx.objectAt(0) as! LifetimeTracked
|
||||
print("object identity matches? \(one === anotherOne)")
|
||||
|
||||
// Because the elements come back at +0, we really don't want to
|
||||
@@ -143,5 +106,5 @@ autoreleasepool() {
|
||||
}
|
||||
|
||||
// CHECK-NEXT: leaks = 0 .
|
||||
print("leaks = \(trackedCount) .")
|
||||
print("leaks = \(LifetimeTracked.instances) .")
|
||||
|
||||
|
||||
@@ -61,6 +61,7 @@ struct X : Collection {
|
||||
return msg.endIndex
|
||||
}
|
||||
subscript(i: Index) -> Element { return msg[i] }
|
||||
func next(i: Index) -> Index { return msg.next(i) }
|
||||
}
|
||||
|
||||
var foobar = X("foobar")
|
||||
@@ -152,25 +153,24 @@ print(isPalindrome1_5(X("FleetoMeRemoteelF")))
|
||||
print(isPalindrome1_5(X("FleetoMeReMoteelF")))
|
||||
|
||||
// Finally, one that actually uses indexing to do half as much work.
|
||||
// BidirectionalIndex traversal finally pays off!
|
||||
// BidirectionalCollection traversal finally pays off!
|
||||
func isPalindrome2<
|
||||
S: Collection
|
||||
S: BidirectionalCollection
|
||||
where
|
||||
S.Index : BidirectionalIndex,
|
||||
S.Iterator.Element: Equatable
|
||||
>(seq: S) -> Bool {
|
||||
|
||||
var b = seq.startIndex, e = seq.endIndex
|
||||
|
||||
while (b != e) {
|
||||
e = e.predecessor()
|
||||
e = seq.previous(e)
|
||||
if (b == e) {
|
||||
break
|
||||
}
|
||||
if seq[b] != seq[e] {
|
||||
return false
|
||||
}
|
||||
b = b.successor()
|
||||
b = seq.next(b)
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ extension LazyFilterSequence where Base : TestProtocol1 {
|
||||
}
|
||||
}
|
||||
|
||||
extension LazyFilterIndex where BaseElements : TestProtocol1 {
|
||||
extension LazyFilterIndex where Base : TestProtocol1 {
|
||||
var _baseIsTestProtocol1: Bool {
|
||||
fatalError("not implemented")
|
||||
}
|
||||
|
||||
@@ -62,15 +62,15 @@ func find(substring: String, within domain: String) -> String.Index? {
|
||||
|
||||
if (domainCount < substringCount) { return nil }
|
||||
var sliceStart = domain.startIndex
|
||||
var sliceEnd = domain.startIndex.advanced(by: substringCount)
|
||||
var sliceEnd = domain.advance(sliceStart, by: substringCount)
|
||||
var i = 0
|
||||
while true {
|
||||
if domain[sliceStart..<sliceEnd] == substring {
|
||||
return sliceStart
|
||||
}
|
||||
if i == domainCount - substringCount { break }
|
||||
sliceStart = sliceStart.successor()
|
||||
sliceEnd = sliceEnd.successor()
|
||||
sliceStart = domain.next(sliceStart)
|
||||
sliceEnd = domain.next(sliceEnd)
|
||||
i += 1
|
||||
}
|
||||
return nil
|
||||
|
||||
@@ -358,13 +358,13 @@ NSStringAPIs.test("compare(_:options:range:locale:)") {
|
||||
|
||||
do {
|
||||
let s = "abcd"
|
||||
let r = s.startIndex.successor()..<s.endIndex
|
||||
let r = s.next(s.startIndex)..<s.endIndex
|
||||
expectEqual(NSComparisonResult.orderedSame,
|
||||
s.compare("bcd", range: r))
|
||||
}
|
||||
do {
|
||||
let s = "абвг"
|
||||
let r = s.startIndex.successor()..<s.endIndex
|
||||
let r = s.next(s.startIndex)..<s.endIndex
|
||||
expectEqual(NSComparisonResult.orderedSame,
|
||||
s.compare("бвг", range: r))
|
||||
}
|
||||
@@ -533,8 +533,8 @@ NSStringAPIs.test("enumerateLines(_:)") {
|
||||
|
||||
NSStringAPIs.test("enumerateLinguisticTagsIn(_:scheme:options:orthography:_:") {
|
||||
let s = "Абв. Глокая куздра штеко будланула бокра и кудрячит бокрёнка. Абв."
|
||||
let startIndex = s.startIndex.advanced(by: 5)
|
||||
let endIndex = s.startIndex.advanced(by: 62)
|
||||
let startIndex = s.advance(s.startIndex, by: 5)
|
||||
let endIndex = s.advance(s.startIndex, by: 62)
|
||||
var tags: [String] = []
|
||||
var tokens: [String] = []
|
||||
var sentences: [String] = []
|
||||
@@ -562,8 +562,8 @@ NSStringAPIs.test("enumerateLinguisticTagsIn(_:scheme:options:orthography:_:") {
|
||||
|
||||
NSStringAPIs.test("enumerateSubstringsIn(_:options:_:)") {
|
||||
let s = "え\u{304b}\u{3099}お\u{263a}\u{fe0f}😀😊"
|
||||
let startIndex = s.startIndex.advanced(by: 1)
|
||||
let endIndex = s.startIndex.advanced(by: 5)
|
||||
let startIndex = s.advance(s.startIndex, by: 1)
|
||||
let endIndex = s.advance(s.startIndex, by: 5)
|
||||
do {
|
||||
var substrings: [String] = []
|
||||
s.enumerateSubstrings(in: startIndex..<endIndex,
|
||||
@@ -600,8 +600,8 @@ NSStringAPIs.test("fastestEncoding") {
|
||||
|
||||
NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remaining:)") {
|
||||
let s = "abc абв def где gh жз zzz"
|
||||
let startIndex = s.startIndex.advanced(by: 8)
|
||||
let endIndex = s.startIndex.advanced(by: 22)
|
||||
let startIndex = s.advance(s.startIndex, by: 8)
|
||||
let endIndex = s.advance(s.startIndex, by: 22)
|
||||
do {
|
||||
// 'maxLength' is limiting.
|
||||
let bufferLength = 100
|
||||
@@ -619,7 +619,7 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
|
||||
expectTrue(result)
|
||||
expectEqualSequence(expectedStr, buffer)
|
||||
expectEqual(11, usedLength)
|
||||
expectEqual(remainingRange.startIndex, startIndex.advanced(by: 8))
|
||||
expectEqual(remainingRange.startIndex, s.advance(startIndex, by: 8))
|
||||
expectEqual(remainingRange.endIndex, endIndex)
|
||||
}
|
||||
do {
|
||||
@@ -640,7 +640,7 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
|
||||
expectTrue(result)
|
||||
expectEqualSequence(expectedStr, buffer)
|
||||
expectEqual(4, usedLength)
|
||||
expectEqual(remainingRange.startIndex, startIndex.advanced(by: 4))
|
||||
expectEqual(remainingRange.startIndex, s.advance(startIndex, by: 4))
|
||||
expectEqual(remainingRange.endIndex, endIndex)
|
||||
}
|
||||
do {
|
||||
@@ -680,7 +680,7 @@ NSStringAPIs.test("getBytes(_:maxLength:usedLength:encoding:options:range:remain
|
||||
expectTrue(result)
|
||||
expectEqualSequence(expectedStr, buffer)
|
||||
expectEqual(4, usedLength)
|
||||
expectEqual(remainingRange.startIndex, startIndex.advanced(by: 4))
|
||||
expectEqual(remainingRange.startIndex, s.advance(startIndex, by: 4))
|
||||
expectEqual(remainingRange.endIndex, endIndex)
|
||||
}
|
||||
}
|
||||
@@ -733,7 +733,7 @@ NSStringAPIs.test("getCString(_:maxLength:encoding:)") {
|
||||
|
||||
NSStringAPIs.test("getLineStart(_:end:contentsEnd:forRange:)") {
|
||||
let s = "Глокая куздра\nштеко будланула\nбокра и кудрячит\nбокрёнка."
|
||||
let r = s.startIndex.advanced(by: 16)..<s.startIndex.advanced(by: 35)
|
||||
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
|
||||
do {
|
||||
var outStartIndex = s.startIndex
|
||||
var outLineEndIndex = s.startIndex
|
||||
@@ -749,7 +749,7 @@ NSStringAPIs.test("getLineStart(_:end:contentsEnd:forRange:)") {
|
||||
|
||||
NSStringAPIs.test("getParagraphStart(_:end:contentsEnd:forRange:)") {
|
||||
let s = "Глокая куздра\nштеко будланула\u{2028}бокра и кудрячит\u{2028}бокрёнка.\n Абв."
|
||||
let r = s.startIndex.advanced(by: 16)..<s.startIndex.advanced(by: 35)
|
||||
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
|
||||
do {
|
||||
var outStartIndex = s.startIndex
|
||||
var outEndIndex = s.startIndex
|
||||
@@ -878,7 +878,7 @@ NSStringAPIs.test("lengthOfBytesUsingEncoding(_:)") {
|
||||
|
||||
NSStringAPIs.test("lineRangeFor(_:)") {
|
||||
let s = "Глокая куздра\nштеко будланула\nбокра и кудрячит\nбокрёнка."
|
||||
let r = s.startIndex.advanced(by: 16)..<s.startIndex.advanced(by: 35)
|
||||
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
|
||||
do {
|
||||
let result = s.lineRange(for: r)
|
||||
expectEqual("штеко будланула\nбокра и кудрячит\n", s[result])
|
||||
@@ -887,8 +887,8 @@ NSStringAPIs.test("lineRangeFor(_:)") {
|
||||
|
||||
NSStringAPIs.test("linguisticTagsIn(_:scheme:options:orthography:tokenRanges:)") {
|
||||
let s = "Абв. Глокая куздра штеко будланула бокра и кудрячит бокрёнка. Абв."
|
||||
let startIndex = s.startIndex.advanced(by: 5)
|
||||
let endIndex = s.startIndex.advanced(by: 17)
|
||||
let startIndex = s.advance(s.startIndex, by: 5)
|
||||
let endIndex = s.advance(s.startIndex, by: 17)
|
||||
var tokenRanges: [Range<String.Index>] = []
|
||||
var tags = s.linguisticTags(in: startIndex..<endIndex,
|
||||
scheme: NSLinguisticTagSchemeTokenType,
|
||||
@@ -1044,7 +1044,7 @@ NSStringAPIs.test("maximumLengthOfBytesUsingEncoding(_:)") {
|
||||
|
||||
NSStringAPIs.test("paragraphRangeFor(_:)") {
|
||||
let s = "Глокая куздра\nштеко будланула\u{2028}бокра и кудрячит\u{2028}бокрёнка.\n Абв."
|
||||
let r = s.startIndex.advanced(by: 16)..<s.startIndex.advanced(by: 35)
|
||||
let r = s.advance(s.startIndex, by: 16)..<s.advance(s.startIndex, by: 35)
|
||||
do {
|
||||
let result = s.paragraphRange(for: r)
|
||||
expectEqual("штеко будланула\u{2028}бокра и кудрячит\u{2028}бокрёнка.\n", s[result])
|
||||
@@ -1100,8 +1100,8 @@ NSStringAPIs.test("rangeOfCharacterFrom(_:options:range:)") {
|
||||
do {
|
||||
let s = "Глокая куздра"
|
||||
let r = s.rangeOfCharacter(from: charset)!
|
||||
expectEqual(s.startIndex.advanced(by: 4), r.startIndex)
|
||||
expectEqual(s.startIndex.advanced(by: 5), r.endIndex)
|
||||
expectEqual(s.advance(s.startIndex, by: 4), r.startIndex)
|
||||
expectEqual(s.advance(s.startIndex, by: 5), r.endIndex)
|
||||
}
|
||||
do {
|
||||
expectEmpty("клмн".rangeOfCharacter(from: charset))
|
||||
@@ -1110,15 +1110,15 @@ NSStringAPIs.test("rangeOfCharacterFrom(_:options:range:)") {
|
||||
let s = "абвклмнабвклмн"
|
||||
let r = s.rangeOfCharacter(from: charset,
|
||||
options: .backwardsSearch)!
|
||||
expectEqual(s.startIndex.advanced(by: 9), r.startIndex)
|
||||
expectEqual(s.startIndex.advanced(by: 10), r.endIndex)
|
||||
expectEqual(s.advance(s.startIndex, by: 9), r.startIndex)
|
||||
expectEqual(s.advance(s.startIndex, by: 10), r.endIndex)
|
||||
}
|
||||
do {
|
||||
let s = "абвклмнабв"
|
||||
let r = s.rangeOfCharacter(from: charset,
|
||||
range: s.startIndex.advanced(by: 3)..<s.endIndex)!
|
||||
expectEqual(s.startIndex.advanced(by: 7), r.startIndex)
|
||||
expectEqual(s.startIndex.advanced(by: 8), r.endIndex)
|
||||
range: s.advance(s.startIndex, by: 3)..<s.endIndex)!
|
||||
expectEqual(s.advance(s.startIndex, by: 7), r.startIndex)
|
||||
expectEqual(s.advance(s.startIndex, by: 8), r.endIndex)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1149,20 +1149,20 @@ NSStringAPIs.test("rangeOfComposedCharacterSequence(at:)") {
|
||||
expectEqual("\u{1F601}", s[s.rangeOfComposedCharacterSequence(
|
||||
at: s.startIndex)])
|
||||
expectEqual("a", s[s.rangeOfComposedCharacterSequence(
|
||||
at: s.startIndex.advanced(by: 1))])
|
||||
at: s.advance(s.startIndex, by: 1))])
|
||||
expectEqual("\u{305f}\u{3099}", s[s.rangeOfComposedCharacterSequence(
|
||||
at: s.startIndex.advanced(by: 5))])
|
||||
at: s.advance(s.startIndex, by: 5))])
|
||||
expectEqual(" ", s[s.rangeOfComposedCharacterSequence(
|
||||
at: s.startIndex.advanced(by: 6))])
|
||||
at: s.advance(s.startIndex, by: 6))])
|
||||
}
|
||||
|
||||
NSStringAPIs.test("rangeOfComposedCharacterSequences(for:)") {
|
||||
let s = "\u{1F601}abc さ\u{3099}し\u{3099}す\u{3099}せ\u{3099}そ\u{3099}"
|
||||
|
||||
expectEqual("\u{1F601}a", s[s.rangeOfComposedCharacterSequences(
|
||||
for: s.startIndex..<s.startIndex.advanced(by: 2))])
|
||||
for: s.startIndex..<s.advance(s.startIndex, by: 2))])
|
||||
expectEqual("せ\u{3099}そ\u{3099}", s[s.rangeOfComposedCharacterSequences(
|
||||
for: s.startIndex.advanced(by: 8)..<s.startIndex.advanced(by: 10))])
|
||||
for: s.advance(s.startIndex, by: 8)..<s.advance(s.startIndex, by: 10))])
|
||||
}
|
||||
|
||||
func toIntRange(
|
||||
@@ -1171,8 +1171,8 @@ func toIntRange(
|
||||
guard let range = maybeRange else { return nil }
|
||||
|
||||
return
|
||||
string.startIndex.distance(to: range.startIndex) ..<
|
||||
string.startIndex.distance(to: range.endIndex)
|
||||
string.distance(from: string.startIndex, to: range.startIndex) ..<
|
||||
string.distance(from: string.startIndex, to: range.endIndex)
|
||||
}
|
||||
|
||||
NSStringAPIs.test("range(of:options:range:locale:)") {
|
||||
@@ -1478,35 +1478,35 @@ NSStringAPIs.test("replacingCharacters(in:with:)") {
|
||||
expectEqual(
|
||||
"す\u{3099}せ\u{3099}そ\u{3099}",
|
||||
s.replacingCharacters(
|
||||
in: s.startIndex..<s.startIndex.advanced(by: 7), with: ""))
|
||||
in: s.startIndex..<s.advance(s.startIndex, by: 7), with: ""))
|
||||
expectEqual(
|
||||
"zzzす\u{3099}せ\u{3099}そ\u{3099}",
|
||||
s.replacingCharacters(
|
||||
in: s.startIndex..<s.startIndex.advanced(by: 7), with: "zzz"))
|
||||
in: s.startIndex..<s.advance(s.startIndex, by: 7), with: "zzz"))
|
||||
expectEqual(
|
||||
"\u{1F602}す\u{3099}せ\u{3099}そ\u{3099}",
|
||||
s.replacingCharacters(
|
||||
in: s.startIndex..<s.startIndex.advanced(by: 7), with: "\u{1F602}"))
|
||||
in: s.startIndex..<s.advance(s.startIndex, by: 7), with: "\u{1F602}"))
|
||||
|
||||
expectEqual("\u{1F601}", s.replacingCharacters(
|
||||
in: s.startIndex.successor()..<s.endIndex, with: ""))
|
||||
in: s.next(s.startIndex)..<s.endIndex, with: ""))
|
||||
expectEqual("\u{1F601}zzz", s.replacingCharacters(
|
||||
in: s.startIndex.successor()..<s.endIndex, with: "zzz"))
|
||||
in: s.next(s.startIndex)..<s.endIndex, with: "zzz"))
|
||||
expectEqual("\u{1F601}\u{1F602}", s.replacingCharacters(
|
||||
in: s.startIndex.successor()..<s.endIndex, with: "\u{1F602}"))
|
||||
in: s.next(s.startIndex)..<s.endIndex, with: "\u{1F602}"))
|
||||
|
||||
expectEqual(
|
||||
"\u{1F601}aす\u{3099}せ\u{3099}そ\u{3099}",
|
||||
s.replacingCharacters(
|
||||
in: s.startIndex.advanced(by: 2)..<s.startIndex.advanced(by: 7), with: ""))
|
||||
in: s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 7), with: ""))
|
||||
expectEqual(
|
||||
"\u{1F601}azzzす\u{3099}せ\u{3099}そ\u{3099}",
|
||||
s.replacingCharacters(
|
||||
in: s.startIndex.advanced(by: 2)..<s.startIndex.advanced(by: 7), with: "zzz"))
|
||||
in: s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 7), with: "zzz"))
|
||||
expectEqual(
|
||||
"\u{1F601}a\u{1F602}す\u{3099}せ\u{3099}そ\u{3099}",
|
||||
s.replacingCharacters(
|
||||
in: s.startIndex.advanced(by: 2)..<s.startIndex.advanced(by: 7),
|
||||
in: s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 7),
|
||||
with: "\u{1F602}"))
|
||||
}
|
||||
|
||||
@@ -1575,12 +1575,12 @@ NSStringAPIs.test("replacingOccurrences(of:with:options:range:)") {
|
||||
s.replacingOccurrences(
|
||||
of: "\u{1F601}", with: "\u{1F602}\u{1F603}",
|
||||
options: NSStringCompareOptions.literalSearch,
|
||||
range: s.startIndex..<s.startIndex.advanced(by: 1)))
|
||||
range: s.startIndex..<s.advance(s.startIndex, by: 1)))
|
||||
|
||||
expectEqual(s, s.replacingOccurrences(
|
||||
of: "\u{1F601}", with: "\u{1F602}\u{1F603}",
|
||||
options: NSStringCompareOptions.literalSearch,
|
||||
range: s.startIndex.advanced(by: 1)..<s.startIndex.advanced(by: 3)))
|
||||
range: s.advance(s.startIndex, by: 1)..<s.advance(s.startIndex, by: 3)))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("replacingPercentEscapes(usingEncoding:)") {
|
||||
@@ -1666,8 +1666,8 @@ NSStringAPIs.test("substring(from:)") {
|
||||
|
||||
expectEqual(s, s.substring(from: s.startIndex))
|
||||
expectEqual("せ\u{3099}そ\u{3099}",
|
||||
s.substring(from: s.startIndex.advanced(by: 8)))
|
||||
expectEqual("", s.substring(from: s.startIndex.advanced(by: 10)))
|
||||
s.substring(from: s.advance(s.startIndex, by: 8)))
|
||||
expectEqual("", s.substring(from: s.advance(s.startIndex, by: 10)))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("substring(to:)") {
|
||||
@@ -1675,8 +1675,8 @@ NSStringAPIs.test("substring(to:)") {
|
||||
|
||||
expectEqual("", s.substring(to: s.startIndex))
|
||||
expectEqual("\u{1F601}abc さ\u{3099}し\u{3099}す\u{3099}",
|
||||
s.substring(to: s.startIndex.advanced(by: 8)))
|
||||
expectEqual(s, s.substring(to: s.startIndex.advanced(by: 10)))
|
||||
s.substring(to: s.advance(s.startIndex, by: 8)))
|
||||
expectEqual(s, s.substring(to: s.advance(s.startIndex, by: 10)))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("substring(with:)") {
|
||||
@@ -1685,12 +1685,12 @@ NSStringAPIs.test("substring(with:)") {
|
||||
expectEqual("", s.substring(with: s.startIndex..<s.startIndex))
|
||||
expectEqual(
|
||||
"",
|
||||
s.substring(with: s.startIndex.advanced(by: 1)..<s.startIndex.advanced(by: 1)))
|
||||
s.substring(with: s.advance(s.startIndex, by: 1)..<s.advance(s.startIndex, by: 1)))
|
||||
expectEqual("", s.substring(with: s.endIndex..<s.endIndex))
|
||||
expectEqual(s, s.substring(with: s.startIndex..<s.endIndex))
|
||||
expectEqual(
|
||||
"さ\u{3099}し\u{3099}す\u{3099}",
|
||||
s.substring(with: s.startIndex.advanced(by: 5)..<s.startIndex.advanced(by: 8)))
|
||||
s.substring(with: s.advance(s.startIndex, by: 5)..<s.advance(s.startIndex, by: 8)))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("localizedUppercase") {
|
||||
@@ -1892,7 +1892,7 @@ NSStringAPIs.test("CompareStringsWithUnpairedSurrogates")
|
||||
let acceptor = "\u{1f601}\u{1f602}\u{1f603}"
|
||||
|
||||
expectEqual("\u{fffd}\u{1f602}\u{fffd}",
|
||||
acceptor[donor.startIndex.advanced(by: 1)..<donor.startIndex.advanced(by: 5)])
|
||||
acceptor[donor.advance(donor.startIndex, by: 1)..<donor.advance(donor.startIndex, by: 5)])
|
||||
}
|
||||
|
||||
NSStringAPIs.test("copy construction") {
|
||||
|
||||
@@ -26,8 +26,7 @@ import StdlibUnittest
|
||||
// Instead of testing with Int elements, we use this wrapper class
|
||||
// that can help us track allocations and find issues with object
|
||||
// lifetime inside Array implementations.
|
||||
final class X : ForwardIndex, Comparable, CustomStringConvertible,
|
||||
IntegerLiteralConvertible
|
||||
final class X : Comparable, CustomStringConvertible, IntegerLiteralConvertible
|
||||
{
|
||||
required init(_ value: Int) {
|
||||
xCount += 1
|
||||
@@ -48,7 +47,7 @@ final class X : ForwardIndex, Comparable, CustomStringConvertible,
|
||||
}
|
||||
|
||||
func successor() -> Self {
|
||||
return self.dynamicType.init(self.value.successor())
|
||||
return self.dynamicType.init(self.value + 1)
|
||||
}
|
||||
|
||||
convenience init(integerLiteral value: Int) {
|
||||
@@ -139,7 +138,7 @@ func test<
|
||||
let bufferId2 = checkReallocation(x, bufferId1, true)
|
||||
|
||||
let y = x
|
||||
x[x.endIndex.predecessor()] = 17
|
||||
x[x.previous(x.endIndex)] = 17
|
||||
let bufferId3 = checkReallocation(x, bufferId2, true)
|
||||
checkEqual(x, y, false)
|
||||
|
||||
|
||||
@@ -104,8 +104,8 @@ func nonASCII() {
|
||||
|
||||
// Slicing the String does not allocate
|
||||
// CHECK-NEXT: String(Contiguous(owner: .cocoa@[[utf16address]], count: 6))
|
||||
let i2 = newNSUTF16.startIndex.advanced(by: 2)
|
||||
let i8 = newNSUTF16.startIndex.advanced(by: 6)
|
||||
let i2 = newNSUTF16.advance(newNSUTF16.startIndex, by: 2)
|
||||
let i8 = newNSUTF16.advance(newNSUTF16.startIndex, by: 6)
|
||||
print(" \(repr(newNSUTF16[i2..<i8]))")
|
||||
|
||||
// Representing a slice as an NSString requires a new object
|
||||
@@ -147,8 +147,8 @@ func ascii() {
|
||||
// CHECK: --- ASCII slicing ---
|
||||
print("--- ASCII slicing ---")
|
||||
|
||||
let i3 = newNSASCII.startIndex.advanced(by: 3)
|
||||
let i6 = newNSASCII.startIndex.advanced(by: 6)
|
||||
let i3 = newNSASCII.advance(newNSASCII.startIndex, by: 3)
|
||||
let i6 = newNSASCII.advance(newNSASCII.startIndex, by: 6)
|
||||
|
||||
// Slicing the String
|
||||
print(" \(repr(newNSASCII[i3..<i6]))")
|
||||
|
||||
@@ -72,7 +72,10 @@ OptionalTests.test("nil comparison") {
|
||||
}
|
||||
|
||||
expectEqual("forced extraction: 1.", "forced extraction: \(x!).")
|
||||
expectEqual("forced extraction use: 2.", "forced extraction use: \(x!.successor()).")
|
||||
expectEqual(
|
||||
"forced extraction use: 2.",
|
||||
"forced extraction use: \(x!.advanced(by: 1))."
|
||||
)
|
||||
}
|
||||
|
||||
func testRelation(p: (Int?, Int?) -> Bool) -> [Bool] {
|
||||
|
||||
@@ -318,7 +318,11 @@ StringTests.test("CompareStringsWithUnpairedSurrogates")
|
||||
let acceptor = "\u{1f601}\u{1f602}\u{1f603}"
|
||||
|
||||
expectEqual("\u{fffd}\u{1f602}\u{fffd}",
|
||||
acceptor[donor.startIndex.advanced(by: 1)..<donor.startIndex.advanced(by: 5)])
|
||||
acceptor[
|
||||
donor.advance(donor.startIndex, by: 1) ..<
|
||||
donor.advance(donor.startIndex, by: 5)
|
||||
]
|
||||
)
|
||||
}
|
||||
|
||||
var CStringTests = TestSuite("CStringTests")
|
||||
|
||||
@@ -26,10 +26,10 @@ StringTraps.test("startIndex/predecessor")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.startIndex
|
||||
i = i.successor()
|
||||
i = i.predecessor()
|
||||
i = s.next(i)
|
||||
i = s.previous(i)
|
||||
expectCrashLater()
|
||||
i = i.predecessor()
|
||||
i = s.previous(i)
|
||||
}
|
||||
|
||||
StringTraps.test("endIndex/successor")
|
||||
@@ -39,11 +39,11 @@ StringTraps.test("endIndex/successor")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.startIndex
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = s.next(i)
|
||||
i = s.next(i)
|
||||
i = s.next(i)
|
||||
expectCrashLater()
|
||||
i = i.successor()
|
||||
i = s.next(i)
|
||||
}
|
||||
|
||||
StringTraps.test("subscript(_:)/endIndex")
|
||||
@@ -53,9 +53,9 @@ StringTraps.test("subscript(_:)/endIndex")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.startIndex
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = s.next(i)
|
||||
i = s.next(i)
|
||||
i = s.next(i)
|
||||
expectCrashLater()
|
||||
s[i]
|
||||
}
|
||||
@@ -67,11 +67,11 @@ StringTraps.test("UTF8ViewEndIndexSuccessor")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.utf8.startIndex
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = s.utf8.next(i)
|
||||
i = s.utf8.next(i)
|
||||
i = s.utf8.next(i)
|
||||
expectCrashLater()
|
||||
i = i.successor()
|
||||
i = s.utf8.next(i)
|
||||
}
|
||||
|
||||
StringTraps.test("UTF8ViewSubscript/endIndex")
|
||||
@@ -81,9 +81,9 @@ StringTraps.test("UTF8ViewSubscript/endIndex")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.utf8.startIndex
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = s.utf8.next(i)
|
||||
i = s.utf8.next(i)
|
||||
i = s.utf8.next(i)
|
||||
expectCrashLater()
|
||||
s.utf8[i]
|
||||
}
|
||||
@@ -95,7 +95,7 @@ StringTraps.test("UTF16ViewSubscript/DecrementedStartIndex")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.utf16.startIndex
|
||||
i = i.predecessor()
|
||||
i = s.utf16.previous(i)
|
||||
expectCrashLater()
|
||||
s.utf16[i]
|
||||
}
|
||||
@@ -107,9 +107,9 @@ StringTraps.test("UTF16ViewSubscript/endIndex")
|
||||
.code {
|
||||
var s = "abc"
|
||||
var i = s.utf16.startIndex
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = i.successor()
|
||||
i = s.utf16.next(i)
|
||||
i = s.utf16.next(i)
|
||||
i = s.utf16.next(i)
|
||||
expectCrashLater()
|
||||
s.utf16[i]
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
func test(s: String)
|
||||
{
|
||||
print(s)
|
||||
var s2 = s[s.startIndex.advanced(by: 2)..<s.startIndex.advanced(by: 4)]
|
||||
var s2 = s[s.advance(s.startIndex, by: 2)..<s.advance(s.startIndex, by: 4)]
|
||||
print(s2)
|
||||
var s3 = s2[s2.startIndex..<s2.startIndex]
|
||||
var s4 = s3[s2.startIndex..<s2.startIndex]
|
||||
|
||||
@@ -71,7 +71,7 @@ func getFirst<R : IteratorProtocol>(r: R) -> R.Element {
|
||||
return r.next()!
|
||||
}
|
||||
|
||||
func testGetFirst(ir: Range<Int>) {
|
||||
func testGetFirst(ir: RangeOfStrideable<Int>) {
|
||||
_ = getFirst(ir.makeIterator()) as Int
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,9 @@
|
||||
// RUN: %target-run-stdlib-swift
|
||||
// REQUIRES: executable_test
|
||||
|
||||
// FIXME: swift-3-indexing-model: This prototype needs to be updated to new indexing model
|
||||
#if false
|
||||
|
||||
public enum ApproximateCount {
|
||||
case Unknown
|
||||
case Precise(IntMax)
|
||||
@@ -1453,3 +1456,5 @@ http://habrahabr.ru/post/255659/
|
||||
*/
|
||||
|
||||
runAllTests()
|
||||
|
||||
#endif
|
||||
@@ -342,7 +342,7 @@ protocol MySeq : _MySeq {
|
||||
}
|
||||
|
||||
protocol _MyCollection : _MySeq {
|
||||
associatedtype Index : ForwardIndex
|
||||
associatedtype Index : Strideable
|
||||
|
||||
var myStartIndex : Index { get }
|
||||
var myEndIndex : Index { get }
|
||||
@@ -361,7 +361,7 @@ struct MyIndexedIterator<C : _MyCollection> : IteratorProtocol {
|
||||
mutating func next() -> C._Element? {
|
||||
if index == container.myEndIndex { return nil }
|
||||
let result = container[index]
|
||||
index = index.successor()
|
||||
index = index.advanced(by: 1)
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -373,7 +373,7 @@ struct OtherIndexedIterator<C : _MyCollection> : IteratorProtocol {
|
||||
mutating func next() -> C._Element? {
|
||||
if index == container.myEndIndex { return nil }
|
||||
let result = container[index]
|
||||
index = index.successor()
|
||||
index = index.advanced(by: 1)
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user