Skip to content

Commit c874dbe

Browse files
committed
Merge pull request #32 from haoxingz/list_test
WIP... This branch is used for testing arrays
2 parents b5bca51 + 2be89f7 commit c874dbe

File tree

6 files changed

+134
-120
lines changed

6 files changed

+134
-120
lines changed

pymatbridge/matlab/functions/json2mat.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,12 @@
3636

3737
function y=extract_struct(x)
3838
%detag arrays first
39+
PRECISION = 10;
3940
indOC=extract_embed(x,'[',']');
4041
n=size(indOC,1);
4142
for i=n:-1:1
4243
tag{i}=json2mat(x(indOC(i,1):indOC(i,2)));
43-
x=[x(1:indOC(i,1)-1),'tag{',num2str(i),'}',x(indOC(i,2)+1:end)];
44+
x=[x(1:indOC(i,1)-1),'tag{',num2str(i, PRECISION),'}',x(indOC(i,2)+1:end)];
4445
end
4546

4647

@@ -66,19 +67,20 @@
6667

6768
function y=extract_cell(x)
6869

70+
PRECISION = 10;
6971
indOC=extract_embed(x,'{','}');
7072
n=size(indOC,1);
7173
for i=n:-1:1
7274
tag{i}=json2mat(x(indOC(i,1):indOC(i,2)));
73-
x=[x(1:indOC(i,1)-1),'tag~<',num2str(i),'>~',x(indOC(i,2)+1:end)];
75+
x=[x(1:indOC(i,1)-1),'tag~<',num2str(i, PRECISION),'>~',x(indOC(i,2)+1:end)];
7476
end
7577
indOC=extract_embed(x,'[',']');
7678
m=size(indOC,1);
7779
for j=m:-1:1
7880
i=n+j;
7981
tag{i}=json2mat(x(indOC(i,1):indOC(i,2)));
8082
try;tag{i}=cell2mat(tag{i});end
81-
x=[x(1:indOC(i,1)-1),'tag{',num2str(i),'}',x(indOC(i,2)+1:end)];
83+
x=[x(1:indOC(i,1)-1),'tag{',num2str(i, PRECISION),'}',x(indOC(i,2)+1:end)];
8284
end
8385
x=strrep(x,'~<','{');
8486
x=strrep(x,'>~','}');

