-
Notifications
You must be signed in to change notification settings - Fork 0
/
testlist.py
107 lines (94 loc) · 3.23 KB
/
testlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
"""
File: testlist.py
Editor: Leigh Stauffer
Project 8
A tester program for list implementations.
"""
from arraylist import ArrayList
from linkedlist import LinkedList
def testList(ListType):
"""Expects a list type as an argument and runs some tests
on objects of that type."""
print("\nTESTING THE LIST TYPE " + str(ListType))
lyst = ListType(["Ken", 63, 1973])
print("Expect 3: " + str(len(lyst)))
print('Expect [Ken, 63, 1973]: ', end = "")
print(lyst)
print("Expect True: " + str("Ken" in lyst))
print("Expect False: " + str("Bill" in lyst))
print("Expect 1: " + str(lyst.index(63)))
lyst.add("Sara")
print('Expect [Ken, 63, 1973, Sara]: ', end = "")
print(lyst)
lyst.insert(2, "Sam")
print('Expect [Ken, 63, Sam, 1973, Sara]: ', end = "")
print(lyst)
print('Expect Ken 63 Sam 1973 Sara: ', end = "")
for i in range(len(lyst)):
print(lyst[i], end = " ")
lyst = ListType(range(ArrayList.DEFAULT_CAPACITY))
print("\nExpect [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: ", end = "")
print(lyst)
print("Popping all, expect 9 8 7 6 5 4 3 2 1 0: ", end = "")
while not lyst.isEmpty(): print(lyst.pop(), end = " ")
print("\nRemoved all items: Expect []: ", end = "")
print(lyst)
lyst1 = ListType([3, 4])
lyst2 = ListType([4, 5, 6])
print("[3, 4] + [4, 5, 6] = ", end = "")
print(lyst1 + lyst2)
for i in range(len(lyst1)):
lyst1[i] = 0
print("Replace [3, 4] with [0, 0], expect [0, 0]: ", end = "")
print(lyst1)
def testListIterator(ListType):
"""Expects a list type as an argument and runs some tests
on a list iterator on that type."""
print("\nTESTING A LIST ITERATOR FOR THE TYPE " + str(ListType) )
print("Create a list with 1-9")
lyst = ListType(range(1, 10))
print("Length:", len(lyst))
print("Items in list (first to last):", lyst)
# Create and use a list iterator
listIterator = lyst.listIterator()
print("Forward traversal with list iterator: ", end="")
listIterator.first()
while listIterator.hasNext():
print(listIterator.next(), end = " ")
print("\nBackward traversal: ", end="")
listIterator.last()
while listIterator.hasPrevious():
print(listIterator.previous(), end=" ")
print("\nInserting 10 before 3: ", end="")
listIterator.first()
for count in range(3):
listIterator.next()
listIterator.insert(10)
print(lyst)
print("Removing 2: ", end="")
listIterator.first()
for count in range(2):
listIterator.next()
listIterator.remove()
print(lyst)
print("Replace all items with 0: ", end="")
listIterator.first()
while listIterator.hasNext():
listIterator.next()
listIterator.replace(0)
print(lyst)
print("Removing all items: Expect []: ", end = "")
listIterator.first()
while listIterator.hasNext():
listIterator.next()
listIterator.remove()
print(lyst)
print("Length:", len(lyst))
def main():
"""Starting point for the application."""
## testList(ArrayList)
## testListIterator(ArrayList)
## testList(LinkedList)
testListIterator(LinkedList)
if __name__ == "__main__":
main()