Skip to content

Commit 1d20faa

Browse files
committed
Merge pull request #37 from haoxingz/test_more
Test more
2 parents c874dbe + 10a9c0c commit 1d20faa

File tree

4 files changed

+121
-5
lines changed

4 files changed

+121
-5
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pymatbridge as pymat
2+
import numpy.testing as npt
3+
import test_utils as tu
4+
5+
6+
class TestFunctionProcessor:
7+
8+
# Start a Matlab session before running any tests
9+
@classmethod
10+
def setup_class(cls):
11+
cls.mlab = tu.connect_to_matlab()
12+
13+
# Tear down the Matlab session after running all the tests
14+
@classmethod
15+
def teardown_class(cls):
16+
tu.stop_matlab(cls.mlab)
17+
18+
# Test the "is_function_processor_working()" function
19+
def test_is_function_processor_working(self):
20+
npt.assert_equal(self.mlab.is_function_processor_working(), True)
21+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import pymatbridge as pymat
2+
import numpy.testing as npt
3+
import test_utils as tu
4+
5+
6+
class TestGetVariable:
7+
8+
# Start a Matlab session before running any tests
9+
@classmethod
10+
def setup_class(cls):
11+
cls.mlab = tu.connect_to_matlab()
12+
13+
# Tear down the Matlab session after running all the tests
14+
@classmethod
15+
def teardown_class(cls):
16+
tu.stop_matlab(cls.mlab)
17+
18+
19+
# Set up a floating point and get it
20+
def test_get_float(self):
21+
self.mlab.run_code("a = 456345.345345")
22+
self.mlab.run_code("b = 0.39748e3")
23+
24+
npt.assert_equal(self.mlab.get_variable('a'), unicode("456345.3453"))
25+
npt.assert_equal(self.mlab.get_variable('b'), unicode("397.48"))
26+
27+
28+
# Get some arrays
29+
def test_get_array(self):
30+
self.mlab.run_code("a = [1 2 3 4]")
31+
self.mlab.run_code("b = [1 2; 3 4]")
32+
33+
npt.assert_equal(self.mlab.get_variable('a'), unicode("[1,2,3,4]"))
34+
npt.assert_equal(self.mlab.get_variable('b'), unicode("[[1,2],[3,4]]"))
35+
36+
37+
# Try to get a non-existent variable
38+
# This one will always fail now since the matlab function cannot handle this situation
39+
# def test_nonexistent_var(self):
40+
# self.mlab.run_code("clear")
41+
42+
# npt.assert_equal(self.mlab.get_variable('a'), unicode("456345.3453"))

pymatbridge/tests/test_precision.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def test_float64_roundtrip(self):
2323
for i in range(0,10):
2424
val = np.float64(rd.random())
2525
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")
26+
npt.assert_almost_equal(res, val, decimal=7, err_msg="Float64 roundtrip error")
2727

2828
# Add two 64-bit floating number in Matlab and return the sum
2929
def test_float64_sum(self):
@@ -32,7 +32,7 @@ def test_float64_sum(self):
3232
val2 = np.float64(rd.random())
3333

3434
res = self.mlab.run_func('precision_sum.m', {'val1':val1, 'val2':val2})['result']
35-
npt.assert_almost_equal(res, val1 + val2, decimal=8, err_msg="Float64 sum error")
35+
npt.assert_almost_equal(res, val1 + val2, decimal=7, err_msg="Float64 sum error")
3636

3737
# Multiply two 64-bit floating number in Matlab and return the product
3838
def test_float64_multiply(self):
@@ -41,7 +41,7 @@ def test_float64_multiply(self):
4141
val2 = np.float64(rd.random())
4242

4343
res = self.mlab.run_func('precision_multiply.m', {'val1':val1, 'val2':val2})['result']
44-
npt.assert_almost_equal(res, val1 * val2, decimal=8, err_msg="Float64 multiply error")
44+
npt.assert_almost_equal(res, val1 * val2, decimal=7, err_msg="Float64 multiply error")
4545

