[Rope] Fix find to return a zero remainder for the end position

This commit is contained in:
Karoy Lorentey committed 2023-06-02 17:37:30 -07:00
1 parent c52a1fbf05
commit 03e7f0c401
2 files changed
+24 -2

No files matched your search

@@ -19,7 +19,7 @@ extension Rope {
let wholeSize = _root == nil ? 0 : metric.size(of: root.summary)
precondition(position >= 0 && position <= wholeSize, "Position out of bounds")
guard !isEmpty, preferEnd || position < wholeSize else {
return (endIndex, position)
return (endIndex, 0)
}
var position = position
var node = root
+23 -1
View File
@@ -235,7 +235,29 @@ class TestRope: CollectionTestCase {
}
}
}
func test_find() {
let c = 500
let chunks = (0 ..< c).map {
Chunk(length: ($0 % 4) + 1, value: $0)
}
let rope = Rope<Chunk>(chunks)
let ref = chunks.flatMap { Array(repeating: $0.value, count: $0.length) }
for i in 0 ..< ref.count {
let (index, remaining) = rope.find(at: i, in: Chunk.Metric(), preferEnd: false)
let chunk = rope[index]
expectEqual(chunk.value, ref[i])
let pos = ref[..<i].lastIndex { $0 != ref[i] }.map { $0 + 1 }
expectEqual(remaining, i - (pos ?? 0))
}
let end = rope.find(at: ref.count, in: Chunk.Metric(), preferEnd: false)
expectEqual(end.index, rope.endIndex)
expectEqual(end.remaining, 0)
}
func test_index_offsetBy() {
let c = 500