Skip to content

Commit 2ea0235

Browse files
committed
WIP...Put tests under a test class so that only one Matlab session is started for each module
1 parent 276f5fa commit 2ea0235

File tree

3 files changed

+139
-145
lines changed

3 files changed

+139
-145
lines changed

pymatbridge/tests/test_array.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,21 @@
44
import numpy.testing as npt
55
import test_utils as tu
66

7-
def test_array_size():
8-
mlab = tu.connect_to_matlab()
7+
class TestArray:
8+
# Start a Matlab session
9+
@classmethod
10+
def setup_class(cls):
11+
cls.mlab = tu.connect_to_matlab()
912

10-
array = np.random.random_sample((50,50)).tolist()
11-
res = mlab.run_func("array_size.m",{'val':array})['result']
12-
npt.assert_almost_equal(res, array, decimal=8, err_msg = "test_array_size: error")
13+
# Tear down the Matlab session
14+
@classmethod
15+
def teardown_class(cls):
16+
tu.stop_matlab(cls.mlab)
17+
18+
# Pass a 50*50 array to Matlab
19+
def test_array_size(self):
20+
array = np.random.random_sample((50,50)).tolist()
21+
res = self.mlab.run_func("array_size.m",{'val':array})['result']
22+
npt.assert_almost_equal(res, array, decimal=8, err_msg = "test_array_size: error")
1323

14-
tu.stop_matlab(mlab)
1524

pymatbridge/tests/test_json.py

Lines changed: 66 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,70 @@
11
import pymatbridge as pymat
22
import numpy.testing as npt
3+
import test_utils as tu
34

