celltest.java

来自「drools 一个开放源码的规则引擎」· Java 代码 · 共 92 行

JAVA
92
字号
package org.drools.examples.conway;

import org.drools.examples.conway.Cell;
import org.drools.examples.conway.CellState;

import junit.framework.TestCase;

/**
 *
 */
public class CellTest extends TestCase {

    public void testDefaultStateIsDead() {
        Cell c1 = new Cell();

        assertEquals("c1 had wrong inital state", CellState.DEAD, c1.getCellState());
    }

    public void testAddNeighbor() {
        Cell c1 = new Cell();
        Cell c2 = new Cell();
        assertEquals("c1 had wrong number of neighbors before adding neighbor", 0, c1.getNumberOfNeighboringCells());
        assertEquals("c2 had wrong number of neighbors before adding neighbor", 0, c2.getNumberOfNeighboringCells());


        c1.addNeighbor(c2);

        assertEquals("c1 had wrong number of neighbors after adding neighbor", 1, c1.getNumberOfNeighboringCells());
        assertEquals("c2 had wrong number of neighbors after adding neighbor", 1, c2.getNumberOfNeighboringCells());
    }

    public void testAddingNeighborMultipleTimes() {
        Cell c1 = new Cell();
        Cell c2 = new Cell();
        assertEquals("c1 had wrong number of neighbors before adding neighbor", 0, c1.getNumberOfNeighboringCells());
        assertEquals("c2 had wrong number of neighbors before adding neighbor", 0, c2.getNumberOfNeighboringCells());


        c1.addNeighbor(c2);

        assertEquals("c1 had wrong number of neighbors after adding neighbor", 1, c1.getNumberOfNeighboringCells());
        assertEquals("c2 had wrong number of neighbors after adding neighbor", 1, c2.getNumberOfNeighboringCells());

        c1.addNeighbor(c2);
        c1.addNeighbor(c2);
        c2.addNeighbor(c1);

        assertEquals("c1 had wrong number of neighbors after adding neighbor again", 1, c1.getNumberOfNeighboringCells());
        assertEquals("c2 had wrong number of neighbors after adding neighbor again", 1, c2.getNumberOfNeighboringCells());
    }

    public void testNumberOfLiveNeighbors() {
        Cell c1 = new Cell();
        Cell c2 = new Cell();
        Cell c3 = new Cell();

        c1.addNeighbor(c2);

        assertEquals("c1 had wrong number of live neighbors initally", 0, c1.getNumberOfLiveNeighbors());

        c2.setCellState(CellState.LIVE);

        assertEquals("c1 had wrong number of live neighbors", 1, c1.getNumberOfLiveNeighbors());
        c3.setCellState(CellState.LIVE);
        c1.addNeighbor(c3);
        assertEquals("c1 had wrong number of live neighbors", 2, c1.getNumberOfLiveNeighbors());

    }

    public void testStateQueuing() {
        Cell c1 = new Cell();
        assertEquals("c1 had wrong inital state", CellState.DEAD, c1.getCellState());

        c1.setCellState(CellState.LIVE);
        assertEquals("c1 had wrong state", CellState.LIVE, c1.getCellState());

        c1.queueNextCellState(CellState.DEAD);
        assertEquals("c1 had wrong state", CellState.LIVE, c1.getCellState());

        c1.transitionState();
        assertEquals("c1 had wrong state", CellState.DEAD, c1.getCellState());

        c1.queueNextCellState(CellState.LIVE);
        assertEquals("c1 had wrong state", CellState.DEAD, c1.getCellState());

        c1.transitionState();
        assertEquals("c1 had wrong state", CellState.LIVE, c1.getCellState());

    }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?