de/joshuagleitze/tilinggame/test

TileTest.java

Download file
package de.joshuagleitze.tilinggame.test;
import static de.joshuagleitze.tilinggame.LineType.GREEN;
import static de.joshuagleitze.tilinggame.LineType.NONE;
import static de.joshuagleitze.tilinggame.LineType.RED;
import static de.joshuagleitze.tilinggame.LineType.YELLOW;
import static de.joshuagleitze.tilinggame.test.TileTestHelper.EMPTY_TILE_DEFINITION;
import static de.joshuagleitze.tilinggame.test.TileTestHelper.TEST_TILES_DEFINITIONS;
import static de.joshuagleitze.tilinggame.test.TileTestHelper.tile;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

import org.junit.Test;

import de.joshuagleitze.tilinggame.LineType;
import de.joshuagleitze.tilinggame.Tile;

public class TileTest {

	@Test
	public void lineTypeConstructor() {
		final LineType[] definitionArray = new LineType[] {RED, YELLOW, GREEN, YELLOW, GREEN, RED};
		final Tile tile = new Tile(definitionArray);
		for (int i = 0; i < definitionArray.length; i++) {
			assertThat("line types are applied", tile.getLineTypeAtIndex(i), is(definitionArray[i]));
		}
		definitionArray[0] = NONE;
		assertThat("line type array is copied", tile.getLineTypeAtIndex(0), is(RED));
	}

	@Test
	public void emptyConstructor() {
		final Tile emptyTile = new Tile();
		for (int i = 0; i < 6; i++) {
			assertThat("tile is empty", emptyTile.getLineTypeAtIndex(i), is(NONE));
		}
	}

	@Test
	public void getNumberOfColors() {
		assertThat("the empty tile has no colors", tile(EMPTY_TILE_DEFINITION).getNumberOfColors(),
				is(0));
		assertThat("the empty tile has no colors", new Tile().getNumberOfColors(), is(0));
		assertThat("R--R-- has one color", tile("R--R--").getNumberOfColors(), is(1));
		assertThat("YRYR-- has two colors", tile("YRYR--").getNumberOfColors(), is(2));
		assertThat("YRGGRY has three colors", tile("YRGGRY").getNumberOfColors(), is(3));
	}

	@Test
	public void isExactlyEqualTo() {
		for (final String definition : TEST_TILES_DEFINITIONS) {
			final Tile tile = tile(definition);
			assertTrue("a tile should be equal to itself: " + definition, tile.isExactlyEqualTo(tile));
			assertTrue("tiles with same definitions should be equal: " + definition,
					tile(definition).isExactlyEqualTo(tile(definition)));
		}
		for (int i = 0; i < TEST_TILES_DEFINITIONS.length - 1; i++) {
			assertFalse(
					"tiles with different definitions should not be equal: " + TEST_TILES_DEFINITIONS[i] + " <-> "
							+ TEST_TILES_DEFINITIONS[i + 1],
							tile(TEST_TILES_DEFINITIONS[i]).isExactlyEqualTo(tile(TEST_TILES_DEFINITIONS[i + 1])));
		}
	}

	@Test
	public void copy() {
		Tile tile = tile("-R---R");
		Tile copy = tile.copy();
		copy.rotateClockwise();
		assertThat("rotating a copy should not rotate the original", tile.getLineTypeAtIndex(0), is(NONE));
		assertThat("rotating a copy should not rotate the original", tile.getLineTypeAtIndex(1), is(RED));
		tile = new Tile();
		copy = tile.copy();
		for (int i = 0; i < 6; i++) {
			assertThat(copy.getLineTypeAtIndex(i), is(NONE));
		}
	}

