2110211 Lab07, Task01

This is a JUnit test for ArrayList_rc1. To use this file, follow these steps.

  1. Create a new "JUnit Test Case" by right-click on the project, choose new, choose JUnit Test Case. A dialog will appear.
  2. Choose new JUnit 4 test.
  3. In "Name", enter TestArrayList_rc1.
  4. In "Class under test", enter ArrayList_rc1.
  5. Click finish.
  6. Replace the content of the newly created file with the following code.

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

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

public class TestArrayList_rc1 { private ArrayList_rc1 list;

@Before
public void setUp() throws Exception {
    list = new ArrayList_rc1(5);
}

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

@Test
public void testRemove() {
    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 testIndexOf() {
    list.add("Obj1");
    list.add("Obj2");
    list.add("Obj3");
    
    assertEquals(0, list.indexOf("Obj1"));
    assertEquals(1, list.indexOf("Obj2"));
    assertEquals(-1, list.indexOf("hahaha"));
}

@Test
public void testGet() {
    list.add("Obj1");
    list.add("Obj2");
    list.add("Obj3");
    
    assertEquals("Obj1", list.get(0));
    assertEquals("Obj2", list.get(1));
    assertEquals("Obj3", list.get(2));
}

@Test
public void testSet() {
    list.add("Obj1");
    list.add("Obj2");
    list.add("Obj3");
    
    assertEquals("Obj1", list.get(0));
    assertEquals("Obj2", list.get(1));
    assertEquals("Obj3", list.get(2));
    
    list.set(1,"Obj9");
    assertEquals("Obj9", list.get(1));
    assertEquals(3,list.size());
    

}

} [/code]