|
1 | 1 | package algorithms.array
|
2 | 2 |
|
| 3 | +import algorithms.array.ReverseArray._ |
3 | 4 | import org.scalatest.{FlatSpec, Matchers}
|
4 | 5 |
|
5 | 6 | class ReverseArraySpec extends FlatSpec with Matchers {
|
6 | 7 |
|
7 | 8 | it should "reverse the whole array" in {
|
8 |
| - //given |
9 |
| - val arr = Array(1, 2, 3, 4, 5) |
10 |
| - |
11 |
| - //when |
12 |
| - val result = ReverseArray.reverse(arr) |
13 |
| - |
14 |
| - //then |
15 |
| - result.toList shouldBe List(5, 4, 3, 2, 1) |
| 9 | + //when & then |
| 10 | + reverse(Array.empty[Int]).toList shouldBe Nil |
| 11 | + reverse(Array(1)).toList shouldBe List(1) |
| 12 | + reverse(Array(1, 2)).toList shouldBe List(2, 1) |
| 13 | + reverse(Array(1, 2, 3, 4, 5)).toList shouldBe List(5, 4, 3, 2, 1) |
| 14 | + } |
| 15 | + |
| 16 | + it should "reverse part of the array" in { |
| 17 | + //when & then |
| 18 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 0).toList shouldBe List(1, 2, 3, 4, 5, 6, 7) |
| 19 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 1).toList shouldBe List(1, 2, 3, 4, 5, 6, 7) |
| 20 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 2).toList shouldBe List(2, 1, 3, 4, 5, 6, 7) |
| 21 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 0, 3).toList shouldBe List(3, 2, 1, 4, 5, 6, 7) |
| 22 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 1, 2).toList shouldBe List(1, 3, 2, 4, 5, 6, 7) |
| 23 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 1, 4).toList shouldBe List(1, 5, 4, 3, 2, 6, 7) |
| 24 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 5, 2).toList shouldBe List(1, 2, 3, 4, 5, 7, 6) |
| 25 | + reverse(Array(1, 2, 3, 4, 5, 6, 7), 6, 0).toList shouldBe List(1, 2, 3, 4, 5, 6, 7) |
16 | 26 | }
|
17 | 27 | }
|
0 commit comments