2025-11-13 13:29:16 +01:00

99 lines
3.2 KiB
Python

import pytest
from src.matrixmania.compute import transpose
def test_square_matrix():
"""Tests a standard 2x2 square matrix."""
matrix = [[1, 2], [3, 4]]
expected = [[1, 3], [2, 4]]
assert transpose(matrix) == expected
def test_rectangular_matrix_wide():
"""Tests a 2x3 rectangular matrix (more columns than rows)."""
matrix = [[1, 2, 3], [4, 5, 6]]
expected = [[1, 4], [2, 5], [3, 6]]
assert transpose(matrix) == expected
def test_rectangular_matrix_tall():
"""Tests a 3x2 rectangular matrix (more rows than columns)."""
matrix = [[1, 2], [3, 4], [5, 6]]
expected = [[1, 3, 5], [2, 4, 6]]
assert transpose(matrix) == expected
def test_row_vector():
"""Tests a single row matrix (1x4)."""
matrix = [[10, 20, 30, 40]]
expected = [[10], [20], [30], [40]]
assert transpose(matrix) == expected
def test_column_vector():
"""Tests a single column matrix (4x1)."""
matrix = [[10], [20], [30], [40]]
expected = [[10, 20, 30, 40]]
assert transpose(matrix) == expected
def test_single_element_matrix():
"""Tests a 1x1 matrix."""
matrix = [[-5]]
expected = [[-5]]
assert transpose(matrix) == expected
def test_identity_matrix():
"""Tests an identity matrix, which is its own transpose."""
matrix = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
expected = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
assert transpose(matrix) == expected
# --- Error Handling Tests (Invalid Inputs) ---
def test_raises_error_if_not_list():
"""Tests that a ValueError is raised if the input is not a list (e.g., None)."""
with pytest.raises(ValueError, match="Argument needs to be a nested list of integers"):
transpose(None)
def test_raises_error_if_not_nested_list():
"""Tests that a ValueError is raised if the input is a flat list (not nested)."""
with pytest.raises(ValueError, match="Argument needs to be a nested list of integers"):
transpose([1, 2, 3])
def test_raises_error_if_not_list_of_int():
"""
Tests that a ValueError is raised if the matrix contains non-integers.
Note: The function's current check only validates the very first element (matrix[0][0]).
"""
with pytest.raises(ValueError, match="Argument needs to be a nested list of integers"):
transpose([[1.0, 2.0], [3.0, 4.0]])
def test_raises_error_on_empty_list_inner():
"""
Tests how the function handles a list with an empty inner list.
The check 'isinstance(matrix[0][0], int)' will fail with an IndexError.
The function *should* raise a ValueError, but it raises an IndexError due to brittle validation.
We test for the 'ValueError' that the *validation line itself* is supposed to catch.
Update: If the input is [[]], matrix[0] is [], and matrix[0][0] causes an IndexError.
This IndexError happens *before* the ValueError can be raised by the 'if' condition.
Therefore, we must test for the actual error that occurs.
"""
with pytest.raises(IndexError):
transpose([[]])
def test_raises_error_on_empty_list_outer():
"""
Tests how the function handles a completely empty list.
The check 'isinstance(matrix[0], list)' will fail with an IndexError.
"""
with pytest.raises(IndexError):
transpose([])