|
| 1 | +"""Smoke test runner for the `data_structures.simple` package.""" |
| 2 | + |
| 3 | +import sys |
| 4 | + |
| 5 | +from data_structures.simple.linked_list import LinkedList |
| 6 | +from data_structures.simple.stack import ListStack, LinkedStack |
| 7 | +from data_structures.simple.queue import Queue |
| 8 | +from data_structures.simple.bst import BinarySearchTree |
| 9 | +from data_structures.simple.graph import Graph |
| 10 | +from data_structures.simple.trie import Trie |
| 11 | +from data_structures.simple.union_find import UnionFind |
| 12 | +from data_structures.simple.min_heap import MinHeap |
| 13 | + |
| 14 | + |
| 15 | +def run() -> None: |
| 16 | + failures = [] |
| 17 | + |
| 18 | + try: |
| 19 | + ll = LinkedList() |
| 20 | + ll.append(1) |
| 21 | + ll.append(2) |
| 22 | + ll.prepend(0) |
| 23 | + assert ll.to_list() == [0, 1, 2] |
| 24 | + assert ll.find(1) is not None |
| 25 | + assert ll.delete(1) is True |
| 26 | + assert ll.to_list() == [0, 2] |
| 27 | + print("LinkedList: OK") |
| 28 | + except Exception as e: |
| 29 | + failures.append(('LinkedList', e)) |
| 30 | + |
| 31 | + try: |
| 32 | + s = ListStack() |
| 33 | + s.push(1) |
| 34 | + s.push(2) |
| 35 | + assert s.peek() == 2 |
| 36 | + assert s.pop() == 2 |
| 37 | + |
| 38 | + ls = LinkedStack() |
| 39 | + ls.push('a') |
| 40 | + ls.push('b') |
| 41 | + assert ls.peek() == 'b' |
| 42 | + assert ls.pop() == 'b' |
| 43 | + print("Stacks: OK") |
| 44 | + except Exception as e: |
| 45 | + failures.append(('Stacks', e)) |
| 46 | + |
| 47 | + try: |
| 48 | + q = Queue() |
| 49 | + q.enqueue(1) |
| 50 | + q.enqueue(2) |
| 51 | + assert q.peek() == 1 |
| 52 | + assert q.dequeue() == 1 |
| 53 | + print("Queue: OK") |
| 54 | + except Exception as e: |
| 55 | + failures.append(('Queue', e)) |
| 56 | + |
| 57 | + try: |
| 58 | + bst = BinarySearchTree() |
| 59 | + for v in [3, 1, 4, 2]: |
| 60 | + bst.insert(v) |
| 61 | + assert bst.search(2) |
| 62 | + assert bst.inorder() == [1, 2, 3, 4] |
| 63 | + print("BST: OK") |
| 64 | + except Exception as e: |
| 65 | + failures.append(('BST', e)) |
| 66 | + |
| 67 | + try: |
| 68 | + g = Graph() |
| 69 | + g.add_edge(1, 2) |
| 70 | + g.add_edge(1, 3) |
| 71 | + order_bfs = g.bfs(1) |
| 72 | + assert 1 in order_bfs |
| 73 | + order_dfs = g.dfs(1) |
| 74 | + assert 1 in order_dfs |
| 75 | + print("Graph: OK") |
| 76 | + except Exception as e: |
| 77 | + failures.append(('Graph', e)) |
| 78 | + |
| 79 | + try: |
| 80 | + t = Trie() |
| 81 | + t.insert('hello') |
| 82 | + assert t.search('hello') |
| 83 | + assert t.starts_with('hel') |
| 84 | + print("Trie: OK") |
| 85 | + except Exception as e: |
| 86 | + failures.append(('Trie', e)) |
| 87 | + |
| 88 | + try: |
| 89 | + uf = UnionFind() |
| 90 | + for x in [1, 2, 3]: |
| 91 | + uf.make_set(x) |
| 92 | + uf.union(1, 2) |
| 93 | + assert uf.find(1) == uf.find(2) |
| 94 | + print("UnionFind: OK") |
| 95 | + except Exception as e: |
| 96 | + failures.append(('UnionFind', e)) |
| 97 | + |
| 98 | + try: |
| 99 | + h = MinHeap() |
| 100 | + h.push(5) |
| 101 | + h.push(1) |
| 102 | + h.push(3) |
| 103 | + assert h.peek() == 1 |
| 104 | + assert h.pop() == 1 |
| 105 | + print("MinHeap: OK") |
| 106 | + except Exception as e: |
| 107 | + failures.append(('MinHeap', e)) |
| 108 | + |
| 109 | + if failures: |
| 110 | + print('\nFAILURES:') |
| 111 | + for name, exc in failures: |
| 112 | + print(f"- {name}: {exc}") |
| 113 | + sys.exit(2) |
| 114 | + else: |
| 115 | + print('\nAll data_structures.simple smoke tests passed.') |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == '__main__': |
| 119 | + run() |
0 commit comments