Sobes.tech
Junior — Senior

Creating a unit test for the insert method in a singly linked list

livecode

Task condition

Write a unit test that checks the correctness of the add function, which is responsible for adding elements to a simple singly linked data structure.

@Override
public boolean add(T item) {
    if (item == null) {
        return false;
    }

    if (data == null) {
        data = item;
        return true;
    }

    // 0 -> 1 -> 2 // next = 1
    // 0 -> 1 // add 2
    ListNode<T> node = next;

    while (node.next != null) {
        node = node.next;
    }

    node.data = item;

    return true;
}

The test should verify that when a valid value is passed, the element appears at the end of the list, and when null is passed, the method returns false and the list structure remains unchanged.