StoneTrayTest fertig
git-svn-id: svn://sunsvr01.isp.uni-luebeck.de/swproj13/trunk@33 72836036-5685-4462-b002-a69064685172
This commit is contained in:
parent
482d7e7f86
commit
7b80311556
2 changed files with 167 additions and 1 deletions
|
@ -1,7 +1,12 @@
|
|||
package jrummikub.model;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import jrummikub.util.Pair;
|
||||
|
||||
/** A StoneTray is a collection of positioned objects (for example {@link Stone}s or {@link StoneSet}s. */
|
||||
public abstract class StoneTray<E extends Sizeable> {
|
||||
public class StoneTray<E extends Sizeable> implements Iterable<Pair<E, Position>> {
|
||||
protected List<E> objects;
|
||||
protected List<Position> positions;
|
||||
|
||||
|
@ -27,5 +32,18 @@ public abstract class StoneTray<E extends Sizeable> {
|
|||
public void drop(E object, Position position) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
public Position getPosition (E object) {
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<Pair<E, Position>> iterator() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,154 @@
|
|||
package jrummikub.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import jrummikub.util.Pair;
|
||||
import static jrummikub.model.StoneColor.*;
|
||||
|
||||
import org.junit.*;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class StoneTrayTest {
|
||||
class Thing implements Sizeable {
|
||||
private float width;
|
||||
private float height;
|
||||
|
||||
public Thing(float width, float height) {
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
|
||||
private StoneTray<Thing> testTray;
|
||||
|
||||
@Before
|
||||
public void createTray() {
|
||||
testTray = new StoneTray<Thing>();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRightDrop() {
|
||||
Thing firstThing = new Thing(3, 4);
|
||||
testTray.drop(firstThing, new Position(5, 23));
|
||||
Thing secondThing = new Thing(5, 8);
|
||||
testTray.drop(secondThing, new Position(42, 8.5f));
|
||||
Position firstPosition = testTray.getPosition(firstThing);
|
||||
Position secondPosition = testTray.getPosition(secondThing);
|
||||
assertEquals(5, firstPosition.getX(), 0.00001);
|
||||
assertEquals(23, firstPosition.getY(), 0.00001);
|
||||
assertEquals(42, secondPosition.getX(), 0.00001);
|
||||
assertEquals(8.5, secondPosition.getY(), 0.00001);
|
||||
}
|
||||
|
||||
// Leftshift
|
||||
@Test
|
||||
public void testLeftDrop() {
|
||||
Thing firstThing = new Thing(5, 5);
|
||||
testTray.drop(firstThing, new Position(0, 0));
|
||||
Thing secondThing = new Thing(3, 3);
|
||||
testTray.drop(secondThing, new Position(4, 1));
|
||||
Position firstPosition = testTray.getPosition(firstThing);
|
||||
Position secondPosition = testTray.getPosition(secondThing);
|
||||
assertEquals(-1, firstPosition.getX(), 0.00001);
|
||||
assertEquals(0, firstPosition.getY(), 0.00001);
|
||||
assertEquals(4, secondPosition.getX(), 0.00001);
|
||||
assertEquals(1, secondPosition.getY(), 0.00001);
|
||||
}
|
||||
|
||||
// Rightshift
|
||||
@Test
|
||||
public void testRightDrop() {
|
||||
Thing firstThing = new Thing(5, 5);
|
||||
testTray.drop(firstThing, new Position(0, 0));
|
||||
Thing secondThing = new Thing(3, 3);
|
||||
testTray.drop(secondThing, new Position(-2, 1));
|
||||
Position firstPosition = testTray.getPosition(firstThing);
|
||||
Position secondPosition = testTray.getPosition(secondThing);
|
||||
assertEquals(1, firstPosition.getX(), 0.00001);
|
||||
assertEquals(0, firstPosition.getY(), 0.00001);
|
||||
assertEquals(-2, secondPosition.getX(), 0.00001);
|
||||
assertEquals(1, secondPosition.getY(), 0.00001);
|
||||
}
|
||||
|
||||
// Upshift
|
||||
@Test
|
||||
public void testUpDrop() {
|
||||
Thing firstThing = new Thing(5, 5);
|
||||
testTray.drop(firstThing, new Position(0, 0));
|
||||
Thing secondThing = new Thing(3, 3);
|
||||
testTray.drop(secondThing, new Position(1, 4));
|
||||
Position firstPosition = testTray.getPosition(firstThing);
|
||||
Position secondPosition = testTray.getPosition(secondThing);
|
||||
assertEquals(0, firstPosition.getX(), 0.00001);
|
||||
assertEquals(-1, firstPosition.getY(), 0.00001);
|
||||
assertEquals(1, secondPosition.getX(), 0.00001);
|
||||
assertEquals(4, secondPosition.getY(), 0.00001);
|
||||
}
|
||||
|
||||
// Downshift
|
||||
@Test
|
||||
public void testDownDrop() {
|
||||
Thing firstThing = new Thing(5, 5);
|
||||
testTray.drop(firstThing, new Position(0, 0));
|
||||
Thing secondThing = new Thing(3, 3);
|
||||
testTray.drop(secondThing, new Position(1, -2));
|
||||
Position firstPosition = testTray.getPosition(firstThing);
|
||||
Position secondPosition = testTray.getPosition(secondThing);
|
||||
assertEquals(0, firstPosition.getX(), 0.00001);
|
||||
assertEquals(1, firstPosition.getY(), 0.00001);
|
||||
assertEquals(1, secondPosition.getX(), 0.00001);
|
||||
assertEquals(-2, secondPosition.getY(), 0.00001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrongPickUp() {
|
||||
Thing firstThing = new Thing(5, 5);
|
||||
testTray.drop(firstThing, new Position(0, 0));
|
||||
Position testPosition = new Position(-2, -2);
|
||||
assertNull(testTray.pickUp(testPosition));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRightPickUp() {
|
||||
Thing firstThing = new Thing(5, 5);
|
||||
testTray.drop(firstThing, new Position(0, 0));
|
||||
Thing secondThing = new Thing(3, 3);
|
||||
testTray.drop(secondThing, new Position(-5, -5));
|
||||
Position testPosition = new Position(3, 3);
|
||||
assertSame(testTray.pickUp(testPosition), firstThing);
|
||||
assertNull(testTray.pickUp(testPosition));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testIterate() {
|
||||
List<Thing> testThings = new ArrayList<Thing>();
|
||||
List<Position> testPositions = new ArrayList<Position>();
|
||||
for (int i = 0; i < 4; i++) {
|
||||
Thing newThing = new Thing(1, 1);
|
||||
Position newPosition = new Position(i, i);
|
||||
testThings.add(newThing);
|
||||
testPositions.add(newPosition);
|
||||
testTray.drop(newThing, newPosition);
|
||||
}
|
||||
for (Pair<Thing, Position> i : testTray) {
|
||||
int index = testThings.indexOf(i.getFirst());
|
||||
assertFalse(index == -1);
|
||||
assertEquals(i.getSecond(), testPositions.get(index));
|
||||
testThings.remove(index);
|
||||
testPositions.remove(index);
|
||||
}
|
||||
assertTrue(testThings.isEmpty());
|
||||
assertTrue(testPositions.isEmpty());
|
||||
}
|
||||
}
|
||||
|
|
Reference in a new issue