From b34201af08ea72df171751dda1df3e3b1b9a45d1 Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 21 Feb 2016 14:10:57 +0100 Subject: Import unit 1 tests --- test/ch/epfl/xblast/CellTest.java | 68 ++++++++++++++++++++++++++ test/ch/epfl/xblast/DirectionTest.java | 41 ++++++++++++++++ test/ch/epfl/xblast/SubCellTest.java | 60 +++++++++++++++++++++++ test/ch/epfl/xblast/namecheck/NameCheck01.java | 49 +++++++++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 test/ch/epfl/xblast/CellTest.java create mode 100644 test/ch/epfl/xblast/DirectionTest.java create mode 100644 test/ch/epfl/xblast/SubCellTest.java create mode 100644 test/ch/epfl/xblast/namecheck/NameCheck01.java diff --git a/test/ch/epfl/xblast/CellTest.java b/test/ch/epfl/xblast/CellTest.java new file mode 100644 index 0000000..ba8becc --- /dev/null +++ b/test/ch/epfl/xblast/CellTest.java @@ -0,0 +1,68 @@ +package ch.epfl.xblast; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; + +import org.junit.Test; + +public class CellTest { + @Test + public void rowMajorIndexCorrespondsToOrder() { + int i = 0; + for (Cell c: Cell.ROW_MAJOR_ORDER) + assertEquals(i++, c.rowMajorIndex()); + assertEquals(Cell.COUNT, i); + } + + @Test + public void spiralOrderContainsAllCells() { + assertEquals(Cell.COUNT, Cell.SPIRAL_ORDER.size()); + + boolean[] cellSeen = new boolean[Cell.COUNT]; + for (Cell c: Cell.SPIRAL_ORDER) { + assertFalse(cellSeen[c.rowMajorIndex()]); + cellSeen[c.rowMajorIndex()] = true; + } + } + + @Test + public void spiralOrderNeighborsAreSpatialNeighbors() { + Cell pred = Cell.SPIRAL_ORDER.get(0); + for (Cell c: Cell.SPIRAL_ORDER.subList(1, Cell.SPIRAL_ORDER.size())) { + int areNeighborsCount = 0; + for (Direction d: Direction.values()) { + if (pred.equals(c.neighbor(d))) + areNeighborsCount += 1; + } + assertEquals(1, areNeighborsCount); + pred = c; + } + } + + @Test + public void constructorCorrectlyNormalizesCoordinates() { + for (int i = -2; i <= 2; ++i) { + Cell c = new Cell(14 + 15 * i, 12 + 13 * i); + assertEquals(14, c.x()); + assertEquals(12, c.y()); + } + } + + @Test + public void neighborsOfOriginAreCorrect() { + Cell c = new Cell(0, 0); + assertEquals(new Cell( 0, 12), c.neighbor(Direction.N)); + assertEquals(new Cell( 1, 0), c.neighbor(Direction.E)); + assertEquals(new Cell( 0, 1), c.neighbor(Direction.S)); + assertEquals(new Cell(14, 0), c.neighbor(Direction.W)); + } + + @Test + public void oppositeNeighborOfNeighborIsThis() { + for (Cell c: Cell.ROW_MAJOR_ORDER) { + for (Direction d: Direction.values()) { + assertEquals(c, c.neighbor(d).neighbor(d.opposite())); + } + } + } +} diff --git a/test/ch/epfl/xblast/DirectionTest.java b/test/ch/epfl/xblast/DirectionTest.java new file mode 100644 index 0000000..a2ac9cd --- /dev/null +++ b/test/ch/epfl/xblast/DirectionTest.java @@ -0,0 +1,41 @@ +package ch.epfl.xblast; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class DirectionTest { + @Test + public void oppositeOfOppositeIsIdentity() { + for (Direction d: Direction.values()) + assertEquals(d, d.opposite().opposite()); + } + + @Test + public void oppositeIsTwoStepsAway() { + for (Direction d: Direction.values()) + assertEquals(2, Math.abs(d.ordinal() - d.opposite().ordinal())); + } + + @Test + public void isHorizontalIsCorrect() { + assertFalse(Direction.N.isHorizontal()); + assertTrue(Direction.E.isHorizontal()); + assertFalse(Direction.S.isHorizontal()); + assertTrue(Direction.W.isHorizontal()); + } + + @Test + public void isParallelIsTrueOnlyForOppositeAndSelf() { + for (Direction d1: Direction.values()) { + for (Direction d2: Direction.values()) { + if (d1 == d2 || d1 == d2.opposite()) + assertTrue(d1.isParallelTo(d2)); + else + assertFalse(d1.isParallelTo(d2)); + } + } + } +} diff --git a/test/ch/epfl/xblast/SubCellTest.java b/test/ch/epfl/xblast/SubCellTest.java new file mode 100644 index 0000000..1c79791 --- /dev/null +++ b/test/ch/epfl/xblast/SubCellTest.java @@ -0,0 +1,60 @@ +package ch.epfl.xblast; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; + +public class SubCellTest { + @Test + public void centralSubCellOfKnowCellIsCorrect() { + SubCell c = SubCell.centralSubCellOf(new Cell(2, 1)); + assertEquals(40, c.x()); + assertEquals(24, c.y()); + } + + @Test + public void centralSubCellIsCentral() { + for (Cell c: Cell.ROW_MAJOR_ORDER) + assertTrue(SubCell.centralSubCellOf(c).isCentral()); + } + + @Test + public void distanceToCentralOfCentralIsZero() { + for (Cell c: Cell.ROW_MAJOR_ORDER) + assertEquals(0, SubCell.centralSubCellOf(c).distanceToCentral()); + } + + @Test + public void constructorCorrectlyNormalizesCoordinates() { + for (int i = -2; i <= 2; ++i) { + SubCell c = new SubCell(239 + 240 * i, 207 + 208 * i); + assertEquals(239, c.x()); + assertEquals(207, c.y()); + } + } + + @Test + public void distanceToCentralOfOriginIsCorrect() { + SubCell s = new SubCell(0, 0); + assertEquals(16, s.distanceToCentral()); + } + + @Test + public void neighborsOfOriginAreCorrect() { + SubCell c = new SubCell(0, 0); + assertEquals(new SubCell( 0, 207), c.neighbor(Direction.N)); + assertEquals(new SubCell( 1, 0), c.neighbor(Direction.E)); + assertEquals(new SubCell( 0, 1), c.neighbor(Direction.S)); + assertEquals(new SubCell(239, 0), c.neighbor(Direction.W)); + } + + @Test + public void containingCellOfCentralsNeighborIsCorrect() { + for (Cell c: Cell.ROW_MAJOR_ORDER) { + SubCell s = SubCell.centralSubCellOf(c); + for (Direction d: Direction.values()) + assertEquals(c, s.neighbor(d).containingCell()); + } + } +} diff --git a/test/ch/epfl/xblast/namecheck/NameCheck01.java b/test/ch/epfl/xblast/namecheck/NameCheck01.java new file mode 100644 index 0000000..278d85b --- /dev/null +++ b/test/ch/epfl/xblast/namecheck/NameCheck01.java @@ -0,0 +1,49 @@ +package ch.epfl.xblast.namecheck; + +import java.util.List; + +import ch.epfl.xblast.Cell; +import ch.epfl.xblast.Direction; +import ch.epfl.xblast.SubCell; + +/** + * Classe abstraite utilisant tous les éléments de l'étape 1, pour essayer de + * garantir que ceux-ci ont le bon nom et les bons types. Attention, ceci n'est + * pas un test unitaire, et n'a pas pour but d'être exécuté! + */ + +abstract class NameCheck01 { + void checkDirection() { + Direction d = Direction.N; + d = Direction.E; + d = Direction.S; + d = Direction.W; + if (d.isHorizontal() || d.isParallelTo(d)) + d = d.opposite(); + } + + void checkCell() { + int c = Cell.COLUMNS; + int r = Cell.ROWS; + int t = Cell.COUNT; + List l = Cell.ROW_MAJOR_ORDER; + l.get(c + r + t); + l = Cell.SPIRAL_ORDER; + l.get(c + r + t); + Cell d = new Cell(c, r); + c = d.x() + d.y() + d.rowMajorIndex(); + d = d.neighbor(Direction.N); + } + + void checkSubCell() { + SubCell c = SubCell.centralSubCellOf(new Cell(0,0)); + c = new SubCell(0, 0); + int t = c.x() + c.y() + c.distanceToCentral(); + if (t < 10 && c.isCentral()) + c = c.neighbor(Direction.N); + else { + Cell cc = c.containingCell(); + System.out.println(cc); + } + } +} -- cgit v1.2.3