A1 P1 Q2 Split up test units for all 3 linked lists into 3 test units

This commit is contained in:
MstrPikachu
2022-01-30 22:13:23 -05:00
parent bad39fbbd9
commit 5e5ab24f2f
+48 -21
View File
@@ -28,22 +28,34 @@ import pytest
from a1_part1 import MoveToFrontLinkedList, SwapLinkedList, CountLinkedList
def test_all_lists_true() -> None:
movetofront = [MoveToFrontLinkedList(range(i)) for i in range(100)]
swap = [SwapLinkedList(range(i)) for i in range(100)]
count = [CountLinkedList(range(i)) for i in range(100)]
assert(all(all(x in movetofront[i] for x in range(i)) for i in range(100)))
assert(all(all(x in swap[i] for x in range(i)) for i in range(100)))
assert(all(all(x in count[i] for x in range(i)) for i in range(100)))
def test_movetofront_true() -> None:
lst = [MoveToFrontLinkedList(range(i)) for i in range(100)]
assert(all(all(x in lst[i] for x in range(i)) for i in range(100)))
def test_all_lists_false() -> None:
movetofront = [MoveToFrontLinkedList(range(i)) for i in range(100)]
swap = [SwapLinkedList(range(i)) for i in range(100)]
count = [CountLinkedList(range(i)) for i in range(100)]
assert(all(all(x not in movetofront[i] for x in range(i, i + 100)) for i in range(100)))
assert(all(all(x not in swap[i] for x in range(i, i + 100)) for i in range(100)))
assert(all(all(x not in count[i] for x in range(i, i + 100)) for i in range(100)))
def test_swap_true() -> None:
lst = [SwapLinkedList(range(i)) for i in range(100)]
assert(all(all(x in lst[i] for x in range(i)) for i in range(100)))
def test_count_true() -> None:
lst = [CountLinkedList(range(i)) for i in range(100)]
assert(all(all(x in lst[i] for x in range(i)) for i in range(100)))
def test_movetofront_false() -> None:
lst = [MoveToFrontLinkedList(range(i)) for i in range(100)]
assert(all(all(x not in lst[i] for x in range(i, i + 100)) for i in range(100)))
def test_swap_false() -> None:
lst = [SwapLinkedList(range(i)) for i in range(100)]
assert(all(all(x not in lst[i] for x in range(i, i + 100)) for i in range(100)))
def test_count_false() -> None:
lst = [CountLinkedList(range(i)) for i in range(100)]
assert(all(all(x not in lst[i] for x in range(i, i + 100)) for i in range(100)))
def generate_ordering(n: int):
@@ -54,27 +66,32 @@ def generate_ordering(n: int):
"""
arr = [(2 << i) % n for i in range(1, n)]
arr.append(0)
assert set(arr) == set(range(n))
# assert set(arr) == set(range(n))
return arr
def test_movetofront_mutation() -> None:
"""Tests the mutation of a MoveToFrontLinkedList by searching for every number from 0-100 inclusive."""
lst = MoveToFrontLinkedList(range(101))
arr = list(range(101))
operations = generate_ordering(101)
bools = [] # test results of mutation after each iteration
for x in operations:
i = arr.index(x) # find index
arr.insert(0, arr.pop(i)) # simulate moving to front
x in lst
assert lst.to_list() == arr
bools.append(lst.to_list() == arr)
assert all(bools)
def test_swap_mutation() -> None:
"""Tests the mutation of a SwapLinkedList by searching for every number from 0-100 inclusive."""
lst = SwapLinkedList(range(101))
arr = list(range(101))
operations = generate_ordering(101)
bools = [] # test results of mutation after each iteration
for x in operations:
i = arr.index(x) # find index
if i > 0: # simulate swapping
@@ -82,21 +99,31 @@ def test_swap_mutation() -> None:
arr[i] = arr[i - 1]
arr[i - 1] = temp
x in lst
assert lst.to_list() == arr
bools.append(lst.to_list() == arr)
assert all(bools)
def test_count_mutation() -> None:
"""Tests the mutation of a CountLinkedList by searching for every number from 0-100 inclusive, and then
every number from 0-49 inclusive."""
lst = CountLinkedList(generate_ordering(101))
operations = list(range(101))
for x in operations:
for x in list(range(101)): # reorder the linked list to natural ordering
x in lst
assert lst.to_list() == operations
for x in reversed(range(50)):
for x in reversed(range(50)): # reverse the first 50 numbers
x in lst
assert lst.to_list() == list(reversed(range(50))) + list(range(50, 101))
if __name__ == '__main__':
pytest.main(['a1_part1_test.py', '-v'])
import python_ta
python_ta.check_all(config={
'max-line-length': 100,
'disable': ['E1136'],
'extra-imports': ['a1_linked_list'],
'max-nested-blocks': 4
}, output='pyta_report.html')