|
| 1 | +package fpoo |
| 2 | + |
| 3 | +import org.scalatest._ |
| 4 | +import org.scalatest.prop.TableDrivenPropertyChecks |
| 5 | +import fpoo.Chapter01._ |
| 6 | + |
| 7 | +class SecondSpec extends UnitSpec { |
| 8 | + "Exercise 1: second" should "return the second item in a given list" in { |
| 9 | + val list = List("Lorem", "ipsum", "dolor", "sit", "amet") |
| 10 | + second(list) should be ("ipsum") |
| 11 | + } |
| 12 | + it should "throw IndexOutOfBoundsException if called on a list with fewer than 2 elements" in { |
| 13 | + val listOf1 = List("sole") |
| 14 | + a [IndexOutOfBoundsException] should be thrownBy { |
| 15 | + second(listOf1) |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +class ThirdSpec extends UnitSpec { |
| 21 | + "Exercise 2a: third" should "return the third item in a given list" in { |
| 22 | + val list = List("Lorem", "ipsum", "dolor", "sit", "amet") |
| 23 | + third(list) should be ("dolor") |
| 24 | + } |
| 25 | + it should "throw IndexOutOfBoundsException if called on a list with fewer than 3 elements" in { |
| 26 | + val listOf2 = List("penulitimate", "ultimate") |
| 27 | + a [IndexOutOfBoundsException] should be thrownBy { |
| 28 | + third(listOf2) |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +class Third2Spec extends UnitSpec { |
| 34 | + "Exercise 2b: third2" should "return the third item in a given list" in { |
| 35 | + val list = List("Lorem", "ipsum", "dolor", "sit", "amet") |
| 36 | + third2(list) should be ("dolor") |
| 37 | + } |
| 38 | + it should "throw NoSuchElementException if called on a list with fewer than 3 elements" in { |
| 39 | + val listOf2 = List("penulitimate", "ultimate") |
| 40 | + a [NoSuchElementException] should be thrownBy { |
| 41 | + third2(listOf2) |
| 42 | + } |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class AddSquaresSpec extends UnitSpec { |
| 47 | + "Exercise 3: addSquares" should "square each item in a list and sum them" in { |
| 48 | + val list = List(1, 2, 5) |
| 49 | + addSquares(list) should be (30) |
| 50 | + } |
| 51 | + it should "return 0 if called on an empty list" in { |
| 52 | + val emptyList = List[Int]() |
| 53 | + addSquares(emptyList) should be (0) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +class BizareFactorialSpec extends UnitSpec with TableDrivenPropertyChecks { |
| 58 | + "Exercise 4: bizarreFactorial" should "compute the factorial, i.e., n!, of a given integer n >= 0" in { |
| 59 | + val factorials = Table( |
| 60 | + ("n", "factorial"), |
| 61 | + (0, 1), |
| 62 | + (1, 1), |
| 63 | + (2, 2), |
| 64 | + (3, 6), |
| 65 | + (4, 24), |
| 66 | + (5, 120) |
| 67 | + ) |
| 68 | + |
| 69 | + forAll (factorials) { (n: Int, factorial: Int) => |
| 70 | + whenever (n >= 0) { |
| 71 | + bizarreFactorial(n) should be (factorial) |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +class OtherFunctionsSpec extends UnitSpec { |
| 78 | + "Exercise 5a: take" should "create a new sequence of the first n elements of an existing sequence" in { |
| 79 | + 1 to 10 take 3 should be (List(1, 2, 3)) |
| 80 | + } |
| 81 | + |
| 82 | + "Exercise 5b: distinct" should "remove duplicates from an existing sequence" in { |
| 83 | + val dupes = Seq(1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 5, 6) |
| 84 | + dupes.distinct should be (1 to 6) |
| 85 | + } |
| 86 | + |
| 87 | + "Exercise 5c: ++" should "concatenate two sequences together" in { |
| 88 | + val a = 1 to 3 |
| 89 | + val b = 4 to 6 |
| 90 | + a ++ b should be (1 to 6) |
| 91 | + } |
| 92 | + |
| 93 | + "Exercise 5d: fill" should "create a sequence containing n copies of the same value" in { |
| 94 | + Seq.fill(5)(2) should be (Seq(2, 2, 2, 2, 2)) |
| 95 | + } |
| 96 | + |
| 97 | + "Exercise 5e: interleave" should "interleave the elements of two sequences together" in { |
| 98 | + val evens = Seq(0, 2, 4, 6, 8) |
| 99 | + val odds = Seq(1, 3, 5, 7, 9) |
| 100 | + evens interleave odds should be (0 to 9) |
| 101 | + } |
| 102 | + |
| 103 | + "Exercise 5f.i: drop" should "remove the first n items from the sequence" in { |
| 104 | + 1 to 10 drop 3 should be (4 to 10) |
| 105 | + } |
| 106 | + "Exercise 5f.ii: dropRight" should "remove the last n items from the sequence" in { |
| 107 | + 1 to 10 dropRight 3 should be (1 to 7) |
| 108 | + } |
| 109 | + |
| 110 | + "Exercise 5g: flatten" should "turn a sequence of sequences into a sequence containing all of the values of each subsequence" in { |
| 111 | + Seq(1 to 3, 4 to 6, 7 to 9).flatten should be (1 to 9) |
| 112 | + } |
| 113 | + |
| 114 | + "Exercise 5h: grouped" should "yield an iterator that turns the given sequence into a sequence of subsequences, each n items long" in { |
| 115 | + (1 to 9 grouped 3).toSeq should be (Seq(1 to 3, 4 to 6, 7 to 9)) |
| 116 | + } |
| 117 | + |
| 118 | + "Exercise 5i: forall" should "test whether all items in a sequence meet a certain condition" in { |
| 119 | + 1 to 9 forall { _ < 10 } should be (true) |
| 120 | + } |
| 121 | + |
| 122 | + "Exercise 5j: filterNot" should "remove items meeting a certain criterion from a given sequence" in { |
| 123 | + 1 to 10 filterNot { _ % 3 == 0 } should be (Seq(1, 2, 4, 5, 7, 8, 10)) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +class StartsWithSpec extends UnitSpec { |
| 128 | + "Exercise 6: startsWith" should "test whether a sequence begins with the same element of another sequence" in { |
| 129 | + (1 to 10) startsWith (1 to 3) should be (true) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +class Tails2Spec extends UnitSpec { |
| 134 | + "Exercise 7: tails2" should "return a sequence of successively smaller subsequences of the argument" in { |
| 135 | + (1 to 4).tails2 should be (Seq(1 to 4, 2 to 4, 3 to 4, 4 to 4, 4 until 4)) |
| 136 | + } |
| 137 | + "Exercise 7a: tails3" should "return a sequence of successively smaller subsequences of the argument" in { |
| 138 | + (1 to 4).tails3 should be (Seq(1 to 4, 2 to 4, 3 to 4, 4 to 4, 4 until 4)) |
| 139 | + } |
| 140 | +} |
| 141 | + |
0 commit comments