Skip to content
Merged
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
112 changes: 102 additions & 10 deletions test/1_stdlib/subString.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,107 @@
// RUN: %target-run-simple-swift | FileCheck %s
// RUN: rm -rf %t
// RUN: mkdir -p %t
// RUN: %target-build-swift %s -o %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: executable_test

func test(_ s: String) {
print(s)
var s2 = s[s.index(s.startIndex, offsetBy: 2)..<s.index(s.startIndex, offsetBy: 4)]
print(s2)
var s3 = s2[s2.startIndex..<s2.startIndex]
var s4 = s3[s2.startIndex..<s2.startIndex]
import StdlibUnittest

var SubstringTests = TestSuite("SubstringTests")

func checkMatch<S: Collection, T: Collection
where S.Index == T.Index, S.Iterator.Element == T.Iterator.Element,
S.Iterator.Element: Equatable>(
_ x: S, _ y: T, _ i: S.Index) {

expectEqual(x[i], y[i])
}

SubstringTests.test("String") {
let s = "abcdefg"
let s1 = s[s.index(s.startIndex, offsetBy: 2) ..<
s.index(s.startIndex, offsetBy: 4)]
let s2 = s1[s1.startIndex..<s1.endIndex]
let s3 = s2[s1.startIndex..<s1.endIndex]

expectEqual(s1, "cd")
expectEqual(s2, "cd")
expectEqual(s3, "cd")
}

SubstringTests.test("CharacterView")
.xfail(.always("CharacterView slices don't share indices"))
.code {
let s = "abcdefg"
var t = s.characters.dropFirst(2)
var u = t.dropFirst(2)

checkMatch(s.characters, t, t.startIndex)
checkMatch(s.characters, t, t.index(after: t.startIndex))
checkMatch(s.characters, t, t.index(before: t.endIndex))

checkMatch(s.characters, t, u.startIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is a valid assertion. t has been mutated before the position referenced by u.startIndex

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point—I'll change it so it only tests string equality after a mutation. The mutations are there to ensure value semantics post-fix; those all pass today.

Copy link
Contributor

@gribozavr gribozavr May 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking that indices are interchangeable after mutation is a valid thing to do, we just need to mutate the tail part of the string that occurs after the index position.

checkMatch(t, u, u.startIndex)
checkMatch(t, u, u.index(after: u.startIndex))
checkMatch(t, u, u.index(before: u.endIndex))

t.replaceSubrange(t.startIndex...t.startIndex, with: ["C"])
u.replaceSubrange(u.startIndex...u.startIndex, with: ["E"])
expectEqual(String(u), "Efg")
expectEqual(String(t), "Cdefg")
expectEqual(s, "abcdefg")
}

test("some text")
SubstringTests.test("UnicodeScalars")
.xfail(.always("UnicodeScalarsView slices don't share indices"))
.code {
let s = "abcdefg"
var t = s.unicodeScalars.dropFirst(2)
var u = t.dropFirst(2)

checkMatch(s.unicodeScalars, t, t.startIndex)
checkMatch(s.unicodeScalars, t, t.index(after: t.startIndex))
checkMatch(s.unicodeScalars, t, t.index(before: t.endIndex))

checkMatch(s.unicodeScalars, t, u.startIndex)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both groups should be fixed now.

checkMatch(t, u, u.startIndex)
checkMatch(t, u, u.index(after: u.startIndex))
checkMatch(t, u, u.index(before: u.endIndex))

t.replaceSubrange(t.startIndex...t.startIndex, with: ["C"])
u.replaceSubrange(u.startIndex...u.startIndex, with: ["E"])
expectEqual(String(u), "Efg")
expectEqual(String(t), "Cdefg")
expectEqual(s, "abcdefg")
}

SubstringTests.test("UTF16View")
.xfail(.always("UTF16View slices don't share indices"))
.code {
let s = "abcdefg"
let t = s.utf16.dropFirst(2)
let u = t.dropFirst(2)

checkMatch(s.utf16, t, t.startIndex)
checkMatch(s.utf16, t, t.index(after: t.startIndex))
checkMatch(s.utf16, t, t.index(before: t.endIndex))

checkMatch(s.utf16, t, u.startIndex)
checkMatch(t, u, u.startIndex)
checkMatch(t, u, u.index(after: u.startIndex))
checkMatch(t, u, u.index(before: u.endIndex))
}

SubstringTests.test("UTF8View") {
let s = "abcdefg"
let t = s.utf8.dropFirst(2)
let u = t.dropFirst(2)

checkMatch(s.utf8, t, t.startIndex)
checkMatch(s.utf8, t, t.index(after: t.startIndex))

checkMatch(s.utf8, t, u.startIndex)
checkMatch(t, u, u.startIndex)
checkMatch(t, u, u.index(after: u.startIndex))
}

// CHECK: some text
// CHECK: me
runAllTests()