Skip to content

Commit

Permalink
Re #1790 bunch of unit tests for hashable objects and some issues ide…
Browse files Browse the repository at this point in the history
…ntified by these tests.
  • Loading branch information
abuts committed Dec 17, 2024
1 parent 0cc79e7 commit 7823015
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 86 deletions.
46 changes: 46 additions & 0 deletions _test/common_functions/hashable_obj_tester.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function hashable_obj_tester(hobj,values)
%hashable_obj_tester Generic function which validates correct hashable
%object operations.
%
% Checks if all hashable properties invalidate hash, hash calculations work
% and hash is stored-restored correctly
%
% Inputs:
% hobj -- instance of hashable object, better with proper values set
% Optional
% values-- cellarray of values to set to hashable object. If present, have
% to have number of elements equal to number of hashable
% properties and values of this properties compartible with
% serializeble, i.e. setting property do not contradict to other
% properties, validated through check_combo_arg method
%

flds = hobj.hashableFields();
n_flds = numel(flds);
if ~exist('values','var')
values = cellfun(@(x)hobj.(x),flds,'UniformOutput',false);
end

if ~hobj.hash_defined
hobj = hobj.build_hash();
end

for i=1:n_flds
assertTrue(hobj.hash_defined)
hobj.(flds{i}) = values{i};
assertFalse(hobj.hash_defined)
hobj = hobj.build_hash();
end

S = hobj.to_struct();
rec_obj= hobj.from_struct(S);

assertTrue(hobj.hash_defined);
assertTrue(hobj == rec_obj)

obj_arr = [hobj,hobj];
S = obj_arr.to_struct();
rec_arr= hobj.from_struct(S);

assertTrue(rec_arr.hash_defined);
assertTrue(obj_arr == rec_arr)
7 changes: 6 additions & 1 deletion _test/test_instrument_classes/test_IX_fermi_chopper.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@

obj.save()
end

%--------------------------------------------------------------------------
function test_hashable_prop(~)
ap = IX_fermi_chopper(12,610,0.049,1.3,0.0228);
hashable_obj_tester(ap);
end

%--------------------------------------------------------------------------
function test_pulse_shape (self)
t = -20:0.001:20;
Expand Down
12 changes: 11 additions & 1 deletion _test/test_instrument_classes/test_IX_inst.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,17 @@

self.save()
end

%--------------------------------------------------------------------------
function test_hashable_prop_DGfermi(self)
ap = IX_inst_DGfermi (self.mod_DGfermi, self.ap_DGfermi, self.chop_DGfermi);
hashable_obj_tester(ap);
end
function test_hashable_prop_DGdisk(self)
ap = IX_inst_DGdisk (self.mod_DGdisk, self.shape_DGdisk, self.mono_DGdisk,...
self.hdiv_DGdisk, self.vdiv_DGdisk);
hashable_obj_tester(ap);
end

%--------------------------------------------------------------------------
function test_create_DGfermi (self)
% Test creation of Fermi instrument
Expand Down
45 changes: 25 additions & 20 deletions _test/test_instrument_classes/test_IX_mod_shape_mono.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

home_folder;
end

methods
%--------------------------------------------------------------------------
function obj = test_IX_mod_shape_mono (name)
Expand All @@ -18,31 +18,36 @@
end
file = fullfile(home_folder,'test_IX_mod_shape_mono_output.mat');
obj@TestCaseWithSave(name,file);
obj.home_folder = home_folder;
obj.home_folder = home_folder;


% Create components needed for an IX_inst_DGdisk
% Use an old-ish LET function for convenience
obj.efix = 8;
instru = let_instrument_struct_for_tests (obj.efix, 280, 140, 20, 2, 2);

obj.mod_DGdisk = instru.moderator;
obj.shape_DGdisk = instru.chop_shape;
obj.mono_DGdisk = instru.chop_mono;

obj.save()
end


%--------------------------------------------------------------------------
function test_hashable_prop(self)
ap = IX_mod_shape_mono(self.mod_DGdisk, self.shape_DGdisk, self.mono_DGdisk);
hashable_obj_tester(ap);
end
%--------------------------------------------------------------------------
function test_covariance_mod (self)
msm = IX_mod_shape_mono(self.mod_DGdisk, self.shape_DGdisk, self.mono_DGdisk);
msm.shaping_chopper.frequency = 171;
msm.energy = self.efix;

% mod FWHH=99.37us, shape_chop FWHH=66.48us
shaped_mod = msm.shaped_mod; % should be false - but only just
assertEqualWithSave(self,shaped_mod);

tcov = msm.covariance();
tmean = msm.mean();
assertEqualToTolWithSave(self, tcov, 'tol', [1e-4,1e-4])
Expand All @@ -52,17 +57,17 @@ function test_covariance_mod (self)
assertEqualToTol(tcov, tcovR, 'tol', [0.5,2e-2])
assertEqualToTol(tmean, tmeanR, 'tol', [0.5,2e-2])
end

