import org.junit.Test; /** * JUnit test class for IntArray * * @author Krysta Yousoufian * */ public class IntArrayTestBetter { /* * Note: IntArray is a theoretical class and we don't provide an * implementation. If you want to compile and run this code, you can * create an IntArray class and stub out all necessary methods, * though of course the tests will fail. * * This test class demonstrates one possible way to split up the * giant testIntArray() method. It adds some additional methods to be * somewhat more comprehensive. There are still lots of edge cases * and such that you could test for (can you think of any)? * * Also, each of the assertions should probably have an error message. */ // Constructs and returns new array with size elements of values // [10, 20, ..., (size-1)*10] private void getArray(int size) { IntArray arr = new IntArray(); for (int i = 0; i < size; i++) { arr.add(i*10); } } // ******************************************************************** // Base tests: testing methods against an empty list // ******************************************************************** @Test public void testIsEmptyForEmptyList() { IntArray arr = new IntArray(); assertTrue(arr.isEmpty()); } @Test public void testSizeEmptyList() { IntArray arr = new IntArray(); assertEquals(0, arr.size()); } @Test public void testContainsEmptyList() { IntArray arr = new IntArray(); assertFalse(arr.contains(10)); } @Test(expected=IndexOutOfBoundsException.class) public void testGetEmptyList() { IntArray arr = new IntArray(); arr.get(0); } // Search for an element that is not in the list @Test public void testIndexOfEmptyList() { IntArray arr = new IntArray(); assertEquals(-1, arr.indexOf(0)); } // ******************************************************************** // One-element tests: test methods after adding a single element // In each method below, we test add() and one other method. Wherever // possible we tested the methods in isolation in the base tests above, // but we can't test add() without calling another method to verify its // side effects. If all of the one-element tests fail, it is likely a // bug in add(), but if only one fails then it is likely a bug in // the "side effect" method. // ******************************************************************** @Test public void testAddOneIsEmpty() { IntArray arr = new IntArray(); arr.add(10); assertFalse(arr.isEmpty()); } @Test public void testAddOneSize() { IntArray arr = new IntArray(); arr.add(10); assertEquals(1, arr.size()); } @Test public void testAddOneContains() { IntArray arr = new IntArray(); arr.add(10); assertTrue(arr.contains(10)); } @Test public void testAddOneGet() { IntArray arr = new IntArray(); arr.add(10); assertEquals(10, arr.get(0)); } @Test public void testAddOneIndexOfInList() { IntArray arr = new IntArray(); arr.add(10); assertEquals(0, arr.indexOf(10)); } @Test public void testAddOneIndexOfNotInList() { IntArray arr = new IntArray(); arr.add(10); assertEquals(-1, arr.indexOf(1)); } // ******************************************************************** // Multi-element tests: add three elements and test appropriate methods. // Because the isolated one-element tests give us some measure of // confidence in these methods, and because it becomes tedious to write, // read, and maintain too many fragmented tests, we combine them into // a single method. // Why does this not count as testing too many different things in a // single test? Because we are only testing one scenario: we add three // items to the list and verify that it is in the correct state afterwards. // It's perhaps a subtle difference but an important one. // ******************************************************************** @Test public void testAddManyElements() { IntArray arr = new IntArray(); arr.add(10); arr.add(20); arr.add(30); assertFalse(arr.isEmpty()); assertEquals(3, arr.size()); for (int i = 0; i < 3; i++) { assertTrue(arr.contains(i*10)); assertEquals(i*10, arr.get(i)); assertEquals(i, arr.indexOf(i*10)); } } // ******************************************************************** // Test remove() and clear(). These tests assume that add(), size(), and // contains() work correctly because they were already tested above. If // there is a problem with one of those methods, these tests will fail, // but so will the tests above. // ******************************************************************** @Test public void testRemoveOne() { IntArray arr = new IntArray(); arr.add(10); arr.remove(10); assertEquals(0, arr.size()); assertFalse(arr.contains(10)); } // Add three elements, remove one. Verify that only one was removed. @Test public void testRemoveOneOfMany() { IntArray arr = new IntArray(); arr.add(10); arr.add(20); arr.add(30); arr.remove(10); assertEquals(2, arr.size()); assertFalse(arr.contains(10)); assertTrue(arr.contains(20)); assertTrue(arr.contains(30)); } // Add and remove three elements @Test public void testRemoveMany() { IntArray arr = new IntArray(); arr.add(10); arr.add(20); arr.add(30); arr.remove(10); arr.remove(20); arr.remove(30); assertEquals(0, arr.size()); assertFalse(arr.contains(10)); assertFalse(arr.contains(20)); assertFalse(arr.contains(30)); } @Test public void testClearOneElement() { IntArray arr = new IntArray(); arr.add(10); arr.clear(); assertTrue(arr.isEmpty()); assertEquals(0, arr.size()); } @Test public void testClearManyElements() { IntArray arr = new IntArray(); arr.add(10); arr.add(20); arr.add(30); arr.clear(); assertTrue(arr.isEmpty()); assertEquals(0, arr.size()); } // ******************************************************************** // Test equals() // ******************************************************************** @Test public void testEqualsEmptyLists() { IntArray expected = new IntArray(); IntArray actual = new IntArray(); assertEquals(expected, actual); } @Test public void testEqualsIdenticalLists() { IntArray expected = new IntArray(); IntArray actual = new IntArray(); for (int i = 0; i < 3; i++) { expected.add(i*10); actual.add(i*10); } assertEquals(expected, actual); } @Test public void testEqualsDifferentSizes() { IntArray expected = new IntArray(); IntArray actual = new IntArray(); expected.add(10); expected.add(20); actual.add(10); assertFalse(expected.equals(actual)); } @Test public void testEqualsSameSizeDifferentElements() { IntArray expected = new IntArray(); IntArray actual = new IntArray(); expected.add(10); expected.add(20); actual.add(10); actual.add(30); assertFalse(expected.equals(actual)); } } }