Skip to content

Commit ee0bb1f

Browse files
committed
[c++/m] Add Section sectionsFilter
1 parent 9eb187c commit ee0bb1f

File tree

5 files changed

+79
-4
lines changed

5 files changed

+79
-4
lines changed

+nix/Section.m

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@
102102
c = nix_mx('Section::sectionCount', obj.nix_handle);
103103
end
104104

105+
function filtered = filter_sections(obj, filter, val)
106+
filtered = nix.Utils.filter(obj, filter, val, ...
107+
'Section::sectionsFiltered', @nix.Section);
108+
end
109+
105110
% ----------------
106111
% Property methods
107112
% ----------------

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ void mexFunction(int nlhs,
414414
methods->add("Section::openSectionIdx", nixsection::openSectionIdx);
415415
methods->add("Section::openPropertyIdx", nixsection::openPropertyIdx);
416416
methods->add("Section::compare", nixsection::compare);
417+
methods->add("Section::sectionsFiltered", nixsection::sectionsFiltered);
417418

418419
classdef<nix::Feature>("Feature", methods)
419420
.desc(&nixfeature::describe)

src/nixsection.cc

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
// LICENSE file in the root of the Project.
88

99
#include "nixsection.h"
10+
1011
#include "mex.h"
1112
#include <nix.hpp>
1213

13-
#include "handle.h"
1414
#include "arguments.h"
15-
#include "struct.h"
15+
#include "filters.h"
16+
#include "handle.h"
1617
#include "mknix.h"
17-
18+
#include "struct.h"
1819

1920
namespace nixsection {
2021

@@ -123,4 +124,13 @@ namespace nixsection {
123124
output.set(0, currObj.compare(other));
124125
}
125126

127+
void sectionsFiltered(const extractor &input, infusor &output) {
128+
nix::Section currObj = input.entity<nix::Section>(1);
129+
std::vector<nix::Section> res = filterFileEntity<nix::Section>(input,
130+
[currObj](const nix::util::Filter<nix::Section>::type &filter) {
131+
return currObj.sections(filter);
132+
});
133+
output.set(0, res);
134+
}
135+
126136
} // namespace nixsection

src/nixsection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ namespace nixsection {
3535

3636
void compare(const extractor &input, infusor &output);
3737

38+
void sectionsFiltered(const extractor &input, infusor &output);
39+
3840
} // namespace nixsection
3941

4042
#endif

tests/TestSection.m

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
funcs{end+1} = @test_referring_block_sources;
4040
funcs{end+1} = @test_referring_blocks;
4141
funcs{end+1} = @test_compare;
42+
funcs{end+1} = @test_filter_section;
4243
end
4344

4445
%% Test: Create Section
@@ -619,4 +620,60 @@
619620
assert(s1.compare(s2) < 0);
620621
assert(s1.compare(s1) == 0);
621622
assert(s2.compare(s1) > 0);
622-
end
623+
end
624+
625+
%% Test: filter Sections
626+
function [] = test_filter_section( varargin )
627+
filterName = 'filterMe';
628+
filterType = 'filterType';
629+
f = nix.File(fullfile(pwd, 'tests', 'testRW.h5'), nix.FileMode.Overwrite);
630+
ms = f.create_section('testSection', 'nixSection');
631+
s = ms.create_section(filterName, 'nixSection');
632+
filterID = s.id;
633+
s = ms.create_section('testSection1', filterType);
634+
filterIDs = {filterID, s.id};
635+
s = ms.create_section('testSection2', filterType);
636+
637+
% ToDO add basic filter crash tests
638+
639+
% test empty id filter
640+
assert(isempty(f.sections{1}.filter_sections(nix.Filter.id, 'IdoNotExist')));
641+
642+
% test nix.Filter.accept_all
643+
filtered = f.sections{1}.filter_sections(nix.Filter.accept_all, '');
644+
assert(size(filtered, 1) == 3);
645+
646+
% test nix.Filter.id
647+
filtered = f.sections{1}.filter_sections(nix.Filter.id, filterID);
648+
assert(size(filtered, 1) == 1);
649+
assert(strcmp(filtered{1}.id, filterID));
650+
651+
% test nix.Filter.ids
652+
filtered = f.sections{1}.filter_sections(nix.Filter.ids, filterIDs);
653+
assert(size(filtered, 1) == 2);
654+
assert(strcmp(filtered{1}.id, filterIDs{1}) || strcmp(filtered{1}.id, filterIDs{2}));
655+
656+
% test nix.Filter.name
657+
filtered = f.sections{1}.filter_sections(nix.Filter.name, filterName);
658+
assert(size(filtered, 1) == 1);
659+
assert(strcmp(filtered{1}.name, filterName));
660+
661+
% test nix.Filter.type
662+
filtered = f.sections{1}.filter_sections(nix.Filter.type, filterType);
663+
assert(size(filtered, 1) == 2);
664+
665+
% test fail on nix.Filter.metadata
666+
err = 'unknown or unsupported filter';
667+
try
668+
f.sections{1}.filter_sections(nix.Filter.metadata, 'someMetadata');
669+
catch ME
670+
assert(strcmp(ME.message, err));
671+
end
672+
673+
% test fail on nix.Filter.source
674+
try
675+
f.sections{1}.filter_sections(nix.Filter.source, 'someSource');
676+
catch ME
677+
assert(strcmp(ME.message, err));
678+
end
679+
end

0 commit comments

Comments
 (0)