-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathChapter03Spec.scala
98 lines (82 loc) · 2.94 KB
/
Chapter03Spec.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
import Chapter03._
import org.scalatest._
import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer
class Chapter03Spec extends FlatSpec with Matchers {
"Chapter03" should "set a to an array of n random integers" in {
val n = 5
val a: Array[Int] = randomIntArray(n)
a.length should be (n)
a.foreach(v => {
v should be >= 0
v should be < n
})
}
it should "swap adjacent elements of integer array" in {
val a = Array(1, 2, 3, 4, 5)
val b = swapAdjacent(a)
b shouldBe theSameInstanceAs(a)
b shouldBe Array(2, 1, 4, 3, 5)
swapAdjacent(Array()) shouldBe Array()
swapAdjacent(Array(1)) shouldBe Array(1)
swapAdjacent(Array(1, 2, 3, 4)) shouldBe Array(2, 1, 4, 3)
}
it should "swap adjacent elements of integer array and return new array" in {
val a = Array(1, 2, 3, 4, 5)
val b = swapAdjacentYield(a)
b shouldNot be theSameInstanceAs a
b shouldBe Array(2, 1, 4, 3, 5)
swapAdjacentYield(Array()) shouldBe Array()
swapAdjacentYield(Array(1)) shouldBe Array(1)
swapAdjacentYield(Array(1, 2, 3, 4)) shouldBe Array(2, 1, 4, 3)
}
it should "produce new positives then negatives array" in {
val a = Array(1, -2, 3, 0, 5, -4)
val b: Array[Int] = positivesThenNegatives(a)
b shouldNot be theSameInstanceAs a
b shouldBe Array(1, 3, 5, -2, 0, -4)
positivesThenNegatives(Array()) shouldBe Array()
positivesThenNegatives(Array(1)) shouldBe Array(1)
positivesThenNegatives(Array(1, 2, 3, 4)) shouldBe Array(1, 2, 3, 4)
positivesThenNegatives(Array(-1, -2, 0, -4)) shouldBe Array(-1, -2, 0, -4)
}
it should "compute the average of an Array[Double]" in {
val a = Array(1.5, -1.5, 3, 0)
val b: Double = computeAverage(a)
b shouldBe 0.75
}
it should "reverse sort Array[Int] in place" in {
val a = Array(1, -1, 3, 0, 0, 2, 1)
val b: Array[Int] = reverseSortArray(a)
b shouldBe theSameInstanceAs(a)
b shouldBe Array(3, 2, 1, 1, 0, 0, -1)
}
it should "reverse sort ArrayBuffer[Int] in place" in {
val a = ArrayBuffer(1, -1, 3, 0, 2, 1)
val b: ArrayBuffer[Int] = reverseSortArrayBuffer(a)
b shouldBe theSameInstanceAs(a)
b shouldBe ArrayBuffer(3, 2, 1, 1, 0, -1)
}
it should "remove duplicates" in {
val a = Array(1, -1, 2, 0, 2, 1)
val b: Array[Int] = removeDuplicates(a)
b shouldNot be theSameInstanceAs a
b shouldBe Array(1, -1, 2, 0)
}
it should "drop negatives except first" in {
val a = ArrayBuffer(1, -1, -3, 0, -2, 1, -1)
val b: ArrayBuffer[Int] = dropNegativesExceptFirst(a)
b shouldBe theSameInstanceAs(a)
b shouldBe ArrayBuffer(1, -1, 0, 1)
}
it should "return America time zones" in {
val a: Array[String] = americaTimeZones
a.length should be > 0
a.head(0) shouldBe 'A'
a.last(0) shouldBe 'Y'
}
it should "return java List as Scala Buffer" in {
val a: mutable.Buffer[String] = javaListAsScalaBuffer
a.length should be > 0
}
}