Skip to content

Commit 756f758

Browse files
committed
test:windows: parameterize
mex: string or char input
1 parent 1dca437 commit 756f758

File tree

8 files changed

+97
-69
lines changed

8 files changed

+97
-69
lines changed

src/is_char_device.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,30 +62,35 @@ class MexFunction : public matlab::mex::Function {
6262
public:
6363
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
6464
// boilerplate engine & ArrayFactory setup
65-
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
65+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
6666

6767
matlab::data::ArrayFactory factory;
68-
// boilerplate input checks
68+
// wrangle inputs
69+
std::string in;
70+
6971
if (inputs.size() != 1) {
70-
matlabPtr->feval(u"error", 0,
72+
matlabEng->feval(u"error", 0,
7173
std::vector<matlab::data::Array>({ factory.createScalar("One input required") }));
7274
}
73-
if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING ||
74-
inputs[0].getNumberOfElements() != 1) {
75-
matlabPtr->feval(u"error", 0,
76-
std::vector<matlab::data::Array>({ factory.createScalar("Input must be a scalar string") }));
75+
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
76+
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
77+
in = stringArr[0];
78+
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
79+
matlab::data::CharArray charArr = inputs[0];
80+
in.assign(charArr.begin(), charArr.end());
81+
} else {
82+
matlabEng->feval(u"error", 0,
83+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
7784
}
7885

79-
// Matlab strings are an array, so we use [0][0] to get the first element
80-
std::string inputStr = inputs[0][0];
81-
8286
// actual function algorithm / computation
83-
bool y = fs_is_char_device(inputStr);
87+
bool y = fs_is_char_device(in);
8488

8589
// convert to Matlab output
8690
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html
8791
// https://www.mathworks.com/help/matlab/apiref/matlab.data.arrayfactory.html
8892

8993
outputs[0] = factory.createScalar<bool>(y);
94+
9095
}
9196
};

