JUnit for Lab07 Task 2

Refer to http://www.nattee.net/node/2123, for usage. Adjust accordingly.

[code lang="java"] import static org.junit.Assert.*;

import org.junit.After; import org.junit.Before; import org.junit.Test;

public class TestArrayList_rc2 { private ArrayList_rc2 list;

@Before
public void setUp() throws Exception {
    list = new ArrayList_rc2(10);
}

@After
public void tearDown() throws Exception {
    list = null;
}

@Test
public void testRemoveObject() {
    list.add("Obj1");
    list.add("Obj2");
    list.add("Obj3");
    list.add("Obj4");
    list.add("Obj5");

    assertEquals(0, list.indexOf("Obj1"));
    assertEquals(1, list.indexOf("Obj2"));
    assertEquals(-1, list.indexOf("hahaha"));

    list.remove("Obj9");

    assertEquals(5, list.size());

    list.remove("Obj2");

    assertEquals("Obj1", list.get(0));
    assertEquals("Obj3", list.get(1));
    assertEquals(4, list.size());
}

@Test
public void testAddIntObject() {
    list.add("Obj1");
    list.add("Obj2");
    list.add("Obj3");
    list.add("Obj4");
    list.add("Obj5");

    assertEquals(0, list.indexOf("Obj1"));
    assertEquals(1, list.indexOf("Obj2"));
    assertEquals(-1, list.indexOf("hahaha"));

    list.remove("Obj9");

    assertEquals(5, list.size());

    list.remove("Obj2");

    assertEquals("Obj1", list.get(0));
    assertEquals("Obj3", list.get(1));
    assertEquals(4, list.size());
    
    list.add(0,"AAA");
    list.add(5,"BBB");
    assertEquals("AAA", list.get(0));
    assertEquals("BBB", list.get(5));
}

@Test
public void testRemoveInt() {
    list.add("Obj1");
    list.add("Obj2");
    list.add("Obj3");
    list.add("Obj4");
    list.add("Obj5");

    assertEquals(0, list.indexOf("Obj1"));
    assertEquals(1, list.indexOf("Obj2"));
    assertEquals(-1, list.indexOf("hahaha"));

    list.remove("Obj9");

    assertEquals(5, list.size());

    list.remove("Obj2");
    list.remove(1);
    list.remove(2);
    list.remove("Obj2");

    assertEquals("Obj1", list.get(0));
    assertEquals("Obj4", list.get(1));
    assertEquals(2, list.size());
    
    list.remove(0);
    list.remove(0);
    assertEquals(0, list.size());
    
    list.add(0,"AAA");
    list.add(1,"BBB");
    assertEquals("AAA", list.get(0));
    assertEquals("BBB", list.get(1));
    list.remove(0);
    assertEquals("BBB", list.get(0));
    
}

} [/code]