4646
# Make a division in Matlab and return the results
4747
def test_float64_divide(self):
@@ -50,12 +50,12 @@ def test_float64_divide(self):
5050
val2 = np.float64(rd.random())
5151

5252
res = self.mlab.run_func('precision_divide.m', {'val1':val1, 'val2':val2})['result']
53-
npt.assert_almost_equal(res, val1 / val2, decimal=8, err_msg="Float64 divide error")
53+
npt.assert_almost_equal(res, val1 / val2, decimal=7, err_msg="Float64 divide error")
5454

5555
# Calculate the square root in Matlab and return the result
5656
def test_float64_sqrt(self):
5757
for i in range(0,10):
5858
val = np.float64(rd.random())
5959

6060
res = self.mlab.run_func('precision_sqrt.m', {'val':val})['result']
61-
npt.assert_almost_equal(res, np.sqrt(val), decimal=8, err_msg="Float64 square root error")
61+
npt.assert_almost_equal(res, np.sqrt(val), decimal=7, err_msg="Float64 square root error")

pymatbridge/tests/test_run_code.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import pymatbridge as pymat
2+
import numpy.testing as npt
3+
import test_utils as tu
4+
5+
6+
class TestRunCode:
7+
8+
# Start a Matlab session before running any tests
9+
@classmethod
10+
def setup_class(cls):
11+
cls.mlab = tu.connect_to_matlab()
12+
13+
# Tear down the Matlab session after running all the tests
14+
@classmethod
15+
def teardown_class(cls):
16+
tu.stop_matlab(cls.mlab)
17+
18+
# Running 'disp()' in Matlab command window
19+
def test_disp(self):
20+
result1 = self.mlab.run_code("disp('Hello world')")['content']['stdout']
21+
result2 = self.mlab.run_code("disp(' ')")['content']['stdout']
22+
result3 = self.mlab.run_code("disp('')")['content']['stdout']
23+
24+
npt.assert_equal(result1, "Hello world\n")
25+
npt.assert_equal(result2, " \n")
26+
npt.assert_equal(result3, "")
27+
28+
# Make some assignments and run basic operations
29+
def test_basic_operation(self):
30+
result_assignment_a = self.mlab.run_code("a = 21.23452261")['content']['stdout']
31+
result_assignment_b = self.mlab.run_code("b = 347.745")['content']['stdout']
32+
result_sum = self.mlab.run_code("a + b")['content']['stdout']
33+
result_diff = self.mlab.run_code("a - b")['content']['stdout']
34+
result_product = self.mlab.run_code("a * b")['content']['stdout']
35+
result_division = self.mlab.run_code("c = a / b")['content']['stdout']
36+
37+
38+
npt.assert_equal(result_assignment_a, unicode("\na =\n\n 21.2345\n\n"))
39+
npt.assert_equal(result_assignment_b, unicode("\nb =\n\n 347.7450\n\n"))
40+
npt.assert_equal(result_sum, unicode("\nans =\n\n 368.9795\n\n"))
41+
npt.assert_equal(result_diff, unicode("\nans =\n\n -326.5105\n\n"))
42+
npt.assert_equal(result_product, unicode("\nans =\n\n 7.3842e+03\n\n"))
43+
npt.assert_equal(result_division, unicode("\nc =\n\n 0.0611\n\n"))
44+
45+
# Put in some undefined code
46+
def test_undefined_code(self):
47+
success = self.mlab.run_code("this_is_nonsense")['success']
48+
message = self.mlab.run_code("this_is_nonsense")['content']['stdout']
49+
50+
npt.assert_equal(success, "false")
51+
npt.assert_equal(message, "Undefined function or variable 'this_is_nonsense'.")
52+
53+

0 commit comments

Comments
 (0)