-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathGameTest.java
More file actions
53 lines (39 loc) · 1.17 KB
/
GameTest.java
File metadata and controls
53 lines (39 loc) · 1.17 KB
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
package trivia;
import org.junit.Test;
import victor.training.trivia.Game;
import victor.training.trivia.GameBetter;
import victor.training.trivia.IGame;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Random;
import static org.junit.Assert.assertEquals;
public class GameTest {
@Test
public void caracterizationTest() {
for (int seed = 1; seed < 10_000; seed++) {
String expectedOutput = extractOutput(new Random(seed), new Game());
String actualOutput = extractOutput(new Random(seed), new GameBetter());
assertEquals(expectedOutput, actualOutput);
}
}
private String extractOutput(Random rand, IGame aGame) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (PrintStream inmemory = new PrintStream(baos)) {
System.setOut(inmemory);
aGame.addPlayer("Chet");
aGame.addPlayer("Pat");
aGame.addPlayer("Sue");
boolean gameNotOver = false;
do {
aGame.roll(rand.nextInt(5) + 1);
if (rand.nextInt(9) == 7) {
aGame.wrongAnswer();
} else {
aGame.correctAnswer();
}
gameNotOver = !aGame.isGameOver();
} while (gameNotOver);
}
return baos.toString();
}
}