Skip to content

Commit 7fa3a83

Browse files
committed
test: device,inode: compact, stricter
1 parent c8b1315 commit 7fa3a83

File tree

8 files changed

+36
-37
lines changed

8 files changed

+36
-37
lines changed

+stdlib/+java/device.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
function i = device(p)
22

33
opt = javaMethod("values", "java.nio.file.LinkOption");
4-
i = javaMethod("getAttribute", "java.nio.file.Files", javaPathObject(p), "unix:dev", opt);
4+
i = uint64(javaMethod("getAttribute", "java.nio.file.Files", javaPathObject(p), "unix:dev", opt));
55

66
end

+stdlib/device.m

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
%% DEVICE filesystem device index of path
22

3-
function i = device(p)
3+
function i = device(file, method)
44
arguments
5-
p {mustBeTextScalar}
5+
file {mustBeTextScalar}
6+
method (1,:) string = ["java", "python", "sys"]
67
end
78

8-
if isunix() && stdlib.has_java()
9-
i = stdlib.java.device(p);
10-
elseif stdlib.has_python()
11-
i = stdlib.python.device(p);
12-
else
13-
i = stdlib.sys.device(p);
14-
end
9+
fun = choose_method(method, "device");
10+
i = fun(file);
1511

1612
end
1713

+stdlib/inode.m

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@
22
%
33
% Windows always returns 0, Unix returns inode number.
44

5-
function i = inode(p)
5+
function i = inode(file, method)
66
arguments
7-
p {mustBeTextScalar}
7+
file {mustBeTextScalar}
8+
method (1,:) string = ["java", "python", "sys"]
89
end
910

10-
if isunix() && stdlib.has_java()
11-
i = stdlib.java.inode(p);
12-
elseif stdlib.has_python()
13-
i = stdlib.python.inode(p);
14-
else
15-
i = stdlib.sys.inode(p);
16-
end
11+
fun = choose_method(method, "inode");
12+
i = fun(file);
1713

1814
end
1915

+stdlib/private/choose_method.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
end
2121
case "java"
2222
has = @stdlib.has_java;
23-
if endsWith(name, "hard_link_count")
23+
if endsWith(name, ["device", "hard_link_count", "inode"])
2424
if ~isunix(), continue, end
2525
end
2626
case "python"

+stdlib/samepath.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
% Unix it is false.
88
% In C/C++ access() or stat() the same behavior is observed Windows vs Unix.
99
%
10+
% Ref: https://devblogs.microsoft.com/oldnewthing/20220128-00/?p=106201
11+
%
1012
%%% Inputs
1113
% * path1, path2: paths to compare
1214

test/TestDisk.m

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
properties (TestParameter)
88
Ps = {".", "", "/", getenv("SystemDrive"), "not-exist"}
99
Po = {mfilename("fullpath") + ".m", pwd(), ".", "", tempname()}
10-
device_fun = {@stdlib.device, @stdlib.sys.device, @stdlib.java.device, @stdlib.python.device}
11-
inode_fun = {@stdlib.inode, @stdlib.sys.inode, @stdlib.java.inode, @stdlib.python.inode}
10+
id_fun = {'sys', 'java', 'python'}
11+
id_name = {"inode", "device"}
1212
disk_ac_fun = {'sys', 'dotnet', 'java', 'python'}
1313
disk_ac_name = {'disk_available', 'disk_capacity'}
1414
hl_fun = {'java', 'python'}
@@ -31,10 +31,11 @@ function test_disk_ac(tc, Ps, disk_ac_fun, disk_ac_name)
3131
tc.assertNotEmpty(which(n))
3232

3333
try
34+
r = h(Ps, disk_ac_fun);
3435
if stdlib.exists(Ps)
35-
tc.verifyGreaterThanOrEqual(h(Ps), 0)
36+
tc.verifyGreaterThanOrEqual(r, 0)
3637
else
37-
tc.verifyEqual(h(Ps), uint64(0))
38+
tc.verifyEqual(r, uint64(0))
3839
end
3940
catch e
4041
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
@@ -47,8 +48,10 @@ function test_hard_link_count(tc, hl_fun)
4748
n = "stdlib." + hl_fun + "." + fname;
4849
h = str2func("stdlib." + fname);
4950
tc.assertNotEmpty(which(n))
51+
P = mfilename("fullpath") + ".m";
5052
try
51-
tc.verifyGreaterThanOrEqual(h(mfilename("fullpath") + ".m"), 1)
53+
r = h(P, hl_fun);
54+
tc.verifyGreaterThanOrEqual(r, 1)
5255
catch e
5356
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
5457
end
@@ -87,19 +90,20 @@ function test_remove_file(tc)
8790
end
8891

8992

90-
function test_device(tc, device_fun)
91-
is_capable(tc, device_fun)
92-
93-
tc.verifyGreaterThan(device_fun(pwd()), uint64(0))
94-
tc.verifyEqual(device_fun("."), device_fun(pwd()))
93+
function test_inode_device(tc, id_fun, id_name)
94+
n = "stdlib." + id_fun + "." + id_name;
95+
h = str2func("stdlib." + id_name);
96+
tc.assertNotEmpty(which(n))
9597

98+
try
99+
ip = h(pwd(), id_fun);
100+
tc.verifyClass(ip, 'uint64')
101+
tc.verifyGreaterThan(ip, 0)
102+
tc.verifyEqual(h(".", id_fun), ip)
103+
catch e
104+
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
96105
end
97106

98-
99-
function test_inode(tc, inode_fun)
100-
is_capable(tc, inode_fun)
101-
102-
tc.verifyEqual(inode_fun("."), inode_fun(pwd()))
103107
end
104108

105109

test/TestExists.m

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ function test_is_rw(tc, Ps, method, fname)
3434
h = str2func("stdlib." + fname);
3535
tc.assertNotEmpty(which(n))
3636
try
37-
tc.verifyEqual(h(Ps{1}, method), Ps{2})
37+
r = h(Ps{1}, method);
38+
tc.verifyEqual(r, Ps{2})
3839
catch e
3940
tc.verifyEqual(e.identifier, 'stdlib:choose_method:NameError')
4041
end

test/is_capable.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ function is_capable(tc, f)
2525
japi = stdlib.java_api();
2626
tc.assumeGreaterThan(japi, 0, "Java not available")
2727

28-
if endsWith(n, ["device", "inode","is_admin"])
28+
if endsWith(n, "is_admin")
2929
tc.assumeTrue(isunix())
3030
end
3131

0 commit comments

Comments
 (0)