From 1417692fb75b9b87574d573db446107f1fc62dfc Mon Sep 17 00:00:00 2001 From: Briguy52 Date: Thu, 7 Apr 2016 16:24:03 -0400 Subject: [PATCH 1/2] Done with TDD --- .classpath | 7 ++++ src/tdd/Sheet.java | 23 ++++++++++++ src/tdd/TestSheet.java | 84 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 .classpath create mode 100644 src/tdd/Sheet.java create mode 100644 src/tdd/TestSheet.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..3e0fb27 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..47690ed --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,23 @@ +package tdd; + +import java.util.HashMap; +import java.util.Map; + + +public class Sheet { + public Map myMap = new HashMap<>(); + + public String get(String string) { + return myMap.get(string) == null? "" : myMap.get(string); + } + + public void put(String key, String value) { + try { + Integer.parseInt(value); + value = value.trim(); + } + catch (Exception e) { + } + myMap.put(key, value); + } +} diff --git a/src/tdd/TestSheet.java b/src/tdd/TestSheet.java new file mode 100644 index 0000000..53cfc7a --- /dev/null +++ b/src/tdd/TestSheet.java @@ -0,0 +1,84 @@ +package tdd; + +import static org.junit.Assert.*; + +import org.junit.Before; +import org.junit.Test; + +public class TestSheet { + + // TDD says you write the test first for what you want to accomplish + + // First test written is simply to get your code to compile (create the class, write some methods, etc.) + + private Sheet mySheet; + + // Initial setup with @Before + @Before + public void setUp() { + mySheet = new Sheet(); + } + + @Test + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + + @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)); + } + + @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")); + } + + @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)); + } + + + +} From dc024c646dfa73c2a8a146ba77d1a41b99ec8185 Mon Sep 17 00:00:00 2001 From: Briguy52 Date: Thu, 7 Apr 2016 17:01:04 -0400 Subject: [PATCH 2/2] Done with Debug --- src/debug/ContainerArray.java | 22 +++++++++++++++++----- src/debug/ContainerArrayTest.java | 13 ++++++++++++- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..53ef641 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,17 +1,21 @@ package debug; +import java.util.ArrayList; +import java.util.Arrays; + +import java.util.List; public class ContainerArray { - private int initialCapacity = 10; + private int limit = 10; private int currentSize = 0; - private Object[] internalArray; + private E[] internalArray; public ContainerArray () { this(10); } public ContainerArray (int initialCapacity) { - internalArray = new Object[initialCapacity]; + internalArray = (E[]) new Object[initialCapacity]; } public void add (E element) { @@ -23,11 +27,19 @@ public int size () { } public void remove (E objectToRemove) { - currentSize--; + List myList = new ArrayList (Arrays.asList(this.internalArray)); + if(myList.remove(objectToRemove)) currentSize--; + this.internalArray = (E[]) myList.toArray(); } @SuppressWarnings("unchecked") public E get (int index) { - return (E)internalArray[index]; + return internalArray[index]; + } + + public static void main (String[] args) { + ContainerArray myContainer = new ContainerArray<>(); + myContainer.add("test"); + myContainer.remove("test"); } } diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..a8f14df 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -7,10 +7,21 @@ public class ContainerArrayTest { private ContainerArray myContainer = null; - + @Before public void setUp () { myContainer = new ContainerArray<>(); + + } + + @Test + public void testRemove() { + String alligator = "Alligator"; + String bear = "Bear"; + myContainer.add(alligator); + myContainer.add(bear); + myContainer.remove(alligator); + assertEquals("Remove should be different reference", bear, myContainer.get(0)); } @Test