Package pair.gameoflife.a20140102

Source Code of pair.gameoflife.a20140102.LinkedCoordsTest

package pair.gameoflife.a20140102;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import pair.gameoflife.a20140102.CoordTest.Coord;

import org.junit.Test;

public class LinkedCoordsTest {

    public interface LinkedCoords {
        int countNeighbours(Coord coord);
       
        LinkedCoords EMPTY = new LinkedCoords() {
            @Override
            public int countNeighbours(Coord coord) {
                return 0;
            }
        };
    }

    public static class OccupiedCoords implements LinkedCoords {

        private final Coord coord;
        private final LinkedCoords next;

        public OccupiedCoords(Coord coord, LinkedCoords next) {
            this.coord = coord;
            this.next = next;
        }

        @Override
        public int countNeighbours(Coord other) {
            return this.coord.oneForNeighbour(other) + next.countNeighbours(other);
        }

    }

    private static final Coord SOME = new Coord(1, 1);

    @Test
    public void emptyCoordsShouldCountNone() {
        LinkedCoords coords = LinkedCoords.EMPTY;
        assertThat(coords.countNeighbours(SOME), is(0));
    }

    @Test
    public void shouldSumSelfNeighbourCountAndNextNeighbourCount() {
        final int selfNeighbourCount = 2;
        final int nextNeighbourCount = 40;

        Coord selfCoord = new Coord(0, 0) {
            @Override
            public int oneForNeighbour(Coord other) {
                return selfNeighbourCount;
            }
        };
       
        LinkedCoords nextCoords = new LinkedCoords() {
            @Override
            public int countNeighbours(Coord coord) {
                return nextNeighbourCount;
            }
        };

        LinkedCoords coords = new OccupiedCoords(selfCoord, nextCoords);
       
        assertThat(coords.countNeighbours(SOME), is(selfNeighbourCount + nextNeighbourCount));
    }

}
TOP

Related Classes of pair.gameoflife.a20140102.LinkedCoordsTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.