Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file added tests/__init__.py
Empty file.
150 changes: 150 additions & 0 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import unittest
import importlib.util
import os
from io import StringIO
from unittest.mock import patch


def _load_module(name, rel_path):
"""Load a module from a file path relative to this test directory."""
spec = importlib.util.spec_from_file_location(
name,
os.path.join(os.path.dirname(__file__), '..', 'algorithms', rel_path)
)
mod = importlib.util.module_from_spec(spec)
with patch('sys.stdout', new_callable=StringIO):
spec.loader.exec_module(mod)
return mod


_mod26 = _load_module("algorithm26", "algorithm26.py")
_mod27 = _load_module("algorithm27", "algorithm27.py")
_mod24 = _load_module("algorithm24", "algorithm24.py")
_mod25 = _load_module("algorithm25", "algorithm25.py")
_mod23 = _load_module("algorithm23", "algorithm23.py")

count_items = _mod26.count_items
square = _mod27.square
find_largest = _mod24.find_largest
check_even_odd = _mod25.check_even_odd
add_numbers = _mod23.add_numbers


class TestCountItems(unittest.TestCase):

def test_count_items_with_strings(self):
self.assertEqual(count_items(["a", "b", "c"]), 3)

def test_count_items_empty_list(self):
self.assertEqual(count_items([]), 0)

def test_count_items_single_element(self):
self.assertEqual(count_items([1]), 1)

def test_count_items_large_list(self):
self.assertEqual(count_items(list(range(100))), 100)

def test_count_items_mixed_types(self):
self.assertEqual(count_items([1, "a", None, True]), 4)


class TestSquare(unittest.TestCase):

def test_square_positive(self):
self.assertEqual(square(6), 36)

def test_square_zero(self):
self.assertEqual(square(0), 0)

def test_square_negative(self):
self.assertEqual(square(-3), 9)

def test_square_one(self):
self.assertEqual(square(1), 1)

def test_square_float(self):
self.assertAlmostEqual(square(2.5), 6.25)

def test_square_large_number(self):
self.assertEqual(square(100), 10000)


class TestFindLargest(unittest.TestCase):
"""Tests for algorithm24.find_largest (print-based, captured via stdout)."""

