Skip to content

Commit abef96f

Browse files
committed
file_checksum: work on empty files and test
1 parent cce54b1 commit abef96f

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

+stdlib/+dotnet/file_checksum.m

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,4 @@
2828

2929
fclose(fid);
3030

31-
3231
end

+stdlib/+java/file_checksum.m

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414

1515
try
1616

17-
inst = java.security.MessageDigest.getInstance(hash_method);
17+
inst = javaMethod('getInstance', 'java.security.MessageDigest', hash_method);
1818
while ~feof(fid)
1919
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#update(byte)
20-
inst.update(fread(fid, file_chunk, '*uint8'));
20+
[bytes, count] = fread(fid, file_chunk, '*uint8');
21+
if count
22+
inst.update(bytes);
23+
end
2124
end
2225
% https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/security/MessageDigest.html#digest()
2326
hash = sprintf('%.2x', typecast(inst.digest, 'uint8'));

test/TestHash.m

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
TestHash < matlab.unittest.TestCase
44

55
properties
6-
file
6+
file = 'hello.txt'
7+
empty = 'empty.txt'
78
end
89

910
properties (TestParameter)
11+
Pe = {{'md5', 'd41d8cd98f00b204e9800998ecf8427e'}, ...
12+
{'sha-256', 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855'}}
13+
1014
Ph = {{'md5', '5d41402abc4b2a76b9719d911017c592'}, ...
1115
{'sha-256', '2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'}}
1216
backend = {'java', 'dotnet', 'sys'}
@@ -17,19 +21,18 @@
1721
function create_file(tc)
1822
tc.applyFixture(matlab.unittest.fixtures.WorkingFolderFixture());
1923

20-
tc.file = fullfile(pwd(), class(tc));
21-
2224
fid = fopen(tc.file, "w");
23-
2425
tc.assumeGreaterThan(fid, 0);
2526
fprintf(fid, "hello");
2627
fclose(fid);
28+
tc.assumeTrue(isfile(tc.file))
29+
tc.assertEqual(stdlib.file_size(tc.file), 5)
2730

28-
if stdlib.matlabOlderThan('R2018a')
29-
tc.assumeTrue(isfile(tc.file))
30-
else
31-
tc.assertThat(tc.file, matlab.unittest.constraints.IsFile)
32-
end
31+
fid = fopen(tc.empty, "w");
32+
tc.assumeGreaterThan(fid, 0);
33+
fclose(fid);
34+
tc.assumeTrue(isfile(tc.empty))
35+
tc.assertEqual(stdlib.file_size(tc.empty), 0)
3336
end
3437
end
3538

@@ -50,6 +53,19 @@ function test_hash_text(tc, Ph, backend)
5053
end
5154

5255

56+
function test_hash_empty(tc, Pe, backend)
57+
58+
r = stdlib.file_checksum(tc.empty, Pe{1}, backend);
59+
tc.verifyClass(r, 'char')
60+
61+
if ismember(backend, stdlib.Backend().select('file_checksum'))
62+
tc.verifyEqual(r, Pe{2})
63+
else
64+
tc.assertEmpty(r)
65+
end
66+
end
67+
68+
5369
function test_has_convenience(tc, Ph)
5470
switch Ph{1}
5571
case 'md5', tc.verifyEqual(stdlib.md5sum(tc.file), Ph{2})

0 commit comments

Comments
 (0)