add test cases

This commit is contained in:
Daniel Langbein 2024-12-14 18:32:16 +00:00
parent 387194ffad
commit a8471c9b98
Signed by: langfingaz
GPG Key ID: 6C47C753F0823002
4 changed files with 54 additions and 7 deletions
ueb08/app/src
main/java/org/example
test/java/org/example

@ -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 {
}

@ -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));
}
}