mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
[test] Add tests for Rope.join’s copy-on-write behavior
This commit is contained in:
1 parent
e166904974
commit
6c31865359
2 files changed
+54
-1
No files matched your search
@@ -544,7 +544,28 @@ class TestBigString: CollectionTestCase {
|
||||
}
|
||||
return pieces
|
||||
}
|
||||
|
||||
|
||||
func test_append_copy_on_write() {
|
||||
let flat = String(repeating: sampleString, count: 10)
|
||||
withEvery("stride", in: [32, 64, 128, 250, 1_000, 10_000, 20_000]) { stride in
|
||||
let pieces = self.pieces(of: flat, by: stride).map {
|
||||
BigString($0.str)
|
||||
}
|
||||
|
||||
var big: BigString = ""
|
||||
withEvery("i", in: 0 ..< pieces.count) { i in
|
||||
let copy = big
|
||||
let expected = String(copy)
|
||||
|
||||
big.append(contentsOf: pieces[i])
|
||||
|
||||
let actual = String(copy)
|
||||
expectTrue(actual == expected)
|
||||
copy._invariantCheck()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_insert_string() {
|
||||
let flat = sampleString
|
||||
let ref = BigString(flat)
|
||||
|
||||
@@ -451,6 +451,38 @@ class TestRope: CollectionTestCase {
|
||||
expectEqual(ranges, [0 ..< c])
|
||||
}
|
||||
|
||||
func test_join_copy_on_write() {
|
||||
let c = 10_000
|
||||
var trees = (0 ..< c).map {
|
||||
let chunk = Chunk(length: ($0 % 4) + 1, value: $0)
|
||||
return Rope(CollectionOfOne(chunk))
|
||||
}
|
||||
var ranges = (0 ..< c).map { $0 ..< $0 + 1 }
|
||||
|
||||
var rng = RepeatableRandomNumberGenerator(seed: 0)
|
||||
while trees.count >= 2 {
|
||||
let i = (0 ..< trees.count - 1).randomElement(using: &rng)!
|
||||
let expectedRange = ranges[i].lowerBound ..< ranges[i + 1].upperBound
|
||||
|
||||
let a = trees[i]
|
||||
let b = trees.remove(at: i + 1)
|
||||
trees[i] = Rope()
|
||||
|
||||
let joined = Rope.join(a, b)
|
||||
|
||||
expectEqualElements(a.map { $0.value }, Array(ranges[i]), "Copy-on-write violation")
|
||||
expectEqualElements(b.map { $0.value }, Array(ranges[i + 1]), "Copy-on-write violation")
|
||||
|
||||
a._invariantCheck()
|
||||
b._invariantCheck()
|
||||
joined._invariantCheck()
|
||||
|
||||
trees[i] = joined
|
||||
ranges.replaceSubrange(i ... i + 1, with: CollectionOfOne(expectedRange))
|
||||
}
|
||||
expectEqual(ranges, [0 ..< c])
|
||||
}
|
||||
|
||||
func chunkify(_ values: [Int]) -> [Chunk] {
|
||||
var result: [Chunk] = []
|
||||
var last = Int.min
|
||||
|
||||
Reference in new issue
Block a user