Skip to content

Commit 089586d

Browse files
committed
Add dedicated test function for eye method
- Add test_eye_method() as requested in review comment - Uses subTest for parameterized testing with sizes [1,2,3,4,5,10] - Uses itertools.product for efficient nested loop iteration - Tests both array comparison and individual element verification - Provides better test isolation for debugging future issues Addresses review comment: Create distinct test function for eye method in addition to exercising it in matmul test function.
1 parent 1c2d13e commit 089586d

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tests/test_gemm.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import unittest
2828
import numpy as np
29+
from itertools import product
2930
import modmesh as mm
3031

3132

@@ -84,6 +85,34 @@ def test_identity_matrix(self):
8485
self.assertEqual(list(result.shape), [3, 3])
8586
np.testing.assert_array_almost_equal(result.ndarray, a_data)
8687

88+
def test_eye_method(self):
89+
"""Test eye method creates correct identity matrices"""
90+
# Test cases: different sizes
91+
test_sizes = [1, 2, 3, 4, 5, 10]
92+
93+
for size in test_sizes:
94+
with self.subTest(size=size):
95+
# Create identity matrix using our eye method
96+
identity = self.SimpleArray.eye(size)
97+
98+
# Create expected identity matrix using NumPy
99+
expected = np.eye(size, dtype=self.dtype)
100+
101+
# Check shape
102+
self.assertEqual(list(identity.shape), [size, size])
103+
104+
# Check array values
105+
np.testing.assert_array_almost_equal(identity.ndarray, expected)
106+
107+
# Verify diagonal and off-diagonal elements explicitly using product
108+
for i, j in product(range(size), repeat=2):
109+
if i == j:
110+
self.assertEqual(identity[i, j], 1.0,
111+
f"Diagonal element ({i},{j}) should be 1.0")
112+
else:
113+
self.assertEqual(identity[i, j], 0.0,
114+
f"Off-diagonal element ({i},{j}) should be 0.0")
115+
87116
def test_zero_matrix(self):
88117
"""Test multiplication with zero matrix"""
89118
a_data = np.array([[1.0, 2.0], [3.0, 4.0]], dtype=self.dtype)

0 commit comments

Comments
 (0)