add test cases
This commit is contained in:
parent
387194ffad
commit
a8471c9b98
ueb08/app/src
@ -1,5 +1,7 @@
|
||||
package org.example;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
public enum DiceEyes {
|
||||
ONE(1),
|
||||
TWO(2),
|
||||
@ -8,6 +10,7 @@ public enum DiceEyes {
|
||||
FIVE(5),
|
||||
SIX(6);
|
||||
|
||||
static Random random = new Random();
|
||||
private final int i;
|
||||
|
||||
DiceEyes(int i) {
|
||||
@ -16,7 +19,7 @@ public enum DiceEyes {
|
||||
|
||||
public static DiceEyes random(){
|
||||
// In range [0,1)
|
||||
double randDouble = Math.random();
|
||||
double randDouble = random.nextDouble();
|
||||
// In range [0,6)
|
||||
randDouble *= 6d;
|
||||
// Round floor -> One of the integers {0, 1, 2, 3, 4, 5}
|
||||
|
@ -10,7 +10,7 @@ public class Player {
|
||||
private final String name;
|
||||
private int totalScore = 0;
|
||||
private int roundScore = 0;
|
||||
private Dice[] dices = new Dice[]{
|
||||
Dice[] dices = new Dice[]{
|
||||
new Dice(), new Dice(), new Dice(),
|
||||
new Dice(), new Dice(), new Dice()
|
||||
};
|
||||
@ -112,6 +112,8 @@ public class Player {
|
||||
}
|
||||
|
||||
private String dicesToString() {
|
||||
return Arrays.stream(dices).map(Dice::toString).collect(Collectors.joining(" "));
|
||||
return Arrays.stream(dices)
|
||||
.map(Dice::toString)
|
||||
.collect(Collectors.joining(" "));
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +0,0 @@
|
||||
package org.example;
|
||||
|
||||
class GameTest {
|
||||
}
|
46
ueb08/app/src/test/java/org/example/PlayerTest.java
Normal file
46
ueb08/app/src/test/java/org/example/PlayerTest.java
Normal file
@ -0,0 +1,46 @@
|
||||
package org.example;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
class PlayerTest {
|
||||
@Test
|
||||
void testRandomness() {
|
||||
// Reproducible random values o.o
|
||||
DiceEyes.random = new Random(1337);
|
||||
Player player;
|
||||
String expected;
|
||||
|
||||
player = new Player("Player1");
|
||||
expected = "0 + 0: 4 5 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
player = new Player("Player2");
|
||||
expected = "0 + 0: 1 2 5 4 2 3 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
player = new Player("Player3");
|
||||
expected = "0 + 0: 1 2 6 1 4 2 ";
|
||||
assertEquals(expected, player.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testToggleAlreadyFixed() {
|
||||
// Reproducible random values o.o
|
||||
DiceEyes.random = new Random(1337);
|
||||
Player player = new Player("Player1");
|
||||
|
||||
int diceIdx = 1;
|
||||
|
||||
// Manually fix one dice
|
||||
player.dices[diceIdx].fix();
|
||||
String expected = "0 + 50: 4 [5] 6 6 2 6 ";
|
||||
assertEquals(expected, player.toString());
|
||||
|
||||
assertThrows(IllegalArgumentException.class, () -> player.toggleMark(diceIdx));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user