Skip to content

Commit

Permalink
arraymap: fix tail removal (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
nedtwigg authored Sep 11, 2023
2 parents 43d7048 + 301f071 commit 31ce925
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,13 @@ class ArrayMap<K : Comparable<K>, V : Any>(private val data: Array<Any>) : Map<K
++currentDataIdx
}
}
check(toRemoveIdx == indicesToRemove.size) {
val tailRemoval =
if (indicesToRemove.contains((data.size / 2) - 1)) {
1
} else {
0
}
check(toRemoveIdx + tailRemoval == indicesToRemove.size) {
"The indices weren't sorted or were too big: $indicesToRemove"
}
return ArrayMap<K, V>(newData as Array<Any>)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.diffplug.selfie
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.matchers.shouldBe
import kotlin.test.Test
import kotlin.test.assertFails

class ArrayMapTest {
@Test
Expand Down Expand Up @@ -146,4 +147,22 @@ class ArrayMapTest {
map shouldBe
ArrayMap.empty<String, String>().plus(key2, value2).plus(key1, value1).plus(key3, value3)
}

@Test
fun removeItemsWorks() {
val empty = ArrayMap.empty<String, String>()
val arrayMap = empty.plus("first", "1").plus("second", "2").plus("third", "3")
arrayMap.minusSortedIndices(listOf(2)) shouldBe mapOf("first" to "1", "second" to "2")
arrayMap.minusSortedIndices(listOf(0)) shouldBe mapOf("second" to "2", "third" to "3")
arrayMap.minusSortedIndices(listOf(1)) shouldBe mapOf("first" to "1", "third" to "3")

arrayMap.minusSortedIndices(listOf(0, 2)) shouldBe mapOf("second" to "2")

assertFails { arrayMap.minusSortedIndices(listOf(1, 0)) }.message shouldBe
"The indices weren't sorted or were too big: [1, 0]"
assertFails { arrayMap.minusSortedIndices(listOf(2, 1)) }.message shouldBe
"The indices weren't sorted or were too big: [2, 1]"
assertFails { arrayMap.minusSortedIndices(listOf(3)) }.message shouldBe
"The indices weren't sorted or were too big: [3]"
}
}

0 comments on commit 31ce925

Please sign in to comment.