|
| 1 | +import unittest |
| 2 | +import pycubool |
| 3 | + |
| 4 | + |
| 5 | +class TestMatrixMethods(unittest.TestCase): |
| 6 | + |
| 7 | + def setUp(self) -> None: |
| 8 | + """ |
| 9 | + Preparatory actions |
| 10 | + Performed BEFORE tests |
| 11 | + """ |
| 12 | + |
| 13 | + pass |
| 14 | + |
| 15 | + def tearDown(self) -> None: |
| 16 | + """ |
| 17 | + Final actions |
| 18 | + Performed AFTER tests |
| 19 | + """ |
| 20 | + |
| 21 | + pass |
| 22 | + |
| 23 | + def test_fill(self): |
| 24 | + """ |
| 25 | + Unit test for creation and filling a matrix |
| 26 | + """ |
| 27 | + a = pycubool.Matrix.empty([4, 4]) |
| 28 | + a_rows = [0, 1, 2, 3, 3, 3, 3] |
| 29 | + a_cols = [0, 1, 2, 0, 1, 2, 3] |
| 30 | + a.build(a_rows, a_cols, nvals=7) |
| 31 | + |
| 32 | + dim = a.shape() |
| 33 | + rows, cols = a.to_lists() |
| 34 | + self.assertEqual(b.nvals, 7) |
| 35 | + self.assertListEqual([4, 4], dim) |
| 36 | + self.assertListEqual(rows, a_rows) |
| 37 | + self.assertListEqual(cols, a_cols) |
| 38 | + |
| 39 | + def test_resize(self): |
| 40 | + """ |
| 41 | + Unit test for resizing a matrix |
| 42 | + """ |
| 43 | + a = pycubool.Matrix.empty([4, 4]) |
| 44 | + a_rows = [] |
| 45 | + a_cols = [] |
| 46 | + a.build(a_rows, a_cols, nvals=0) |
| 47 | + |
| 48 | + a.resize(5, 5) |
| 49 | + dim = a.shape() |
| 50 | + self.assertListEqual([5, 5], dim) |
| 51 | + |
| 52 | + def test_duplicate(self): |
| 53 | + """ |
| 54 | + Unit test for duplication a matrix |
| 55 | + """ |
| 56 | + |
| 57 | + a = pycubool.Matrix.empty([4, 4]) |
| 58 | + a_rows = [0, 1, 2, 3, 3, 3, 3] |
| 59 | + a_cols = [0, 1, 2, 0, 1, 2, 3] |
| 60 | + a.build(a_rows, a_cols, nvals=7) |
| 61 | + |
| 62 | + b = a.duplicate() |
| 63 | + |
| 64 | + dim = b.shape() |
| 65 | + b_rows, b_cols = b.to_lists() |
| 66 | + self.assertEqual(b.nvals, 7) |
| 67 | + self.assertListEqual([4, 4], dim) |
| 68 | + self.assertListEqual(b_rows, a_rows) |
| 69 | + self.assertListEqual(b_cols, a_cols) |
| 70 | + |
| 71 | + def test_transpose(self): |
| 72 | + """ |
| 73 | + Unit test for transposing a matrix |
| 74 | + """ |
| 75 | + |
| 76 | + a = pycubool.Matrix.empty([2, 3]) |
| 77 | + a_rows = [1, 0] |
| 78 | + a_cols = [0, 1] |
| 79 | + a.build(a_rows, a_cols, nvals=2) |
| 80 | + |
| 81 | + b = a.transpose() |
| 82 | + |
| 83 | + dim = b.shape() |
| 84 | + b_rows, b_cols = b.to_lists() |
| 85 | + self.assertEqual(b.nvals, 2) |
| 86 | + self.assertListEqual([3, 2], dim) |
| 87 | + self.assertListEqual(b_cols, a_rows) |
| 88 | + self.assertListEqual(b_rows, a_cols) |
| 89 | + |
| 90 | + |
| 91 | +if __name__ == "__main__": |
| 92 | + unittest.main() |
0 commit comments