From 55088e15a11b285b112a3f87eaef15b1e3654147 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 30 May 2011 20:49:36 +0200 Subject: Test different stone heap configurations with JUnit theories; allow creating stone heaps with different stone set numbers git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@329 72836036-5685-4462-b002-a69064685172 --- test/jrummikub/model/StoneHeapTest.java | 113 ++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 33 deletions(-) (limited to 'test/jrummikub/model') diff --git a/test/jrummikub/model/StoneHeapTest.java b/test/jrummikub/model/StoneHeapTest.java index ffe0cdf..811a2d1 100644 --- a/test/jrummikub/model/StoneHeapTest.java +++ b/test/jrummikub/model/StoneHeapTest.java @@ -1,54 +1,90 @@ package jrummikub.model; +import static org.junit.Assert.*; + import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; -import org.junit.*; -import static org.junit.Assert.*; +import jrummikub.util.Pair; + +import org.junit.experimental.theories.DataPoints; +import org.junit.experimental.theories.Theories; +import org.junit.experimental.theories.Theory; +import org.junit.runner.RunWith; /** * Tests for {@link StoneHeap} */ +@RunWith(Theories.class) public class StoneHeapTest { - private StoneHeap testHeap; - private GameSettings testSettings; - /** */ - @Before - public void createHeap() { - testHeap = new StoneHeap(testSettings = new GameSettings()); + private static Pair createHeap(GameSettings settings) { + return new Pair(settings, new StoneHeap(settings)); } - private int calculateTotalNumberOfStones() { + /** + * @return the data points + */ + @DataPoints + public static Pair[] createDataPoints() { + GameSettings testSettings1 = new GameSettings(); + + GameSettings testSettings2 = new GameSettings(); + testSettings2.setJokerNumber(10); + + GameSettings testSettings3 = new GameSettings(); + testSettings3.setStoneSetNumber(1); + + GameSettings testSettings4 = new GameSettings(); + testSettings4.setJokerNumber(5); + testSettings4.setStoneSetNumber(5); + + @SuppressWarnings("unchecked") + Pair[] ret = new Pair[4]; + ret[0] = createHeap(testSettings1); + ret[1] = createHeap(testSettings2); + ret[2] = createHeap(testSettings3); + ret[3] = createHeap(testSettings4); + + return ret; + } + + private int calculateTotalNumberOfStones(GameSettings testSettings) { int totalStones = testSettings.getHighestCard() * testSettings.getStoneSetNumber() - * testSettings.getStoneColors().size() - + testSettings.getJokerNumber(); + * testSettings.getStoneColors().size() + testSettings.getJokerNumber(); return totalStones; } /** * Is the right number of Stones in heap? + * + * @param data + * data */ - @Test - public void fullStoneHeap() { - assertEquals(calculateTotalNumberOfStones(), testHeap.heap.size()); + @Theory + public void fullStoneHeap(Pair data) { + assertEquals(calculateTotalNumberOfStones(data.getFirst()), + data.getSecond().heap.size()); } /** * Enough stones of each color in heap? + * + * @param data + * data */ - @Test - public void fullColor() { - int stonesOfAColor = testSettings.getHighestCard() - * testSettings.getStoneSetNumber(); + @Theory + public void fullColor(Pair data) { + int stonesOfAColor = data.getFirst().getHighestCard() + * data.getFirst().getStoneSetNumber(); Map counters = new HashMap(); for (StoneColor c : EnumSet.allOf(StoneColor.class)) { counters.put(c, 0); } - for (Stone i : testHeap.heap) { + for (Stone i : data.getSecond().heap) { if (i.isJoker()) continue; int count = counters.get(i.getColor()); @@ -61,29 +97,40 @@ public class StoneHeapTest { /** * Enough Jokers? + * + * @param data + * data */ - @Test - public void fullJoker() { + @Theory + public void fullJoker(Pair data) { int countJoker = 0; - for (Stone i : testHeap.heap) { + for (Stone i : data.getSecond().heap) { if (i.isJoker()) countJoker++; } - assertEquals(testSettings.getJokerNumber(), countJoker); + assertEquals(data.getFirst().getJokerNumber(), countJoker); } - /** */ - @Test - public void drawStoneTest() { - assertNotNull(testHeap.drawStone()); - assertEquals(calculateTotalNumberOfStones() - 1, testHeap.heap.size()); + /** + * @param data + * data + */ + @Theory + public void drawStoneTest(Pair data) { + assertNotNull(data.getSecond().drawStone()); + assertEquals(calculateTotalNumberOfStones(data.getFirst()) - 1, + data.getSecond().heap.size()); } - /** */ - @Test - public void drawStonesTest() { - List testStones = testHeap.drawStones(5); + /** + * @param data + * data + */ + @Theory + public void drawStonesTest(Pair data) { + List testStones = data.getSecond().drawStones(5); assertEquals(5, testStones.size()); - assertEquals(calculateTotalNumberOfStones() - 5, testHeap.heap.size()); + assertEquals(calculateTotalNumberOfStones(data.getFirst()) - 5, + data.getSecond().heap.size()); } } -- cgit v1.2.3