In the Linked List exercise, I am having problem understanding two of the tests (I reformated for the sake of this post).
{
"uuid": "59db191a-b17f-4ab7-9c5c-60711ec1d013",
"description": "deletes the element with the specified value from the list, re-assigns tail",
"property": "list",
"input": {"operations": [
push(71), push(83), push(79), delete(83), count()==2,
pop()==79, pop()==71]
}...},
The comment associated with the test βdeletes the element with the specified value from the list, re-assigns tailβ makes me think that the test will delete the tail value (which will re-assign the tail to the middle (of three) elements). But the delete instead delete the middle element (which doesnβt touch the tail - really).
Based on the title, I would have expected the test to look more like
Am I missing the point or misinterpreting the title? (The next test does the same by checking that the head is reassigned and I have the same question).
You should be reading the tests from the test file, not the specs ;) The titles in the specs are often not very descriptive.
This test boils down to:
# Push three items.
list.push(71)
list.push(83)
list.push(79)
# Delete the middle item, 83.
list.delete(83)
# Check the list contains two items.
assert list.count() == 2
# Check we can pop twice and get the expected values.
assert list.pop() == 79
assert list.pop() == 71
The above should make the test expectations pretty clear. Donβt worry too much about the title.
I believe the idea here is that you will reassign prev (or next) to the tail/head. The test does two pop to check if removing the middle element does not break the linked list, i.e., the nodes remain connected.
The test with id fd65e422-51f3-45c0-9fd0-c33da638f89b builds a linked list with three elements (71, 83, 79), deletes the middle element and checks with count() == 2, pop() == 79, and shift() == 71 whether the deletion was performed correctly.
But that leaves room for errors. Imagine the implementation of delete() just decrements an internal node counter but does not modify the list. That would pass the test because the first and last element are the same, with or without the actual removal of the middle element.
Or, the implementation could (incorrectly) only adjust one of the two directions. Imagine it only adjusts the next pointer but not the prev pointer. After the (incorrect) deletion the list could look like this:
The test fd65e422 would pass because it only checks the number of nodes and whether pop() and shift() return the values of the last and first node.
Thatβs where the next two tests come into play:
The next test two tests with id 59db191a-b17f-4ab7-9c5c-60711ec1d013 and 58242222-5d39-415b-951d-8128247f8993 build the same list with three elements (71, 83, 79) and also delete the middle element. They then check whether the deletion was performed correctly with three slightly different operations:
count() == 2, pop() == 79, and pop() == 71.
count() == 2, shift() == 71, and shift() == 79.
These two tests would catch an incorrectly implemented delete() function.