Skip to content

Instantly share code, notes, and snippets.

@edipetres
Last active February 24, 2018 15:02
Show Gist options
  • Save edipetres/aa0b079826fddd5d9f9970d765f15e25 to your computer and use it in GitHub Desktop.
Save edipetres/aa0b079826fddd5d9f9970d765f15e25 to your computer and use it in GitHub Desktop.
Test Cases Exercise
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package myarraylistwithbugs;
/**
*
* @author eddmond
*/
public class MyArrayListWithBugs {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
mylist.get(0);
}
private Object[] list;
int nextFree;
// Creates a new empty list
public MyArrayListWithBugs() {
list = new Object[5];
nextFree = 0;
}
// Inserts object at the end of list
public void add(Object o) {
// check capacity
if (list.length <= nextFree) {
list = getLongerList();
}
list[nextFree] = o;
nextFree++;
}
// Returns the number of objects in the list
public int size() {
return nextFree;
}
// Returns a reference to the object at position index
// Throws IndexOutOfBoundsException
public Object get(int index) {
if (index < 0 || index >= nextFree) {
throw new IndexOutOfBoundsException("Error (get): Invalid index"
+ index);
}
return list[index];
}
// Inserts object at position index
// Throws IndexOutOfBoundsException
public void add(int index, Object o) {
if (index < 0 || nextFree < index) {
throw new IndexOutOfBoundsException("Error (add): Invalid index"
+ index);
}
// check capacity
if (list.length <= nextFree) {
list = getLongerList();
}
// Shift elements upwards to make position index free
// Start with last element and move backwards
for (int i = nextFree - 1; i >= index; i--) {
list[i] = list[i - 1];
}
list[index] = o;
nextFree++;
}
// Removes object at position index
// Returns a reference to the removed object
// Throws IndexOutOfBoundsException
public Object remove(int index) {
if (index < 0 || nextFree <= index) {
throw new IndexOutOfBoundsException("Error (remove): Invalid index: "
+ index);
}
// Shift elements down to fill indexed position // Start with first element
for (int i = index; i < nextFree - 1; i++) {
list[i] = list[i + 1];
}
nextFree--;
return list[index];
}
//============== private helper methods ==========
// create a list with double capacity and
// copy all elements to this
private Object[] getLongerList() {
Object[] tempList = new Object[list.length * 2];
System.arraycopy(list, 0, tempList, 0, list.length);
return tempList;
}
}
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import myarraylistwithbugs.MyArrayListWithBugs;
import org.junit.Test;
import static org.junit.Assert.*;
import org.junit.Before;
/**
*
* @author eddmond
*/
public class StateDiagramTests {
String randomObject = "someObject";
@Test
public void createNewList() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
int actual = mylist.size();
int expected = 0;
assertEquals(expected, actual);
}
@Test(expected = IndexOutOfBoundsException.class)
public void removeFromEmptyList() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
mylist.remove(0);
}
@Test(expected = IndexOutOfBoundsException.class)
public void getFromEmptyList() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
mylist.get(0);
}
@Test
public void addNewElement() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
mylist.add(randomObject);
int expected = 1;
int actual = mylist.size();
assertEquals(expected, actual);
}
@Test
public void getLastAddedElement() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
mylist.add(randomObject);
Object actual = mylist.get(0);
assertEquals(randomObject, actual);
}
@Test
public void addSixElements() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
for (int i = 0; i <= 6; i++) {
mylist.add(randomObject);
}
int expected = 7;
int actual = mylist.size();
assertEquals(expected, actual);
}
@Test
public void insertAtIndex() {
MyArrayListWithBugs mylist = new MyArrayListWithBugs();
mylist.add(0, randomObject);
Object actual = mylist.get(0);
assertEquals(randomObject, actual);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment