Skip to content

Commit 7e3e7e6

Browse files
committed
add more check and error ids
1 parent a9df517 commit 7e3e7e6

File tree

10 files changed

+163
-77
lines changed

10 files changed

+163
-77
lines changed

+hdf5nc/TestHDF5.m

Lines changed: 66 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,17 @@
55
end
66

77
properties (TestParameter)
8-
type = {'single', 'double', 'int32', 'int64'};
8+
type = {'single', 'double', 'int32', 'int64'}
9+
vars = {'A0', 'A1', 'A2', 'A3', 'A4'}
10+
str = {"string", 'char'}
911
end
1012

1113

12-
methods (TestClassSetup)
14+
methods (TestMethodSetup)
1315

1416
function setup_file(tc)
1517
import hdf5nc.h5save
18+
import matlab.unittest.constraints.IsFile
1619

1720
A0 = 42.;
1821
A1 = [42.; 43.];
@@ -36,11 +39,17 @@ function setup_file(tc)
3639
h5save(basic, '/A2', A2)
3740
h5save(basic, '/A3', A3, "size", size(A3))
3841
h5save(basic, '/A4', A4)
42+
43+
h5save(basic, '/t/x', 12)
44+
h5save(basic, '/t/y', 13)
45+
h5save(basic, '/j/a/b', 6)
46+
47+
tc.assumeThat(basic, IsFile)
3948
end
4049
end
4150

4251

43-
methods (TestClassTeardown)
52+
methods (TestMethodTeardown)
4453
function cleanup(tc)
4554
delete(tc.TestData.basic)
4655
end
@@ -58,25 +67,49 @@ function test_auto_chunk_size(tc)
5867

5968
function test_get_variables(tc)
6069
import hdf5nc.h5variables
70+
basic = tc.TestData.basic;
71+
72+
v = h5variables(basic);
73+
tc.verifyEqual(sort(v), ["A0", "A1", "A2", "A3", "A4"])
74+
75+
[v1,g] = h5variables(basic);
76+
tc.verifyEqual(v,v1)
77+
tc.verifyEqual(sort(g), ["/j", "/t"])
78+
79+
% 1-level group
80+
[v, g] = h5variables(basic, "/t");
81+
tc.verifyEqual(sort(v), ["x", "y"])
82+
tc.verifyEmpty(g)
83+
84+
% traversal
85+
[v, g] = h5variables(basic, "/j");
86+
tc.verifyEmpty(v)
87+
tc.verifyEqual(g, "/j/a")
88+
89+
[v, g] = h5variables(basic, "/j/a");
90+
tc.verifyEqual(v, "b")
91+
tc.verifyEmpty(g)
6192

62-
vars = h5variables(tc.TestData.basic);
63-
tc.verifyEqual(sort(vars), ["A0", "A1", "A2", "A3", "A4"])
6493
end
6594

6695

67-
function test_exists(tc)
96+
function test_exists(tc, vars)
6897
import hdf5nc.h5exists
6998
import matlab.unittest.constraints.IsScalar
99+
basic = tc.TestData.basic;
70100

71-
e0 = h5exists(tc.TestData.basic, '/A3');
72-
tc.verifyThat(e0, IsScalar)
73-
tc.verifyTrue(e0)
101+
e = h5exists(basic, "/" + vars);
102+
tc.verifyThat(e, IsScalar)
103+
tc.verifyTrue(e)
74104

75-
tc.verifyFalse(h5exists(tc.TestData.basic, '/oops'))
105+
% vector
106+
e = h5exists(basic, [vars, "oops", ""]);
107+
tc.verifyTrue(isrow(e))
108+
tc.verifyEqual(e, [true, false, false])
76109

77-
e1 = h5exists(tc.TestData.basic, ["A3", "oops"]);
78-
tc.verifyTrue(isrow(e1))
79-
tc.verifyEqual(e1, [true, false])
110+
% empty
111+
e = h5exists(basic, string.empty);
112+
tc.verifyEmpty(e)
80113
end
81114

82115

@@ -104,14 +137,15 @@ function test_size(tc)
104137
s = h5size(basic, '/A4');
105138
tc.verifyTrue(isvector(s))
106139
tc.verifyEqual(s, [4,3,2,5])
140+
141+
% empty
142+
tc.verifyError(@() h5size(basic, string.empty), 'MATLAB:validation:IncompatibleSize')
107143
end
108144

109145

110146
function test_read(tc)
111147
import matlab.unittest.constraints.IsScalar
112-
import matlab.unittest.constraints.IsFile
113148
basic = tc.TestData.basic;
114-
tc.assumeThat(basic, IsFile)
115149

116150
s = h5read(basic, '/A0');
117151
tc.verifyThat(s, IsScalar)
@@ -161,22 +195,31 @@ function test_rewrite(tc)
161195
tc.verifyEqual(h5read(basic, '/A2'), 3*magic(4))
162196
end
163197

164-
function test_string(tc)
198+
function test_string(tc, str)
165199
import hdf5nc.h5save
166200
basic = tc.TestData.basic;
167201

168-
h5save(basic, "/a_string", "hello")
169-
h5save(basic, "/a_char", 'there')
202+
h5save(basic, "/"+str, str)
170203

171-
astr = h5read(basic, "/a_string");
172-
achar = h5read(basic, "/a_char");
173-
tc.verifyEqual(astr, "hello")
174-
tc.verifyEqual(achar, "there")
204+
a = h5read(basic, "/"+str);
205+
tc.verifyEqual(a, string(str))
206+
end
207+
208+
function test_file_missing(tc)
209+
210+
tc.verifyError(@() hdf5nc.h5exists(tempname,""), 'hdf5nc:h5variables:fileNotFound')
211+
tc.verifyError(@() hdf5nc.h5variables(tempname), 'hdf5nc:h5variables:fileNotFound')
212+
tc.verifyError(@() hdf5nc.h5size(tempname,""), 'hdf5nc:h5size:fileNotFound')
213+
[~,badname] = fileparts(tempname);
214+
tc.verifyError(@() hdf5nc.h5save(badname,"",0), 'hdf5nc:h5save:fileNotFound')
175215
end
176216

177217
function test_real_only(tc)
178218
import hdf5nc.h5save
179-
tc.verifyError(@() h5save(tc.TestData.basic, "/bad_imag", 1j), 'MATLAB:validators:mustBeReal')
219+
basic = tc.TestData.basic;
220+
221+
tc.verifyError(@() h5save(basic, "/bad_imag", 1j), 'MATLAB:validators:mustBeReal')
222+
tc.verifyError(@() h5save(basic, "", 0), 'MATLAB:expectedNonempty')
180223
end
181224

182225
end

+hdf5nc/TestNetCDF.m

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
end
66

77
properties (TestParameter)
8-
type = {'single', 'double', 'int32', 'int64'};
8+
type = {'single', 'double', 'int32', 'int64'}
9+
vars = {'A0', 'A1', 'A2', 'A3', 'A4'}
910
end
1011

1112

12-
methods (TestClassSetup)
13+
methods (TestMethodSetup)
1314

1415
function setup_file(tc)
1516
import hdf5nc.ncsave
17+
import matlab.unittest.constraints.IsFile
1618

1719
A0 = 42.;
1820
A1 = [42.; 43.];
@@ -36,11 +38,13 @@ function setup_file(tc)
3638
ncsave(basic, 'A2', A2, "dims", {'x2', size(A2,1), 'y2', size(A2,2)})
3739
ncsave(basic, 'A3', A3, "dims", {'x3', size(A3,1), 'y3', size(A3,2), 'z3', size(A3,3)})
3840
ncsave(basic, 'A4', A4, "dims", {'x4', size(A4,1), 'y4', size(A4,2), 'z4', size(A4,3), 'w4', size(A4,4)})
41+
42+
tc.assumeThat(basic, IsFile)
3943
end
4044
end
4145

4246

43-
methods (TestClassTeardown)
47+
methods (TestMethodTeardown)
4448
function cleanup(tc)
4549
delete(tc.TestData.basic)
4650
end
@@ -51,24 +55,27 @@ function cleanup(tc)
5155
function test_get_variables(tc)
5256
import hdf5nc.ncvariables
5357

54-
vars = ncvariables(tc.TestData.basic);
55-
tc.verifyEqual(sort(vars), ["A0", "A1", "A2", "A3", "A4"])
58+
tc.verifyEqual(sort(ncvariables(tc.TestData.basic)), ["A0", "A1", "A2", "A3", "A4"])
5659
end
5760

5861

59-
function test_exists(tc)
62+
function test_exists(tc, vars)
6063
import hdf5nc.ncexists
6164
import matlab.unittest.constraints.IsScalar
65+
basic = tc.TestData.basic;
6266

63-
e0 = ncexists(tc.TestData.basic, 'A3');
64-
tc.verifyThat(e0, IsScalar)
65-
tc.verifyTrue(e0)
67+
e = ncexists(basic, vars);
68+
tc.verifyThat(e, IsScalar)
69+
tc.verifyTrue(e)
6670

67-
tc.verifyFalse(ncexists(tc.TestData.basic, '/oops'))
71+
% vector
72+
e = ncexists(basic, [vars, "oops", ""]);
73+
tc.verifyTrue(isrow(e))
74+
tc.verifyEqual(e, [true, false, false])
6875

69-
e1 = ncexists(tc.TestData.basic, ["A3", "oops"]);
70-
tc.verifyTrue(isrow(e1))
71-
tc.verifyEqual(e1, [true, false])
76+
% empty
77+
e = ncexists(basic, string.empty);
78+
tc.verifyEmpty(e)
7279
end
7380

7481

@@ -96,14 +103,15 @@ function test_size(tc)
96103
s = ncsize(basic, 'A4');
97104
tc.verifyTrue(isvector(s))
98105
tc.verifyEqual(s, [4,3,2,5])
106+
107+
% empty
108+
tc.verifyError(@() ncsize(basic, string.empty), 'MATLAB:validation:IncompatibleSize')
99109
end
100110

101111

102112
function test_read(tc)
103113
import matlab.unittest.constraints.IsScalar
104-
import matlab.unittest.constraints.IsFile
105114
basic = tc.TestData.basic;
106-
tc.assumeThat(basic, IsFile)
107115

108116
s = ncread(basic, '/A0');
109117
tc.verifyThat(s, IsScalar)
@@ -159,9 +167,21 @@ function test_no_char_string(tc)
159167

160168
end
161169

170+
function test_file_missing(tc)
171+
172+
tc.verifyError(@() hdf5nc.ncexists(tempname,""), 'hdf5nc:ncvariables:fileNotFound')
173+
tc.verifyError(@() hdf5nc.ncvariables(tempname), 'hdf5nc:ncvariables:fileNotFound')
174+
tc.verifyError(@() hdf5nc.ncsize(tempname,""), 'hdf5nc:ncsize:fileNotFound')
175+
[~,badname] = fileparts(tempname);
176+
tc.verifyError(@() hdf5nc.ncsave(badname,"",0), 'hdf5nc:ncsave:fileNotFound')
177+
end
178+
162179
function test_real_only(tc)
163180
import hdf5nc.ncsave
164-
tc.verifyError(@() ncsave(tc.TestData.basic, "/bad_imag", 1j), 'MATLAB:validators:mustBeReal')
181+
basic = tc.TestData.basic;
182+
183+
tc.verifyError(@() ncsave(basic, "bad_imag", 1j), 'MATLAB:validators:mustBeReal')
184+
tc.verifyError(@() ncsave(basic, "", 0), 'MATLAB:imagesci:netcdf:badLocationString')
165185
end
166186

167187
end

