Skip to content

Commit 363f758

Browse files
committed
test, backend: more efficient, robust
1 parent aa18621 commit 363f758

File tree

20 files changed

+173
-177
lines changed

20 files changed

+173
-177
lines changed

+stdlib/+dotnet/is_admin.m

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22

33
function y = is_admin()
44

5-
% com.sun.security.auth.module.NTSystem().getGroupIDs();
6-
% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
7-
% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
8-
95
% Use .NET System.Security.Principal to check for admin privileges
106
identity = System.Security.Principal.WindowsIdentity.GetCurrent();
117
principal = System.Security.Principal.WindowsPrincipal(identity);

+stdlib/+dotnet/ram_total.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,11 @@
33
function bytes = ram_total()
44
% .NET is 2-3x faster than Java for this
55
% https://learn.microsoft.com/en-us/dotnet/api/system.gcmemoryinfo.totalavailablememorybytes
6-
bytes = uint64(System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes);
6+
try
7+
bytes = System.GC.GetGCMemoryInfo().TotalAvailableMemoryBytes;
8+
catch
9+
bytes = [];
10+
end
11+
12+
bytes = uint64(bytes);
713
end

+stdlib/+java/is_admin.m

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
function y = is_admin()
1+
function ok = is_admin()
22

3-
y = com.sun.security.auth.module.UnixSystem().getUid() == 0;
3+
% com.sun.security.auth.module.NTSystem().getGroupIDs();
4+
% Administrator group SID (S-1-5-32-544) is not an appropriate check .getGroupIDs because it
5+
% only tells if a user is *allowed* to run as admin, not if they are currently running as admin.
6+
7+
ok = logical.empty;
8+
9+
try %#ok<TRYNC>
10+
ok = com.sun.security.auth.module.UnixSystem().getUid() == 0;
11+
end
412

513
end

+stdlib/+python/cpu_load.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
function L = cpu_load()
22

3-
Lavg = py.os.getloadavg();
4-
L = double(Lavg(1));
3+
L = [];
4+
5+
try %#ok<TRYNC>
6+
L = py.os.getloadavg();
7+
L = double(L(1));
8+
end
9+
10+
511

612
end

+stdlib/+python/ram_free.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
vm = py.psutil.virtual_memory();
55
n = vm.available;
66
catch
7-
n = 0;
7+
n = [];
88
end
99

1010
n = uint64(n);

+stdlib/+python/ram_total.m

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
try
44
vm = py.psutil.virtual_memory();
55
n = vm.total;
6-
catch e
7-
warning(e.identifier, 'stdlib.python.ram_total() failed: %s', e.message)
8-
n = 0;
6+
catch
7+
n = [];
98
end
109

1110
n = uint64(n);

+stdlib/private/hbackend.m

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,25 @@
1010
minVersion (1,1) string = ""
1111
end
1212

13+
if isscalar(backend)
14+
% bypass checks
15+
m = backend;
16+
else
17+
m = switchyard(backend, name, minVersion);
18+
end
19+
20+
n = "stdlib." + m + "." + name;
21+
fun = str2func(n);
22+
23+
end
24+
25+
26+
function m = switchyard(backend, name, minVersion)
27+
28+
1329
for m = backend
1430
switch m
15-
case "dotnet"
31+
case 'dotnet'
1632
has = @stdlib.has_dotnet;
1733

1834
if endsWith(name, ["create_symlink", "ram_total", "read_symlink"])
@@ -30,7 +46,7 @@
3046
if ~isunix(), continue, end
3147
end
3248

33-
case "python"
49+
case 'python'
3450

3551
has = @stdlib.has_python;
3652

@@ -46,17 +62,17 @@
4662
if ~isunix() || stdlib.matlabOlderThan('R2024a'), continue, end
4763
end
4864

49-
case "legacy", has = true;
65+
case 'legacy', has = true;
5066

51-
case "native"
67+
case 'native'
5268

