Testing and Debugging

1.

class TestDoublePreceding(unittest.TestCase):
    """Tests for double_preceding.""" 

    def test_identical(self):
        """Test a list with multiple identical values""" 
        argument = [1, 1, 1]
        expected = [0, 2, 2]
        double_preceding(argument)
        self.assertEqual(expected, argument, "The list has multiple identical values.")

The bug in the code is inside the for-loop. Instead of doubling the value of the most recently seen item in the list, it doubles the last value computed. So the result of the function is solely dependent on the first item and the length of the list. All other items are ignored. Instead, the for-loop needs to store the next value to read before overwriting it:

for i in range(1, len(values)):
    double = 2 * temp
    temp = values[i]
    values[i] = double

2.

>>> line_intersect([[-1, -1], [1, 1]], [[-1, 1], [1, -1]])
[0, 0]

   The arguments intersect so we expect to get their point of intersection.

>>> line_intersect([[0, 0], [0, 0]], [[0, 0], [0, 1]])
None

   The first argument is not a pair of distinct points so they can’t intersect.

>>> line_intersect([[0, 0], [0, 1]], [[0, 0], [0, 0]])
None

   The second argument is not a pair of distinct points so they can’t intersect.

>>> line_intersect([[0, 0], [1, 0]], [[0, 0], [2, 0]])
[[0, 0], [1, 0]]

   The lines are coincident so we expect the first line as the return value.

>>> line_intersect([[0, 0], [2, 0]], [[0, 0], [1, 0]])
[[0, 0], [2, 0]]

   Same as the previous, but we switch the order. We still expect the first line as the return value.

>>> line_intersect([[0, 0], [1, 0]], [[0, 1], [1, 1]])
None

   The lines are parallel but not coincident, so they don’t intersect. This ensures we detect coincident lines properly.