Skip to content

Commit 9fd5b06

Browse files
committed
[c++/m] add compare Property function
1 parent 3dc8eed commit 9fd5b06

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

+nix/Property.m

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@
6565
function [] = values_delete(obj)
6666
nix_mx('Property::deleteValues', obj.nix_handle);
6767
end
68+
69+
% return value 0 means name and id of two properties are
70+
% identical, any other value means either name or id differ.
71+
function cmp_val = compare(obj, property)
72+
if (~strcmp(class(property), class(obj)))
73+
error('Function only supports comparison of Properties.');
74+
end
75+
cmp_val = nix_mx('Property::compare', obj.nix_handle, property.nix_handle);
76+
end
77+
6878
end
6979

7080
end

nix_mx.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ void mexFunction(int nlhs,
367367
methods->add("Property::values", nixproperty::values);
368368
methods->add("Property::updateValues", nixproperty::updateValues);
369369
methods->add("Property::deleteValues", nixproperty::deleteValues);
370+
methods->add("Property::compare", nixproperty::compare);
370371

371372
classdef<nix::SetDimension>("SetDimension", methods)
372373
.desc(&nixdimensions::describe)

src/nixproperty.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,10 @@ namespace nixproperty {
6969
prop.deleteValues();
7070
}
7171

72+
void compare(const extractor &input, infusor &output) {
73+
nix::Property pm = input.entity<nix::Property>(1);
74+
nix::Property pc = input.entity<nix::Property>(2);
75+
output.set(0, pm.compare(pc));
76+
}
77+
7278
} // namespace nixproperty

src/nixproperty.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ namespace nixproperty {
2121

2222
void deleteValues(const extractor &input, infusor &output);
2323

24+
void compare(const extractor &input, infusor &output);
25+
2426
} // namespace nixproperty
2527

2628
#endif

tests/TestProperty.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
funcs{end+1} = @test_values;
1717
funcs{end+1} = @test_value_count;
1818
funcs{end+1} = @test_values_delete;
19+
funcs{end+1} = @test_property_compare;
1920
end
2021

2122
%% Test: Access Attributes
@@ -145,3 +146,27 @@
145146
f = nix.File(testFile, nix.FileMode.ReadOnly);
146147
assert(isempty(f.sections{1}.allProperties{1}.values));
147148
end
149+
150+
%% Test: Compare properties
151+
function [] = test_property_compare( varargin )
152+
testFile = fullfile(pwd,'tests','testRW.h5');
153+
f = nix.File(testFile, nix.FileMode.Overwrite);
154+
s1 = f.create_section('testSection1', 'nixSection');
155+
s2 = f.create_section('testSection2', 'nixSection');
156+
157+
p = s1.create_property_with_value('property', {true, false, true});
158+
159+
% test invalid property comparison
160+
try
161+
p.compare('I shall crash and burn');
162+
catch ME
163+
assert(strcmp(ME.message, 'Function only supports comparison of Properties.'));
164+
end
165+
166+
% test property equal comparison
167+
assert(~p.compare(p));
168+
169+
% test property not eqal
170+
pNEq = s2.create_property_with_value('property', {true, false});
171+
assert(p.compare(pNEq)~=0);
172+
end

0 commit comments

Comments
 (0)