+hdf5nc/h5exists.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
function exists = h5exists(filename, varnames)
1+
function exists = h5exists(file, varnames)
22
% check if variable(s) exists in HDF5 file
33
%
44
% parameters
55
% ----------
6-
% filename: HDF5 filename
6+
% file: HDF5 filename
77
% varname: name of variable inside HDF5 file
88
%
99
% returns
1010
% -------
1111
% exists: boolean (scalar or vector)
1212

1313
arguments
14-
filename (1,1) string
14+
file (1,1) string
1515
varnames (1,:) string
1616
end
1717

1818
i = startsWith(varnames, "/");
1919
varnames(i) = extractAfter(varnames(i), 1);
2020
% NOT contains because we want exact string match
21-
exists = ismember(varnames, hdf5nc.h5variables(filename));
21+
exists = ismember(varnames, hdf5nc.h5variables(file));
2222

2323
end % function

+hdf5nc/h5save.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ function exist_file(filename, varname, A, sizeA)
5959
elseif all(diskshape == fliplr(sizeA))
6060
h5write(filename, varname, A.', start, fliplr(sizeA))
6161
else
62-
error('h5save:value_error', ['shape of ',varname,': ', int2str(sizeA), ' does not match existing HDF5 shape ', int2str(diskshape)])
62+
error('hdf5nc:h5save:value_error', ['shape of ',varname,': ', int2str(sizeA), ' does not match existing HDF5 shape ', int2str(diskshape)])
6363
end
6464

6565
end % function
@@ -68,7 +68,9 @@ function exist_file(filename, varname, A, sizeA)
6868
function new_file(filename, varname, A, sizeA)
6969

7070
folder = fileparts(filename);
71-
assert(isfolder(folder), '%s is not a folder, cannot create %s', folder, filename)
71+
if ~isfolder(folder)
72+
error('hdf5nc:h5save:fileNotFound', '%s is not a folder, cannot create %s', folder, filename)
73+
end
7274

7375
if isvector(A)
7476
h5create(filename, varname, sizeA, 'DataType', class(A))

+hdf5nc/h5size.m

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
function fsize = h5size(filename, variable)
1+
function fsize = h5size(file, variable)
22
% get size (shape) of an HDF5 dataset
33
%
44
% filename: HDF5 filename
55
% variable: name of variable inside HDF5 file
66
%
77
% fsize: vector of variable size per dimension
88
arguments
9-
filename (1,1) string
9+
file (1,1) string
1010
variable (1,1) string
1111
end
1212

13-
fsize = h5info(expanduser(filename), variable).Dataspace.Size;
13+
file = expanduser(file);
14+
if ~isfile(file)
15+
error('hdf5nc:h5size:fileNotFound', "%s does not exist", file)
16+
end
17+
18+
fsize = h5info(file, variable).Dataspace.Size;
1419

15-
end % function
20+
end

+hdf5nc/h5variables.m

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,28 @@
1717
group string = string.empty
1818
end
1919

20+
file = expanduser(file);
21+
if ~isfile(file)
22+
error('hdf5nc:h5variables:fileNotFound', "%s does not exist", file)
23+
end
24+
2025
names = string.empty;
2126
groups = string.empty;
2227

23-
finf = h5info(expanduser(file));
24-
ds = finf.Datasets;
25-
if isempty(ds)
26-
return
28+
if isempty(group)
29+
finf = h5info(file);
30+
else
31+
finf = h5info(file, group);
2732
end
2833

29-
if ~isempty(group)
30-
gs = finf.Groups;
31-
i = string({gs(:).Name}) == group;
32-
if ~any(i)
33-
return
34-
end
35-
ds = gs(i).Datasets;
36-
end
34+
ds = finf.Datasets;
35+
gs = finf.Groups;
3736

38-
names = string({ds(:).Name});
37+
if ~isempty(ds)
38+
names = string({ds.Name});
39+
end
3940

40-
if nargout > 1
41-
gs = finf.Groups;
41+
if nargout > 1 && ~isempty(gs)
4242
groups = string({gs.Name});
4343
end
4444

0 commit comments

Comments
 (0)