%--------------------------------------------------------------------------
function test_covariance_shape (self)
msm = IX_mod_shape_mono(self.mod_DGdisk, self.shape_DGdisk, self.mono_DGdisk);
msm.shaping_chopper.frequency = 172;
msm.energy = self.efix;

% FWHH=99.37us, shape_chop FWHH=66.09us
shaped_mod = msm.shaped_mod; % should be true - but only just
assertEqualWithSave(self,shaped_mod);

tcov = msm.covariance();
tmean = msm.mean();
assertEqualToTolWithSave(self, tcov, 'tol', [1e-4,1e-4])
Expand All @@ -72,17 +77,17 @@ function test_covariance_shape (self)
assertEqualToTol(tcov, tcovR, 'tol', [0.5,2e-2])
assertEqualToTol(tmean, tmeanR, 'tol', [0.5,2e-2])
end

%--------------------------------------------------------------------------
function test_covariance_mod_only (self)
msm = IX_mod_shape_mono(self.mod_DGdisk, self.shape_DGdisk, self.mono_DGdisk);
msm.shaping_chopper.frequency = 1;
msm.energy = self.efix;

% mod FWHH=99.37us, shape_chop FWHH=11368us
shaped_mod = msm.shaped_mod; % should be true - extreme case
assertEqualWithSave(self,shaped_mod);

tcov = msm.covariance();
tmean = msm.mean();
assertEqualToTolWithSave(self, tcov, 'tol', [1e-4,1e-4])
Expand All @@ -92,18 +97,18 @@ function test_covariance_mod_only (self)
assertEqualToTol(tcov, tcovR, 'tol', [0.5,4e-2])
assertEqualToTol(tmean, tmeanR, 'tol', [0.5,2e-2])
end

%--------------------------------------------------------------------------
function test_covariance_shaped_only (self)
msm = IX_mod_shape_mono(self.mod_DGdisk, self.shape_DGdisk, self.mono_DGdisk);
msm.moderator.pp(1)=10000;
msm.shaping_chopper.frequency = 171;
msm.energy = self.efix;

% mod FWHH=33947us, shape_chop FWHH=66.48us
shaped_mod = msm.shaped_mod; % should be true - extreme case
assertTrue(shaped_mod);

tcov = msm.covariance();
tmean = msm.mean();
assertEqualToTolWithSave(self, tcov, 'tol', [1e-4,1e-4])
Expand All @@ -112,7 +117,7 @@ function test_covariance_shaped_only (self)
[tcovR,tmeanR] = rand_covariance (msm, npnt);
assertEqualToTol(tcov, tcovR, 'tol', [0.5,2e-2])
assertEqualToTol(tmean, tmeanR, 'tol', [0.5,2e-2])
end
end
%--------------------------------------------------------------------------
function test_prev_versions(obj)
% Scalar example
Expand All @@ -133,7 +138,7 @@ function test_prev_versions(obj)
check_matfile_IO(verstr, save_variables, sample_files_location ,mod_sm);
end
end

end
end

Expand Down
5 changes: 5 additions & 0 deletions _test/test_instrument_classes/test_IX_moderator.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@

obj.save()
end
%--------------------------------------------------------------------------
function test_hashable_prop(~)
ap = IX_moderator(15,30,'ikcarp',[5,25,0.13]);
hashable_obj_tester(ap);
end

%--------------------------------------------------------------------------
function test_pulse_shape_default (~)
Expand Down
6 changes: 6 additions & 0 deletions _test/test_instrument_classes/test_IX_mosaic.m
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ function test_mosaic_anisotropic(obj)
mosaic = IX_mosaic ([1,1,0],[1,-1,0],[10,12,7]);
assertEqualWithSave(obj,mosaic);
end

function test_hashable_prop(~)
ap = IX_mosaic ([1,1,0],[1,-1,0],[10,12,7]);
hashable_obj_tester(ap);
end

%
function test_mosaic_matrix(obj)
xmos = [1,1,0];
Expand Down
6 changes: 5 additions & 1 deletion _test/test_instrument_classes/test_IX_sample.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ function test_to_from_struct(~)

assertEqual(sample,samp_rec);
end

%--------------------------------------------------------------------------
function test_hashable_prop(~)
ap = IX_sample([1,0,0], [0,1,0], 'cuboid', [2,3,4], 'eta', 4134);
hashable_obj_tester(ap);
end
%--------------------------------------------------------------------------
function test_IX_sample_constructor_error_if_required_args_missing(~)
f = @()IX_sample([1,0,0],[0,1,0],'cuboid');
Expand Down
5 changes: 5 additions & 0 deletions _test/test_instrument_classes/test_IX_source.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
self.save()
end

function test_hashable_prop(~)
ap = IX_source('ISIS','-freq',50);
hashable_obj_tester(ap);
end

%--------------------------------------------------------------------------
function test_simple_source(self)
ap = IX_source('ISIS','-freq',50);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@
end
function obj=set.parameters(obj,val)
obj.parameters_=val;
obj = obj.clear_hash();
end
%------------------------------------------------------------------
% Get methods for dependent properties
Expand Down
Loading

0 comments on commit 7823015

Please sign in to comment.