coder
coder

Reputation: 63

How to test remove element in linked list?

I have a Linked list below:

enter image description here

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

Answers (4)

GhostCat
GhostCat

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

Zabuzard
Zabuzard

Reputation: 25903

Explanation

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.


Testing

There are several possibilities to test correctness (for a JUnit Test for example):

  • Test if the method removes something at all
  • Test if the method removed the correct element (the first)
  • Test if all other elements moved to the front (indices changed)
  • Test if the method returns the removed element (according to its documentation)
  • Test if the method correctly handles empty lists (throwing an exception)
  • Test if the method correctly removes the element if its null

Examples

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

Minh Bui
Minh Bui

Reputation: 1030

You should test your operation with all cases below:

  1. Verify that a has value.
  2. Check get(-1) to make sure that cause an index out of bound exception.
  3. Check get(0) to return value is 21.
  4. Calling list at size to check to.

Hope it would help.

Upvotes: 2

sunny mattas
sunny mattas

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

Related Questions