Skip to content

Commit

Permalink
Added test case for delete last element in DynamicArray
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasmusson committed Nov 11, 2019
1 parent 1f3732a commit 082f680
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/main/java/datastructures/DynamicArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ public void delete(int index) {
data[j] = data[j + 1];
}
size--;

// Clear if last element in array
if (index == size) {
data[index] = null;
}
}

public boolean isEmpty() {
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/datastructures/DynamicArrayTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public void Delete() {
Assert.assertEquals("c", array.get(1));
}

@Test
public void DeleteLast() {
array.add("a");
array.add("b");
array.add("c");

array.delete(2);

Assert.assertEquals(2, array.size());
Assert.assertEquals("a", array.get(0));
Assert.assertEquals("b", array.get(1));
Assert.assertEquals(null, array.get(2));
}

@Test
public void isEmpty() {
Assert.assertTrue(array.isEmpty());
Expand Down

0 comments on commit 082f680

Please sign in to comment.