-
Notifications
You must be signed in to change notification settings - Fork 25
/
Chapter20Spec.scala
150 lines (119 loc) · 4.28 KB
/
Chapter20Spec.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
142
143
144
145
146
147
148
149
150
import Chapter20._
import java.awt.Color
import javax.imageio.ImageIO
import org.scalatest.{FlatSpec, Matchers}
class Chapter20Spec extends FlatSpec with Matchers {
"RandCalc" should "compute the average of random numbers" in {
//given
val n = 1000000
//when
val average: Double = RandCalc.calcAverageFor(n, useActors = true)
//then
average should be > 0.0
}
"ImageProgram" should "invert colors of the large image" in {
//given
val tmpFile = TestUtils.resourceToTmpFile("/scaladays.png")
val sourceImage = ImageIO.read(tmpFile)
//when
ImageProgram.invert(tmpFile, tmpFile)
//then
val invertedImage = ImageIO.read(tmpFile)
sourceImage.getWidth shouldBe invertedImage.getWidth
sourceImage.getHeight shouldBe invertedImage.getHeight
for (x <- 0 until sourceImage.getWidth) {
for (y <- 0 until sourceImage.getHeight) {
var col = new Color(sourceImage.getRGB(x, y), true)
col = new Color(255 - col.getRed, 255 - col.getGreen, 255 - col.getBlue)
invertedImage.getRGB(x, y) shouldBe col.getRGB
}
}
}
"WordsCountProgram" should "count matched words in all files and subdirectories" in {
//given
val dirPath = "src/main/"
val fileExtensions = List("txt", "html", "xhtml")
//when
val result: Int = WordsCountProgram.calcMatchedWords(dirPath, fileExtensions: _*)
//then
result shouldBe 3
}
"WordsPrintProgram" should "print matched words in all files and subdirectories" in {
//given
val dirPath = "src/main/"
val fileExtensions = List("txt", "html", "xhtml")
//when
val result: String = WordsPrintProgram.printMatchedWords(dirPath, fileExtensions: _*)
//then
result shouldBe """text
|text
|text
|""".stripMargin
}
"WordsPrintFilesProgram" should "print matched words in all files and subdirectories" in {
//given
val dirPath = "src/main/"
val fileExtensions = List("txt", "html", "xhtml")
//when
val result: String = WordsPrintFilesProgram.printMatchedWordsWithFiles(dirPath,
fileExtensions: _*)
//then
result shouldBe """found "text" in
|src/main/resources/Chapter16Task04.html
|src/main/resources/Chapter16Task10.xhtml
|src/main/resources/myfile.txt
|
|""".stripMargin
}
"ThreadActorsProgram" should "print threads count of different type of actors" in {
//when
val whileReceiveActorsCount = ThreadActorsProgram.calcActorThreads(whileReceiveActors = true)
println(s"whileReceiveActorsCount: $whileReceiveActorsCount")
val loopReactActorsCount = ThreadActorsProgram.calcActorThreads(whileReceiveActors = false)
println(s"loopReactActorsCount: $loopReactActorsCount")
//then
whileReceiveActorsCount should be > loopReactActorsCount
}
"WordsSupervisorProgram" should "supervise file workers and log any Exception(s)" in {
//given
val dirPath = "src/main/"
val fileExtensions = List("txt", "html", "xhtml")
//when
var result: Int = 0
val error = TestUtils.withOutput {
result = WordsSupervisorProgram.calcMatchedWords(dirPath, fileExtensions: _*)
}
//then
result shouldBe 2
error shouldBe "java.lang.IllegalArgumentException: requirement failed: " +
"Given path should represent file: src/main/resources/myfile.txt-not-exist\n\n"
}
"DeadlockProgram" should "deadlock on synchronous messages" in {
//when
val (millis, result: Int) = TestUtils.withTiming {
DeadlockProgram.run()
}
//then
result shouldBe -1
millis should be > 3000L
}
"SharedCounterProgram" should "update a shared counter" in {
//given
val dirPath = "src/main/"
val fileExtensions = List("txt", "html", "xhtml")
//when
SharedCounterProgram.counter = 0
val result: Int = SharedCounterProgram.calcMatchedWords(dirPath, fileExtensions: _*)
//then
result shouldBe 3
SharedCounterProgram.counter should not be result
}
"ChannelCalc" should "use channels to compute the average of random numbers" in {
//given
val n = 1000000
//when
val average: Double = ChannelCalc.calcAverageFor(n)
//then
average should be > 0.0
}
}