5369
has = stdlib.strempty(minVersion) || ~stdlib.matlabOlderThan(minVersion);
5470

5571
if endsWith(name, "create_symlink")
5672
if ~has || ispc(), continue, end
5773
end
5874

59-
case "sys"
75+
case 'sys'
6076

6177
has = true;
6278

@@ -69,8 +85,6 @@
6985
end
7086

7187
if has()
72-
n = "stdlib." + m + "." + name;
73-
fun = str2func(n);
7488
return
7589
end
7690
end

test/TestCanonical.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
{"not-exist/a/..", "not-exist"}, ...
1111
{"./not-exist", "not-exist"}
1212
};
13-
backend = {"native", "legacy"}
13+
backend = {'native', 'legacy'}
1414
end
1515

1616
methods(TestClassSetup)

test/TestDisk.m

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
properties (TestParameter)
99
Ps = {".", "", "/", getenv("SystemDrive"), "not-exist"}
1010
Po = {mfilename("fullpath") + ".m", pwd(), ".", "", "not-exist"}
11-
java_python_sys = {'sys', 'java', 'python'}
11+
backend_jps = {'sys', 'java', 'python'}
1212
id_name = {"inode", "device"}
13-
all_fun = {'sys', 'dotnet', 'java', 'python'}
13+
backend_djps = {'sys', 'dotnet', 'java', 'python'}
1414
disk_ac_name = {'disk_available', 'disk_capacity'}
15-
is_remove = {'dotnet', 'sys'}
16-
py_sys = {'python', 'sys'}
15+
backend_ds = {'dotnet', 'sys'}
16+
backend_ps = {'python', 'sys'}
1717
end
1818

1919
methods(TestClassSetup)
@@ -26,13 +26,11 @@ function test_dirs(tc)
2626

2727
methods (Test)
2828

29-
function test_disk_ac(tc, Ps, all_fun, disk_ac_name)
30-
n = "stdlib." + all_fun + "." + disk_ac_name;
29+
function test_disk_ac(tc, Ps, backend_djps, disk_ac_name)
3130
h = str2func("stdlib." + disk_ac_name);
32-
tc.assertNotEmpty(which(n))
3331

3432
try
35-
r = h(Ps, all_fun);
33+
r = h(Ps, backend_djps);
3634
if stdlib.exists(Ps)
3735
tc.verifyGreaterThanOrEqual(r, 0)
3836
else
@@ -44,9 +42,9 @@ function test_disk_ac(tc, Ps, all_fun, disk_ac_name)
4442
end
4543

4644

47-
function test_is_removable(tc, is_remove)
45+
function test_is_removable(tc, backend_ds)
4846
try
49-
y = stdlib.is_removable(pwd(), is_remove);
47+
y = stdlib.is_removable(pwd(), backend_ds);
5048
catch e
5149
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
5250
return
@@ -56,46 +54,42 @@ function test_is_removable(tc, is_remove)
5654
end
5755

5856

59-
function test_is_mount(tc, py_sys)
57+
function test_is_mount(tc, backend_ps)
6058
try
61-
y = stdlib.is_mount(pwd(), py_sys);
59+
y = stdlib.is_mount(pwd(), backend_ps);
6260
catch e
6361
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
6462
return
6563
end
6664

6765
tc.verifyClass(y, 'logical')
68-
tc.verifyTrue(stdlib.is_mount("/", py_sys))
69-
tc.verifyFalse(stdlib.is_mount(tempname(), py_sys))
66+
tc.verifyTrue(stdlib.is_mount("/", backend_ps))
67+
tc.verifyFalse(stdlib.is_mount(tempname(), backend_ps))
7068

