diff --git a/src/main/java/ArrayListCombiner/ArrayListCombiner.java b/src/main/java/ArrayListCombiner/ArrayListCombiner.java index d302cd2..d24ebb6 100644 --- a/src/main/java/ArrayListCombiner/ArrayListCombiner.java +++ b/src/main/java/ArrayListCombiner/ArrayListCombiner.java @@ -8,5 +8,12 @@ * The first method should be called extendCombiner and should use ? extends E * The second method should be called superCombiner and should use ? super E */ -public class ArrayListCombiner { -} +public class ArrayListCombiner { //the second array extends from the first array so it will inherit the behavior from the first array + public static void superCombiner(ArrayList firstArray, ArrayList secondArray){ + firstArray.addAll(secondArray); //addall will append all elements of the second array to end of the first array + } + public static void extendCombiner(ArrayList firstArray, ArrayList secondArray){ //making the arrays able to combine + firstArray.addAll(secondArray);//add all will combine the second array elements to the end of the first + } //the firstArray is the parent class to the second array according to the test + //Manager extends employee +} \ No newline at end of file diff --git a/src/main/java/MapFunc/MapFunc.java b/src/main/java/MapFunc/MapFunc.java index ed4bf66..a9d3927 100644 --- a/src/main/java/MapFunc/MapFunc.java +++ b/src/main/java/MapFunc/MapFunc.java @@ -7,6 +7,12 @@ * Create a function called `map` that takes an ArrayList and a `Function` object, * and returns an ArrayList with all of the elements of the first after the function is applied to them. */ -public class MapFunc { - +public class MapFunc {//T being my input type and R being my output type + public static ArrayList map (ArrayList array, Function functor){ + ArrayList arrayList = new ArrayList<>();//empty arraylist + for (T thelid : array) { //iterate through my array for each of the lids of type T + arrayList.add(functor.apply(thelid));//in my new list add the function being applied + } //to each of the lids + return arrayList; //arrayList being my output type + } } diff --git a/src/main/java/Pair/Arrays.java b/src/main/java/Pair/Arrays.java index 5bdf780..f9384c9 100644 --- a/src/main/java/Pair/Arrays.java +++ b/src/main/java/Pair/Arrays.java @@ -11,6 +11,28 @@ * And a minmax method that returns a pair containing the largest and smallest items from the array list */ public class Arrays { - public static <___> Pair firstLast(ArrayList<___> a) { + + @SuppressWarnings("unchecked") + public static Pair firstLast(ArrayList arrayList) { + return new Pair(arrayList.get(0), arrayList.get(arrayList.size() - 1)); + } + + @SuppressWarnings("unchecked") + public static E min(ArrayList arrayList) { + Collections.sort(arrayList); + return arrayList.get(0); + } + + @SuppressWarnings("unchecked") + public static E max(ArrayList arrayList) { + Collections.sort(arrayList); + return arrayList.get(arrayList.size() - 1); + } + + @SuppressWarnings("unchecked") + public static Pair minMax(ArrayList arrayList) { + Collections.sort(arrayList); + return new Pair(arrayList.get(0), arrayList.get(arrayList.size() - 1)); } } + diff --git a/src/main/java/Pair/Pair.java b/src/main/java/Pair/Pair.java index c4dd905..cf89ba0 100644 --- a/src/main/java/Pair/Pair.java +++ b/src/main/java/Pair/Pair.java @@ -7,6 +7,48 @@ * min -> returns the minimum of the pair * max -> returns the maximum of the pair */ -public class Pair { +public class Pair { + //without extending you cannot compare generic types + //by extending comparing individual values of generic types is now viable + private E firstValue; + private E secondValue; -} + Pair(E first, E second){ + this.firstValue = first; + this.secondValue = second; + } + + private int compareTo(E otherValue) { + return firstValue.compareTo(otherValue); + } + + public E getFirst(){ + return firstValue; + } + + public E getSecond(){ + return secondValue; + } + + public E min(){ + int compareResult = compareTo(firstValue); + boolean secondValueIsGreater = compareResult == -1; + + if (secondValueIsGreater) { + return secondValue; + } else { + return firstValue; + } + } + + public E max(){ + int compareResult = compareTo(secondValue); + boolean firstValIsGreater = compareResult == 1; + + if (firstValIsGreater) { + return firstValue; + } else { + return secondValue; + } + } +} \ No newline at end of file diff --git a/src/main/java/StackArray/GenericStack.java b/src/main/java/StackArray/GenericStack.java index d84c4db..7d2fa5e 100644 --- a/src/main/java/StackArray/GenericStack.java +++ b/src/main/java/StackArray/GenericStack.java @@ -9,7 +9,27 @@ */ public class GenericStack { private E[] elements; + private int size; //actual number of things as opposed to length public GenericStack() { + this.elements = (E[]) new Object[0]; + } + + public boolean isEmpty() { + return size==0; + } + + public E push(E foobar) { //push onto to the top of the stack which is the end + this.elements = Arrays.copyOf(this.elements,this.elements.length + 1); + this.elements[this.elements.length - 1] = foobar; //capacity of array not size + size++; + return foobar; + } + + public E pop() { //remove object at the top of the stack + E thingy = this.elements[elements.length - 1]; //capacity of array not size + this.elements = Arrays.copyOf(this.elements,this.elements.length - 1); + size--; // + return thingy; } } diff --git a/src/main/java/StackArray/ObjectStack.java b/src/main/java/StackArray/ObjectStack.java index 1124698..2500cf8 100644 --- a/src/main/java/StackArray/ObjectStack.java +++ b/src/main/java/StackArray/ObjectStack.java @@ -9,8 +9,27 @@ */ public class ObjectStack { private Object[] elements; + private int size; public ObjectStack() { + this.elements = new Object[0]; + } + + public boolean isEmpty() { + return size==0; + } + + public Object push(Object foobar) { //push onto to the top of the stack which is the end + this.elements = Arrays.copyOf(this.elements,this.elements.length + 1); + this.elements[this.elements.length - 1] = foobar; //capacity of array not size + size++; + return foobar; + } + public Object pop() { //remove object at the top of the stack + Object thingy = this.elements[elements.length - 1]; //capacity of array not size + this.elements = Arrays.copyOf(this.elements,this.elements.length - 1); + size--; // + return thingy; } } diff --git a/src/main/java/StackArrayList/Stack.java b/src/main/java/StackArrayList/Stack.java index 0338de3..bb1510a 100644 --- a/src/main/java/StackArrayList/Stack.java +++ b/src/main/java/StackArrayList/Stack.java @@ -7,10 +7,25 @@ * If you pop on an empty stack, throw an IndexOutOfBoundsException. */ public class Stack { - private ArrayList elements; + private ArrayList elements; - public Stack(){ + public Stack() { + this.elements = new ArrayList(); + } + + public boolean isEmpty() {//if the array is empty return true otherwise return false + if (elements.size() == 0) return true; + else return false; + } + + //@SuppressWarnings("unchecked") + public void push(E foobar) { + elements.add(foobar); + } + //@SuppressWarnings("unchecked") + public E pop() { + return elements.remove(elements.size()-1); + } } -} diff --git a/src/main/java/Table/Entry.java b/src/main/java/Table/Entry.java index 8c97478..998d897 100644 --- a/src/main/java/Table/Entry.java +++ b/src/main/java/Table/Entry.java @@ -16,5 +16,4 @@ public K getKey() { public V getValue() { return value; } - } diff --git a/src/main/java/Table/Table.java b/src/main/java/Table/Table.java index 5ccce23..e6878cf 100644 --- a/src/main/java/Table/Table.java +++ b/src/main/java/Table/Table.java @@ -10,8 +10,33 @@ * Void return on `remove`. */ public class Table { - private ArrayList entries; + private ArrayList entries; + @SuppressWarnings("unchecked") public Table() { + this.entries = new ArrayList(); + } + + @SuppressWarnings("unchecked") + public V get(K foo) { + for (Entry submission : entries) { //for each submission in my entries array + if (submission.getKey().equals(foo)) { //if key of my submission object is equal to the key passed in + return (V)submission.getValue(); //return that value mapped to that key + } + } + return null; + } + + public void put(K foo, V i) { + remove(foo); //remove foo from the key + Entry entry = new Entry<>(foo, i); //create empty entry object for table + entries.add(entry); //add foo back into the empty entry within entries + } + + public void remove(K foo) { + for (int i = 0; i < entries.size(); i++){ //iterate through arrayList + if (entries.get(i).getKey().equals(foo)) //if the key of the current index of entries is equal to foo + entries.remove(i);//if the above is true then remove it + } } } diff --git a/src/main/java/TableNested/TableNested.java b/src/main/java/TableNested/TableNested.java index 7e0dfdd..29dfb2a 100644 --- a/src/main/java/TableNested/TableNested.java +++ b/src/main/java/TableNested/TableNested.java @@ -1,5 +1,6 @@ package TableNested; +import Table.Entry; //imported the entire table entry class import java.util.ArrayList; /** @@ -7,5 +8,33 @@ * Think about how nested classes should work with generics. */ public class TableNested { + private ArrayList entries; + @SuppressWarnings("unchecked") + public TableNested() { + this.entries = new ArrayList(); + } + + @SuppressWarnings("unchecked") + public V get(K foo) { + for (Entry submission : entries) { //for each submission in my entries array + if (submission.getKey().equals(foo)) { //if key of my submission object is equal to the key passed in + return (V) submission.getValue(); //return that value mapped to that key + } + } + return null; + } + + public void put(K foo, V i) { + remove(foo); //remove foo from the key + Entry entry = new Entry<>(foo, i); //create empty entry object for table + entries.add(entry); //add foo back into the empty entry within entries + } + + public void remove(K foo) { + for (int i = 0; i < entries.size(); i++) { //iterate through arrayList + if (entries.get(i).getKey().equals(foo)) //if the key of the current index of entries is equal to foo + entries.remove(i);//if the above is true then remove it + } + } } diff --git a/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java b/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java index 957a878..9e16484 100644 --- a/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java +++ b/src/test/java/ArrayListCombiner/ArrayListCombinerTest.java @@ -9,40 +9,40 @@ import java.util.ArrayList; public class ArrayListCombinerTest { -// Employee foo = new Employee("FOO", 100); -// Manager bar = new Manager("BAR", 100); -// @Test -// public void testExtendCombiner() throws Exception { -// // Given an array list with employees -// ArrayList first = new ArrayList<>(); -// first.add(foo); -// // An an array list with managers -// ArrayList second = new ArrayList<>(); -// second.add(bar); -// // When I combine them -// ArrayListCombiner.extendCombiner(first, second); -// // Then I should get an arrayList with both -// ArrayList expected = new ArrayList<>(); -// expected.add(foo); -// expected.add(bar); -// Assert.assertEquals(expected, first); -// } -// -// @Test -// public void testSuperCombiner() throws Exception { -// // Given an array list with employees -// ArrayList first = new ArrayList<>(); -// first.add(foo); -// // An an array list with managers -// ArrayList second = new ArrayList<>(); -// second.add(bar); -// // When I combine them -// ArrayListCombiner.superCombiner(first, second); -// // Then I should get an arrayList with both -// ArrayList expected = new ArrayList<>(); -// expected.add(foo); -// expected.add(bar); -// Assert.assertEquals(expected, first); -// } + Employee foo = new Employee("FOO", 100); + Manager bar = new Manager("BAR", 100); + @Test + public void testExtendCombiner() throws Exception { + // Given an array list with employees + ArrayList first = new ArrayList<>(); + first.add(foo); + // An an array list with managers + ArrayList second = new ArrayList<>(); + second.add(bar); + // When I combine them + ArrayListCombiner.extendCombiner(first, second); + // Then I should get an arrayList with both + ArrayList expected = new ArrayList<>(); + expected.add(foo); + expected.add(bar); + Assert.assertEquals(expected, first); + } + + @Test + public void testSuperCombiner() throws Exception { + // Given an array list with employees + ArrayList first = new ArrayList<>(); + first.add(foo); + // An an array list with managers + ArrayList second = new ArrayList<>(); + second.add(bar); + // When I combine them + ArrayListCombiner.superCombiner(first, second); + // Then I should get an arrayList with both + ArrayList expected = new ArrayList<>(); + expected.add(foo); + expected.add(bar); + Assert.assertEquals(expected, first); + } } \ No newline at end of file diff --git a/src/test/java/MapFunc/MapFuncTest.java b/src/test/java/MapFunc/MapFuncTest.java index 3c7534f..a3fdf58 100644 --- a/src/test/java/MapFunc/MapFuncTest.java +++ b/src/test/java/MapFunc/MapFuncTest.java @@ -1,36 +1,36 @@ -//package MapFunc; -// -//import MapFunc.MapFunc; -//import org.junit.Test; -// -//import java.util.ArrayList; -//import org.junit.Assert; -// -//public class MapFuncTest { -// @Test -// public void testSingleTypeMap() throws Exception { -// // Given an integer array list -// ArrayList intList = new ArrayList<>(); -// intList.add(1); -// intList.add(2); -// // When it's mapped with a function to double the value -// ArrayList mappedList = MapFunc.map(intList, num -> num*2); -// // Then all the values are doubled -// Assert.assertEquals(new Integer(2), mappedList.get(0)); -// Assert.assertEquals(new Integer(4), mappedList.get(1)); -// } -// -// @Test -// public void testMultipleTypeMap() throws Exception { -// // Given an integer array list -// ArrayList intList = new ArrayList<>(); -// intList.add(1); -// intList.add(2); -// // When it's mapped with to string -// ArrayList mappedList = MapFunc.map(intList, num -> num.toString()); -// // Then all the values are doubled -// Assert.assertEquals("1", mappedList.get(0)); -// Assert.assertEquals("2", mappedList.get(1)); -// } -// -//} \ No newline at end of file +package MapFunc; + +import MapFunc.MapFunc; +import org.junit.Test; + +import java.util.ArrayList; +import org.junit.Assert; + +public class MapFuncTest { + @Test + public void testSingleTypeMap() throws Exception { + // Given an integer array list + ArrayList intList = new ArrayList<>(); + intList.add(1); + intList.add(2); + // When it's mapped with a function to double the value + ArrayList mappedList = MapFunc.map(intList, num -> num*2); + // Then all the values are doubled + Assert.assertEquals(new Integer(2), mappedList.get(0)); + Assert.assertEquals(new Integer(4), mappedList.get(1)); + } + + @Test + public void testMultipleTypeMap() throws Exception { + // Given an integer array list + ArrayList intList = new ArrayList<>(); + intList.add(1); + intList.add(2); + // When it's mapped with to string + ArrayList mappedList = MapFunc.map(intList, num -> num.toString()); + // Then all the values are doubled + Assert.assertEquals("1", mappedList.get(0)); + Assert.assertEquals("2", mappedList.get(1)); + } + +} \ No newline at end of file diff --git a/src/test/java/Pair/ArraysTest.java b/src/test/java/Pair/ArraysTest.java index 4d32e23..0d51970 100644 --- a/src/test/java/Pair/ArraysTest.java +++ b/src/test/java/Pair/ArraysTest.java @@ -1,74 +1,74 @@ -//package Pair; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//import java.util.ArrayList; -// -//public class ArraysTest { -// @Test -// public void firstLast() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When firstLast is called -// Pair result = Arrays.firstLast(al); -// // Then it should return the first and last items -// Assert.assertEquals(new Integer(1), result.getFirst()); -// Assert.assertEquals(new Integer(1000), result.getSecond()); -// } -// -// @Test -// public void testMin() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When min is called assert that it gets the smallest item -// Assert.assertEquals(new Integer(0), Arrays.min(al)); -// } -// -// @Test -// public void testMax() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When min is called assert that it gets the largest item -// Assert.assertEquals(new Integer(1000), Arrays.max(al)); -// } -// -// @Test -// public void testMinMax() throws Exception { -// // Given an ArrayList of Integers -// ArrayList al = new ArrayList<>(); -// al.add(1); -// al.add(5); -// al.add(3); -// al.add(4); -// al.add(2); -// al.add(0); -// al.add(1000); -// // When minMax is called -// Pair result = Arrays.minMax(al); -// // Then it should return the first and last items -// Assert.assertEquals(new Integer(0), result.min()); -// Assert.assertEquals(new Integer(1000), result.max()); -// } -//} +package Pair; + +import org.junit.Assert; +import org.junit.Test; + +import java.util.ArrayList; + +public class ArraysTest { + @Test + public void firstLast() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When firstLast is called + Pair result = Arrays.firstLast(al); + // Then it should return the first and last items + Assert.assertEquals(new Integer(1), result.getFirst()); + Assert.assertEquals(new Integer(1000), result.getSecond()); + } + + @Test + public void testMin() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When min is called assert that it gets the smallest item + Assert.assertEquals(new Integer(0), Arrays.min(al)); + } + + @Test + public void testMax() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When min is called assert that it gets the largest item + Assert.assertEquals(new Integer(1000), Arrays.max(al)); + } + + @Test + public void testMinMax() throws Exception { + // Given an ArrayList of Integers + ArrayList al = new ArrayList<>(); + al.add(1); + al.add(5); + al.add(3); + al.add(4); + al.add(2); + al.add(0); + al.add(1000); + // When minMax is called + Pair result = Arrays.minMax(al); + // Then it should return the first and last items + Assert.assertEquals(new Integer(0), result.min()); + Assert.assertEquals(new Integer(1000), result.max()); + } +} diff --git a/src/test/java/Pair/PairTest.java b/src/test/java/Pair/PairTest.java index d616178..baf2071 100644 --- a/src/test/java/Pair/PairTest.java +++ b/src/test/java/Pair/PairTest.java @@ -1,32 +1,32 @@ -//package Pair; -// -//import org.junit.Test; -//import org.junit.Assert; -// -//public class PairTest { -// -// @Test -// public void testGetters() throws Exception { -// // Given a pair with "Foo" and "Bar" -// Pair p = new Pair("Foo", "Bar"); -// // When getFirst and getSecond are called, they should be returned. -// Assert.assertEquals("Foo", p.getFirst()); -// Assert.assertEquals("Bar", p.getSecond()); -// } -// -// @Test -// public void testMin() throws Exception { -// // Given a pair with two values -// Pair p = new Pair(1.23, 2.34); -// // When p.min() is called, the smallest should be returned. -// Assert.assertEquals(new Double(1.23), p.min()); -// } -// -// @Test -// public void testMax() throws Exception { -// // Given a pair with two values -// Pair p = new Pair(1.23, 2.34); -// // When p.max() is called, the largest should be returned. -// Assert.assertEquals(new Double(2.34), p.max()); -// } -//} \ No newline at end of file +package Pair; + +import org.junit.Test; +import org.junit.Assert; + +public class PairTest { + + @Test + public void testGetters() throws Exception { + // Given a pair with "Foo" and "Bar" + Pair p = new Pair("Foo", "Bar"); + // When getFirst and getSecond are called, they should be returned. + Assert.assertEquals("Foo", p.getFirst()); + Assert.assertEquals("Bar", p.getSecond()); + } + + @Test + public void testMin() throws Exception { + // Given a pair with two values + Pair p = new Pair(1.23, 2.34); + // When p.min() is called, the smallest should be returned. + Assert.assertEquals(new Double(1.23), p.min()); + } + + @Test + public void testMax() throws Exception { + // Given a pair with two values + Pair p = new Pair(1.23, 2.34); + // When p.max() is called, the largest should be returned. + Assert.assertEquals(new Double(2.34), p.max()); + } +} \ No newline at end of file diff --git a/src/test/java/StackArray/GenericStackTest.java b/src/test/java/StackArray/GenericStackTest.java index 0aacd92..c61b904 100644 --- a/src/test/java/StackArray/GenericStackTest.java +++ b/src/test/java/StackArray/GenericStackTest.java @@ -1,41 +1,41 @@ -//package StackArray; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class GenericStackTest { -// @Test -// public void testPushingGrowsTheStack() throws Exception { -// // Given an empty stack -// GenericStack stack = new GenericStack<>(); -// -// // Assert that it is empty. -// Assert.assertEquals(true, stack.isEmpty()); -// // When we push something onto the stack -// stack.push("foobar"); -// // Then it shouldn't be empty -// Assert.assertEquals(false, stack.isEmpty()); -// } -// -// @Test -// public void testPushingAndPoppingOrder() throws Exception { -// // Given an empty stack -// GenericStack stack = new GenericStack<>(); -// // When we push two elements on it -// stack.push("foo"); -// stack.push("bar"); -// // Then we should see them returned in the correct order -// Assert.assertEquals("bar", stack.pop()); -// Assert.assertEquals("foo", stack.pop()); -// } -// -// @Test(expected = IndexOutOfBoundsException.class) -// public void testPopFirst() throws Exception { -// // Given an empty stack -// GenericStack stack = new GenericStack<>(); -// // When it's popped -// stack.pop(); -// // Then we should get an exception -// } -// -//} \ No newline at end of file +package StackArray; + +import org.junit.Assert; +import org.junit.Test; + +public class GenericStackTest { + @Test + public void testPushingGrowsTheStack() throws Exception { + // Given an empty stack + GenericStack stack = new GenericStack<>(); + + // Assert that it is empty. + Assert.assertEquals(true, stack.isEmpty()); + // When we push something onto the stack + stack.push("foobar"); + // Then it shouldn't be empty + Assert.assertEquals(false, stack.isEmpty()); + } + + @Test + public void testPushingAndPoppingOrder() throws Exception { + // Given an empty stack + GenericStack stack = new GenericStack<>(); + // When we push two elements on it + stack.push("foo"); + stack.push("bar"); + // Then we should see them returned in the correct order + Assert.assertEquals("bar", stack.pop()); + Assert.assertEquals("foo", stack.pop()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPopFirst() throws Exception { + // Given an empty stack + GenericStack stack = new GenericStack<>(); + // When it's popped + stack.pop(); + // Then we should get an exception + } + +} \ No newline at end of file diff --git a/src/test/java/StackArray/ObjectStackTest.java b/src/test/java/StackArray/ObjectStackTest.java index 9ec9615..2576f1b 100644 --- a/src/test/java/StackArray/ObjectStackTest.java +++ b/src/test/java/StackArray/ObjectStackTest.java @@ -1,39 +1,39 @@ -//package StackArray; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class ObjectStackTest { -// @Test -// public void testPushingGrowsTheStack() throws Exception { -// // Given an empty stack -// ObjectStack stack = new ObjectStack<>(); -// // Assert that it is empty. -// Assert.assertEquals(true, stack.isEmpty()); -// // When we push something onto the stack -// stack.push("foobar"); -// // Then it shouldn't be empty -// Assert.assertEquals(false, stack.isEmpty()); -// } -// -// @Test -// public void testPushingAndPoppingOrder() throws Exception { -// // Given an empty stack -// ObjectStack stack = new ObjectStack<>(); -// // When we push two elements on it -// stack.push("foo"); -// stack.push("bar"); -// // Then we should see them returned in the correct order -// Assert.assertEquals("bar", stack.pop()); -// Assert.assertEquals("foo", stack.pop()); -// } -// -// @Test(expected = IndexOutOfBoundsException.class) -// public void testPopFirst() throws Exception { -// // Given an empty stack -// ObjectStack stack = new ObjectStack<>(); -// // When it's popped -// stack.pop(); -// // Then we should get an exception -// } -//} \ No newline at end of file +package StackArray; + +import org.junit.Assert; +import org.junit.Test; + +public class ObjectStackTest { + @Test + public void testPushingGrowsTheStack() throws Exception { + // Given an empty stack + ObjectStack stack = new ObjectStack<>(); + // Assert that it is empty. + Assert.assertEquals(true, stack.isEmpty()); + // When we push something onto the stack + stack.push("foobar"); + // Then it shouldn't be empty + Assert.assertEquals(false, stack.isEmpty()); + } + + @Test + public void testPushingAndPoppingOrder() throws Exception { + // Given an empty stack + ObjectStack stack = new ObjectStack<>(); + // When we push two elements on it + stack.push("foo"); + stack.push("bar"); + // Then we should see them returned in the correct order + Assert.assertEquals("bar", stack.pop()); + Assert.assertEquals("foo", stack.pop()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPopFirst() throws Exception { + // Given an empty stack + ObjectStack stack = new ObjectStack<>(); + // When it's popped + stack.pop(); + // Then we should get an exception + } +} \ No newline at end of file diff --git a/src/test/java/StackArrayList/StackTest.java b/src/test/java/StackArrayList/StackTest.java index 0ce7cf0..00dc659 100644 --- a/src/test/java/StackArrayList/StackTest.java +++ b/src/test/java/StackArrayList/StackTest.java @@ -1,45 +1,45 @@ -//package StackArrayList; -// -//import org.junit.Test; -// -//import org.junit.Assert; -// -//public class StackTest { -// @Test -// public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception { -// // Given an empty stack -// Stack stack = new Stack<>(); -// // Assert that it starts empty -// Assert.assertEquals(true, stack.isEmpty()); -// // When an element gets pushed -// stack.push("foobar"); -// // Then the stack should not be empty. -// Assert.assertEquals(false, stack.isEmpty()); -// } -// -// @Test -// public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception { -// // Given an empty stack -// Stack stack = new Stack<>(); -// -// //When two items are pushed -// stack.push("foo"); -// stack.push("bar"); -// -// // Then they should come off in reverse order. -// Assert.assertEquals("bar", stack.pop()); -// Assert.assertEquals("foo", stack.pop()); -// -// // And then the stack should be empty -// Assert.assertEquals(true, stack.isEmpty()); -// } -// -// @Test(expected = IndexOutOfBoundsException.class) -// public void testPopFirst() throws Exception { -// // Given an empty stack -// Stack stack = new Stack<>(); -// // Then it is popped -// stack.pop(); -// // We should get an exception -// } -//} \ No newline at end of file +package StackArrayList; + +import org.junit.Test; + +import org.junit.Assert; + +public class StackTest { + @Test + public void testEmptyStackStopsBeingEmptyWhenAnItemIsAdded() throws Exception { + // Given an empty stack + Stack stack = new Stack<>(); + // Assert that it starts empty + Assert.assertEquals(true, stack.isEmpty()); + // When an element gets pushed + stack.push("foobar"); + // Then the stack should not be empty. + Assert.assertEquals(false, stack.isEmpty()); + } + + @Test + public void testTwoItemsPushedComeOutInCorrectOrder() throws Exception { + // Given an empty stack + Stack stack = new Stack<>(); + + //When two items are pushed + stack.push("foo"); + stack.push("bar"); + + // Then they should come off in reverse order. + Assert.assertEquals("bar", stack.pop()); + Assert.assertEquals("foo", stack.pop()); + + // And then the stack should be empty + Assert.assertEquals(true, stack.isEmpty()); + } + + @Test(expected = IndexOutOfBoundsException.class) + public void testPopFirst() throws Exception { + // Given an empty stack + Stack stack = new Stack<>(); + // Then it is popped + stack.pop(); + // We should get an exception + } +} \ No newline at end of file diff --git a/src/test/java/Swap/SwapTest.java b/src/test/java/Swap/SwapTest.java index 2583b9d..a5f414e 100644 --- a/src/test/java/Swap/SwapTest.java +++ b/src/test/java/Swap/SwapTest.java @@ -1,16 +1,16 @@ package Swap; -// -//import org.junit.Assert; -//import org.junit.Test; -// -///** -// * Get the tests passing. -// */ -//public class SwapTest { -// @Test -// public void testSwap() throws Exception { -// Double[] result = Swap.swap(0,1, 1.5, 2,3); -// Double[] expected = {2.0, 1.5, 3.0}; -// Assert.assertArrayEquals(expected, result); -// } -//} \ No newline at end of file + +import org.junit.Assert; +import org.junit.Test; + +/** + * Get the tests passing. + */ +public class SwapTest { + @Test + public void testSwap() throws Exception { + Integer[] result = Swap.swap(0,1, 50, 2,3); + Integer[] expected = {2, 50, 3}; + Assert.assertArrayEquals(expected, result); + } +} \ No newline at end of file diff --git a/src/test/java/Table/TableTest.java b/src/test/java/Table/TableTest.java index 15ac19c..8bebde6 100644 --- a/src/test/java/Table/TableTest.java +++ b/src/test/java/Table/TableTest.java @@ -1,50 +1,50 @@ -//package Table; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class TableTest { -// @Test -// public void testGetWithoutAnItemReturnsNull() throws Exception { -// // Given an empty table -// Table table = new Table(); -// // When we try and get an item then it returns null -// Assert.assertEquals(table.get("foo"), null); -// } -// -// @Test -// public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { -// //Given an empty table -// Table table = new Table(); -// // When we put an item in it -// table.put("foo", 1); -// // Then we should be able to get it's value -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// // And then we should be able to get it again as it wasn't removed -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// } -// -// @Test -// public void testOverwritingAnItem() throws Exception { -// //Given an empty table -// Table table = new Table(); -// // When we put an item in it -// table.put("foo", 1); -// // And we put a new value with the same key -// table.put("foo", 2); -// // Then we should get back the new value -// Assert.assertEquals(table.get("foo"), new Integer(2)); -// } -// -// @Test -// public void testRemoveAnItem() throws Exception { -// //Given an empty table -// Table table = new Table(); -// // When we put an item in it -// table.put("foo", 1); -// // And we remove that item -// table.remove("foo"); -// // Then we should get back null for that balue -// Assert.assertEquals(table.get("foo"), null); -// } -//} \ No newline at end of file +package Table; + +import org.junit.Assert; +import org.junit.Test; + +public class TableTest { + @Test + public void testGetWithoutAnItemReturnsNull() throws Exception { + // Given an empty table + Table table = new Table(); + // When we try and get an item then it returns null + Assert.assertEquals(table.get("foo"), null); + } + + @Test + public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { + //Given an empty table + Table table = new Table(); + // When we put an item in it + table.put("foo", 1); + // Then we should be able to get it's value + Assert.assertEquals(table.get("foo"), new Integer(1)); + // And then we should be able to get it again as it wasn't removed + Assert.assertEquals(table.get("foo"), new Integer(1)); + } + + @Test + public void testOverwritingAnItem() throws Exception { + //Given an empty table + Table table = new Table(); + // When we put an item in it + table.put("foo", 1); + // And we put a new value with the same key + table.put("foo", 2); + // Then we should get back the new value + Assert.assertEquals(table.get("foo"), new Integer(2)); + } + + @Test + public void testRemoveAnItem() throws Exception { + //Given an empty table + Table table = new Table(); + // When we put an item in it + table.put("foo", 1); + // And we remove that item + table.remove("foo"); + // Then we should get back null for that balue + Assert.assertEquals(table.get("foo"), null); + } +} \ No newline at end of file diff --git a/src/test/java/TableNested/TableNestedTest.java b/src/test/java/TableNested/TableNestedTest.java index 8432277..fdf12d5 100644 --- a/src/test/java/TableNested/TableNestedTest.java +++ b/src/test/java/TableNested/TableNestedTest.java @@ -1,50 +1,50 @@ -//package TableNested; -// -//import org.junit.Assert; -//import org.junit.Test; -// -//public class TableNestedTest { -// @Test -// public void testGetWithoutAnItemReturnsNull() throws Exception { -// // Given an empty table -// TableNested table = new TableNested(); -// // When we try and get an item then it returns null -// Assert.assertEquals(table.get("foo"), null); -// } -// -// @Test -// public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { -// //Given an empty table -// TableNested table = new TableNested(); -// // When we put an item in it -// table.put("foo", 1); -// // Then we should be able to get it's value -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// // And then we should be able to get it again as it wasn't removed -// Assert.assertEquals(table.get("foo"), new Integer(1)); -// } -// -// @Test -// public void testOverwritingAnItem() throws Exception { -// //Given an empty table -// TableNested table = new TableNested(); -// // When we put an item in it -// table.put("foo", 1); -// // And we put a new value with the same key -// table.put("foo", 2); -// // Then we should get back the new value -// Assert.assertEquals(table.get("foo"), new Integer(2)); -// } -// -// @Test -// public void testRemoveAnItem() throws Exception { -// //Given an empty table -// TableNested table = new TableNested(); -// // When we put an item in it -// table.put("foo", 1); -// // And we remove that item -// table.remove("foo"); -// // Then we should get back null for that balue -// Assert.assertEquals(table.get("foo"), null); -// } -//} \ No newline at end of file +package TableNested; + +import org.junit.Assert; +import org.junit.Test; + +public class TableNestedTest { + @Test + public void testGetWithoutAnItemReturnsNull() throws Exception { + // Given an empty table + TableNested table = new TableNested(); + // When we try and get an item then it returns null + Assert.assertEquals(table.get("foo"), null); + } + + @Test + public void testPutAnItemReturnsAndDoesNotDelete() throws Exception { + //Given an empty table + TableNested table = new TableNested(); + // When we put an item in it + table.put("foo", 1); + // Then we should be able to get it's value + Assert.assertEquals(table.get("foo"), new Integer(1)); + // And then we should be able to get it again as it wasn't removed + Assert.assertEquals(table.get("foo"), new Integer(1)); + } + + @Test + public void testOverwritingAnItem() throws Exception { + //Given an empty table + TableNested table = new TableNested(); + // When we put an item in it + table.put("foo", 1); + // And we put a new value with the same key + table.put("foo", 2); + // Then we should get back the new value + Assert.assertEquals(table.get("foo"), new Integer(2)); + } + + @Test + public void testRemoveAnItem() throws Exception { + //Given an empty table + TableNested table = new TableNested(); + // When we put an item in it + table.put("foo", 1); + // And we remove that item + table.remove("foo"); + // Then we should get back null for that balue + Assert.assertEquals(table.get("foo"), null); + } +} \ No newline at end of file