Skip to content

Commit 7fc8d54

Browse files
committed
ci:hdf5 function based tests
1 parent bc7e5fd commit 7fc8d54

File tree

6 files changed

+135
-83
lines changed

6 files changed

+135
-83
lines changed

+hdf5nc/+tests/test_hdf5.m

Lines changed: 0 additions & 75 deletions
This file was deleted.

+hdf5nc/test_hdf5.m

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
function tests = test_hdf5
2+
tests = functiontests(localfunctions);
3+
end
4+
5+
function setupOnce(tc)
6+
import hdf5nc.*
7+
8+
A0 = 42.;
9+
A1 = [42.; 43.];
10+
A2 = magic(4);
11+
A3 = A2(:,1:3,1);
12+
A3(:,:,2) = 2*A3;
13+
A4(:,:,:,5) = A3;
14+
15+
tc.TestData.A0 = A0;
16+
tc.TestData.A1 = A1;
17+
tc.TestData.A2 = A2;
18+
tc.TestData.A3 = A3;
19+
tc.TestData.A4 = A4;
20+
21+
basic = tempname + ".h5";
22+
tc.TestData.basic = basic;
23+
% create test data first, so that parallel tests works
24+
h5save(basic, '/A0', A0)
25+
h5save(basic, '/A1', A1)
26+
h5save(basic, '/A2', A2)
27+
h5save(basic, '/A3', A3, "size", size(A3))
28+
h5save(basic, '/A4', A4)
29+
end
30+
31+
32+
function test_auto_chunk_size(tc)
33+
34+
assertEqual(tc, hdf5nc.auto_chunk_size([1500,2500,1000,500,100]), [12,20,8,8,2], '5D chunk fail')
35+
assertEqual(tc, hdf5nc.auto_chunk_size([15,250,100]), [2,32,25], '3D chunk fail')
36+
assertEqual(tc, hdf5nc.auto_chunk_size([15,250]), [15,250], '2D small chunk fail')
37+
38+
end
39+
40+
41+
function test_get_variables(tc)
42+
vars = hdf5nc.h5variables(tc.TestData.basic);
43+
assertEqual(tc, sort(vars), ["A0", "A1", "A2", "A3", "A4"], 'missing variables')
44+
end
45+
46+
47+
function test_exists(tc)
48+
e0 = hdf5nc.h5exists(tc.TestData.basic, '/A3');
49+
assert(isscalar(e0))
50+
assert(e0, 'A3 exists')
51+
52+
assert(~hdf5nc.h5exists(tc.TestData.basic, '/oops'), 'oops not exist')
53+
54+
e1 = hdf5nc.h5exists(tc.TestData.basic, ["A3", "oops"]);
55+
assert(isrow(e1))
56+
assert(all(e1 == [true, false]), 'h5exists array')
57+
end
58+
59+
60+
function test_size(tc)
61+
import hdf5nc.h5size
62+
basic = tc.TestData.basic;
63+
64+
s = h5size(basic, '/A0');
65+
assert(isscalar(s))
66+
assertEqual(tc, s, 1, 'A0 shape')
67+
68+
s = h5size(basic, '/A1');
69+
assert(isscalar(s))
70+
assertEqual(tc, s, 2, 'A1 shape')
71+
72+
s = h5size(basic, '/A2');
73+
assert(isvector(s))
74+
assertEqual(tc, s, [4,4], 'A2 shape')
75+
76+
s = h5size(basic, '/A3');
77+
assert(isvector(s))
78+
assertEqual(tc, s, [4,3,2], 'A3 shape')
79+
80+
s = h5size(basic, '/A4');
81+
assert(isvector(s))
82+
assertEqual(tc, s, [4,3,2,5], 'A4 shape')
83+
end
84+
85+
86+
function test_read(tc)
87+
s = h5read(tc.TestData.basic, '/A0');
88+
assert(isscalar(s))
89+
assertEqual(tc, s, 42, 'A0 read')
90+
91+
s = h5read(tc.TestData.basic, '/A1');
92+
assert(isvector(s))
93+
assertEqual(tc, s, tc.TestData.A1, 'A1 read')
94+
95+
s = h5read(tc.TestData.basic, '/A2');
96+
assert(ismatrix(s))
97+
assertEqual(tc, s, tc.TestData.A2, 'A2 read')
98+
99+
s = h5read(tc.TestData.basic, '/A3');
100+
assert(ndims(s)==3)
101+
assertEqual(tc, s, tc.TestData.A3, 'A3 read')
102+
103+
s = h5read(tc.TestData.basic, '/A4');
104+
assert(ndims(s)==4)
105+
assertEqual(tc, s, tc.TestData.A4, 'A4 read')
106+
end
107+
108+
109+
function test_coerce(tc)
110+
111+
basic = tc.TestData.basic;
112+
A0 = tc.TestData.A0;
113+
114+
hdf5nc.h5save(basic, '/int32', A0, "type", 'int32')
115+
hdf5nc.h5save(basic, '/int64', A0, "type", 'int64')
116+
hdf5nc.h5save(basic, '/float32', A0, "type", 'float32')
117+
118+
assertClass(tc, h5read(basic, '/int32'), 'int32', 'int32')
119+
assertClass(tc, h5read(basic, '/int64'), 'int64', 'int64')
120+
assertClass(tc, h5read(basic, '/float32'), 'single', 'float32')
121+
end
122+
123+
124+
function test_rewrite(tc)
125+
hdf5nc.h5save(tc.TestData.basic, '/A2', 3*magic(4))
126+
assertEqual(tc, h5read(tc.TestData.basic, '/A2'), 3*magic(4), 'rewrite 2D fail')
127+
end
File renamed without changes.

.travis.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
dist: focal
2-
31
language: matlab
42

3+
os:
4+
- linux
5+
56
matlab:
6-
- latest
7+
- latest
8+
9+
script:
10+
- matlab -batch "r = runtests('hdf5nc'); assert(~isempty(r)); assertSuccess(r)"

Readme.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ or append `hdf5nc.` to each function call.
1919
Run selftests by:
2020

2121
```matlab
22-
hdf5nc.tests.test_hdf5
23-
hdf5nc.tests.test_netcdf
22+
runtests('hdf5nc')
2423
```
2524

2625
* Check that a dataset exists in file:

test_hdf5nc.m

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)