Commit 83390478 authored by mva021's avatar mva021
Browse files

Replaced tests

parent 60771cf7
......@@ -4,6 +4,7 @@ import inf101.v20.sem2.grid.Grid;
import inf101.v20.sem2.grid.GridDirection;
import inf101.v20.sem2.grid.GridLocationIterator;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.tools.Range;
/**
* Keeps track of a grid and where on this grid different Pieces are placed.
......@@ -137,4 +138,12 @@ public class GameBoard extends Grid<Player>{
}
return true;
}
public Iterable<Integer> rowIndices(){
return Range.range(numRows());
}
public Iterable<Integer> colIndices(){
return Range.range(numCols());
}
}
package inf101.v20.sem2.tools;
import java.util.Iterator;
import java.util.NoSuchElementException;
/**
* This class main purpose is to implement an Iterator that iterates over a range of integers.
* If you want to use Range in a for loop it also needs to implement Iterable.
* We could probably have defined 2 classes to make it more clear what this class does
* - Range implements Iterable<Integer>
* - RangeIterator implements Iterator<Integer>
* But it is also an example of how to implement 2 interfaces in the same class
* @author mva021
*
*/
public class Range implements Iterator<Integer>, Iterable<Integer> {
int numInts; // the total number of integers to loop over
int current; // the current position in the iterator
/**
* The constructor creates the range: [0,1,2,... ,numInts-1]
* @param numInts
*/
public Range(int numInts) {
this.numInts = numInts;
this.current = 0;
}
@Override
public boolean hasNext() {
return current < numInts; //simply checks if we have reached the end
}
@Override
public Integer next() {
if(!hasNext())
throw new NoSuchElementException("Reached the end of the iterator.");
int next = current;
current ++;
return next;
}
@Override
public Iterator<Integer> iterator() {
//creates a new iterator of the right size
//important that new is used and not return e.g. "return this;" will not work
//Because if you want to iterate over the same range multiple times e.g. a double for loop
//you need to restart at 0 and that is only done in the constructor
return new Range(this.numInts);
}
/**
* This method returns an Iterable<Integer> which can be used in a python style for loop.
* There is 2 possible ways to make this, the simples might be to return an ArrayList containing
* those numbers you want to iterate over (see commented out code).
* But here we choose to do a bit more elaborate solution by returning a new Range object
* @param max
* @return
*/
public static Range range(int max) {
return new Range(max);
}
}
package inf101.v20.sem2.games;
import org.junit.jupiter.api.Test;
import inf101.v20.sem2.terminal.ConsolePlayer;
class ConsolePlayerTest {
@Test
void testConstructor() {
String symbol = "X";
String name = "Martin";
Player p = new ConsolePlayer(symbol, name);
PlayerTest.testConstructor(symbol, name, p);
}
}
package inf101.v20.sem2.games;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Random;
import java.util.Scanner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import inf101.v20.sem2.grid.Location;
import inf101.v20.sem2.terminal.ConsolePlayer;
class GameBoardTest {
public final static Player player1 = new ConsolePlayer("X", "Martin");
public final static Player player2 = new ConsolePlayer("O", "Laura");
GameBoard board;
public final Random rand = new Random();
@BeforeEach
private void createGame() {
board = new GameBoard(4,5);
}
@Test
void testCanConstruct() {
createGame();
try {
board = new GameBoard(5,-3);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException,"Wrong type of exception is thrown");
return;
}
fail("No Exception thrown on illegal input");
}
@Test
void testSetAndGet() {
Location loc = new Location(3,3);
board.set(loc, player1);
assertEquals(player1, board.get(loc));
board.set(new Location(board.numRows()-1,board.numCols()-1), player1);
boolean getException = false;
try {
board.set(new Location(3,board.numCols()), player1);
} catch (Exception e) {
assertTrue(e instanceof IndexOutOfBoundsException);
getException = true;
}
if(!getException)
fail("should get IndexOutOfBoundsException");
getException = false;
try {
board.set(new Location(3,3), player2);
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException,e.getMessage());
getException = true;
}
if(!getException)
fail("should get IllegalArgumentException");
}
@Test
void testIsEmpty() {
board.set(new Location(3,3), player1);
assertTrue(board.isEmpty(new Location(2,2)));
assertFalse(board.isEmpty(new Location(3,3)));
}
@Test
void testCountPlayerDiagonalUp() {
assertEquals(0,board.countNumInRow(player1));
board.set(new Location(1, 1), player1);
board.set(new Location(2, 2), player1);
board.set(new Location(3, 3), player1);
assertEquals(3,board.countNumInRow(player1));
}
@Test
void testCountPlayerDiagonalDown() {
assertEquals(0,board.countNumInRow(player1));
board.set(new Location(1, 3), player1);
board.set(new Location(2, 2), player1);
board.set(new Location(3, 1), player1);
assertEquals(3,board.countNumInRow(player1));
}
@Test
void testCountPlayerHorizontal() {
assertEquals(0,board.countNumInRow(player1));
for(int col : board.colIndices())
board.set(new Location(3, col), player1);
assertEquals(board.numCols(),board.countNumInRow(player1));
assertEquals(0,board.countNumInRow(player2));
}
@Test
void testCountPlayerVertical() {
assertEquals(0,board.countNumInRow(player1));
for(int row : board.rowIndices())
board.set(new Location(row, 3), player1);
assertEquals(board.numRows(),board.countNumInRow(player1));
assertEquals(0,board.countNumInRow(player2));
}
@Test
void testIsFull() {
int num = board.numRows()*board.numCols();
for(Location loc : board.locations()) {
board.set(loc, getRandomPlayer());
num--;
if(num>0) {
assertFalse(board.isFull(),"Not full, there is still "+num +" pieces to place.");
}
}
assertTrue(board.isFull());
}
private void fillBoard() {
for(Location loc : board.locations()) {
board.set(loc, getRandomPlayer());
}
}
private Player getRandomPlayer() {
if(rand.nextBoolean())
return player1;
else
return player2;
}
@Test
void testGetHeightAndWidth() {
int rows = 4;
int cols = 5;
board = new GameBoard(rows,cols);
assertEquals(rows,board.numRows());
assertEquals(cols,board.numCols());
}
@Test
void testToString() {
fillBoard();
Scanner sc = new Scanner(board.toString());
int numLines=0;
while(sc.hasNextLine()) {
String line = sc.nextLine();
int numPlayers=0;
for(char c : line.toCharArray()) {
if(c==player1.getSymbol().charAt(0) || c==player2.getSymbol().charAt(0)) {
numPlayers++;
}
}
if(numPlayers>0) {
assertEquals(board.numCols(), numPlayers);
numLines++;
}
}
assertEquals(board.numRows(), numLines);
}
}
package inf101.v20.lab4.grid;
package inf101.v20.sem2.games;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Random;
import org.junit.jupiter.api.Test;
import inf101.v20.sem2.grid.Grid;
import inf101.v20.sem2.grid.IGrid;
import inf101.v20.sem2.grid.Location;
public class GridTest {
Random random = new Random();
@Test
public void constructTest1() {
IGrid<String> grid = new Grid<>(11, 17, "S");
IGrid<String> grid = new Grid<String>(11, 17, "S");
// TODO: sjekk at bredde og høyde faktisk er 11 og 17
assertEquals(11, grid.getWidth());
assertEquals(17, grid.getHeight());
assertEquals(11, grid.numRows());
assertEquals(17, grid.numCols());
}
@Test
public void constructTest2() {
assertThrows(IllegalArgumentException.class,()->{new Grid<String>(0, 17, "S");} );
assertThrows(IllegalArgumentException.class,()->{new Grid<String>(3, -1, "S");} );
}
/**
* Tests that trying to access outside of the dimensions of the grid throws
* an IndexOutOfBoundsException.
......@@ -29,13 +38,13 @@ public class GridTest {
IGrid<String> grid = new Grid<>(10, 10, "S");
try {
grid.set(11, 0, "S");
grid.set(new Location(11, 0), "S");
fail("Should throw exception");
} catch (IndexOutOfBoundsException e) {
;
}
try {
grid.set(0, 11, "S");
grid.set(new Location(0, 11), "S");
fail("Should throw exception");
} catch (IndexOutOfBoundsException e) {
;
......@@ -46,48 +55,63 @@ public class GridTest {
public void setGetTest1() {
IGrid<String> grid = new Grid<>(100, 100, "S");
for (int x = 0; x < 100; x++)
for (int y = 0; y < 100; y++) {
String cs = ""+random.nextDouble();
grid.set(x, y, cs);
assertEquals(cs, grid.get(x, y));
}
for(Location loc : grid.locations()) {
String cs = ""+random.nextDouble();
grid.set(loc, cs);
assertEquals(cs, grid.get(loc));
}
}
@Test
public void setGetTest2() {
IGrid<String> grid = new Grid<>(100, 100, "S");
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) {
grid.set(x, y, ""+random.nextDouble());
}
for(Location loc : grid.locations()) {
grid.set(loc, ""+random.nextDouble());
}
for (int x = 0; x < 100; x++)
for (int y = 0; y < 100; y++) {
String cs = ""+random.nextDouble();
grid.set(x, y, cs);
assertEquals(cs, grid.get(x, y));
}
for(Location loc : grid.locations()) {
String cs = ""+random.nextDouble();
grid.set(loc, cs);
assertEquals(cs, grid.get(loc));
}
}
@Test
public void copyTest() {
IGrid<String> grid = new Grid<>(100, 100, "S");
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) {
String cs = ""+random.nextDouble();
grid.set(x, y, cs);
}
for(Location loc : grid.locations()) {
String cs = ""+random.nextDouble();
grid.set(loc, cs);
}
IGrid<String> newGrid = grid.copy();
for (int x = 0; x < 100; x++) {
for (int y = 0; y < 100; y++) {
assertEquals(grid.get(x, y), newGrid.get(x, y));
}
for(Location loc : grid.locations()) {
assertEquals(grid.get(loc), newGrid.get(loc));
}
}
@Test
public void containsTest() {
IGrid<String> grid = new Grid<>(23, 67, "EMPTY");
String elem = "Element";
assertFalse(grid.contains(elem));
Location loc = new Location(12,17);
grid.set(loc, elem);
assertTrue(grid.contains(elem));
}
@Test
public void LocationOfTest() {
IGrid<String> grid = new Grid<>(23, 67, "EMPTY");
String elem = "Element";
assertThrows(IndexOutOfBoundsException.class,()->{grid.locationOf(elem);} );
Location loc = new Location(12,17);
grid.set(loc, elem);
assertEquals(loc, grid.locationOf(elem));
assertTrue(loc.equals(grid.locationOf(elem)));
}
}
package inf101.v20.sem2.games;
import static org.junit.jupiter.api.Assertions.*;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Random;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import inf101.v20.sem2.terminal.ConsolePlayer;
class PlayerListTest {
public final static Player player1 = new ConsolePlayer("X", "Martin");
public final static Player player2 = new ConsolePlayer("O", "Laura");
PlayerList players;
Random r = new Random();
@BeforeEach
void createList() {
players = new PlayerList();
players.add(player1);
players.add(player2);
}
@Test
void testConstructor() {
PlayerList players = new PlayerList();
assertEquals(0, players.size());
}
@Test
void testCurrentPlayer() {
assertEquals(player1, players.currentPlayer());
//consecutive calls to current player should not change the current player
assertEquals(player1, players.currentPlayer());
}
@Test
void testNextPlayer() {
for(int i=1; i<100; i++) {
if(i%2==1)
assertEquals(player1, players.currentPlayer());
else
assertEquals(player2, players.currentPlayer());
players.nextPlayer();
}
}
@Test
void testRemoveInt() {
players.nextPlayer();
assertEquals(player2, players.currentPlayer());
assertEquals(player1,players.remove(0));
assertEquals(player2, players.currentPlayer());
}
@Test
void testRemoveIntCurrent() {
for(int i=1; i<100; i++) {
createList();
testRemoveInt(r.nextInt(100), true);
}
}
@Test
void testRemoveOtherInt() {
for(int i=1; i<100; i++) {
createList();
testRemoveInt(r.nextInt(100), false);
}
}
private void advancePlayer(int n) {
for(int i=0; i<n; i++) {
players.nextPlayer();
}
}
private Player other(Player p) {
if(p==player1) return player2;
if(p==player2) return player1;
throw new IllegalStateException("Unexpected player as input");
}
void testRemoveInt(int n, boolean removeCurrent) {
advancePlayer(n);
Player cur = players.currentPlayer();
if(n%2 == 0)
assertEquals(player1, cur);
else
assertEquals(player2, cur);
if(removeCurrent) {
assertEquals(cur,players.remove(n%2));
assertEquals(other(cur),players.currentPlayer());
} else {
assertNotEquals(cur, players.remove((n+1)%2));
assertEquals(cur, players.currentPlayer());
}
}
@Test
void testRemovePlayer() {
for(int i=1; i<100; i++) {
createList();
testRemove(r.nextInt(100), true);
}
}
@Test
void testRemoveOtherPlayer() {
for(int i=1; i<100; i++) {
createList();
testRemove(r.nextInt(100), false);
}
}
void testRemove(int n, boolean removeCurrent) {
advancePlayer(n);
Player cur = players.currentPlayer();
if(removeCurrent) {
assertTrue(players.remove(cur));
assertEquals(other(cur),players.currentPlayer());
} else {
assertNotEquals(cur, players.remove((n+1)%2));
assertEquals(cur, players.currentPlayer());
}
}
@Test
void testRemoveTwice() {
players.remove(player1);
assertFalse(players.remove(player1),"Can not remove same player twice");
}
@Test
void testAddAndSize() {
assertEquals(2, players.size());
try {
players.add(new ConsolePlayer(player2.getSymbol(),"Anna"));
} catch (Exception e) {
assertTrue(e instanceof IllegalArgumentException);
return;
}
fail("Should get Exception when adding 2 players with same symbol");
}
@Test
void testIterator() {
Iterator<Player> iter = players.iterator();
assertEquals(player1, iter.next());
assertEquals(player2, iter.next());
assertFalse(iter.hasNext());
try {
iter.next();
} catch (Exception e) {
assertTrue(e instanceof NoSuchElementException);
}
}
}
package inf101.v20.sem2.games;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;
import inf101.v20.sem2.grid.Location;
class PlayerTest {