forked from keithfma/newmatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_newmatic.m
147 lines (108 loc) · 4.13 KB
/
test_newmatic.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
% Unit tests for newmatic function
%
% Usage:
% results = runtests(test_newmatic.m);
% %
function tests = test_newmatic()
% returns handles to test functions from this file (i.e., those that begin with "test")
tests = functiontests(localfunctions);
end
function setup(testCase)
% per-test fixture
testCase.TestData.filename = [tempname, '.mat'];
end
function teardown(testCase)
% per-test fixture cleanup
delete(testCase.TestData.filename);
end
function result = in_cell_array(value, array)
% return true if 'value' is found in cell array 'array', else False
result = any(cellfun(@(x) strcmp(value, x), array));
end
function check(testCase, fname, vars)
% check that the MAT-file created by newmatic matches expectations
assertTrue(testCase, isfile(fname));
mat = matfile(fname);
mat_info = h5info(fname);
mat_chunks = containers.Map();
for ii = 1:length(mat_info.Datasets)
mat_chunks(mat_info.Datasets(ii).Name) = mat_info.Datasets(ii).ChunkSize;
end
for ii = 1:length(vars)
var = vars(ii);
% variable exists?
assertTrue(testCase, in_cell_array(var.name, who(mat)));
% variable data type matches expectations?
assertTrue(testCase, strcmp(var.type, class(mat.(var.name))));
% variable size matches expectations?
if ~isempty(var.size)
assertEqual(testCase, length(var.size), length(size(mat, var.name)));
assertTrue(testCase, all(var.size == size(mat, var.name)));
end
% variable chunking matches expectations?
if ~isempty(var.chunks)
assertTrue(testCase, all(var.chunks == mat_chunks(var.name)));
end
end
end
function test_single_variable_nosize_nochunks(testCase)
fname = testCase.TestData.filename;
var = newmatic_variable('x', 'single');
newmatic(fname, var);
check(testCase, fname, var);
end
function test_single_variable_nochunks(testCase)
fname = testCase.TestData.filename;
var = newmatic_variable('x', 'single', [10, 20, 30]);
newmatic(fname, var);
check(testCase, fname, var);
end
function test_single_variable(testCase)
fname = testCase.TestData.filename;
var = newmatic_variable('x', 'single', [10, 20, 30], [10, 10, 10]);
newmatic(fname, var);
check(testCase, fname, var);
end
function test_multi_variable_nosize_nochunks(testCase)
fname = testCase.TestData.filename;
vars = {...
newmatic_variable('x', 'uint8'), ...
newmatic_variable('y', 'single'), ...
newmatic_variable('z', 'double')};
newmatic(fname, vars{:});
check(testCase, fname, cell2mat(vars));
end
function test_multi_variable_nochunks(testCase)
fname = testCase.TestData.filename;
vars = {...
newmatic_variable('x', 'uint8', [30, 20]), ...
newmatic_variable('y', 'single', [5, 10]), ...
newmatic_variable('z', 'double', [40, 50, 10])};
newmatic(fname, vars{:});
check(testCase, fname, cell2mat(vars));
end
function test_multi_variable(testCase)
fname = testCase.TestData.filename;
vars = {...
newmatic_variable('x', 'uint8', [30, 20], [15, 10]), ...
newmatic_variable('y', 'single', [5, 10], [5, 5]), ...
newmatic_variable('z', 'double', [40, 50, 10], [10, 10, 10])};
newmatic(fname, vars{:});
check(testCase, fname, cell2mat(vars));
end
function test_input_validation(testCase)
a_var = newmatic_variable('x', 'double');
% path type incorrect
assertError(testCase, @() newmatic(5, a_var), 'MATLAB:invalidType');
% protect against overwriting
fname = testCase.TestData.filename;
fclose(fopen(fname, 'w')); % create file by touching it
assertError(testCase, @() newmatic(fname, a_var), 'newmatic:OverwriteError');
end
function test_preserve_logical_dtype(testCase)
fname = testCase.TestData.filename;
mat = newmatic(fname, newmatic_variable('x', 'logical', [100, 200], [50, 100]));
mat.x(:, :) = false(100, 200);
% data read back from file should still be logical
assertClass(testCase, mat.x, 'logical');
end