Skip to content

Commit

Permalink
Fix invalid indexOf SplayTree with single node (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
skhugh authored Feb 17, 2023
1 parent 1789c22 commit 04225bc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion yorkie/src/main/kotlin/dev/yorkie/util/SplayTreeSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal class SplayTreeSet<V>(
*/
fun indexOf(value: V?): Int {
val node = valueToNodes[value]
if (node == null || !node.hasLinks) {
if (node == null || node !== root && !node.hasLinks) {
return -1
}
var index = 0
Expand Down
9 changes: 9 additions & 0 deletions yorkie/src/test/kotlin/dev/yorkie/util/SplayTreeSetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ class SplayTreeSetTest {
assertIfRangeCutOff(nodes, 3..7)
}

@Test
fun `should handle indexOf correctly with single node`() {
val node = Node("A")
target.insert(node)
assertEquals(0, target.indexOf(node))
target.delete(node)
assertEquals(-1, target.indexOf(node))
}

private fun assertIfRangeCutOff(nodes: List<Node>, targetRange: IntRange) {
assertEquals(
0,
Expand Down

0 comments on commit 04225bc

Please sign in to comment.