pymatbridge/matlab/functions/mat2json.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
else
4343
s=size(M);
4444
if (length(s)==2)&(s(1)<2) % horizontal or null vector
45-
J=['[',num2str(M),']']; % and of destroying dimensionality
45+
J=['[',num2str(M, PRECISION),']']; % and of destroying dimensionality
4646
J=regexprep(J,'\s+',',');
4747
elseif length(s)==2 %2D solution
4848
J='[';
@@ -51,7 +51,7 @@
5151
end
5252
J(end)=']';
5353
elseif length(s)>2 % for now treat higher dimensions as linear vectors
54-
J=['[',num2str(M(:)'),']']; % and of destroying dimensionality
54+
J=['[',num2str(M(:)', PRECISION),']']; % and of destroying dimensionality
5555
J=regexprep(J,'\s+',',');
5656
end
5757
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function res = array_size(args)
2+
% Simply returns the input array
3+
4+
res = args.val;
5+
6+
end %end

pymatbridge/tests/test_array.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pymatbridge as pymat
2+
import random as rd
3+
import numpy as np
4+
import numpy.testing as npt
5+
import test_utils as tu
6+
7+
class TestArray:
8+
9+
# Start a Matlab session before any tests
10+
@classmethod
11+
def setup_class(cls):
12+
cls.mlab = tu.connect_to_matlab()
13+
14+
# Tear down the Matlab session after all the tests are done
15+
@classmethod
16+
def teardown_class(cls):
17+
tu.stop_matlab(cls.mlab)
18+
19+
20+
# Pass a 50*50 array to Matlab
21+
def test_array_size(self):
22+
array = np.random.random_sample((50,50)).tolist()
23+
res = self.mlab.run_func("array_size.m",{'val':array})['result']
24+
npt.assert_almost_equal(res, array, decimal=8, err_msg = "test_array_size: error")
25+
26+

pymatbridge/tests/test_json.py

Lines changed: 53 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,69 @@
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")
5+
class TestJson:
96

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")
7+
# Start a Matlab session before doing any tests
8+
@classmethod
9+
def setup_class(cls):
10+
cls.mlab = tu.connect_to_matlab()
1411

15-
mlab.stop()
16-
npt.assert_(not mlab.is_connected(), msg = "Disconnection failed")
12+
# Tear down the Matlab session after all the tests are done
13+
@classmethod
14+
def teardown_class(cls):
15+
tu.stop_matlab(cls.mlab)
1716

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")
2317

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")
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")
2824

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")
25+
# Print some strange characters in Matlab, get them back and compare.
26+
def test_special_character(self):
3327

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")
28+
# Test 1:"hi"\n
29+
res = self.mlab.run_code('fprintf(1, char([34, 104, 105, 34, 10]));')['content']['stdout']
30+
ans = unichr(34) + unichr(104) + unichr(105) + unichr(34) + unichr(10)
31+
npt.assert_equal(res, ans, err_msg = "Special Character Test 1 failed")
3832

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")
33+
# Test 2:\b\n
34+
res = self.mlab.run_code('fprintf(1, char([8,10]));')['content']['stdout']
35+
ans = unichr(8) + unichr(10)
36+
npt.assert_equal(res, ans, err_msg = "Special Character Test 2 failed")
4337

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")
38+
# Test 3:\f\n
39+
res = self.mlab.run_code('fprintf(1, char([12,10]));')['content']['stdout']
40+
ans = unichr(12) + unichr(10)
41+
npt.assert_equal(res, ans, err_msg = "Special Character Test 3 failed")
4842

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")
43+
# Test 4:\r\n
44+
res = self.mlab.run_code('fprintf(1, char([13,10]));')['content']['stdout']
45+
ans = unichr(13) + unichr(10)
46+
npt.assert_equal(res, ans, err_msg = "Special Character Test 4 failed")
5347

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")
48+
# Test 5:\t\n
49+
res = self.mlab.run_code('fprintf(1, char([9,10]));')['content']['stdout']
50+
ans = unichr(9) + unichr(10)
51+
npt.assert_equal(res, ans, err_msg = "Special Character Test 5 failed")
5852

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")
53+
# Test 6:\\\n
54+
res = self.mlab.run_code('fprintf(1, char([92,92,10]));')['content']['stdout']
55+
ans = unichr(92) + unichr(10) # Python already encoded double-slash as 92
56+
npt.assert_equal(res, ans, err_msg = "Special Character Test 6 failed")
6557

66-
mlab.stop()
67-
npt.assert_(not mlab.is_connected(), msg = "Disconnection failed")
58+
# Test 7:/\n
59+
res = self.mlab.run_code('fprintf(1, char([47,10]));')['content']['stdout']
60+
ans = unichr(47) + unichr(10)
61+
npt.assert_equal(res, ans, err_msg = "Special Character Test 7 failed")
62+
63+
# Test 8: Lots of strange characters
64+
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']
65+
ans = unichr(47) + unichr(92) + unichr(47) + unichr(8) + unichr(12) + unichr(10) \
66+
+ unichr(13) + unichr(9) + unichr(12) + unichr(8) + unichr(8) + unichr(12) \
67+
+ unichr(47) + unichr(34) + unichr(47) + unichr(10) + unichr(10) + unichr(47) + unichr(34)
68+
npt.assert_equal(res, ans, err_msg = "Special Character Test 8 failed")
6869

pymatbridge/tests/test_precision.py

Lines changed: 42 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,79 +4,58 @@
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")
8+
class TestPrecision:
159

16-
tu.stop_matlab(mlab)
10+
# Start a Matlab session before running any tests
11+
@classmethod
12+
def setup_class(cls):
13+
cls.mlab = tu.connect_to_matlab()
1714

15+
# Tear down the Matlab session after running all the tests
16+
@classmethod
17+
def teardown_class(cls):
18+
tu.stop_matlab(cls.mlab)
1819

19-
# Add two 64-bit floating number in Matlab and return the sum
20-
def test_float64_sum():
21-
mlab = tu.connect_to_matlab()
2220

23-
for i in range(0,10):
24-
val1 = np.float64(rd.random())
25-
val2 = np.float64(rd.random())
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")
2627

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")
28+
# Add two 64-bit floating number in Matlab and return the sum
29+
def test_float64_sum(self):
30+
for i in range(0,10):
31+
val1 = np.float64(rd.random())
32+
val2 = np.float64(rd.random())
2933

30-
tu.stop_matlab(mlab)
34+
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")
3136

37+
# Multiply two 64-bit floating number in Matlab and return the product
38+
def test_float64_multiply(self):
39+
for i in range(0,10):
40+
val1 = np.float64(rd.random())
41+
val2 = np.float64(rd.random())
3242

33-
# Multiply two 64-bit floating number in Matlab and return the product
34-
def test_float64_multiply():
35-
mlab = tu.connect_to_matlab()
43+
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")
3645

37-
for i in range(0,10):
38-
val1 = np.float64(rd.random())
39-
val2 = np.float64(rd.random())
46+
# Make a division in Matlab and return the results
47+
def test_float64_divide(self):
48+
for i in range(0,10):
49+
val1 = np.float64(rd.random())
50+
val2 = np.float64(rd.random())
4051

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")
52+
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")
4354

44-
tu.stop_matlab(mlab)
55+
# Calculate the square root in Matlab and return the result
56+
def test_float64_sqrt(self):
57+
for i in range(0,10):
58+
val = np.float64(rd.random())
4559

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
60+
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")

0 commit comments

Comments
 (0)