Skip to content

Commit cf9205b

Browse files
committed
samepath: array
1 parent 8c5a545 commit cf9205b

File tree

3 files changed

+35
-19
lines changed

3 files changed

+35
-19
lines changed

+stdlib/+native/samepath.m

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@
44

55
function y = samepath(path1, path2)
66

7-
if stdlib.strempty(path1) || stdlib.strempty(path2)
8-
y = false;
9-
else
10-
c1 = stdlib.canonical(path1, true);
11-
c2 = stdlib.canonical(path2, true);
12-
y = ~stdlib.strempty(c1) && strcmp(c1, c2);
13-
end
7+
i = stdlib.strempty(path1) | stdlib.strempty(path2);
8+
y(i) = false;
9+
10+
c1 = stdlib.canonical(path1(~i), true);
11+
c2 = stdlib.canonical(path2(~i), true);
12+
y(~i) = ~stdlib.strempty(c1) & strcmp(c1, c2);
1413

1514
end

+stdlib/samepath.m

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,19 @@
1414

1515
function y = samepath(path1, path2, backend)
1616
arguments
17-
path1 {mustBeTextScalar}
18-
path2 {mustBeTextScalar}
17+
path1 string
18+
path2 string
1919
backend (1,:) string = ["python", "java", "sys", "native"]
2020
end
2121

2222
% For this function, Python is over 10x faster than Java
2323

24-
fun = hbackend(backend, "samepath");
24+
[fun, b] = hbackend(backend, "samepath");
2525

26-
y = fun(path1, path2);
26+
if (isscalar(path1) && isscalar(path2)) || b == "native"
27+
y = fun(path1, path2);
28+
else
29+
y = arrayfun(fun, path1, path2);
30+
end
2731

2832
end

test/TestSame.m

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
{"..", pwd() + "/.."}, ...
88
{pwd(), pwd() + "/."}}
99

10-
fun = {'sys', 'java', 'python', 'native'}
10+
backend = {'sys', 'java', 'python', 'native'}
1111
end
1212

1313
methods(TestClassSetup)
@@ -19,22 +19,35 @@ function pkg_path(tc)
1919

2020
methods(Test)
2121

22-
function test_samepath(tc, p_same, fun)
23-
tc.assertNotEmpty(which("stdlib." + fun + ".samepath"))
22+
function test_samepath(tc, p_same, backend)
23+
tc.assertNotEmpty(which("stdlib." + backend + ".samepath"))
2424
try
25-
y = stdlib.samepath(p_same{:}, fun);
26-
tc.verifyTrue(y)
25+
r = stdlib.samepath(p_same{:}, backend);
26+
tc.verifyClass(r, 'logical')
27+
tc.verifyTrue(r)
2728
catch e
2829
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
2930
end
3031
end
3132

3233

33-
function test_samepath_notexist(tc, fun)
34+
function test_samepath_notexist(tc, backend)
3435
t = tempname();
3536
try
36-
tc.verifyFalse(stdlib.samepath("", "", fun))
37-
tc.verifyFalse(stdlib.samepath(t, t, fun))
37+
tc.verifyFalse(stdlib.samepath("", "", backend))
38+
tc.verifyFalse(stdlib.samepath(t, t, backend))
39+
catch e
40+
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
41+
end
42+
end
43+
44+
45+
function test_samepath_array(tc, backend)
46+
in = [string(mfilename), string(mfilename('fullpath'))] + ".m";
47+
try
48+
r = stdlib.samepath(in, fliplr(in), backend);
49+
tc.verifyClass(r, 'logical')
50+
tc.verifyEqual(r, [true, true])
3851
catch e
3952
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
4053
end

0 commit comments

Comments
 (0)