7169
if ispc()
7270
sd = getenv("SystemDrive");
7371
tc.assertTrue(sd == stdlib.root_name(sd), sd)
74-
tc.verifyFalse(stdlib.is_mount(sd, py_sys), sd)
75-
tc.verifyTrue(stdlib.is_mount(sd + "/", py_sys), sd)
76-
tc.verifyTrue(stdlib.is_mount(sd + "\", py_sys), sd)
72+
tc.verifyFalse(stdlib.is_mount(sd, backend_ps), sd)
73+
tc.verifyTrue(stdlib.is_mount(sd + "/", backend_ps), sd)
74+
tc.verifyTrue(stdlib.is_mount(sd + "\", backend_ps), sd)
7775
end
7876
end
7977

8078

81-
function test_hard_link_count(tc, java_python_sys)
82-
fname = "hard_link_count";
83-
n = "stdlib." + java_python_sys + "." + fname;
84-
h = str2func("stdlib." + fname);
85-
tc.assertNotEmpty(which(n))
79+
function test_hard_link_count(tc, backend_jps)
8680
P = mfilename("fullpath") + ".m";
8781
try
88-
r = h(P, java_python_sys);
82+
r = stdlib.hard_link_count(P, backend_jps);
8983
tc.verifyGreaterThanOrEqual(r, 1)
9084
catch e
9185
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
9286
end
9387
end
9488

9589

96-
function test_filesystem_type(tc, Ps, all_fun)
90+
function test_filesystem_type(tc, Ps, backend_djps)
9791
try
98-
t = stdlib.filesystem_type(Ps, all_fun);
92+
t = stdlib.filesystem_type(Ps, backend_djps);
9993
catch e
10094
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
10195
return
@@ -125,27 +119,27 @@ function test_remove_file(tc)
125119
end
126120

127121

128-
function test_inode_device(tc, java_python_sys, id_name)
122+
function test_inode_device(tc, backend_jps, id_name)
129123

130124
h = str2func("stdlib." + id_name);
131125

132126
try
133-
ip = h(pwd(), java_python_sys);
127+
ip = h(pwd(), backend_jps);
134128
tc.verifyClass(ip, 'uint64')
135129
tc.verifyGreaterThan(ip, 0)
136130

137-
tc.verifyEqual(h(".", java_python_sys), ip)
131+
tc.verifyEqual(h(".", backend_jps), ip)
138132
catch e
139133
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
140134
end
141135

142136
end
143137

144138

145-
function test_owner(tc, Po, all_fun)
139+
function test_owner(tc, Po, backend_djps)
146140

147141
try
148-
o = stdlib.get_owner(Po, all_fun);
142+
o = stdlib.get_owner(Po, backend_djps);
149143
catch e
150144
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
151145
return
@@ -160,9 +154,9 @@ function test_owner(tc, Po, all_fun)
160154
end
161155

162156

163-
function test_owner_array(tc, all_fun)
157+
function test_owner_array(tc, backend_djps)
164158
try
165-
o = stdlib.get_owner([".", pwd(), "not-exist", ""], all_fun);
159+
o = stdlib.get_owner([".", pwd(), "not-exist", ""], backend_djps);
166160
L = strlength(o);
167161
tc.verifyEqual(L(3:4), [0, 0])
168162
tc.verifyGreaterThan(strlength(o(1:2)), 0);

test/TestExists.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
% on CI matlabroot can be writable!
1414
fname = {'is_readable', 'is_writable'}
1515
backend = {'java', 'native', 'legacy'}
16-
icm = {'python', 'sys'}
16+
backend_ps = {'python', 'sys'}
1717
end
1818

1919
methods(TestClassSetup)
@@ -57,12 +57,12 @@ function test_is_rw_array(tc, backend, fname)
5757
end
5858

5959

60-
function test_is_char_device(tc, icm)
60+
function test_is_char_device(tc, backend_ps)
6161
% /dev/stdin may not be available on CI systems
6262
n = stdlib.null_file();
6363

6464
try
65-
tc.verifyTrue(stdlib.is_char_device(n, icm), n)
65+
tc.verifyTrue(stdlib.is_char_device(n, backend_ps), n)
6666
catch e
6767
tc.verifyEqual(e.identifier, 'stdlib:hbackend:NameError', e.message)
6868
end

0 commit comments

Comments
 (0)