Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path=""/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
<classpathentry kind="lib" path="C:/Users/Ryan/Documents/GitHub/Project2/deliverable2_repo/lib/mockito-all-1.9.5.jar"/>
<classpathentry kind="output" path=""/>
</classpath>
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/bin/
/bin/
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>JavaLife</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
5 changes: 5 additions & 0 deletions com/laboon/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/Cell.class
/JavaLife.class
/State.class
/World.class
/WorldTest.class
28 changes: 21 additions & 7 deletions com/laboon/World.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,18 @@

import java.util.Random;

/*
* Modification Log:
* + 3-30-2015 : Ryan McDonald
* Refactored toString() method to use StringBuilder while
* building the gameboard as a string since using the "+="
* with Strings is inefficent in Java as they are immutable
* so this would continually create new Strings. Also, we
* can avoid the String.valueOf() method call alltogether.
*
*/


public class World {

/**
Expand Down Expand Up @@ -115,19 +127,21 @@ public World iterate() {
*/

public String toString() {
String toReturn = " ";
StringBuilder toReturn = new StringBuilder(" ");

for (int j= 0; j < _size; j++) {
toReturn += String.valueOf(j % 10);
toReturn.append(j % 10);
}
toReturn += "\n";
toReturn.append("\n");
for (int j = 0; j < _size; j++ ) {
toReturn += String.valueOf(j % 10) + " ";
toReturn.append(j % 10);
toReturn.append(" ");
for (int k = 0; k < _size; k++) {
toReturn += (_world[j][k].getStateRep());
toReturn.append(_world[j][k].getStateRep());
}
toReturn += "\n";
toReturn.append("\n");
}
return toReturn;
return toReturn.toString();
}

/**
Expand Down
190 changes: 190 additions & 0 deletions com/laboon/WorldTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
package com.laboon;

import static org.junit.Assert.*;

import java.util.Random;
import org.junit.BeforeClass;
import org.junit.Test;

import static org.mockito.Mockito.*;
import org.mockito.*;
import org.mockito.stubbing.OngoingStubbing;

public class WorldTest
{

@Test
/*
* Test that calling World's toString() method is invoked
* appropriately as one would would expect
*/
public void testToStringMethod()
{
String dummy = new String("dummy");
World mockWorld = mock(World.class);
when(mockWorld.toString()).thenReturn(dummy);

assertTrue(mockWorld.toString().equals(dummy));
}

@Test
/*
* Test edge case that generating an empty world produces
* a String representing no meaningful content seeing as
* how there is nothing to represent
*/
public void testEmptyWorld()
{
Cell[][] emptyCells = new Cell[0][0];
Random rando = new Random();
World w = new World(emptyCells, rando);

String expectedReturn = " \n";
assertTrue(expectedReturn.equals(w.toString()));
}

@Test
/*
* Verify that when a world of dead cells is generated
* the toString() method will print a world of dead
* cells to represent that
*/
public void testWorldOfDeadCells()
{
Random rando = new Random();

Cell deadCell = mock(Cell.class);
when(deadCell.getStateRep()).thenReturn('X');
Cell[][] worldToBe = {{deadCell, deadCell},
{deadCell, deadCell}};

World deadWorld = new World(worldToBe, rando);

String expectedWorld = " 01\n"+
"0 XX\n"+
"1 XX\n";

assertTrue(expectedWorld.equals(deadWorld.toString()));

}

@Test
/*
* Verify that when a world of living cells is generated
* the toString() method will return a string of living
* cells to represent that
*/
public void testWorldOfLivingCells()
{
Random rando = new Random();

Cell livingCell = mock(Cell.class);
when(livingCell.getStateRep()).thenReturn('.');
Cell[][] worldToBe = {{livingCell, livingCell},
{livingCell, livingCell}};

World livingWorld = new World(worldToBe, rando);

String expectedWorld = " 01\n"+
"0 ..\n"+
"1 ..\n";

assertTrue(expectedWorld.equals(livingWorld.toString()));
}

@Test
/*
* Verify that when a world created with both living
* and dead cells will return the proper String when
* the toString() method is called
*/
public void testWorldOfLivingAndDeadCells()
{
Random rando = new Random();

Cell livingCell = mock(Cell.class);
Cell deadCell = mock(Cell.class);

when(livingCell.getStateRep()).thenReturn('.');
when(deadCell.getStateRep()).thenReturn('X');

Cell[][] worldToBe = {{livingCell, deadCell, livingCell, deadCell},
{deadCell, deadCell, livingCell, livingCell},
{deadCell, livingCell, deadCell, livingCell},
{livingCell, livingCell, deadCell, deadCell}};

World livingDeadWorld = new World(worldToBe, rando);

String expectedWorld = " 0123\n"+
"0 .X.X\n"+
"1 XX..\n"+
"2 X.X.\n"+
"3 ..XX\n";

assertTrue(expectedWorld.equals(livingDeadWorld.toString()));
}

@Test
/*
* Verify whether the row labeling is for a world is
* correct within the string of the World. This will
* also confirm whether the edge case of having more
* than ten rows still appropriately wraps the numbering
* around to start back over at 0
*/
public void testRowsInString()
{
int randoSeed = 0;
int boardSize = 100;
int percentAlive = 10;

World testWorld = new World(boardSize, randoSeed, percentAlive);

String worldString = testWorld.toString();

String[] worldRows = worldString.split("\n");

//Test row numbering
for(int i = 0; i < worldRows.length-1; i++)
{
int foundRow = Character.getNumericValue(worldRows[i+1].charAt(0));
int expectedRow = i % 10;

assertTrue(foundRow == expectedRow);
}

}

@Test
/*
* Verify whether the column labeling is for a world is
* correct within the string of the World. This will
* also confirm whether the edge case of having more
* than ten columns still appropriately wraps the numbering
* around to start back over at 0
*/
public void testColumnsInString()
{
int randoSeed = 0;
int boardSize = 100;
int percentAlive = 10;

World testWorld = new World(boardSize, randoSeed, percentAlive);

String worldString = testWorld.toString();

String[] worldRows = worldString.split("\n");

//Test column numbering
char[] columnNums = worldRows[0].substring(2).toCharArray();
for(int i = 0; i < columnNums.length; i++)
{
int foundColumn = Character.getNumericValue(columnNums[i]);
int expectedColumn = i % 10;
assertTrue(foundColumn == expectedColumn);
}

}


}
Binary file added doc/Deliverable4.docx
Binary file not shown.