diff --git a/src/debug/ContainerArray.java b/src/debug/ContainerArray.java index 124ece6..1e1bc13 100755 --- a/src/debug/ContainerArray.java +++ b/src/debug/ContainerArray.java @@ -1,8 +1,10 @@ package debug; +import java.util.Arrays; + public class ContainerArray { - private int initialCapacity = 10; + private int capacity = 10; private int currentSize = 0; private Object[] internalArray; @@ -11,6 +13,8 @@ public ContainerArray () { } public ContainerArray (int initialCapacity) { + + capacity = initialCapacity; internalArray = new Object[initialCapacity]; } @@ -23,9 +27,26 @@ public int size () { } public void remove (E objectToRemove) { - currentSize--; + for (int i = 0; i < currentSize; i++){ + if (get(i).equals(objectToRemove)){ + internalArray = concatArray(capacity, Arrays.copyOfRange(internalArray,0,i),Arrays.copyOfRange(internalArray,i+1,currentSize)); + currentSize--; + i--; + } + } } + private Object[] concatArray(int size, Object[] a1, Object[] a2){ + Object[] arr = new Object[size]; + int i; + for (i = 0; i < a1.length; i++){ + arr[i] = a1[i]; + } + for (int j = 0; j < a2.length; j++){ + arr[i++] = a2[j]; + } + return arr; + } @SuppressWarnings("unchecked") public E get (int index) { return (E)internalArray[index]; diff --git a/src/debug/ContainerArrayTest.java b/src/debug/ContainerArrayTest.java index c566d50..a3e6b9c 100755 --- a/src/debug/ContainerArrayTest.java +++ b/src/debug/ContainerArrayTest.java @@ -5,6 +5,7 @@ import org.junit.Test; + public class ContainerArrayTest { private ContainerArray myContainer = null; @@ -44,4 +45,27 @@ public void testObjectIsRemoved () { myContainer.remove("Bear"); assertEquals("Remove should be same reference", alligator, myContainer.get(0)); } + + @Test + public void testIfRemoveTwiceFails(){ + myContainer.add("Alligator"); + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + assertEquals("Size should be 1", 1, myContainer.size()); + } + @Test + public void testIfAllObjectsAreRemoved(){ + myContainer.add("Alligator"); + myContainer.add("Alligator"); + myContainer.remove("Alligator"); + assertEquals("Array should have no elements", 0, myContainer.size()); + } + @Test + public void testIfObjectIsRemoved(){ + myContainer.add("Alligator"); + myContainer.add("Bear"); + myContainer.remove("Alligator"); + assertEquals("Remove element", "Bear", myContainer.get(0)); + } } diff --git a/src/tdd/Sheet.java b/src/tdd/Sheet.java new file mode 100644 index 0000000..eb08bf5 --- /dev/null +++ b/src/tdd/Sheet.java @@ -0,0 +1,41 @@ +package tdd; + +import java.util.HashMap; + +/** + * Created by aamir on 4/7/2016. + */ +public class Sheet{ + private HashMap map; + + public Sheet(){ + map = new HashMap<>(); + } + public void put(String key, String val){ + map.put(key,val); + } + + public String get(String key){ + String value = map.get(key); + if (isNumeric(key.trim())){ + return key.trim(); + } + return key; + } + + public String getLiteral(String key){ + return map.get(key); + } + private boolean isNumeric(String str) + { + try + { + double d = Double.parseDouble(str); + } + catch(NumberFormatException nfe) + { + return false; + } + return true; + } +} diff --git a/src/tdd/Tests.java b/src/tdd/Tests.java new file mode 100644 index 0000000..c9aced9 --- /dev/null +++ b/src/tdd/Tests.java @@ -0,0 +1,90 @@ +package tdd; +import static org.junit.Assert.*; + +/** + * Created by aamir on 4/7/2016. + */ +public class Tests { + public void testThatCellsAreEmptyByDefault() { + Sheet sheet = new Sheet(); + assertEquals("", sheet.get("A1")); + assertEquals("", sheet.get("ZX347")); + } + +// Implement each test before going to the next one. + + 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. + + 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. + + 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. + + 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)); + } + + +// We'll talk about "get" and formulas next time. +} diff --git a/src/voogasalad/Tests.md b/src/voogasalad/Tests.md new file mode 100644 index 0000000..b234c41 --- /dev/null +++ b/src/voogasalad/Tests.md @@ -0,0 +1,7 @@ +### Possible Tests for VoogaSalad + +The tests that we wanted to implement, once our code is refactored and working, are as follows: + +1. Test if, when a user adds a unit, it is represented correctly. And if you change the image of a unit, that image is properly stored in its object. + +2. Test if a unit's default abilities can be changed several times, i.e. if I set the default offensive ability as convert, then attack, that the default abilities properly represent that even after changing it real time.