4-
# Add 1 to the argument and return it
5-
def test_demo_func():
6-
mlab = pymat.Matlab()
7-
mlab.start()
8-
npt.assert_(mlab.is_connected(), msg = "Connection failed")
9-
10-
for i in range(5):
11-
res = mlab.run_func('demo_func.m', {'a': i})['result']
12-
ans = i + 1
13-
npt.assert_equal(res, ans, err_msg = "demo_func.m test failed")
14-
15-
mlab.stop()
16-
npt.assert_(not mlab.is_connected(), msg = "Disconnection failed")
17-
18-
# Print some strange characters in Matlab, get them back and compare.
19-
def test_special_character():
20-
mlab = pymat.Matlab()
21-
mlab.start()
22-
npt.assert_(mlab.is_connected(), msg = "Connection failed")
23-
24-
# Test 1:"hi"\n
25-
res = mlab.run_code('fprintf(1, char([34, 104, 105, 34, 10]));')['content']['stdout']
26-
ans = unichr(34) + unichr(104) + unichr(105) + unichr(34) + unichr(10)
27-
npt.assert_equal(res, ans, err_msg = "Special Character Test 1 failed")
28-
29-
# Test 2:\b\n
30-
res = mlab.run_code('fprintf(1, char([8,10]));')['content']['stdout']
31-
ans = unichr(8) + unichr(10)
32-
npt.assert_equal(res, ans, err_msg = "Special Character Test 2 failed")
33-
34-
# Test 3:\f\n
35-
res = mlab.run_code('fprintf(1, char([12,10]));')['content']['stdout']
36-
ans = unichr(12) + unichr(10)
37-
npt.assert_equal(res, ans, err_msg = "Special Character Test 3 failed")
38-
39-
# Test 4:\r\n
40-
res = mlab.run_code('fprintf(1, char([13,10]));')['content']['stdout']
41-
ans = unichr(13) + unichr(10)
42-
npt.assert_equal(res, ans, err_msg = "Special Character Test 4 failed")
43-
44-
# Test 5:\t\n
45-
res = mlab.run_code('fprintf(1, char([9,10]));')['content']['stdout']
46-
ans = unichr(9) + unichr(10)
47-
npt.assert_equal(res, ans, err_msg = "Special Character Test 5 failed")
48-
49-
# Test 6:\\\n
50-
res = mlab.run_code('fprintf(1, char([92,92,10]));')['content']['stdout']
51-
ans = unichr(92) + unichr(10) # Python already encoded double-slash as 92
52-
npt.assert_equal(res, ans, err_msg = "Special Character Test 6 failed")
53-
54-
# Test 7:/\n
55-
res = mlab.run_code('fprintf(1, char([47,10]));')['content']['stdout']
56-
ans = unichr(47) + unichr(10)
57-
npt.assert_equal(res, ans, err_msg = "Special Character Test 7 failed")
58-
59-
# Test 8: Lots of strange characters
60-
res = mlab.run_code('fprintf(1,char([47,92,92,47,8,12,10,13,9,12,8,8,12,47,34,47,10,10,47,34]));')['content']['stdout']
61-
ans = unichr(47) + unichr(92) + unichr(47) + unichr(8) + unichr(12) + unichr(10) \
62-
+ unichr(13) + unichr(9) + unichr(12) + unichr(8) + unichr(8) + unichr(12) \
63-
+ unichr(47) + unichr(34) + unichr(47) + unichr(10) + unichr(10) + unichr(47) + unichr(34)
64-
npt.assert_equal(res, ans, err_msg = "Special Character Test 8 failed")
65-
66-
mlab.stop()
67-
npt.assert_(not mlab.is_connected(), msg = "Disconnection failed")
5+
class TestJson:
6+
7+
# Start a Matlab session
8+
@classmethod
9+
def setup_class(cls):
10+
cls.mlab = tu.connect_to_matlab()
11+
12+
# Tear down the Matlab session
13+
@classmethod
14+
def teardown_class(cls):
15+
tu.stop_matlab(cls.mlab)
16+
17+
18+
# Add 1 to the argument and return it
19+
def test_demo_func(self):
20+
for i in range(5):
21+
res = self.mlab.run_func('demo_func.m', {'a': i})['result']
22+
ans = i + 1
23+
npt.assert_equal(res, ans, err_msg = "demo_func.m test failed")
24+
25+
26+
# Print some strange characters in Matlab, get them back and compare.
27+
def test_special_character(self):
28+
29+
# Test 1:"hi"\n
30+
res = self.mlab.run_code('fprintf(1, char([34, 104, 105, 34, 10]));')['content']['stdout']
31+
ans = unichr(34) + unichr(104) + unichr(105) + unichr(34) + unichr(10)
32+
npt.assert_equal(res, ans, err_msg = "Special Character Test 1 failed")
33+
34+
# Test 2:\b\n
35+
res = self.mlab.run_code('fprintf(1, char([8,10]));')['content']['stdout']
36+
ans = unichr(8) + unichr(10)
37+
npt.assert_equal(res, ans, err_msg = "Special Character Test 2 failed")
38+
39+
# Test 3:\f\n
40+
res = self.mlab.run_code('fprintf(1, char([12,10]));')['content']['stdout']
41+
ans = unichr(12) + unichr(10)
42+
npt.assert_equal(res, ans, err_msg = "Special Character Test 3 failed")
43+
44+
# Test 4:\r\n
45+
res = self.mlab.run_code('fprintf(1, char([13,10]));')['content']['stdout']
46+
ans = unichr(13) + unichr(10)
47+
npt.assert_equal(res, ans, err_msg = "Special Character Test 4 failed")
48+
49+
# Test 5:\t\n
50+
res = self.mlab.run_code('fprintf(1, char([9,10]));')['content']['stdout']
51+
ans = unichr(9) + unichr(10)
52+
npt.assert_equal(res, ans, err_msg = "Special Character Test 5 failed")
53+
54+
# Test 6:\\\n
55+
res = self.mlab.run_code('fprintf(1, char([92,92,10]));')['content']['stdout']
56+
ans = unichr(92) + unichr(10) # Python already encoded double-slash as 92
57+
npt.assert_equal(res, ans, err_msg = "Special Character Test 6 failed")
58+
59+
# Test 7:/\n
60+
res = self.mlab.run_code('fprintf(1, char([47,10]));')['content']['stdout']
61+
ans = unichr(47) + unichr(10)
62+
npt.assert_equal(res, ans, err_msg = "Special Character Test 7 failed")
63+
64+
# Test 8: Lots of strange characters
65+
res = self.mlab.run_code('fprintf(1,char([47,92,92,47,8,12,10,13,9,12,8,8,12,47,34,47,10,10,47,34]));')['content']['stdout']
66+
ans = unichr(47) + unichr(92) + unichr(47) + unichr(8) + unichr(12) + unichr(10) \
67+
+ unichr(13) + unichr(9) + unichr(12) + unichr(8) + unichr(8) + unichr(12) \
68+
+ unichr(47) + unichr(34) + unichr(47) + unichr(10) + unichr(10) + unichr(47) + unichr(34)
69+
npt.assert_equal(res, ans, err_msg = "Special Character Test 8 failed")
6870

pymatbridge/tests/test_precision.py

Lines changed: 58 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,62 @@
44
import numpy.testing as npt
55
import test_utils as tu
66

7-
# Pass a 64-bit floating number through the signal path
8-
def test_float64_roundtrip():
9-
mlab = tu.connect_to_matlab()
107

