Skip to content

Commit

Permalink
feat(#12): add element's removing in NotEmptyMutableList
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Aug 11, 2022
1 parent 2a46683 commit 9f58ad2
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/main/kotlin/kotools/types/collections/NotEmptyMutableList.kt
Expand Up @@ -112,7 +112,20 @@ public class NotEmptyMutableList<E>(override var head: E, vararg tail: E) :
}

override fun get(index: Int): E = if (index == 0) head else tail[index - 1]
override fun removeAt(index: Int): E = TODO("Not implemented yet")

/**
* Removes an element at the specified [index] from the list, or throws an
* [IndexOutOfBoundsException] if the index is out of bounds.
*
* Because this list shouldn't be empty, the element will not be removed if
* this list contains only one element.
*/
@Throws(IndexOutOfBoundsException::class)
override infix fun removeAt(index: Int): E = when (index) {
in 1 until size -> tail.removeAt(index - 1)
0 -> head.also { if (tail.isNotEmpty()) head = tail.removeFirst() }
else -> indexOutOfBounds(index, size)
}

/**
* Replaces the element at the specified [index] in this list with the
Expand Down
Expand Up @@ -10,6 +10,7 @@ import org.junit.jupiter.api.assertDoesNotThrow
import kotlin.test.Test
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class NotEmptyMutableListTest {
@Nested
Expand Down Expand Up @@ -173,7 +174,78 @@ class NotEmptyMutableListTest {
}

@Nested
inner class RemoveAt // TODO: 21/07/2022 Not implemented yet
inner class RemoveAt {
@Test
fun `should remove the head from a list containing several elements and with an index that equals 0`() {
// GIVEN
val expectedList: NotEmptyList<String> = NotEmptyList("one", "two")
val index = 0
val target = "target"
val list: NotEmptyMutableList<String> = expectedList
.toNotEmptyMutableList()
.apply { add(index, target) }
// WHEN
val element: String = assertDoesNotThrow { list removeAt index }
// THEN
element assertEquals target
list.run {
assertFalse { element in this }
head assertNotEquals element
size assertEquals expectedList.size
head assertEquals expectedList.head
forEachIndexed { index2: Int, element2: String ->
element2 assertEquals expectedList[index2]
}
}
}

@Test
fun `shouldn't remove the head from a singleton list and with an index that equals 0`() {
// GIVEN
val expectedList: NotEmptyList<String> = NotEmptyList("one")
val list: NotEmptyMutableList<String> =
expectedList.toNotEmptyMutableList()
val index = 0
// WHEN
val element: String = assertDoesNotThrow { list removeAt index }
// THEN
element assertEquals expectedList.head
assertTrue { element in list }
list.size assertEquals expectedList.size
list.head assertEquals expectedList.head
}

@Test
fun `should remove an element with an index in 1 until the list's size`() {
// GIVEN
val expectedList: NotEmptyList<String> = NotEmptyList("one", "two")
val target = "three"
val list: NotEmptyMutableList<String> = expectedList
.toNotEmptyMutableList()
.also { it += target }
val index: Int = list.size - 1
// WHEN
val element: String = assertDoesNotThrow { list removeAt index }
// THEN
element assertEquals target
list.run {
assertFalse { element in this }
size assertEquals expectedList.size
forEachIndexed { index2: Int, element2: String ->
element2 assertEquals expectedList[index2]
}
}
}

@Test
fun `should throw an error with an index that is out of bounds`() {
// GIVEN
val list: NotEmptyMutableList<String> = NotEmptyMutableList("one")
val index: Int = list.size
// WHEN & THEN
assertFailsWith<IndexOutOfBoundsException> { list removeAt index }
}
}

@Nested
inner class Set {
Expand Down

0 comments on commit 9f58ad2

Please sign in to comment.