-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter13Spec.scala
135 lines (105 loc) · 4.01 KB
/
Chapter13Spec.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import Chapter13._
import org.scalatest.{FlatSpec, Matchers}
import scala.collection.mutable
import scala.io.Source
class Chapter13Spec extends FlatSpec with Matchers {
"indexes" should "produce a mutable map of mutable set of indexes of all characters" in {
//when
val result: mutable.Map[Char, mutable.Set[Int]] = indexes("Mississippi")
//then
result('M') shouldBe mutable.Set(0)
result('i') shouldBe mutable.Set(1, 4, 7, 10)
result('s') shouldBe mutable.Set(2, 3, 5, 6)
result('p') shouldBe mutable.Set(8, 9)
}
"indexes2" should "produce an immutable map of lists of indexes of all characters" in {
//when
val result: Map[Char, List[Int]] = indexes2("Mississippi")
//then
result('M') shouldBe List(0)
result('i') shouldBe List(1, 4, 7, 10)
result('s') shouldBe List(2, 3, 5, 6)
result('p') shouldBe List(8, 9)
}
"removeAllZeroes" should "remove all zeroes from a linked list of integers" in {
//when & then
val list: LinkedList[Int] = newLinkedList(1, 2, 3)
val result: LinkedList[Int] = removeAllZeroes(list)
result shouldBe theSameInstanceAs(list)
removeAllZeroes(newLinkedList(0, 1, 2, 3, 4)) shouldBe newLinkedList(1, 2, 3, 4)
removeAllZeroes(newLinkedList(1, 2, 0, 3, 4)) shouldBe newLinkedList(1, 2, 3, 4)
removeAllZeroes(newLinkedList(1, 2, 3, 4, 0)) shouldBe newLinkedList(1, 2, 3, 4)
removeAllZeroes(newLinkedList(0, 1, 2, 0, 3, 4, 0)) shouldBe newLinkedList(1, 2, 3, 4)
removeAllZeroes(newLinkedList(0, 0, 1, 2, 0, 0, 3, 0, 0, 4, 0, 0)) shouldBe
newLinkedList(1, 2, 3, 4)
}
private def newLinkedList(elems: Int*): LinkedList[Int] = mutable.LinkedList(elems: _*)
"mapToValues" should "return collection of corresponding integer values from map" in {
//given
val coll = Array("Tom", "Fred", "Harry")
val map = Map("Tom" -> 3, "Dick" -> 4, "Harry" -> 5)
//when
val result = mapToValues(coll, map)
//then
result shouldBe Array(3, 5)
}
"collToString" should "works just like mkString, using reduceLeft" in {
//when & then
collToString(Nil) shouldBe ""
collToString(Array(1)) shouldBe "1"
collToString(Seq(1, 2, 3)) shouldBe "1, 2, 3"
collToString(List("1", "2", "3")) shouldBe "1, 2, 3"
}
"reversList" should "revers the given list" in {
//given
val lst = List(1, 2, 3, 4, 5)
//when
val result: List[Int] = reversList(lst)
//then
result shouldBe List(5, 4, 3, 2, 1)
}
"multiply" should "apply Function.tupled to the multiplication function" in {
//given
val prices = List(1, 2, 3)
val quantities = List(10, 20, 30)
//when
val result = multiply(prices, quantities)
//then
result shouldBe List(10, 40, 90)
}
"twoDimensionalArray" should "turn an array of Double values into a two-dimensional array" in {
//given
val arr = Array[Double](1, 2, 3, 4, 5, 6)
val columns = 3
//when
val result: Array[Array[Double]] = twoDimensionalArray(arr, columns)
//then
result shouldBe Array[Array[Double]](Array(1, 2, 3), Array(4, 5, 6))
}
"getLetterFrequencyMap" should "return letter frequency map for the given files" in {
//given
val files = Array.fill(5)("/myfile.txt")
//when
val result: Map[Char, Int] = getLetterFrequencyMap(files)
//then
result shouldBe getTestFrequencyMap(files.head, files.length)
}
"getLetterFrequencyMap2" should "return letter frequency map for the given string" in {
//given
val str = Source.fromInputStream(getClass.getResourceAsStream("/myfile.txt")).mkString
//when
val result: Map[Char, Int] = getLetterFrequencyMap2(str)
//then
result shouldBe getTestFrequencyMap("/myfile.txt", 1)
}
private def getTestFrequencyMap(file: String, multiplier: Int): Map[Char, Int] = {
val result = new mutable.HashMap[Char, Int]()
for (c <- Source.fromInputStream(getClass.getResourceAsStream(file))) {
result(c) = result.getOrElse(c, 0) + 1
}
for ((k, v) <- result) {
result(k) = v * multiplier
}
result.toMap
}
}