	@Test
	public void rotateClockwise() {
		Tile tile = tile("-RG-GR");
		tile.rotateClockwise();
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(0), is(RED));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(1), is(NONE));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(2), is(RED));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(3), is(GREEN));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(4), is(NONE));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(5), is(GREEN));
		final Tile emptyTile = new Tile();
		for (int i = 0; i < 6; i++) {
			emptyTile.rotateClockwise();
			assertTrue("rotating the empty tile should have no effect", emptyTile.isExactlyEqualTo(new Tile()));
		}
		for (final String definition : TEST_TILES_DEFINITIONS) {
			tile = tile(definition);
			for (int i = 0; i < 6; i++) {
				tile.rotateClockwise();
			}
			assertTrue("rotating a tile 6 times should result in the same tile: " + definition,
					tile.isExactlyEqualTo(tile(definition)));
		}
	}

	@Test
	public void rotateCounterClockwise() {
		Tile tile = tile("-RG-GR");
		tile.rotateCounterClockwise();
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(0), is(RED));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(1), is(GREEN));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(2), is(NONE));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(3), is(GREEN));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(4), is(RED));
		assertThat("all line types should be shifted", tile.getLineTypeAtIndex(5), is(NONE));
		final Tile emptyTile = new Tile();
		for (int i = 0; i < 6; i++) {
			emptyTile.rotateCounterClockwise();
			assertThat("rotating the empty tile should have no effect", emptyTile.isExactlyEqualTo(new Tile()),
					is(true));
		}
		for (final String definition : TEST_TILES_DEFINITIONS) {
			tile = tile(definition);
			for (int i = 0; i < 6; i++) {
				tile.rotateCounterClockwise();
			}
			assertTrue("rotating a tile 6 times should result in the same tile: " + definition,
					tile.isExactlyEqualTo(tile(definition)));
		}
	}

	@Test
	public void isEmpty() {
		assertTrue("the empty tile is empty", new Tile().isEmpty());
		assertTrue("the empty tile is empty", tile(EMPTY_TILE_DEFINITION).isEmpty());
		for (final String definition : TEST_TILES_DEFINITIONS) {
			if (!definition.equals(EMPTY_TILE_DEFINITION)) {
				final Tile tile = tile(definition);
				assertFalse("a tile with lines is not empty: " + definition, tile.isEmpty());
			}
		}
	}

	@Test
	public void isRotationEqualTo() {
		assertTrue(new Tile().isRotationEqualTo(new Tile()));
		assertTrue(tile("----YY").isRotationEqualTo(tile("-YY---")));
		assertTrue(tile("-YY---").isRotationEqualTo(tile("----YY")));
		assertTrue(tile("R----R").isRotationEqualTo(tile("RR----")));
		assertTrue(tile("R----R").isRotationEqualTo(tile("----RR")));
		assertFalse("same position but different colors should not be rotation equal",
				tile("----RR").isRotationEqualTo(tile("-YY---")));
		assertFalse("same position but different colors should not be rotation equal",
				tile("-YY---").isRotationEqualTo(tile("----RR")));
		assertFalse("added lines should not be rotation equal", tile("GG--RR").isRotationEqualTo(tile("----RR")));
		assertFalse("added lines should not be rotation equal", tile("----RR").isRotationEqualTo(tile("GG--RR")));
		for (final String definition : TEST_TILES_DEFINITIONS) {
			final Tile tile = tile(definition);
			for (int i = 0; i <= 6; i++) {
				assertTrue("a tile after " + i + " rotations is rotational equal to its source: " + definition,
						tile.isRotationEqualTo(tile(definition)));
				tile.rotateClockwise();
			}
		}
	}

	@Test
	public void canBeRecoloredTo() {
		assertTrue("the empty tile can be recolored to itself", new Tile().canBeRecoloredTo(new Tile()));
		for (final String definition : TEST_TILES_DEFINITIONS) {
			final Tile tile = tile(definition);
			assertTrue("every tile can be recolored to itself: " + definition, tile.canBeRecoloredTo(tile));
			assertTrue("every tile can be recolored to an equal tile: " + definition,
					tile.canBeRecoloredTo(tile(definition)));
		}
		assertTrue("an empty tile can be recolored to an empty tile",
				new Tile().canBeRecoloredTo(tile(EMPTY_TILE_DEFINITION)));
		assertFalse("adding lines is not allowed when recoloring", tile("YY----").canBeRecoloredTo(tile("YY--RR")));
		assertFalse("adding lines is not allowed when recoloring", tile("GG----").canBeRecoloredTo(tile("YY--RR")));
		assertFalse("adding lines is not allowed when recoloring", tile("GG--RR").canBeRecoloredTo(tile("GGYYRR")));
		assertFalse("rotation is not allowed when recoloring", tile("YY----").canBeRecoloredTo(tile("-YY---")));
		assertFalse("rotation is not allowed when recoloring", tile("RR----").canBeRecoloredTo(tile("-YY---")));
		assertFalse("lines must be colored consistently", tile("RYRY--").canBeRecoloredTo(tile("RYYR--")));
		assertFalse("lines must be colored consistently", tile("RYRGYG").canBeRecoloredTo(tile("RRYYGG")));
		assertFalse("lines must be colored consistently", tile("RGYRGY").canBeRecoloredTo(tile("YGRYRG")));
		assertTrue("multiple recolorings must be considered", tile("RGYYRG").canBeRecoloredTo(tile("GYRRGY")));
	}

	@Test
	public void dominates() {
		assertFalse("the empty tile does not dominate itselft", new Tile().dominates(new Tile()));
		for (final String definition : TEST_TILES_DEFINITIONS) {
			final Tile tile = tile(definition);
			assertFalse("no tile dominates itself: " + definition, tile.dominates(tile));
			assertFalse("no tile dominates an equal tile: " + definition, tile.dominates(tile(definition)));
			if (!definition.equals(EMPTY_TILE_DEFINITION)) {
				assertTrue(definition + " dominates the empty tile", tile.dominates(new Tile()));
				assertTrue(definition + " dominates the empty tile",
						tile.dominates(tile(EMPTY_TILE_DEFINITION)));
			}
		}
		assertTrue(tile("RRGGYY").dominates(tile("--GGYY")));
		assertTrue(tile("RRGGYY").dominates(tile("RR--YY")));
		assertTrue(tile("RRGGYY").dominates(tile("RRGG--")));
		assertTrue(tile("RRGGYY").dominates(tile("----YY")));
		assertTrue(tile("RRGGYY").dominates(tile("RR----")));
		assertTrue(tile("RRGGYY").dominates(tile("--GG--")));
		assertTrue(tile("RRGGYY").dominates(tile("------")));
		assertFalse("domination is not symmetric", tile("--GG--").dominates(tile("RRGGYY")));
		assertFalse("domination is not symmetric", tile("RR----").dominates(tile("RRGGYY")));
		assertFalse("domination is not symmetric", tile("RRGG--").dominates(tile("RRGGYY")));
		assertFalse("recoloring is not allowed for domination", tile("RRYY--").dominates(tile("--GG--")));
		assertFalse("rotation is not allowed for domination", tile("RRYY--").dominates(tile("YY----")));
	}

	@Test
	public void hasSameColorsAs() {
		for (final String definition : TEST_TILES_DEFINITIONS) {
			final Tile tile = tile(definition);
			assertTrue("a tile has the same colors as itself: " + definition, tile.hasSameColorsAs(tile));
			assertTrue("a tile has the same colors as an equal tile: " + definition,
					tile.hasSameColorsAs(tile(definition)));
			if (!definition.equals(EMPTY_TILE_DEFINITION)) {
				assertFalse("the empty tile has not the same colors as " + definition,
						new Tile().hasSameColorsAs(tile));
				assertFalse("the empty tile has not the same colors as " + definition,
						tile(EMPTY_TILE_DEFINITION).hasSameColorsAs(tile));
				assertFalse(definition + "does not have the same colors as the empty tile",
						tile.hasSameColorsAs(new Tile()));
				assertFalse("the empty tile has not the same colors as " + definition,
						tile.hasSameColorsAs(tile(EMPTY_TILE_DEFINITION)));
			}
		}
		assertTrue(tile("R-R---").hasSameColorsAs(tile("R----R")));
		assertTrue(tile("R----R").hasSameColorsAs(tile("R-R---")));
		assertFalse(tile("R-R---").hasSameColorsAs(tile("G-G---")));
		assertFalse(tile("G-G---").hasSameColorsAs(tile("R-R---")));
		assertFalse(tile("R-R---").hasSameColorsAs(tile("R-R-GG")));
		assertFalse(tile("R-R-GG").hasSameColorsAs(tile("R-R---")));
		assertTrue(tile("RGYRGY").hasSameColorsAs(tile("RRGGYY")));
		assertTrue(tile("RRGGYY").hasSameColorsAs(tile("RGYRGY")));
	}

	@Test
	public void toStringT() {
		assertThat("toString for the empty tile", new Tile().toString(), is(EMPTY_TILE_DEFINITION));
		for (final String definition : TEST_TILES_DEFINITIONS) {
			assertThat("toString for " + definition, tile(definition).toString(), is(definition));
		}
	}

	@Test
	public void fitsTo() {
		for (final String definition : TEST_TILES_DEFINITIONS) {
			for (int i = 0; i < 6; i++) {
				assertTrue("every tile fits to the empty tile: " + definition, new Tile().fitsTo(tile(definition), i));
				assertTrue("every tile fits to the empty tile: " + definition,
						tile(definition).fitsTo(tile(EMPTY_TILE_DEFINITION), i));
			}
		}
		for (int i = 0; i < 6; i++) {
			assertTrue("check at edege" + i, tile("RR----").fitsTo(tile("-GR-RG"), i));
		}
		assertFalse(tile("Y----Y").fitsTo(tile("---R-R"), 0));
		for (int i = 0; i < 6; i++) {
			assertFalse("YRGYRG does not fit to RGYRGY: " + i, tile("YRGYRG").fitsTo(tile("RGYRGY"), i));
			assertFalse("RGYRGY does not fit to YRGYRG: " + i, tile("RGYRGY").fitsTo(tile("YRGYRG"), i));
		}
	}
}