src/is_rosetta.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ class MexFunction : public matlab::mex::Function {
1111
public:
1212
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
1313
// boilerplate engine & ArrayFactory setup
14-
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
14+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
1515

1616
matlab::data::ArrayFactory factory;
1717
// boilerplate input checks
1818
if (inputs.size() != 0) {
19-
matlabPtr->feval(u"error", 0,
20-
std::vector<matlab::data::Array>({ factory.createScalar("No input required") }));
19+
matlabEng->feval(u"error", 0,
20+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: No input required") }));
2121
}
2222

2323
// actual function algorithm / computation

src/is_symlink.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,29 @@ class MexFunction : public matlab::mex::Function {
4444
public:
4545
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
4646
// boilerplate engine & ArrayFactory setup
47-
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
47+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
4848

4949
matlab::data::ArrayFactory factory;
50-
// boilerplate input checks
50+
// wrangle inputs
51+
std::string in;
52+
5153
if (inputs.size() != 1) {
52-
matlabPtr->feval(u"error", 0,
54+
matlabEng->feval(u"error", 0,
5355
std::vector<matlab::data::Array>({ factory.createScalar("One input required") }));
5456
}
55-
if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING ||
56-
inputs[0].getNumberOfElements() != 1) {
57-
matlabPtr->feval(u"error", 0,
58-
std::vector<matlab::data::Array>({ factory.createScalar("Input must be a scalar string") }));
57+
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
58+
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
59+
in = stringArr[0];
60+
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
61+
matlab::data::CharArray charArr = inputs[0];
62+
in.assign(charArr.begin(), charArr.end());
63+
} else {
64+
matlabEng->feval(u"error", 0,
65+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
5966
}
6067

61-
// Matlab strings are an array, so we use [0][0] to get the first element
62-
std::string inputStr = inputs[0][0];
63-
6468
// actual function algorithm / computation
65-
bool y = fs_is_symlink(inputStr);
69+
bool y = fs_is_symlink(in);
6670

6771
// convert to Matlab output
6872
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html

src/set_permissions.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,35 @@ class MexFunction : public matlab::mex::Function {
6363
public:
6464
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
6565
// boilerplate engine & ArrayFactory setup
66-
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
66+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
6767

6868
matlab::data::ArrayFactory factory;
69-
// boilerplate input checks
69+
// wrangle inputs
70+
std::string in;
71+
7072
if (inputs.size() != 4) {
71-
matlabPtr->feval(u"error", 0,
73+
matlabEng->feval(u"error", 0,
7274
std::vector<matlab::data::Array>({ factory.createScalar("Four inputs required") }));
7375
}
74-
if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING ||
75-
inputs[0].getNumberOfElements() != 1) {
76-
matlabPtr->feval(u"error", 0,
77-
std::vector<matlab::data::Array>({ factory.createScalar("Input must be a scalar string") }));
76+
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
77+
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
78+
in = stringArr[0];
79+
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
80+
matlab::data::CharArray charArr = inputs[0];
81+
in.assign(charArr.begin(), charArr.end());
82+
} else {
83+
matlabEng->feval(u"error", 0,
84+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
7885
}
7986
if (inputs[1].getType() != matlab::data::ArrayType::DOUBLE ||
8087
inputs[2].getType() != matlab::data::ArrayType::DOUBLE ||
8188
inputs[3].getType() != matlab::data::ArrayType::DOUBLE) {
82-
matlabPtr->feval(u"error", 0,
83-
std::vector<matlab::data::Array>({ factory.createScalar("Permission inputs must be doubles") }));
89+
matlabEng->feval(u"error", 0,
90+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: Permission inputs must be scalar doubles") }));
8491
}
8592

86-
// Matlab strings are an array, so we use [0][0] to get the first element
87-
std::string inputStr = inputs[0][0];
88-
8993
// actual function algorithm / computation
90-
bool y = fs_set_permissions(inputStr, inputs[1][0], inputs[2][0], inputs[3][0]);
94+
bool y = fs_set_permissions(in, inputs[1][0], inputs[2][0], inputs[3][0]);
9195

9296
// convert to Matlab output
9397
// https://www.mathworks.com/help/matlab/matlab_external/create-matlab-array-with-matlab-data-cpp-api.html

src/windows_shortname.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,31 @@ class MexFunction : public matlab::mex::Function {
1919
public:
2020
void operator()(matlab::mex::ArgumentList outputs, matlab::mex::ArgumentList inputs) {
2121
// boilerplate engine & ArrayFactory setup
22-
std::shared_ptr<matlab::engine::MATLABEngine> matlabPtr = getEngine();
22+
std::shared_ptr<matlab::engine::MATLABEngine> matlabEng = getEngine();
2323

2424
matlab::data::ArrayFactory factory;
25-
// boilerplate input checks
25+
26+
// wrangle Inputs
27+
std::string in;
28+
2629
if (inputs.size() != 1) {
27-
matlabPtr->feval(u"error", 0,
30+
matlabEng->feval(u"error", 0,
2831
std::vector<matlab::data::Array>({ factory.createScalar("One input required") }));
2932
}
30-
if (inputs[0].getType() != matlab::data::ArrayType::MATLAB_STRING ||
31-
inputs[0].getNumberOfElements() != 1) {
32-
matlabPtr->feval(u"error", 0,
33-
std::vector<matlab::data::Array>({ factory.createScalar("Input must be a scalar string") }));
33+
if ((inputs[0].getType() == matlab::data::ArrayType::MATLAB_STRING && inputs[0].getNumberOfElements() == 1)){
34+
matlab::data::TypedArray<matlab::data::MATLABString> stringArr = inputs[0];
35+
in = stringArr[0];
36+
} else if (inputs[0].getType() == matlab::data::ArrayType::CHAR){
37+
matlab::data::CharArray charArr = inputs[0];
38+
in.assign(charArr.begin(), charArr.end());
39+
} else {
40+
matlabEng->feval(u"error", 0,
41+
std::vector<matlab::data::Array>({ factory.createScalar("Mex: First input must be a scalar string or char vector") }));
3442
}
3543

36-
// Matlab strings are an array, so we use [0][0] to get the first element
37-
std::string inputStr = inputs[0][0];
38-
3944
// actual function algorithm / computation
4045
#if defined(_WIN32)
41-
std::string s = fs_shortname(inputStr);
46+
std::string s = fs_shortname(in);
4247
#else
4348
std::string s = "";
4449
#endif

test/TestHash.m

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
properties (TestParameter)
88
type = {'sha256', 'md5'}
99
hash = {"36c1bbbdfd8d04ef546ffb15b9c0a65767fd1fe9a6135a257847e3a51fb1426c", "d58cfb32e075781ba59082a8b18287f9"}
10+
Pe = {"file:///", "", "/"}
1011
end
1112

1213
methods(TestClassSetup)
@@ -73,12 +74,8 @@ function test_hash_text(tc)
7374
end
7475

7576

76-
function test_hash_empty(tc)
77-
78-
tc.verifyEmpty(stdlib.file_checksum("file:///", "sha256"))
79-
tc.verifyEmpty(stdlib.file_checksum("", "sha256"))
80-
tc.verifyEmpty(stdlib.file_checksum("/", "sha256"))
81-
77+
function test_hash_empty(tc, Pe)
78+
tc.verifyEmpty(stdlib.file_checksum(Pe, "sha256"))
8279
end
8380

8481
end

test/TestPermissions.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ function test_set_permissions(tc)
2727

2828
import matlab.unittest.constraints.StartsWithSubstring
2929
import matlab.unittest.fixtures.TemporaryFolderFixture
30+
import matlab.unittest.fixtures.CurrentFolderFixture
31+
32+
tc.applyFixture(CurrentFolderFixture(".."))
33+
% matlab exist() doesn't work for MEX detection with ".." leading path
34+
35+
tc.assumeEqual(exist("+stdlib/set_permissions", "file"), 3)
3036

3137
fixture = tc.applyFixture(TemporaryFolderFixture);
3238

test/TestWindowsCOM.m

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
properties (TestParameter)
44
Pn = {"", "not-exist", "file:///"}
5+
Pmr = {matlabroot, stdlib.posix(matlabroot)}
56
end
67

78
methods (Test)
@@ -13,35 +14,41 @@ function test_not(tc, Pn)
1314
function test_short_folder(tc)
1415
import matlab.unittest.constraints.IsFolder
1516

16-
tc.assumeTrue(ispc, "Windows only")
17-
1817
progdir = stdlib.posix(getenv("PROGRAMFILES"));
18+
if ispc
1919
tc.assumeThat(progdir, IsFolder, "$Env:PROGRAMFILES is not a directory")
20+
end
2021

2122
short = stdlib.windows_shortname(progdir);
2223

23-
tc.verifySubstring(short, "PROGRA~1")
24-
25-
tc.verifyEqual(stdlib.canonical(short), progdir)
24+
if ispc
25+
tc.verifySubstring(short, "PROGRA~1")
26+
tc.verifyEqual(stdlib.canonical(short), progdir)
27+
else
28+
tc.verifyEqual(short, "")
29+
end
2630

2731
end
2832

2933

30-
function test_short_file(tc)
34+
function test_short_file(tc, Pmr)
3135
import matlab.unittest.constraints.IsFile
3236

33-
tc.assumeTrue(ispc, "Windows only")
34-
p = matlabroot;
35-
tc.assumeSubstring(p, " ", "name won't shorten if it doesn't have a space")
37+
if ispc
38+
tc.assumeSubstring(Pmr, " ", "name won't shorten if it doesn't have a space")
39+
end
3640

37-
for d = [p, stdlib.posix(p)]
38-
s = stdlib.windows_shortname(d);
41+
s = stdlib.windows_shortname(Pmr);
3942

40-
tc.verifySubstring(s, "~")
43+
if ispc
44+
if contains(Pmr, " ")
45+
tc.verifySubstring(s, "~")
46+
end
47+
tc.verifyEqual(stdlib.canonical(s), stdlib.posix(Pmr), "shortname didn't resolve same as canonical")
48+
else
49+
tc.verifyEqual(s, "")
4150
end
4251

43-
tc.verifyEqual(stdlib.canonical(s), stdlib.posix(p), "shortname didn't resolve same as canonical")
44-
4552
end
4653

4754
end

0 commit comments

Comments
 (0)