Skip to content

Commit 6c6c1c5

Browse files
authored
Adds optional validation for whole numbers to validateNumber (#260)
* Adds optional validation for whole numbers to validateNumber * Addresses review comment
1 parent d626445 commit 6c6c1c5

4 files changed

Lines changed: 33 additions & 18 deletions

File tree

API/controlsClass.m

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
end
8585

8686
function set.updatePlotFreq(obj, val)
87-
validateNumber(val, 'updatePlotFreq must be a number');
87+
validateNumber(val, 'updatePlotFreq must be a whole number', true);
8888
if val < 1
8989
throw(exceptions.invalidValue('updatePlotFreq must be greater or equal to 1'));
9090
end
@@ -117,16 +117,16 @@
117117
end
118118

119119
function set.maxFuncEvals(obj, val)
120-
obj.maxFuncEvals = validateNumber(val, 'maxFuncEvals must be a number');
120+
obj.maxFuncEvals = validateNumber(val, 'maxFuncEvals must be a whole number', true);
121121
end
122122

123123
function set.maxIterations(obj, val)
124-
obj.maxIterations = validateNumber(val, 'maxIterations must be a number');
124+
obj.maxIterations = validateNumber(val, 'maxIterations must be a whole number', true);
125125
end
126126

127127
% DE controls methods
128128
function set.populationSize(obj, val)
129-
validateNumber(val, 'populationSize must be a number');
129+
validateNumber(val, 'populationSize must be a whole number', true);
130130
if val < 1
131131
throw(exceptions.invalidValue('populationSize must be greater or equal to 1'));
132132
end
@@ -161,7 +161,7 @@
161161
end
162162

163163
function set.numGenerations(obj, val)
164-
validateNumber(val, 'numGenerations value must be a number');
164+
validateNumber(val, 'numGenerations value must be a whole number', true);
165165
if val < 1
166166
throw(exceptions.invalidValue('numGenerations must be greater or equal to 1'));
167167
end
@@ -170,15 +170,15 @@
170170

171171
% NS control methods
172172
function set.nLive(obj, val)
173-
validateNumber(val, 'nLive must be a number');
173+
validateNumber(val, 'nLive must be a whole number', true);
174174
if val < 1
175175
throw(exceptions.invalidValue('nLive must be greater or equal to 1'));
176176
end
177177
obj.nLive = val;
178178
end
179179

180180
function set.nMCMC(obj, val)
181-
validateNumber(val, 'nMCMC must be a number');
181+
validateNumber(val, 'nMCMC must be a whole number', true);
182182
if val < 0
183183
throw(exceptions.invalidValue('nMCMC must be greater or equal than 0'));
184184
end
@@ -203,16 +203,16 @@
203203

204204
% DREAM methods
205205
function set.nSamples(obj,val)
206-
validateNumber(val, 'nSample must be a number ');
206+
validateNumber(val, 'nSamples must be a whole number', true);
207207
if val < 0
208208
throw(exceptions.invalidValue('nSample must be greater or equal to 0'));
209209
end
210210
obj.nSamples = val;
211211
end
212212

213213
function set.nChains(obj,val)
214-
validateNumber(val, 'nChains must be a number ');
215-
if (~(round(val) == val) || val <= 0 || isnan(val) || isinf(val))
214+
validateNumber(val, 'nChains must be a whole number', true);
215+
if val <= 0
216216
throw(exceptions.invalidValue('nChains must be a finite integer greater than 0'));
217217
end
218218
obj.nChains = val;

tests/testControlsClass.m

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ function testUpdatePlotFreq(testCase)
9494
testCase.verifyEqual(testCase.controls.updatePlotFreq, 5, 'set.updatePlotFreq method is not working')
9595
% bad updatePlotFreq type
9696
testCase.verifyError(@() setUpdatePlotFreq('a'), exceptions.invalidType.errorID);
97+
testCase.verifyError(@() setUpdatePlotFreq(1.5), exceptions.invalidValue.errorID);
9798
testCase.verifyError(@() setUpdatePlotFreq(0), exceptions.invalidValue.errorID);
9899
function setUpdatePlotFreq(value)
99100
testCase.controls.updatePlotFreq = value;
@@ -118,16 +119,18 @@ function testSimplexArguments(testCase)
118119

119120
testCase.controls.maxFuncEvals = 123;
120121
testCase.verifyEqual(testCase.controls.maxFuncEvals, 123, 'set.maxFuncEvals method is not working')
121-
testCase.verifyError(@setMaxFuncEvals, exceptions.invalidType.errorID); % bad maxFuncEvals type
122-
function setMaxFuncEvals
123-
testCase.controls.maxFuncEvals = 'a';
122+
testCase.verifyError(@() setMaxFuncEvals('a'), exceptions.invalidType.errorID); % bad maxFuncEvals type
123+
testCase.verifyError(@() setMaxFuncEvals(1.5), exceptions.invalidValue.errorID);
124+
function setMaxFuncEvals(value)
125+
testCase.controls.maxFuncEvals = value;
124126
end
125127

126128
testCase.controls.maxIterations = 456;
127129
testCase.verifyEqual(testCase.controls.maxIterations, 456, 'set.maxIterations method is not working')
128-
testCase.verifyError(@setMaxIterations, exceptions.invalidType.errorID); % bad maxIterations type
129-
function setMaxIterations
130-
testCase.controls.maxIterations = 'a';
130+
testCase.verifyError(@() setMaxIterations('a'), exceptions.invalidType.errorID); % bad maxIterations type
131+
testCase.verifyError(@() setMaxIterations(1.5), exceptions.invalidValue.errorID);
132+
function setMaxIterations(value)
133+
testCase.controls.maxIterations = value;
131134
end
132135
end
133136

@@ -138,6 +141,7 @@ function testDEArguments(testCase)
138141
% bad populationSize type
139142
testCase.verifyError(@() setPopulationSize('a'), exceptions.invalidType.errorID);
140143
testCase.verifyError(@() setPopulationSize(0), exceptions.invalidValue.errorID);
144+
testCase.verifyError(@() setPopulationSize(1.5), exceptions.invalidValue.errorID);
141145
function setPopulationSize(value)
142146
testCase.controls.populationSize = value;
143147
end
@@ -186,6 +190,7 @@ function setTargetValue(value)
186190
% bad numGenerations type
187191
testCase.verifyError(@() setNumGenerations('a'), exceptions.invalidType.errorID);
188192
testCase.verifyError(@() setNumGenerations(0), exceptions.invalidValue.errorID);
193+
testCase.verifyError(@() setNumGenerations(1.5), exceptions.invalidValue.errorID);
189194
function setNumGenerations(value)
190195
testCase.controls.numGenerations = value;
191196
end
@@ -198,6 +203,7 @@ function testNSArguments(testCase)
198203
% bad nLive type
199204
testCase.verifyError(@() setnLive('a'), exceptions.invalidType.errorID);
200205
testCase.verifyError(@() setnLive(0), exceptions.invalidValue.errorID);
206+
testCase.verifyError(@() setnLive(1.5), exceptions.invalidValue.errorID);
201207
function setnLive(value)
202208
testCase.controls.nLive = value;
203209
end
@@ -207,6 +213,7 @@ function setnLive(value)
207213
% bad nMCMC type
208214
testCase.verifyError(@() setnMCMC('a'), exceptions.invalidType.errorID);
209215
testCase.verifyError(@() setnMCMC(-1), exceptions.invalidValue.errorID);
216+
testCase.verifyError(@() setnMCMC(1.5), exceptions.invalidValue.errorID);
210217
function setnMCMC(value)
211218
testCase.controls.nMCMC = value;
212219
end
@@ -238,6 +245,7 @@ function testDreamArguments(testCase)
238245
% bad nSamples type
239246
testCase.verifyError(@() setNSamples('a'), exceptions.invalidType.errorID);
240247
testCase.verifyError(@() setNSamples(-1), exceptions.invalidValue.errorID);
248+
testCase.verifyError(@() setNSamples(1.5), exceptions.invalidValue.errorID);
241249
function setNSamples(value)
242250
testCase.controls.nSamples = value;
243251
end
@@ -605,6 +613,5 @@ function testSetProcedureWithCalculate(testCase)
605613

606614
end
607615

608-
609616
end
610617
end

tests/testUtilities.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ function testValidateNumber(testCase)
2222
testCase.verifyEqual(validateNumber([1, 2, 3]), [1, 2, 3], 'validateNumber function is not working');
2323
testCase.verifyError(@() validateNumber('a'), exceptions.invalidType.errorID);
2424
testCase.verifyError(@() validateNumber(false), exceptions.invalidType.errorID);
25+
testCase.verifyEqual(validateNumber(-1.6, '', false), -1.6, 'validateNumber function is not working');
26+
testCase.verifyError(@() validateNumber(-1.6, '', true), exceptions.invalidValue.errorID);
2527
end
2628

2729
function testValidateOption(testCase)

utilities/validateNumber.m

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
function input = validateNumber(input, message)
1+
function input = validateNumber(input, message, validateInt)
22
% Checks input is a number and throws an exception with given message.
33
%
4+
% If validateInt is true, also check that input is a whole number.
5+
%
46
% validateNumber(2, 'This is not a number');
7+
% validateNumber(2.5, 'This is not a whole number', true);
58
arguments
69
input
710
message {mustBeTextScalar} = 'The input is not a number'
11+
validateInt {mustBeA(validateInt,'logical')} = false
812
end
913

1014
if ~isnumeric(input)
1115
throw(exceptions.invalidType(message));
16+
elseif validateInt && mod(input, 1) ~= 0
17+
throw(exceptions.invalidValue(message));
1218
end
1319
end

0 commit comments

Comments
 (0)