-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter14Spec.scala
141 lines (108 loc) · 3.29 KB
/
Chapter14Spec.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
136
137
138
139
140
141
import Chapter14._
import org.scalatest.{FlatSpec, Matchers}
class Chapter14Spec extends FlatSpec with Matchers {
"swap" should "returns the pair with the components swapped" in {
//given
val pair = (1, 2)
//when
val result: (Int, Int) = swap(pair)
//then
result shouldBe (2, 1)
}
"swap2" should "swap the first two elements of an array" in {
//when & then
var arr: Array[Int] = Array()
val r1: Array[Int] = swap2(arr)
r1 shouldBe theSameInstanceAs(arr)
r1 shouldBe Array()
arr = Array(1)
val r2 = swap2(arr)
r2 shouldBe theSameInstanceAs(arr)
r2 shouldBe Array(1)
arr = Array(1, 2)
val r3 = swap2(arr)
r3 shouldBe theSameInstanceAs(arr)
r3 shouldBe Array(2, 1)
arr = Array(1, 2, 3, 4)
val r4 = swap2(arr)
r4 shouldBe theSameInstanceAs(arr)
r4 shouldBe Array(2, 1, 3, 4)
}
"price" should "handle Multiple class items" in {
//given
val item = Bundle("Father's day special", 20.0,
Article("Scala for the Impatient", 39.95),
Bundle("Anchor Distilley Sampler", 10.0,
Article("Old Potrero Straight Rye Whiskey", 79.95),
Article("Junipero Gin", 32.95)),
Multiple(10, Article("Blackwell Toaster", 29.95)))
//when
val result: Double = price(item)
//then
result.formatted("%.2f") shouldBe "422.35"
}
it should "be able to handle any items, such as bundles or multiples" in {
//when & then
price(Multiple(1,
Multiple(2,
Bundle("Black Friday special", 49.0,
Multiple(3, Article("iPhone 5s", 549.99)))))
).formatted("%.2f") shouldBe "3201.94"
}
"leafSum" should "compute the sum of all elements in the leaves" in {
//given
val list: List[Any] = List(List(3, 8), 2, List(5))
//when
val result: Double = leafSum(list)
//then
result shouldBe 18
}
"Task6.leafSum" should "compute the sum of all elements in the BinaryTree" in {
import Task6._
//given
val bt: Task6.BinaryTree = Node(Node(Leaf(3), Leaf(8)), Node(Leaf(2), Leaf(5)))
//when
val result: Int = leafSum(bt)
//then
result shouldBe 18
}
"Task7.leafSum" should "compute the sum of all elements in the multi-node tree" in {
import Task7._
//given
val bt: Task7.BinaryTree = Node(Node(Leaf(3), Leaf(8)), Leaf(2), Node(Leaf(5)))
//when
val result: Int = leafSum(bt)
//then
result shouldBe 18
}
"eval" should "compute the value of the operator-node tree" in {
import Task8._
import Task8.Op._
//given
val bt: Task8.BinaryTree =
Node(Plus, Node(Product, Leaf(3), Leaf(8)), Leaf(2), Node(Minus, Leaf(5)))
//when
val result: Int = eval(bt)
//then
result shouldBe 21
}
"sumOfNonNoneValues" should "compute the sum of the non-None values" in {
//given
val list: List[Option[Int]] = List(None, Some(1), None, Some(2), None, Some(3))
//when
val result: Int = sumOfNonNoneValues(list)
//then
result shouldBe 6
}
"compose" should "yield None if either function does" in {
//given
def f(x: Double) = if (x >= 0) Some(Math.sqrt(x)) else None
def g(x: Double) = if (x != 1) Some(1 / (x - 1)) else None
//when
val h = compose(f, g)
//then
h(2) shouldBe Some(1)
h(1) shouldBe None
h(0) shouldBe None
}
}