Skip to content

Instantly share code, notes, and snippets.

@samueltcsantos
Created February 5, 2015 04:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samueltcsantos/2089ec68875f6f783a71 to your computer and use it in GitHub Desktop.
Save samueltcsantos/2089ec68875f6f783a71 to your computer and use it in GitHub Desktop.
Testing the ArrayList implementation.
package adt.list;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
/**
* Testing the ArrayList implementation.
*
* @author Samuel T. C. Santos
* @version 1.0
*
*/
public class ArrayListTest {
private List<String> list;
@Before
public void setUp(){
list = new ArrayList<String>();
}
@Test
public void testListInit(){
assertTrue(list.isEmpty());
assertTrue(list.size() == 0);
}
@Test (expected = IllegalArgumentException.class)
public void testInvalidCapacity(){
list = new ArrayList<String>(-1);
}
@Test
public void testAddElements(){
list.add(0, "Karol");
list.add(1, "Vanessa");
list.add(2, "Amanda");
assertEquals("Karol", list.get(0));
assertEquals("Vanessa", list.get(1));
assertEquals("Amanda", list.get(2));
list.add(1, "Mariana");
assertEquals("Karol", list.get(0));
assertEquals("Mariana", list.get(1));
assertEquals("Vanessa", list.get(2));
assertEquals("Amanda", list.get(3));
assertTrue(list.size()==4);
}
@Test (expected = NullPointerException.class)
public void testAddElementNull(){
list.add(0, null);
}
@Test (expected = NullPointerException.class)
public void testSetElementNull(){
list.add(0, "Kheyla");
list.set(0, null);
}
@Test
public void testSetElement(){
list.add(0, "Karol");
list.add(1, "Vanessa");
list.add(2, "Amanda");
list.set(1, "Livia");
assertEquals("Karol", list.get(0));
assertEquals("Livia", list.get(1));
assertEquals("Amanda", list.get(2));
}
@Test
public void testRemoveElement(){
list.add(0, "Karol");
list.add(1, "Vanessa");
list.add(2, "Amanda");
assertEquals("Amanda", list.remove(2));
assertTrue(list.size() == 2);
}
@Test (expected = IndexOutOfBoundsException.class)
public void testRemoveWithEmptyList(){
list.remove(0);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment