Skip to content
Open
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
62 changes: 34 additions & 28 deletions src/debug/ContainerArray.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
package debug;

import java.util.ArrayList;

public class ContainerArray<E> {
private int initialCapacity = 10;
private int currentSize = 0;
private Object[] internalArray;

public ContainerArray () {
this(10);
}

public ContainerArray (int initialCapacity) {
internalArray = new Object[initialCapacity];
}

public void add (E element) {
internalArray[currentSize++] = element;
}

public int size () {
return currentSize;
}

public void remove (E objectToRemove) {
currentSize--;
}

@SuppressWarnings("unchecked")
public E get (int index) {
return (E)internalArray[index];
}
private int initialCapacity = 10;
private int currentSize = 0;
private ArrayList<Object> internalArray;

public ContainerArray() {
this(10);
}

public ContainerArray(int initialCapacity) {
internalArray = new ArrayList<>();
}

public void add(E element) {
internalArray.add(element);
}

public int size() {
return internalArray.size();
}

public void remove(E objectToRemove) {
internalArray.remove(objectToRemove);
}

@SuppressWarnings("unchecked")
public E get(int index) {

try {
return (E) internalArray.get(index);
} catch (Exception e) {
return null;
}
}
}
47 changes: 46 additions & 1 deletion src/debug/ContainerArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,52 @@ public class ContainerArrayTest {
public void setUp () {
myContainer = new ContainerArray<>();
}


@Test
public void testGetNegative()
{
assertEquals("Get negative", null, myContainer.get(-10));
}

@Test
public void testRemoveMany()
{
for(int i = 0; i < 100; i++)
myContainer.remove(Integer.toString(i));

assertEquals("Removes many", 0, myContainer.size());
}

@Test
public void testAddMany()
{
for(int i = 0; i < 100; i++)
myContainer.add(Integer.toString(i));

assertEquals("Add many", 100, myContainer.size());
}

@Test
public void testGetUnset()
{
myContainer.add("Alligator");
myContainer.add("Bear");
myContainer.add("Camel");

assertEquals("Get unset item", null, myContainer.get(10));
}

@Test
public void testRemove()
{
myContainer.add("Alligator");
myContainer.add("Bear");
myContainer.add("Camel");
myContainer.remove("Alligator");

assertEquals("Remove object", "Bear", myContainer.get(0));
}

@Test
public void testSizeChangeWithAdd () {
myContainer.add("Alligator");
Expand Down
33 changes: 33 additions & 0 deletions src/tdd/ContainerArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package debug;


public class ContainerArray<E> {
private int initialCapacity = 10;
private int currentSize = 0;
private Object[] internalArray;

public ContainerArray () {
this(10);
}

public ContainerArray (int initialCapacity) {
internalArray = new Object[initialCapacity];
}

public void add (E element) { //more than 10 items means error, need to copy to new array
internalArray[currentSize++] = element;
}

public int size () {
return currentSize;
}

public void remove (E objectToRemove) { //removing item which isn't in container, also item still stays there,
currentSize--; //only works as stack right now
} //may result in negative size

@SuppressWarnings("unchecked")
public E get (int index) { //negative index, index out of bounds
return (E)internalArray[index];
}
}
47 changes: 47 additions & 0 deletions src/tdd/ContainerArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package debug;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;


public class ContainerArrayTest {
private ContainerArray<String> myContainer = null;

@Before
public void setUp () {
myContainer = new ContainerArray<>();
}

@Test
public void testSizeChangeWithAdd () {
myContainer.add("Alligator");
myContainer.add("Bear");
myContainer.add("Camel");
assertEquals("Add size", 3, myContainer.size());
}

@Test
public void testObjectIsStored () {
String alligator = "Alligator";
myContainer.add(alligator);
assertEquals("Add should be same reference", alligator, myContainer.get(0));
}

@Test
public void testSizeChangeWithRemove () {
myContainer.add("Alligator");
myContainer.add("Bear");
myContainer.remove("Alligator");
assertEquals("Remove size", 1, myContainer.size());
}

@Test
public void testObjectIsRemoved () {
String alligator = "Alligator";
myContainer.add("Alligator");
myContainer.add("Bear");
myContainer.remove("Bear");
assertEquals("Remove should be same reference", alligator, myContainer.get(0));
}
}
41 changes: 41 additions & 0 deletions src/tdd/Sheet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package debug;

import java.util.HashMap;
import java.util.Map;

import com.sun.xml.internal.ws.util.StringUtils;

public class Sheet {

Map<String, String> data;

public Sheet() {
data = new HashMap<String, String>();
}

public void put(String cell, String value) {
data.put(cell, value);
}

public String get(String cell) {
if (data.containsKey(cell)) {
String newData = data.get(cell).trim();
if (newData.length() != 0 && newData.matches("[A-Za-z0-9]+")) {
return newData;
} else {
return data.get(cell);
}
} else {
return "";
}
}

public String getLiteral(String cell) {
if (data.containsKey(cell)) {
return data.get(cell);
} else {
return "";
}
}

}
98 changes: 98 additions & 0 deletions src/tdd/SheetTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package debug;

import static org.junit.Assert.*;
import org.junit.Test;


public class SheetTest {



@Test
public void testThatCellsAreEmptyByDefault() {
Sheet sheet = new Sheet();
assertEquals("", sheet.get("A1"));
assertEquals("", sheet.get("ZX347"));
}

// Implement each test before going to the next one.

@Test
public void testThatTextCellsAreStored() {
Sheet sheet = new Sheet();
String theCell = "A21";

sheet.put(theCell, "A string");
assertEquals("A string", sheet.get(theCell));

sheet.put(theCell, "A different string");
assertEquals("A different string", sheet.get(theCell));

sheet.put(theCell, "");
assertEquals("", sheet.get(theCell));
}

// Implement each test before going to the next one; then refactor.

@Test
public void testThatManyCellsExist() {
Sheet sheet = new Sheet();
sheet.put("A1", "First");
sheet.put("X27", "Second");
sheet.put("ZX901", "Third");

assertEquals("A1", "First", sheet.get("A1"));
assertEquals("X27", "Second", sheet.get("X27"));
assertEquals("ZX901", "Third", sheet.get("ZX901"));

sheet.put("A1", "Fourth");
assertEquals("A1 after", "Fourth", sheet.get("A1"));
assertEquals("X27 same", "Second", sheet.get("X27"));
assertEquals("ZX901 same", "Third", sheet.get("ZX901"));
}


// Implement each test before going to the next one.
// You can split this test case if it helps.

@Test
public void testThatNumericCellsAreIdentifiedAndStored() {
Sheet sheet = new Sheet();
String theCell = "A21";

sheet.put(theCell, "X99"); // "Obvious" string
assertEquals("X99", sheet.get(theCell));

sheet.put(theCell, "14"); // "Obvious" number
assertEquals("14", sheet.get(theCell));

sheet.put(theCell, " 99 X"); // Whole string must be numeric
assertEquals(" 99 X", sheet.get(theCell));

sheet.put(theCell, " 1234 "); // Blanks ignored
assertEquals("1234", sheet.get(theCell));

sheet.put(theCell, " "); // Just a blank
assertEquals(" ", sheet.get(theCell));
}

// Refactor before going to each succeeding test.

@Test
public void testThatWeHaveAccessToCellLiteralValuesForEditing() {
Sheet sheet = new Sheet();
String theCell = "A21";

sheet.put(theCell, "Some string");
assertEquals("Some string", sheet.getLiteral(theCell));

sheet.put(theCell, " 1234 ");
assertEquals(" 1234 ", sheet.getLiteral(theCell));

sheet.put(theCell, "=7"); // Foreshadowing formulas:)
assertEquals("=7", sheet.getLiteral(theCell));
}



}
Loading