|
| 1 | +import unittest |
| 2 | + |
| 3 | + |
| 4 | +class TestRuntime(unittest.TestCase): |
| 5 | + |
| 6 | + def test_import_package(self): |
| 7 | + try: |
| 8 | + import spm |
| 9 | + except Exception: |
| 10 | + self.fail('Cannot import package.') |
| 11 | + |
| 12 | + def test_import_runtime(self): |
| 13 | + try: |
| 14 | + from spm import Runtime |
| 15 | + except Exception: |
| 16 | + self.fail('Cannot import Runtime.') |
| 17 | + |
| 18 | + def test_initialise_runtime(self): |
| 19 | + try: |
| 20 | + from spm import Runtime |
| 21 | + Runtime.instance() |
| 22 | + except Exception: |
| 23 | + self.fail('Cannot initialise Runtime.') |
| 24 | + |
| 25 | + def test_call_matlab(self): |
| 26 | + result = None |
| 27 | + try: |
| 28 | + from spm import Runtime |
| 29 | + result = Runtime.call('eval', '1+1') |
| 30 | + except Exception: |
| 31 | + self.fail('Calling Matlab failed.') |
| 32 | + |
| 33 | + self.assertEqual(result, 2) |
| 34 | + |
| 35 | + def test_call_matlab_noargout(self): |
| 36 | + result = True |
| 37 | + try: |
| 38 | + from spm import Runtime |
| 39 | + result = Runtime.call('magic', 0, nargout=0) |
| 40 | + except Exception: |
| 41 | + self.fail('Calling Matlab without argout failed.') |
| 42 | + |
| 43 | + self.assertTrue(result is None) |
| 44 | + |
| 45 | + def test_0d_empty_cell_to_matlab(self): |
| 46 | + try: |
| 47 | + from spm import Runtime, Cell |
| 48 | + idt = Runtime.call('eval', '@(x) x') |
| 49 | + c = Cell() |
| 50 | + c2 = idt(c) |
| 51 | + except Exception: |
| 52 | + self.fail('OD empty cell to Matlab failed.') |
| 53 | + |
| 54 | + self.assertIsInstance(c2, Cell) |
| 55 | + self.assertEqual(c2.shape, c.shape) |
| 56 | + |
| 57 | + def test_1d_empty_cell_to_matlab(self): |
| 58 | + try: |
| 59 | + from spm import Runtime, Cell |
| 60 | + idt = Runtime.call('eval', '@(x) x') |
| 61 | + c = Cell(3) |
| 62 | + c2 = idt(c) |
| 63 | + except Exception: |
| 64 | + self.fail('1D empty cell to Matlab failed.') |
| 65 | + |
| 66 | + self.assertIsInstance(c2, Cell) |
| 67 | + self.assertEqual(c2.shape, c.shape) |
| 68 | + |
| 69 | + def test_2d_empty_cell_to_matlab(self): |
| 70 | + try: |
| 71 | + from spm import Runtime, Cell |
| 72 | + idt = Runtime.call('eval', '@(x) x') |
| 73 | + c = Cell(3, 2) |
| 74 | + c2 = idt(c) |
| 75 | + except Exception: |
| 76 | + self.fail('2D empty cell to Matlab failed.') |
| 77 | + |
| 78 | + self.assertIsInstance(c2, Cell) |
| 79 | + self.assertEqual(c2.shape, c.shape) |
| 80 | + |
| 81 | + def test_1d_cell_to_matlab(self): |
| 82 | + try: |
| 83 | + from spm import Runtime, Cell |
| 84 | + idt = Runtime.call('eval', '@(x) x') |
| 85 | + c = Cell.from_any([1, 2, 3]) |
| 86 | + c2 = idt(c) |
| 87 | + except Exception: |
| 88 | + self.fail('1D cell to Matlab failed.') |
| 89 | + |
| 90 | + self.assertIsInstance(c2, Cell) |
| 91 | + self.assertEqual(c2.shape, c.shape) |
| 92 | + self.assertTrue(c2.tolist() == c.tolist()) |
| 93 | + |
| 94 | + def test_2d_cell_to_matlab(self): |
| 95 | + try: |
| 96 | + from spm import Runtime, Cell |
| 97 | + import numpy as np |
| 98 | + idt = Runtime.call('eval', '@(x) x') |
| 99 | + c = Cell.from_array(np.random.randn(2, 3)) |
| 100 | + c2 = idt(c) |
| 101 | + except Exception: |
| 102 | + self.fail('1D cell to Matlab failed.') |
| 103 | + |
| 104 | + self.assertIsInstance(c2, Cell) |
| 105 | + self.assertEqual(c2.shape, c.shape) |
| 106 | + self.assertTrue(c2.tolist() == c.tolist()) |
| 107 | + |
| 108 | + def test_empty_struct_to_matlab(self): |
| 109 | + try: |
| 110 | + from spm import Runtime, Struct |
| 111 | + idt = Runtime.call('eval', '@(x) x') |
| 112 | + s = Struct() |
| 113 | + s2 = idt(s) |
| 114 | + except Exception: |
| 115 | + self.fail('Empty struct to Matlab failed.') |
| 116 | + |
| 117 | + self.assertIsInstance(s2, Struct) |
| 118 | + self.assertEqual(s2.shape, s.shape) |
| 119 | + |
| 120 | + def test_empty_array_to_matlab(self): |
| 121 | + try: |
| 122 | + from spm import Runtime, Array |
| 123 | + idt = Runtime.call('eval', '@(x) x') |
| 124 | + a = Array() |
| 125 | + a2 = idt(a) |
| 126 | + except Exception: |
| 127 | + self.fail('Empty array to Matlab failed.') |
| 128 | + |
| 129 | + self.assertIsInstance(a2, Array) |
| 130 | + self.assertEqual(a2.shape, a.shape) |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == '__main__': |
| 137 | + unittest.main() |
0 commit comments