def test_find_largest_basic(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
find_largest([4, 9, 2, 7, 5])
self.assertIn("9", mock_out.getvalue())

def test_find_largest_single_element(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
find_largest([42])
self.assertIn("42", mock_out.getvalue())

def test_find_largest_negative_numbers(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
find_largest([-5, -1, -10])
self.assertIn("-1", mock_out.getvalue())

def test_find_largest_all_same(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
find_largest([3, 3, 3])
self.assertIn("3", mock_out.getvalue())


class TestCheckEvenOdd(unittest.TestCase):
"""Tests for algorithm25.check_even_odd (print-based, captured via stdout)."""

def test_even_number(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
check_even_odd(6)
self.assertIn("Even", mock_out.getvalue())

def test_odd_number(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
check_even_odd(7)
self.assertIn("Odd", mock_out.getvalue())

def test_zero_is_even(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
check_even_odd(0)
self.assertIn("Even", mock_out.getvalue())

def test_negative_even(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
check_even_odd(-4)
self.assertIn("Even", mock_out.getvalue())

def test_negative_odd(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
check_even_odd(-3)
self.assertIn("Odd", mock_out.getvalue())


class TestAddNumbers(unittest.TestCase):
"""Tests for algorithm23.add_numbers (print-based, captured via stdout)."""

def test_add_positive_numbers(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
add_numbers(5, 7)
self.assertIn("12", mock_out.getvalue())

def test_add_zero(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
add_numbers(0, 0)
self.assertIn("0", mock_out.getvalue())

def test_add_negative_numbers(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
add_numbers(-3, -7)
self.assertIn("-10", mock_out.getvalue())

def test_add_mixed_signs(self):
with patch('sys.stdout', new_callable=StringIO) as mock_out:
add_numbers(10, -3)
self.assertIn("7", mock_out.getvalue())


if __name__ == '__main__':
unittest.main()
63 changes: 63 additions & 0 deletions tests/test_general_node.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import unittest
import importlib.util
import os
from unittest.mock import patch
from io import StringIO

# Load Node from algorithms/DataStructures/trees/example1.py using importlib
_spec = importlib.util.spec_from_file_location(
"ds_trees_example1",
os.path.join(os.path.dirname(__file__), '..', 'algorithms', 'DataStructures', 'trees', 'example1.py')
)
_mod = importlib.util.module_from_spec(_spec)
with patch('sys.stdout', new_callable=StringIO):
_spec.loader.exec_module(_mod)
Node = _mod.Node


class TestNode(unittest.TestCase):

def test_node_creation(self):
node = Node("A")
self.assertEqual(node.value, "A")
self.assertEqual(node.children, [])

def test_add_child(self):
parent = Node("P")
child = Node("C")
parent.children.append(child)
self.assertEqual(len(parent.children), 1)
self.assertEqual(parent.children[0].value, "C")

def test_multiple_children(self):
root = Node("root")
root.children.append(Node("child1"))
root.children.append(Node("child2"))
root.children.append(Node("child3"))
self.assertEqual(len(root.children), 3)
self.assertEqual(root.children[0].value, "child1")
self.assertEqual(root.children[1].value, "child2")
self.assertEqual(root.children[2].value, "child3")

def test_nested_tree(self):
root = Node("A")
b = Node("B")
c = Node("C")
root.children.append(b)
root.children.append(c)
d = Node("D")
b.children.append(d)
self.assertEqual(root.children[0].children[0].value, "D")

def test_numeric_value(self):
node = Node(99)
self.assertEqual(node.value, 99)

def test_children_initially_empty(self):
node = Node("X")
self.assertIsInstance(node.children, list)
self.assertEqual(len(node.children), 0)


if __name__ == '__main__':
unittest.main()
82 changes: 82 additions & 0 deletions tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import unittest
import importlib.util
import os
from unittest.mock import patch
from io import StringIO

# Load Queue from queues/example3.py using importlib to avoid name collisions
_spec = importlib.util.spec_from_file_location(
"queues_example3",
os.path.join(os.path.dirname(__file__), '..', 'queues', 'example3.py')
)
_mod = importlib.util.module_from_spec(_spec)
with patch('sys.stdout', new_callable=StringIO):
_spec.loader.exec_module(_mod)
Queue = _mod.Queue


class TestQueue(unittest.TestCase):

def setUp(self):
self.q = Queue()

def test_new_queue_is_empty(self):
self.assertTrue(self.q.is_empty())

def test_enqueue_makes_queue_non_empty(self):
self.q.enqueue(1)
self.assertFalse(self.q.is_empty())

def test_enqueue_and_dequeue(self):
self.q.enqueue(10)
self.q.enqueue(20)
self.assertEqual(self.q.dequeue(), 10)
self.assertEqual(self.q.dequeue(), 20)

def test_fifo_order(self):
for i in range(5):
self.q.enqueue(i)
for i in range(5):
self.assertEqual(self.q.dequeue(), i)

def test_front_returns_first_without_removing(self):
self.q.enqueue("a")
self.q.enqueue("b")
self.assertEqual(self.q.front(), "a")
self.assertEqual(self.q.size(), 2)

def test_front_empty_queue(self):
self.assertIsNone(self.q.front())

def test_dequeue_empty_queue(self):
self.assertEqual(self.q.dequeue(), "Queue underflow")

def test_size(self):
self.assertEqual(self.q.size(), 0)
self.q.enqueue(1)
self.assertEqual(self.q.size(), 1)
self.q.enqueue(2)
self.assertEqual(self.q.size(), 2)
self.q.dequeue()
self.assertEqual(self.q.size(), 1)

def test_enqueue_multiple_types(self):
self.q.enqueue(42)
self.q.enqueue("hello")
self.q.enqueue([1, 2])
self.assertEqual(self.q.size(), 3)
self.assertEqual(self.q.dequeue(), 42)
self.assertEqual(self.q.dequeue(), "hello")
self.assertEqual(self.q.dequeue(), [1, 2])

def test_dequeue_until_empty(self):
self.q.enqueue(1)
self.q.enqueue(2)
self.q.dequeue()
self.q.dequeue()
self.assertTrue(self.q.is_empty())
self.assertEqual(self.q.dequeue(), "Queue underflow")


if __name__ == '__main__':
unittest.main()
76 changes: 76 additions & 0 deletions tests/test_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import unittest
import importlib.util
import os
from unittest.mock import patch
from io import StringIO

# Load the Stack class from stacks/example1.py using importlib to avoid name collisions
_spec = importlib.util.spec_from_file_location(
"stacks_example1",
os.path.join(os.path.dirname(__file__), '..', 'stacks', 'example1.py')
)
_mod = importlib.util.module_from_spec(_spec)
with patch('sys.stdout', new_callable=StringIO):
_spec.loader.exec_module(_mod)
Stack = _mod.Stack


class TestStack(unittest.TestCase):

def setUp(self):
self.stack = Stack()

def test_new_stack_is_empty(self):
self.assertTrue(self.stack.is_empty())

def test_push_makes_stack_non_empty(self):
self.stack.push(1)
self.assertFalse(self.stack.is_empty())

def test_push_and_pop(self):
self.stack.push(10)
self.stack.push(20)
self.assertEqual(self.stack.pop(), 20)
self.assertEqual(self.stack.pop(), 10)

def test_peek_returns_top_without_removing(self):
self.stack.push("a")
self.stack.push("b")
self.assertEqual(self.stack.peek(), "b")
self.assertEqual(self.stack.size(), 2)

def test_size(self):
self.assertEqual(self.stack.size(), 0)
self.stack.push(1)
self.assertEqual(self.stack.size(), 1)
self.stack.push(2)
self.assertEqual(self.stack.size(), 2)
self.stack.pop()
self.assertEqual(self.stack.size(), 1)

def test_pop_empty_stack_raises(self):
with self.assertRaises(IndexError):
self.stack.pop()

def test_peek_empty_stack_raises(self):
with self.assertRaises(IndexError):
self.stack.peek()

def test_push_multiple_types(self):
self.stack.push(1)
self.stack.push("hello")
self.stack.push([1, 2, 3])
self.assertEqual(self.stack.size(), 3)
self.assertEqual(self.stack.pop(), [1, 2, 3])
self.assertEqual(self.stack.pop(), "hello")
self.assertEqual(self.stack.pop(), 1)

def test_lifo_order(self):
for i in range(5):
self.stack.push(i)
for i in range(4, -1, -1):
self.assertEqual(self.stack.pop(), i)


if __name__ == '__main__':
unittest.main()
Loading