|
| 1 | +package oop.Iterator |
| 2 | + |
| 3 | +import org.hamcrest.CoreMatchers.`is` |
| 4 | +import org.hamcrest.MatcherAssert.assertThat |
| 5 | +import org.junit.Test |
| 6 | + |
| 7 | +class IteratorShould { |
| 8 | + private val list = mutableListOf("10", "12", "15", "29", "30", "36") |
| 9 | + private val iterator: Iterator<String> = NormalIterator(list) |
| 10 | + |
| 11 | + @Test |
| 12 | + fun `Return first element with first`() { |
| 13 | + assertThat(iterator.first(), `is`("10")) |
| 14 | + } |
| 15 | + |
| 16 | + @Test |
| 17 | + fun `Return fourth element doing first-next-next-next`() { |
| 18 | + iterator.first() |
| 19 | + iterator.next() |
| 20 | + iterator.next() |
| 21 | + iterator.next() |
| 22 | + assertThat(iterator.get(), `is`("29")) |
| 23 | + } |
| 24 | + |
| 25 | + @Test(expected = NoSuchElementException::class) |
| 26 | + fun `Throw NoSuchElementException if element don't have next `() { |
| 27 | + iterator.first() |
| 28 | + (1..7).forEach { iterator.next() } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + fun `Return -1 when ask to prev index in first element`() { |
| 33 | + iterator.first() |
| 34 | + assertThat(iterator.prevIndex(), `is`(-1)) |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `Transform all elements of a list when using map`() { |
| 40 | + val transformList = iterator.map { it.toInt() } |
| 41 | + assertThat(transformList, `is`(listOf(10, 12, 15, 29, 30, 36))) |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun `Filter list with criterion using filter`() { |
| 46 | + val filterList = iterator.filter { it.contains("1") } |
| 47 | + assertThat(filterList, `is`(listOf("10", "12", "15"))) |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun `Apply function to all elements`() { |
| 52 | + var res = 0 |
| 53 | + iterator.forEach { res += it.toInt() } |
| 54 | + assertThat(res, `is`(132)) |
| 55 | + } |
| 56 | +} |
0 commit comments