Added seeds support to random util:

* all xmage code uses shared util to generate random values;
 * tests can uses seeds to repeat "random" results like deck builds or AI plays;
This commit is contained in:
Oleg Agafonov 2018-12-30 03:52:30 +04:00
parent 52df594396
commit 2ebad63595
13 changed files with 415 additions and 265 deletions

View file

@ -1,19 +1,11 @@
package org.mage.test.load;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
import mage.cards.Card;
import mage.cards.Sets;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.cards.repository.CardScanner;
import mage.constants.*;
import mage.game.match.MatchOptions;
import mage.player.ai.ComputerPlayer;
import mage.players.PlayerType;
import mage.remote.Connection;
import mage.remote.MageRemoteException;
@ -22,15 +14,23 @@ import mage.remote.SessionImpl;
import mage.util.RandomUtil;
import mage.view.*;
import org.apache.log4j.Logger;
import org.junit.*;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.utils.DeckTestUtils;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.*;
/**
* Intended to test Mage server under different load patterns.
*
* <p>
* These tests do use server started separately, so Mage server should be
* started before running them. In case you want to debug these tests, use
* -Ddebug.mage that would disable client-server request timeout.
*
* <p>
* Then it's also better to use -Xms256M -Xmx512M JVM options for these stests.
*
* @author JayDi85
@ -55,43 +55,50 @@ public class LoadTest {
Deck deck;
deck = generateRandomDeck("G", false);
deck = DeckTestUtils.buildRandomDeck("G", false);
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in G",
card.getColorIdentity().isGreen());
}
deck = generateRandomDeck("U", false);
deck = DeckTestUtils.buildRandomDeck("U", false);
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in U",
card.getColorIdentity().isBlue());
}
deck = generateRandomDeck("BR", false);
deck = DeckTestUtils.buildRandomDeck("BR", false);
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in BR",
card.getColorIdentity().isBlack() || card.getColorIdentity().isRed());
}
deck = generateRandomDeck("BUG", false);
deck = DeckTestUtils.buildRandomDeck("BUG", false);
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in BUG",
card.getColorIdentity().isBlack() || card.getColorIdentity().isBlue() || card.getColorIdentity().isGreen());
}
// lands
deck = generateRandomDeck("UR", true);
deck = DeckTestUtils.buildRandomDeck("UR", true);
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in UR",
card.getColorIdentity().isBlue() || card.getColorIdentity().isRed());
Assert.assertEquals("card " + card.getName() + " must be basic land ", Rarity.LAND, card.getRarity());
}
deck = generateRandomDeck("B", true);
deck = DeckTestUtils.buildRandomDeck("B", true);
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in B", card.getColorIdentity().isBlack());
Assert.assertEquals("card " + card.getName() + " must be basic land ", Rarity.LAND, card.getRarity());
}
// allowed sets
deck = DeckTestUtils.buildRandomDeck("B", true, "GRN");
for (Card card : deck.getCards()) {
Assert.assertTrue("card " + card.getName() + " color " + card.getColorIdentity().toString() + " must be in B", card.getColorIdentity().isBlack());
Assert.assertEquals("card " + card.getName() + " have wrong set code " + card.getExpansionSetCode(), "GRN", card.getExpansionSetCode());
}
}
@Test
@ -136,7 +143,7 @@ public class LoadTest {
UUID tableId = game.getTableId();
Assert.assertEquals(player1.userName, game.getControllerName());
DeckCardLists deckList = createSimpleDeck("GR", true);
DeckCardLists deckList = DeckTestUtils.buildRandomDeckAndInitCards("GR", true);
Optional<TableView> checkGame;
/*
@ -181,9 +188,9 @@ public class LoadTest {
}
}
@Test
@Ignore
public void test_TwoAIPlayGameUntilEnd() {
public void playTwoAIGame(String deckColors, String deckAllowedSets) {
Assert.assertFalse("need deck colors", deckColors.isEmpty());
Assert.assertFalse("need allowed sets", deckAllowedSets.isEmpty());
// monitor and game source
LoadPlayer monitor = new LoadPlayer("monitor");
@ -194,7 +201,7 @@ public class LoadTest {
TableView game = monitor.session.createTable(monitor.roomID, gameOptions);
UUID tableId = game.getTableId();
DeckCardLists deckList = createSimpleDeck("GR", false);
DeckCardLists deckList = DeckTestUtils.buildRandomDeckAndInitCards(deckColors, false, deckAllowedSets);
Optional<TableView> checkGame;
// join AI
@ -225,7 +232,6 @@ public class LoadTest {
for (PlayerView p : gameView.getPlayers()) {
logger.info(p.getName() + " - Life=" + p.getLife() + "; Lib=" + p.getLibraryCount());
}
}
try {
@ -236,6 +242,31 @@ public class LoadTest {
}
}
@Test
@Ignore
public void test_TwoAIPlayGame_One() {
playTwoAIGame("GR", "GRN");
}
@Test
@Ignore
public void test_TwoAIPlayGame_Multiple() {
// save random seeds for repeated results
Integer gamesAmount = 1000;
List<Integer> seedsList = new ArrayList<>();
for (int i = 1; i <= gamesAmount; i++) {
seedsList.add(RandomUtil.nextInt());
}
for (int i = 1; i <= 1000; i++) {
long randomSeed = seedsList.get(i);
logger.info("RANDOM seed: " + randomSeed);
RandomUtil.setSeed(randomSeed);
playTwoAIGame("WGUBR", "SWS");
}
}
@Test
@Ignore
public void test_GameThread() {
@ -244,8 +275,8 @@ public class LoadTest {
LoadGame game = new LoadGame(
"game",
"thread",
createSimpleDeck("GR", true),
createSimpleDeck("GR", true)
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, ""),
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, "")
);
game.gameStart();
@ -268,8 +299,8 @@ public class LoadTest {
LoadGame game = new LoadGame(
"game",
"thread",
createSimpleDeck("GR", true),
createSimpleDeck("GR", true)
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, ""),
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, "")
);
game.gameStart();
game.gameEnd(true); // abort -- close client thread
@ -284,8 +315,8 @@ public class LoadTest {
LoadGame game = new LoadGame(
"game",
"thread",
createSimpleDeck("GR", false),
createSimpleDeck("GR", false)
DeckTestUtils.buildRandomDeckAndInitCards("GR", false, ""),
DeckTestUtils.buildRandomDeckAndInitCards("GR", false, "")
);
game.gameStart();
@ -301,8 +332,8 @@ public class LoadTest {
LoadGame game = new LoadGame(
"game",
"thread",
createSimpleDeck("GR", true),
createSimpleDeck("GR", true)
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, ""),
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, "")
);
game.gameStart();
@ -334,8 +365,8 @@ public class LoadTest {
LoadGame game = new LoadGame(
"game" + i,
"game" + i,
createSimpleDeck("GR", true),
createSimpleDeck("GR", true)
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, ""),
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, "")
);
gamesList.add(game);
@ -426,31 +457,6 @@ public class LoadTest {
return createSimpleGameOptions("AI test game", gameTypeView, session, PlayerType.COMPUTER_MAD);
}
private Deck generateRandomDeck(String colors, boolean onlyBasicLands) {
logger.info("Building " + (onlyBasicLands ? "only lands" : "random") + " deck with colors: " + colors);
List<ColoredManaSymbol> allowedColors = new ArrayList<>();
for (int i = 0; i < colors.length(); i++) {
char c = colors.charAt(i);
allowedColors.add(ColoredManaSymbol.lookup(c));
}
List<Card> cardPool = Sets.generateRandomCardPool(45, allowedColors, onlyBasicLands);
return ComputerPlayer.buildDeck(cardPool, allowedColors, onlyBasicLands);
}
private DeckCardLists createSimpleDeck(String colors, boolean onlyBasicLands) {
Deck deck = generateRandomDeck(colors, onlyBasicLands);
DeckCardLists deckList = new DeckCardLists();
for (Card card : deck.getCards()) {
CardInfo cardInfo = CardRepository.instance.findCard(card.getExpansionSetCode(), card.getCardNumber());
if (cardInfo != null) {
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
return deckList;
}
private class LoadPlayer {
String userName;
@ -546,8 +552,8 @@ public class LoadTest {
public LoadGame(String gameName, String playerPrefix) {
this(gameName, playerPrefix,
createSimpleDeck("GR", true),
createSimpleDeck("GR", true)
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, ""),
DeckTestUtils.buildRandomDeckAndInitCards("GR", true, "")
);
}

View file

@ -1,18 +1,6 @@
package org.mage.test.player;
import java.io.Serializable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import mage.abilities.Ability;
import mage.abilities.ActivatedAbility;
import mage.abilities.Mode;
import mage.abilities.Modes;
import mage.abilities.TriggeredAbility;
import mage.abilities.*;
import mage.abilities.common.PassAbility;
import mage.abilities.costs.mana.GenericManaCost;
import mage.cards.Card;
@ -30,9 +18,12 @@ import mage.players.Player;
import mage.target.Target;
import mage.target.TargetAmount;
import mage.target.TargetCard;
import mage.util.RandomUtil;
import java.io.Serializable;
import java.util.*;
/**
*
* plays randomly
*
* @author BetaSteward_at_googlemail.com
@ -40,7 +31,6 @@ import mage.target.TargetCard;
public class RandomPlayer extends ComputerPlayer {
private boolean isSimulatedPlayer;
private static Random rnd = new Random();
private int actionCount = 0;
protected PassAbility pass = new PassAbility();
@ -89,21 +79,21 @@ public class RandomPlayer extends ComputerPlayer {
if (playables.size() == 1) {
ability = playables.get(0);
} else {
ability = playables.get(rnd.nextInt(playables.size()));
ability = playables.get(RandomUtil.nextInt(playables.size()));
}
List<Ability> options = getPlayableOptions(ability, game);
if (!options.isEmpty()) {
if (options.size() == 1) {
ability = options.get(0);
} else {
ability = options.get(rnd.nextInt(options.size()));
ability = options.get(RandomUtil.nextInt(options.size()));
}
}
if (!ability.getManaCosts().getVariableCosts().isEmpty()) {
int amount = getAvailableManaProducers(game).size() - ability.getManaCosts().convertedManaCost();
if (amount > 0) {
ability = ability.copy();
ability.getManaCostsToPay().add(new GenericManaCost(rnd.nextInt(amount)));
ability.getManaCostsToPay().add(new GenericManaCost(RandomUtil.nextInt(amount)));
}
}
// check if ability kills player, if not then it's ok to play
@ -142,7 +132,7 @@ public class RandomPlayer extends ComputerPlayer {
if (options.size() == 1) {
ability = options.get(0);
} else {
ability = options.get(rnd.nextInt(options.size()));
ability = options.get(RandomUtil.nextInt(options.size()));
}
}
if (ability.isUsesStack()) {
@ -170,7 +160,7 @@ public class RandomPlayer extends ComputerPlayer {
List<Permanent> attackersList = super.getAvailableAttackers(defenderId, game);
//use binary digits to calculate powerset of attackers
int powerElements = (int) Math.pow(2, attackersList.size());
int value = rnd.nextInt(powerElements);
int value = RandomUtil.nextInt(powerElements);
StringBuilder binary = new StringBuilder();
binary.append(Integer.toBinaryString(value));
while (binary.length() < attackersList.size()) {
@ -196,7 +186,7 @@ public class RandomPlayer extends ComputerPlayer {
List<Permanent> blockers = getAvailableBlockers(game);
for (Permanent blocker : blockers) {
int check = rnd.nextInt(numGroups + 1);
int check = RandomUtil.nextInt(numGroups + 1);
if (check < numGroups) {
CombatGroup group = game.getCombat().getGroups().get(check);
if (!group.getAttackers().isEmpty()) {
@ -222,7 +212,7 @@ public class RandomPlayer extends ComputerPlayer {
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = rnd.nextInt(possibleTargets.size());
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
@ -237,7 +227,7 @@ public class RandomPlayer extends ComputerPlayer {
return false;
}
if (!target.isRequired(source)) {
if (rnd.nextInt(possibleTargets.size() + 1) == 0) {
if (RandomUtil.nextInt(possibleTargets.size() + 1) == 0) {
return false;
}
}
@ -246,7 +236,7 @@ public class RandomPlayer extends ComputerPlayer {
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = rnd.nextInt(possibleTargets.size());
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
@ -275,7 +265,7 @@ public class RandomPlayer extends ComputerPlayer {
return !false;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = rnd.nextInt(possibleTargets.size());
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
@ -309,7 +299,7 @@ public class RandomPlayer extends ComputerPlayer {
return !target.isRequired(source);
}
if (!target.isRequired(source)) {
if (rnd.nextInt(possibleTargets.size() + 1) == 0) {
if (RandomUtil.nextInt(possibleTargets.size() + 1) == 0) {
return false;
}
}
@ -318,28 +308,28 @@ public class RandomPlayer extends ComputerPlayer {
return true;
}
Iterator<UUID> it = possibleTargets.iterator();
int targetNum = rnd.nextInt(possibleTargets.size());
int targetNum = RandomUtil.nextInt(possibleTargets.size());
UUID targetId = it.next();
for (int i = 0; i < targetNum; i++) {
targetId = it.next();
}
target.addTarget(targetId, rnd.nextInt(target.getAmountRemaining()) + 1, source, game);
target.addTarget(targetId, RandomUtil.nextInt(target.getAmountRemaining()) + 1, source, game);
return true;
}
@Override
public boolean chooseMulligan(Game game) {
return rnd.nextBoolean();
return RandomUtil.nextBoolean();
}
@Override
public boolean chooseUse(Outcome outcome, String message, Ability source, Game game) {
return rnd.nextBoolean();
return RandomUtil.nextBoolean();
}
@Override
public boolean choosePile(Outcome outcome, String message, List<? extends Card> pile1, List<? extends Card> pile2, Game game) {
return rnd.nextBoolean();
return RandomUtil.nextBoolean();
}
@Override
@ -350,12 +340,12 @@ public class RandomPlayer extends ComputerPlayer {
@Override
public int chooseReplacementEffect(Map<String, String> rEffects, Game game) {
return rnd.nextInt(rEffects.size());
return RandomUtil.nextInt(rEffects.size());
}
@Override
public TriggeredAbility chooseTriggeredAbility(List<TriggeredAbility> abilities, Game game) {
return abilities.get(rnd.nextInt(abilities.size()));
return abilities.get(RandomUtil.nextInt(abilities.size()));
}
@Override
@ -365,7 +355,7 @@ public class RandomPlayer extends ComputerPlayer {
if (modes.size() == 1) {
return mode;
}
int modeNum = rnd.nextInt(modes.getAvailableModes(source, game).size());
int modeNum = RandomUtil.nextInt(modes.getAvailableModes(source, game).size());
for (int i = 0; i < modeNum; i++) {
mode = it.next();
}
@ -374,12 +364,12 @@ public class RandomPlayer extends ComputerPlayer {
@Override
public UUID chooseAttackerOrder(List<Permanent> attackers, Game game) {
return attackers.get(rnd.nextInt(attackers.size())).getId();
return attackers.get(RandomUtil.nextInt(attackers.size())).getId();
}
@Override
public UUID chooseBlockerOrder(List<Permanent> blockers, CombatGroup combatGroup, List<UUID> blockerOrder, Game game) {
return blockers.get(rnd.nextInt(blockers.size())).getId();
return blockers.get(RandomUtil.nextInt(blockers.size())).getId();
}
@Override
@ -392,8 +382,8 @@ public class RandomPlayer extends ComputerPlayer {
targetId = targets.get(0);
amount = remainingDamage;
} else {
targetId = targets.get(rnd.nextInt(targets.size()));
amount = rnd.nextInt(damage + 1);
targetId = targets.get(RandomUtil.nextInt(targets.size()));
amount = RandomUtil.nextInt(damage + 1);
}
Permanent permanent = game.getPermanent(targetId);
if (permanent != null) {
@ -412,7 +402,7 @@ public class RandomPlayer extends ComputerPlayer {
@Override
public int getAmount(int min, int max, String message, Game game) {
return rnd.nextInt(max - min) + min;
return RandomUtil.nextInt(max - min) + min;
}
}

View file

@ -1,11 +1,5 @@
package org.mage.test.serverside;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import mage.cards.Card;
import mage.cards.Sets;
import mage.cards.decks.Deck;
@ -19,10 +13,17 @@ import mage.game.TwoPlayerDuel;
import mage.player.ai.ComputerPlayer;
import mage.players.Player;
import mage.players.PlayerType;
import mage.util.RandomUtil;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.serverside.base.MageTestBase;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
/**
* @author ayratn
*/
@ -79,7 +80,7 @@ public class PlayGameTest extends MageTestBase {
}
private Deck generateRandomDeck() {
String selectedColors = colorChoices.get(new Random().nextInt(colorChoices.size())).toUpperCase(Locale.ENGLISH);
String selectedColors = colorChoices.get(RandomUtil.nextInt(colorChoices.size())).toUpperCase(Locale.ENGLISH);
List<ColoredManaSymbol> allowedColors = new ArrayList<>();
logger.info("Building deck with colors: " + selectedColors);
for (int i = 0; i < selectedColors.length(); i++) {

View file

@ -1,11 +1,5 @@
package org.mage.test.serverside;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import mage.cards.Card;
import mage.cards.Sets;
import mage.cards.decks.Deck;
@ -18,10 +12,17 @@ import mage.game.GameOptions;
import mage.game.TwoPlayerDuel;
import mage.player.ai.ComputerPlayer;
import mage.players.Player;
import mage.util.RandomUtil;
import org.junit.Ignore;
import org.junit.Test;
import org.mage.test.serverside.base.MageTestBase;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
/**
* @author ayratn
*/
@ -70,7 +71,7 @@ public class TestPlayRandomGame extends MageTestBase {
}
private Deck generateRandomDeck() {
String selectedColors = colorChoices.get(new Random().nextInt(colorChoices.size())).toUpperCase(Locale.ENGLISH);
String selectedColors = colorChoices.get(RandomUtil.nextInt(colorChoices.size())).toUpperCase(Locale.ENGLISH);
List<ColoredManaSymbol> allowedColors = new ArrayList<>();
logger.info("Building deck with colors: " + selectedColors);
for (int i = 0; i < selectedColors.length(); i++) {

View file

@ -1,33 +1,28 @@
package org.mage.test.serverside.rating;
import org.junit.Assert;
import mage.server.rating.GlickoRating;
import mage.server.rating.GlickoRatingSystem;
import mage.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.Random;
/**
*
* @author Quercitron
*/
public class GlickoRatingSystemTest {
private final Random random = new Random();
@Test
public void testRatingsAreEqualAfterDraws() {
GlickoRatingSystem glickoRatingSystem = new GlickoRatingSystem();
int count = 1000;
for (int i = 0; i < count; i++) {
double startRating = random.nextDouble() * 2500 + 500;
double startRatingDeviation = Math.min(random.nextDouble() * 300 + 100, GlickoRatingSystem.BaseRD);
double startRating = RandomUtil.nextDouble() * 2500 + 500;
double startRatingDeviation = Math.min(RandomUtil.nextDouble() * 300 + 100, GlickoRatingSystem.BaseRD);
GlickoRating player1 = new GlickoRating(startRating, startRatingDeviation, 1);
GlickoRating player2 = new GlickoRating(startRating, startRatingDeviation, 1);
int gamesCount = random.nextInt(50) + 1;
int gamesCount = RandomUtil.nextInt(50) + 1;
for (int j = 0; j < gamesCount; j++) {
glickoRatingSystem.updateRating(player1, player2, 0.5, j + 2);
@ -43,21 +38,20 @@ public class GlickoRatingSystemTest {
int count = 1000;
for (int i = 0; i < count; i++) {
double startRating1 = random.nextDouble() * 2500 + 500;
double startRating2 = random.nextDouble() * 2500 + 500;
double startRatingDeviation = Math.min(random.nextDouble() * 300 + 100, GlickoRatingSystem.BaseRD);
double startRating1 = RandomUtil.nextDouble() * 2500 + 500;
double startRating2 = RandomUtil.nextDouble() * 2500 + 500;
double startRatingDeviation = Math.min(RandomUtil.nextDouble() * 300 + 100, GlickoRatingSystem.BaseRD);
GlickoRating player1 = new GlickoRating(startRating1, startRatingDeviation, 1);
GlickoRating player2 = new GlickoRating(startRating2, startRatingDeviation, 1);
glickoRatingSystem.updateRating(player1, player2, random.nextDouble(), 1);
glickoRatingSystem.updateRating(player1, player2, RandomUtil.nextDouble(), 1);
Assert.assertEquals(player1.getRating() - startRating1, startRating2 - player2.getRating(), 1e-5);
Assert.assertEquals(player1.getRatingDeviation(), player2.getRatingDeviation(), 1e-5);
}
}
@Test
public void testExactResult1()
{
public void testExactResult1() {
GlickoRatingSystem glickoRatingSystem = new GlickoRatingSystem();
GlickoRating player1 = new GlickoRating(1500, 350, 1);
@ -73,8 +67,7 @@ public class GlickoRatingSystemTest {
}
@Test
public void testExactResult2()
{
public void testExactResult2() {
GlickoRatingSystem glickoRatingSystem = new GlickoRatingSystem();
GlickoRating player1 = new GlickoRating(1500, 350, 1);
@ -90,8 +83,7 @@ public class GlickoRatingSystemTest {
}
@Test
public void testExactResult3()
{
public void testExactResult3() {
GlickoRatingSystem glickoRatingSystem = new GlickoRatingSystem();
GlickoRating player1 = new GlickoRating(1500, 350, 1);
@ -107,8 +99,7 @@ public class GlickoRatingSystemTest {
}
@Test
public void testExactResult4()
{
public void testExactResult4() {
GlickoRatingSystem glickoRatingSystem = new GlickoRatingSystem();
GlickoRating player1 = new GlickoRating(1500, 250, 1);
@ -124,8 +115,7 @@ public class GlickoRatingSystemTest {
}
@Test
public void testExactResult5()
{
public void testExactResult5() {
GlickoRatingSystem glickoRatingSystem = new GlickoRatingSystem();
GlickoRating player1 = new GlickoRating(1500, 100, 1);

View file

@ -1,21 +1,24 @@
package org.mage.test.serverside.tournament;
import java.util.*;
import mage.game.tournament.*;
import mage.game.tournament.Round;
import mage.game.tournament.TournamentPairing;
import mage.game.tournament.TournamentPlayer;
import mage.game.tournament.pairing.RoundPairings;
import mage.game.tournament.pairing.SwissPairingMinimalWeightMatching;
import mage.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
import org.mage.test.stub.PlayerStub;
import org.mage.test.stub.TournamentStub;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
*
* @author Quercitron
*/
public class SwissPairingMinimalWeightMatchingTest {
@ -264,7 +267,6 @@ public class SwissPairingMinimalWeightMatchingTest {
}
private void SimulateTournament(int playersCount, int roundsCount) {
Random rnd = new Random();
List<TournamentPlayer> players = new ArrayList<>();
for (int i = 0; i < playersCount; i++) {
@ -294,7 +296,7 @@ public class SwissPairingMinimalWeightMatchingTest {
playedPairs.add(pairing);
round.addPairing(pairing);
if (rnd.nextBoolean()) {
if (RandomUtil.nextBoolean()) {
pairing.getPlayer1().setPoints(pairing.getPlayer1().getPoints() + 3);
} else {
pairing.getPlayer2().setPoints(pairing.getPlayer2().getPoints() + 3);

View file

@ -0,0 +1,63 @@
package org.mage.test.utils;
import mage.cards.Card;
import mage.cards.Sets;
import mage.cards.decks.Deck;
import mage.cards.decks.DeckCardInfo;
import mage.cards.decks.DeckCardLists;
import mage.cards.repository.CardInfo;
import mage.cards.repository.CardRepository;
import mage.constants.ColoredManaSymbol;
import mage.player.ai.ComputerPlayer;
import java.util.ArrayList;
import java.util.List;
/**
* @author JayDi85
*/
public class DeckTestUtils {
public static Deck buildRandomDeck(String colors, boolean onlyBasicLands) {
return buildRandomDeck(colors, onlyBasicLands, "");
}
public static Deck buildRandomDeck(String colors, boolean onlyBasicLands, String allowedSets) {
List<ColoredManaSymbol> allowedColors = new ArrayList<>();
for (int i = 0; i < colors.length(); i++) {
char c = colors.charAt(i);
allowedColors.add(ColoredManaSymbol.lookup(c));
}
List<String> allowedList = new ArrayList<>();
if (allowedSets != null && !allowedSets.isEmpty()) {
String[] codes = allowedSets.split(",");
for (String code : codes) {
if (!code.trim().isEmpty()) {
allowedList.add(code.trim());
}
}
}
List<Card> cardPool = Sets.generateRandomCardPool(45, allowedColors, onlyBasicLands, allowedList);
return ComputerPlayer.buildDeck(cardPool, allowedColors, onlyBasicLands);
}
public static DeckCardLists buildRandomDeckAndInitCards(String colors, boolean onlyBasicLands) {
return buildRandomDeckAndInitCards(colors, onlyBasicLands, "");
}
public static DeckCardLists buildRandomDeckAndInitCards(String colors, boolean onlyBasicLands, String allowedSets) {
Deck deck = buildRandomDeck(colors, onlyBasicLands, allowedSets);
DeckCardLists deckList = new DeckCardLists();
for (Card card : deck.getCards()) {
CardInfo cardInfo = CardRepository.instance.findCard(card.getExpansionSetCode(), card.getCardNumber());
if (cardInfo != null) {
deckList.getCards().add(new DeckCardInfo(cardInfo.getName(), cardInfo.getCardNumber(), cardInfo.getSetCode()));
}
}
return deckList;
}
}

View file

@ -0,0 +1,60 @@
package org.mage.test.utils;
import mage.cards.decks.DeckCardLists;
import mage.util.RandomUtil;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author JayDi85
*/
public class RandomTest {
@Test
public void test_SeedAndSameResults() {
RandomUtil.setSeed(123);
List<Integer> listSameA = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
listSameA.add(RandomUtil.nextInt());
}
RandomUtil.setSeed(321);
List<Integer> listDifferent = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
listDifferent.add(RandomUtil.nextInt());
}
RandomUtil.setSeed(123);
List<Integer> listSameB = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
listSameB.add(RandomUtil.nextInt());
}
Assert.assertEquals("same seed must have same random values", listSameA.stream().mapToInt(Integer::intValue).sum(), listSameB.stream().mapToInt(Integer::intValue).sum());
Assert.assertNotEquals("different seed must have different random values", listSameA.stream().mapToInt(Integer::intValue).sum(), listDifferent.stream().mapToInt(Integer::intValue).sum());
}
@Test
public void test_SeedAndSameRandomDecks() {
RandomUtil.setSeed(123);
DeckCardLists listSameA = DeckTestUtils.buildRandomDeckAndInitCards("WGUBR", false, "GRN");
String infoSameA = listSameA.getCards().stream().map(c -> (c.getSetCode() + "-" + c.getCardName())).collect(Collectors.joining(","));
RandomUtil.setSeed(321);
DeckCardLists listDifferent = DeckTestUtils.buildRandomDeckAndInitCards("WGUBR", false, "GRN");
String infoDifferent = listDifferent.getCards().stream().map(c -> (c.getSetCode() + "-" + c.getCardName())).collect(Collectors.joining(","));
RandomUtil.setSeed(123);
DeckCardLists listSameB = DeckTestUtils.buildRandomDeckAndInitCards("WGUBR", false, "GRN");
String infoSameB = listSameB.getCards().stream().map(c -> (c.getSetCode() + "-" + c.getCardName())).collect(Collectors.joining(","));
Assert.assertEquals("same seed must have same deck", infoSameA, infoSameB);
Assert.assertNotEquals("different seed must have different deck", infoSameA, infoDifferent);
}
}