Skip to content

Commit ba731a6

Browse files
committed
completify: add missing File functions and tests
1 parent a69b9a2 commit ba731a6

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

+nix/File.m

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@
2424
% ----------------
2525
% Block methods
2626
% ----------------
27-
28-
function retObj = openBlock(obj, id_or_name)
29-
retObj = nix.Utils.open_entity(obj, ...
30-
'File::openBlock', id_or_name, @nix.Block);
31-
end
32-
27+
3328
function newBlock = createBlock(obj, name, type)
3429
newBlock = nix.Block(nix_mx('File::createBlock', obj.nix_handle, name, type));
3530
obj.blocksCache.lastUpdate = 0;
3631
end;
3732

33+
function hasBlock = hasBlock(obj, id_or_name)
34+
hasBlock = nix_mx('File::hasBlock', obj.nix_handle, id_or_name);
35+
end;
36+
37+
function retObj = openBlock(obj, id_or_name)
38+
retObj = nix.Utils.open_entity(obj, ...
39+
'File::openBlock', id_or_name, @nix.Block);
40+
end
41+
3842
function delCheck = deleteBlock(obj, del)
3943
[delCheck, obj.blocksCache] = nix.Utils.delete_entity(obj, ...
4044
del, 'nix.Block', 'File::deleteBlock', obj.blocksCache);
@@ -43,17 +47,21 @@
4347
% ----------------
4448
% Section methods
4549
% ----------------
46-
47-
function retObj = openSection(obj, id_or_name)
48-
retObj = nix.Utils.open_entity(obj, ...
49-
'File::openSection', id_or_name, @nix.Section);
50-
end
5150

5251
function newSec = createSection(obj, name, type)
5352
newSec = nix.Section(nix_mx('File::createSection', obj.nix_handle, name, type));
5453
obj.sectionsCache.lastUpdate = 0;
5554
end;
5655

56+
function hasSec = hasSection(obj, id_or_name)
57+
hasSec = nix_mx('File::hasSection', obj.nix_handle, id_or_name);
58+
end;
59+
60+
function retObj = openSection(obj, id_or_name)
61+
retObj = nix.Utils.open_entity(obj, ...
62+
'File::openSection', id_or_name, @nix.Section);
63+
end
64+
5765
function delCheck = deleteSection(obj, del)
5866
delCheck = nix.Utils.delete_entity(obj, del, 'nix.Section', 'File::deleteSection');
5967
obj.sectionsCache.lastUpdate = 0;

nix_mx.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ void mexFunction(int nlhs,
9797
.add("open", nixfile::open)
9898
.reg("blocks", GETTER(std::vector<nix::Block>, nix::File, blocks))
9999
.reg("sections", GETTER(std::vector<nix::Section>, nix::File, sections))
100+
.reg("hasBlock", GETBYSTR(bool, nix::File, hasBlock))
101+
.reg("hasSection", GETBYSTR(bool, nix::File, hasSection))
100102
.reg("deleteBlock", REMOVER(nix::Block, nix::File, deleteBlock))
101103
.reg("deleteSection", REMOVER(nix::Section, nix::File, deleteSection))
102104
.reg("openBlock", GETBYSTR(nix::Block, nix::File, getBlock))

tests/TestFile.m

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
funcs{end+1} = @test_open_block;
1515
funcs{end+1} = @test_delete_block;
1616
funcs{end+1} = @test_delete_section;
17+
funcs{end+1} = @test_has_block;
18+
funcs{end+1} = @test_has_section;
1719

1820
end
1921

@@ -129,3 +131,35 @@
129131
getBlock = test_file.openBlock('I dont exist');
130132
assert(isempty(getBlock));
131133
end
134+
135+
%% Test: nix.File has nix.Block by ID or name
136+
function [] = test_has_block( varargin )
137+
fileName = 'testRW.h5';
138+
blockName = 'hasBlockTest';
139+
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.Overwrite);
140+
b = f.createBlock(blockName, 'nixBlock');
141+
bID = b.id;
142+
143+
assert(~f.hasBlock('I do not exist'));
144+
assert(f.hasBlock(blockName));
145+
146+
clear b f;
147+
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.ReadOnly);
148+
assert(f.hasBlock(bID));
149+
end
150+
151+
%% Test: nix.File has nix.Section by ID or name
152+
function [] = test_has_section( varargin )
153+
fileName = 'testRW.h5';
154+
secName = 'hasSectionTest';
155+
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.Overwrite);
156+
s = f.createSection(secName, 'nixSection');
157+
sID = s.id;
158+
159+
assert(~f.hasSection('I do not exist'));
160+
assert(f.hasSection(secName));
161+
162+
clear s f;
163+
f = nix.File(fullfile(pwd, 'tests', fileName), nix.FileMode.ReadOnly);
164+
assert(f.hasSection(sID));
165+
end

0 commit comments

Comments
 (0)