11-
for i in range(0,10):
12-
val = np.float64(rd.random())
13-
res = mlab.run_func('precision_pass.m', {'val':val})['result']
14-
npt.assert_almost_equal(res, val, decimal=8, err_msg="Float64 roundtrip error")
15-
16-
tu.stop_matlab(mlab)
17-
18-
19-
# Add two 64-bit floating number in Matlab and return the sum
20-
def test_float64_sum():
21-
mlab = tu.connect_to_matlab()
22-
23-
for i in range(0,10):
24-
val1 = np.float64(rd.random())
25-
val2 = np.float64(rd.random())
26-
27-
res = mlab.run_func('precision_sum.m', {'val1':val1, 'val2':val2})['result']
28-
npt.assert_almost_equal(res, val1 + val2, decimal=8, err_msg="Float64 sum error")
29-
30-
tu.stop_matlab(mlab)
31-
32-
33-
# Multiply two 64-bit floating number in Matlab and return the product
34-
def test_float64_multiply():
35-
mlab = tu.connect_to_matlab()
36-
37-
for i in range(0,10):
38-
val1 = np.float64(rd.random())
39-
val2 = np.float64(rd.random())
40-
41-
res = mlab.run_func('precision_multiply.m', {'val1':val1, 'val2':val2})['result']
42-
npt.assert_almost_equal(res, val1 * val2, decimal=8, err_msg="Float64 multiply error")
43-
44-
tu.stop_matlab(mlab)
45-
46-
47-
# Make a division in Matlab and return the results
48-
def test_float64_divide():
49-
mlab = tu.connect_to_matlab()
50-
51-
for i in range(0,10):
52-
val1 = np.float64(rd.random())
53-
val2 = np.float64(rd.random())
54-
55-
res = mlab.run_func('precision_divide.m', {'val1':val1, 'val2':val2})['result']
56-
npt.assert_almost_equal(res, val1 / val2, decimal=8, err_msg="Float64 divide error")
57-
58-
tu.stop_matlab(mlab)
59-
60-
61-
# Calculate the square root in Matlab and return the result
62-
def test_float64_sqrt():
63-
mlab = tu.connect_to_matlab()
64-
65-
for i in range(0,10):
66-
val = np.float64(rd.random())
67-
68-
res = mlab.run_func('precision_sqrt.m', {'val':val})['result']
69-
npt.assert_almost_equal(res, np.sqrt(val), decimal=8, err_msg="Float64 square root error")
70-
71-
tu.stop_matlab(mlab)
72-
73-
def test_tuple():
74-
pass
75-
76-
77-
def test_dict():
78-
pass
79-
80-
81-
def test_array():
82-
pass
8+
class TestPrecision:
9+
10+
# Start a Matlab session
11+
@classmethod
12+
def setup_class(cls):
13+
cls.mlab = tu.connect_to_matlab()
14+
15+
# Tear down the Matlab session
16+
@classmethod
17+
def teardown_class(cls):
18+
tu.stop_matlab(cls.mlab)
19+
20+
21+
# Pass a 64-bit floating number through the signal path
22+
def test_float64_roundtrip(self):
23+
for i in range(0,10):
24+
val = np.float64(rd.random())
25+
res = self.mlab.run_func('precision_pass.m', {'val':val})['result']
26+
npt.assert_almost_equal(res, val, decimal=8, err_msg="Float64 roundtrip error")
27+
28+
29+
# Add two 64-bit floating number in Matlab and return the sum
30+
def test_float64_sum(self):
31+
for i in range(0,10):
32+
val1 = np.float64(rd.random())
33+
val2 = np.float64(rd.random())
34+
35+
res = self.mlab.run_func('precision_sum.m', {'val1':val1, 'val2':val2})['result']
36+
npt.assert_almost_equal(res, val1 + val2, decimal=8, err_msg="Float64 sum error")
37+
38+
39+
# Multiply two 64-bit floating number in Matlab and return the product
40+
def test_float64_multiply(self):
41+
for i in range(0,10):
42+
val1 = np.float64(rd.random())
43+
val2 = np.float64(rd.random())
44+
45+
res = self.mlab.run_func('precision_multiply.m', {'val1':val1, 'val2':val2})['result']
46+
npt.assert_almost_equal(res, val1 * val2, decimal=8, err_msg="Float64 multiply error")
47+
48+
49+
# Make a division in Matlab and return the results
50+
def test_float64_divide(self):
51+
for i in range(0,10):
52+
val1 = np.float64(rd.random())
53+
val2 = np.float64(rd.random())
54+
55+
res = self.mlab.run_func('precision_divide.m', {'val1':val1, 'val2':val2})['result']
56+
npt.assert_almost_equal(res, val1 / val2, decimal=8, err_msg="Float64 divide error")
57+
58+
59+
# Calculate the square root in Matlab and return the result
60+
def test_float64_sqrt(self):
61+
for i in range(0,10):
62+
val = np.float64(rd.random())
63+
64+
res = self.mlab.run_func('precision_sqrt.m', {'val':val})['result']
65+
npt.assert_almost_equal(res, np.sqrt(val), decimal=8, err_msg="Float64 square root error")

0 commit comments

Comments
 (0)