Skip to content

Commit

Permalink
feat(#12): add element's updating in NotEmptyMutableList
Browse files Browse the repository at this point in the history
  • Loading branch information
LVMVRQUXL committed Aug 11, 2022
1 parent 3abc6f9 commit 2a46683
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
13 changes: 12 additions & 1 deletion src/main/kotlin/kotools/types/collections/NotEmptyMutableList.kt
Expand Up @@ -113,5 +113,16 @@ 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")
override fun set(index: Int, element: E): E = TODO("Not implemented yet")

/**
* Replaces the element at the specified [index] in this list with the
* specified [element], or throws an [IndexOutOfBoundsException] if the
* [index] is out of bounds.
*/
@Throws(IndexOutOfBoundsException::class)
override fun set(index: Int, element: E): E = when (index) {
in 1 until size -> element.also { tail[index - 1] = it }
0 -> element.also { head = it }
else -> indexOutOfBounds(index, size)
}
}
Expand Up @@ -176,7 +176,80 @@ class NotEmptyMutableListTest {
inner class RemoveAt // TODO: 21/07/2022 Not implemented yet

@Nested
inner class Set // TODO: 21/07/2022 Not implemented yet
inner class Set {
@Test
fun `should replace the head with an index that equals 0`() {
// GIVEN
val expectedList: NotEmptyList<String> =
NotEmptyList("one", "two", "three")
val index = 0
val previousElement = "four"
val list: NotEmptyMutableList<String> = expectedList
.subList(1, expectedList.size)
.toNotEmptyMutableList()
.apply { add(index, previousElement) }
val element: String = expectedList.head
// WHEN
assertDoesNotThrow { list[index] = element }
// THEN
list.run {
head assertEquals element
head assertNotEquals previousElement
this[index] assertEquals element
this[index] assertNotEquals previousElement
size assertEquals expectedList.size
forEachIndexed { index2: Int, element2: String ->
element2 assertEquals expectedList[index2]
}
}
}

@Test
fun `should replace a tail's element with an index in 1 until the list's size`() {
// GIVEN
val expectedList: NotEmptyList<String> =
NotEmptyList("one", "two", "three")
val previousElement = "four"
val list: NotEmptyMutableList<String> = NotEmptyMutableList(
expectedList.head,
previousElement,
expectedList.last()
)
val index = 1
val element: String = expectedList[index]
// WHEN
assertDoesNotThrow { list[index] = element }
// THEN
list.run {
this[index] assertEquals element
this[index] assertNotEquals previousElement
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 expectedList: NotEmptyList<String> =
NotEmptyList("one", "two", "three")
val list: NotEmptyMutableList<String> =
expectedList.toNotEmptyMutableList()
val index = list.size
val element = "error"
// WHEN
assertFailsWith<IndexOutOfBoundsException> { list[index] = element }
// THEN
list.run {
size assertEquals expectedList.size
forEachIndexed { index2: Int, element2: String ->
element2 assertEquals expectedList[index2]
}
}
}
}

// ---------- Conversions ----------

Expand Down

0 comments on commit 2a46683

Please sign in to comment.