Skip to content

[test] stdlib/StringIndex: Spin off O(n^4) substring replacement test into a standalone long test #42409

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 1 commit into from
Apr 16, 2022
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
77 changes: 0 additions & 77 deletions test/stdlib/StringIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -822,80 +822,3 @@ suite.test("String.replaceSubrange index validation")
}
}
}

suite.test("Substring.replaceSubrange index validation")
.forEach(in: examples) { string in
guard #available(SwiftStdlib 5.7, *) else {
// Index navigation in 5.7 always rounds input indices down to the nearest
// Character, so that we always have a well-defined distance between
// indices, even if they aren't valid.
//
// 5.6 and below did not behave consistently in this case.
return
}

string.dumpIndices()

let scalarMap = string.scalarMap()
let allIndices = string.allIndices()

for i in allIndices {
print(i)
for j in allIndices {
guard i <= j else { continue }
let si = scalarMap[i]!.index
let sj = scalarMap[j]!.index

let substring = string[i ..< j]

let subindices = substring.allIndices()
for m in subindices {
for n in subindices {
guard m <= n else { continue }
let sm = scalarMap[m]!.index
let sn = scalarMap[n]!.index

let replacement = "x"

var _expected = "".unicodeScalars
_expected += string.unicodeScalars[si ..< sm]
_expected += replacement.unicodeScalars
_expected += string.unicodeScalars[sn ..< sj]
let expected = String(_expected)[...]

// Check Substring.replaceSubrange(_:with:)
do {
var actual = substring
actual.replaceSubrange(m ..< n, with: Array(replacement))

expectEqual(actual, expected,
"""
string: \(string.debugDescription)
i: \(i)
j: \(j)
m: \(m)
n: \(n)
""")
}

// Check String.unicodeScalars.replaceSubrange(_:with:)
do {
var actual = substring
actual.unicodeScalars.replaceSubrange(
m ..< n, with: Array(replacement.unicodeScalars))

expectEqual(actual, expected,
"""
string: \(string.debugDescription)
i: \(i)
j: \(j)
m: \(m)
n: \(n)
""")
}
}
}
}
}
}

103 changes: 103 additions & 0 deletions validation-test/stdlib/StringIndexLong.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// RUN: %target-run-simple-swift
// REQUIRES: executable_test
// REQUIRES: long_test

import StdlibUnittest
#if _runtime(_ObjC)
import Foundation
#endif

var suite = TestSuite("StringIndexLong")
defer { runAllTests() }

let _examples: [StaticString] = [
"abc\r\ndefg",
"a\r\ncдe\u{301}日🧟‍♀️x🏳️‍🌈🇺🇸🇨🇦",
]

let examples: [String] = _examples.flatMap { s in
let str = "\(s)"
#if _runtime(_ObjC)
let unichars = Array(str.utf16)
let nsstr = NSString(characters: unichars, length: unichars.count)
return [str, nsstr as String]
#else
return [str]
#endif
}

suite.test("Substring.replaceSubrange index validation")
.forEach(in: examples) { string in
guard #available(SwiftStdlib 5.7, *) else {
// Index navigation in 5.7 always rounds input indices down to the nearest
// Character, so that we always have a well-defined distance between
// indices, even if they aren't valid.
//
// 5.6 and below did not behave consistently in this case.
return
}

string.dumpIndices()

let scalarMap = string.scalarMap()
let allIndices = string.allIndices()

for i in allIndices {
print(i)
for j in allIndices {
guard i <= j else { continue }
let si = scalarMap[i]!.index
let sj = scalarMap[j]!.index

let substring = string[i ..< j]

let subindices = substring.allIndices()
for m in subindices {
for n in subindices {
guard m <= n else { continue }
let sm = scalarMap[m]!.index
let sn = scalarMap[n]!.index

let replacement = "x"

var _expected = "".unicodeScalars
_expected += string.unicodeScalars[si ..< sm]
_expected += replacement.unicodeScalars
_expected += string.unicodeScalars[sn ..< sj]
let expected = String(_expected)[...]

// Check Substring.replaceSubrange(_:with:)
do {
var actual = substring
actual.replaceSubrange(m ..< n, with: Array(replacement))

expectEqual(actual, expected,
"""
string: \(string.debugDescription)
i: \(i)
j: \(j)
m: \(m)
n: \(n)
""")
}

// Check String.unicodeScalars.replaceSubrange(_:with:)
do {
var actual = substring
actual.unicodeScalars.replaceSubrange(
m ..< n, with: Array(replacement.unicodeScalars))

expectEqual(actual, expected,
"""
string: \(string.debugDescription)
i: \(i)
j: \(j)
m: \(m)
n: \(n)
""")
}
}
}
}
}
}