Reputation: 63
I have a Linked list below:
if I want to remove an element with this code:
int a = list1.remove(0);
so what verification code should I include to make sure this operation works correctly?
Thank you and hope to receive answer from you soon.
Upvotes: 2
Views: 1694
Reputation: 140427
The other answers are all correct - but it would still be possible to create a wrong list implementation that passes all these specific tests - but contains some obscure bugs.
This is a scenario where test frameworks based on the QuickCheck idea have certain merit. Instead of writing various test cases manually - you rather specify the rules/contract that must be true for your operations to QuickCheck. And then the framework creates random tests - and when it finds something that violates your rules, it will even reduce the input to the "minimum" required to show the problem.
I am not saying that QuickCheck is the "better" solution here - but it is definitely a good companion to the kind of testing that the other answers point you to.
Upvotes: 0
Reputation: 25903
The method LinkedList#remove(int)
removes the element at the given position (in your case index 0
, so the first element), here is its documentation.
After removal all other elements move to the front (index 1
becomes index 0
and so on). As a note: This comes of no additional cost due to the character of a LinkedList
.
There are several possibilities to test correctness (for a JUnit Test for example):
null
Here are some implementations for all mentioned cases:
@Test
public void testSizeChange() {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
Assert.assertEquals(5, list.size());
list.remove(0);
Assert.assertEquals(4, list.size());
}
@Test
public void testRemovedCorrectElement() {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
Assert.assertTrue(list.contains(1));
list.remove(0);
Assert.assertFalse(list.contains(1));
}
@Test
public void testElementsMoved() {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
list.remove(0);
Iterator<Integer> iter = list.iterator();
for (int i = 2; i <= 5; i++) {
Assert.assertTrue(iter.hasNext());
Assert.assertEquals(i, iter.next());
}
Assert.assertFalse(iter.hasNext());
}
@Test
public void testReturnRemovedValue() {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(1, 2, 3, 4, 5));
Assert.assertEquals(1, list.remove(0));
}
@Test(expected = IndexOutOfBoundsException.class)
public void testHandleEmpty() {
LinkedList<Integer> list = new LinkedList<>();
list.remove(0);
}
@Test
public void testRemoveNull() {
LinkedList<Integer> list = new LinkedList<>(Arrays.asList(null, 2, 3, 4, 5));
Assert.assertNull(list.remove(0));
}
Note that if your LinkedList
is not java.util.LinkedList
but an own implementation you may need to modify the later examples a bit according to your documentation and needs.
Upvotes: 2
Reputation: 1030
You should test your operation with all cases below:
a
has value.Hope it would help.
Upvotes: 2
Reputation: 11
You can test it by simply calling size method on linked list. Then do assertequals on expected size of linked list.
assertEquals(EXPECTED_SIZE, list1.size())
Expected Size would be an int value which would be 1 value less than initial size.
To assert if actual value is removed you can assert on contains
assertFalse(list1.contains(65))
Here 65 is the actual value which was removed.
Upvotes: 0