Skip to content

Commit 1ec7240

Browse files
committed
add py2mat.m test
1 parent 2df1055 commit 1ec7240

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

TestPy2Mat.m

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
classdef TestPy2Mat < matlab.unittest.TestCase
2+
3+
4+
properties (TestParameter)
5+
matlabA = {"single", "double", "single", "complex", "complex", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "logical"}
6+
pythonA = {"float32", "float64", "float16", "complex64", "complex128", "int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64", "bool"}
7+
matlabS = {"datetime", "double", "complex", "int64", "logical"}
8+
end
9+
10+
methods (Test, ParameterCombination = 'sequential')
11+
12+
function test_py2mat_numpy(tc, matlabA, pythonA)
13+
import matlab.unittest.constraints.IsOfClass;
14+
15+
A = 1:5;
16+
17+
if matlabA == "complex"
18+
A = complex(A);
19+
else
20+
A = cast(A, matlabA);
21+
end
22+
23+
% https://numpy.org/doc/stable/reference/generated/numpy.array.html
24+
pyA = py.numpy.array(A, pythonA);
25+
26+
B = py2mat(pyA);
27+
28+
if matlabA == "complex"
29+
tc.verifyFalse(isreal(B));
30+
else
31+
tc.verifyThat(B, IsOfClass(matlabA));
32+
tc.verifyEqual(B, A);
33+
end
34+
35+
end
36+
37+
38+
function test_py2mat_scalar(tc, matlabS)
39+
import matlab.unittest.constraints.IsOfClass;
40+
41+
A = 1;
42+
43+
switch matlabS
44+
case "complex"
45+
A = complex(A);
46+
pyA = py.complex(A);
47+
case "datetime"
48+
A = datetime(2021, 12, 25, 1, 2, 34.56);
49+
pyA = mpickle.datetime2pydatetime(A);
50+
case "int64"
51+
A = int64(A);
52+
pyA = py.int(A);
53+
case "double"
54+
pyA = py.float(A);
55+
case "logical"
56+
pyA = py.bool(A);
57+
A = logical(A); %#ok<LOGL>
58+
otherwise
59+
error("Unknown type")
60+
end
61+
62+
B = py2mat(pyA);
63+
64+
if matlabS == "complex"
65+
tc.verifyFalse(isreal(B));
66+
else
67+
tc.verifyThat(B, IsOfClass(matlabS));
68+
tc.verifyEqual(B, A);
69+
end
70+
71+
end
72+
73+
end
74+
75+
end

0 commit comments

Comments
 (0)