Week 5
This week I did not learn so much because of the first term test was hold on Wednesday night. I took the early course which taught by Mrs. Diane but not Mr. Heap because I had to take MAT137 test that night. Mrs. Diane told us about the unit test, which we`ve learned in CSC108. She show us an example of insert_after (L, n1, n2) and run it in unit test. The function was like this:
def insert_after(L, n1, n2):
"""
(list, int, int) -> NoneType
After each occurrence of n1 in L, insert n2.
>>> L = [5, 1, 2, 1, 6]
>>> insert_after(L, 1, 99)
>>> L
[5, 1, 99, 2, 1, 99, 6]
"""
i = 0
while i < len(L):
if L[i] == n1:
L.insert(i+1, n2)
i += 1
i += 1
if __name__ == "__main__":
import doctest
doctest.testmod()
The unit test is this:
import unittest
from insert import insert_after
# Note: this test suite does not contain a complete set of test cases.
# Challenge: Extend it to the point that either you are convinced the
# function works as advertised in the docstring, or you have demonstrated
# that it doesn't.
class TestInsertAfter(unittest.TestCase):
def test_insert_after_at_front(self):
""" Test insert_after with one occurrence of n1 in L, at the front."""
input_list = [0, 1, 2, 3]
return_value = insert_after(input_list, 0, 99)
expected = [0, 99, 1, 2, 3]
self.assertEqual(input_list, expected)
self.assertEqual(return_value, None)
def test_insert_after_at_back(self):
""" Test insert_after with one occurrence of n1 in L, at the back."""
input_list = [0, 1, 2, 3]
return_value = insert_after(input_list, 3, 99)
expected = [0, 1, 2, 3, 99]
self.assertEqual(input_list, expected)
self.assertEqual(return_value, None)
def test_insert_after_middle(self):
""" Test insert_after with one occurrence of n1 in L, not on
either end."""
input_list = [0, 1, 2, 3]
return_value = insert_after(input_list, 1, 99)
expected = [0, 1, 99, 2, 3]
self.assertEqual(input_list, expected)
self.assertEqual(return_value, None)
if __name__ == '__main__':
unittest.main(exit=False)
Unit test is not hard enough but it was always the necessary one in function testing. Wish I can get a good mark in test.