Skip to content
Snippets Groups Projects
Verified Commit 5ab97a67 authored by Thomas Lambert's avatar Thomas Lambert :helicopter:
Browse files

test: cleanup tests for +utils functions

parent 47fd8e06
No related branches found
No related tags found
No related merge requests found
% TEST_APPENDEXTENSION Unitary tests for the appendextension sub-function
% Note:
% The function needs to be imported at each test, because apparently Matlab
% does not pass it from setupOnce() or setup().
% For some reasons, the whole package needs to be imported, not just the
% function under testing.
% Matlab package functionality is not very test-framework friendly.
% Either the whole package has to be imported in EVERY SINGLE TEST or the
% functions must be called as <package>.function() everytime.
% The second option is preferred for the tests as it has the smaller scope.
% -----
% Copyright 2022 Thomas Lambert <t.lambert@uliege.be>
......@@ -24,28 +24,28 @@ end
%% Setup and teardown
function setupOnce(testCase) % do not change function name
function setupOnce(testCase)
addpath('../.'); % Add repository to Matlab Path
addpath('./test_utils'); % Add utils to Matlab Path
% Set random number generator settings.
% Set random number generator settings
testCase.TestData.currentRNG = rng;
end
function teardownOnce(testCase) % do not change function name
function teardownOnce(testCase)
rmpath('../.'); % Remove repository from Matlab Path
rmpath('./test_utils'); % Remove utils from Matlab Path
% Restore the random number generator settings.
% Restore the random number generator settings
s = testCase.TestData.currentRNG;
rng(s)
end
function teardown(~) % do not change function name
function teardown(~)
close all; % Close all figures that would have been openend
......@@ -55,28 +55,24 @@ end
%% Test for correct output
function test_noExtension(testCase)
% Ensure that the function appends the extension if there is none
import af_tools.utils.*
% Append extension if there is none
string = 'test';
ext = '.m';
filename = [string,ext];
verifyEqual(testCase, appendextension(string, ext), filename)
verifyEqual(testCase, af_tools.utils.appendextension(string, ext), filename)
end
function test_existExtension(testCase)
% Ensure that the function does not re-append the extension if it is already one
import af_tools.utils.*
% Do not append extension if there is already one
string = 'test.m';
ext = '.m';
verifyEqual(testCase, appendextension(string, ext), string)
verifyEqual(testCase, af_tools.utils.appendextension(string, ext), string)
end
\ No newline at end of file
end
......@@ -26,7 +26,7 @@ end
%% Setup and teardown
function setupOnce(testCase) % do not change function name
function setupOnce(testCase)
addpath('../.'); % Add repository to Matlab Path
addpath('./test_utils'); % Add utils to Matlab Path
......@@ -36,7 +36,7 @@ testCase.TestData.currentRNG = rng;
end
function teardownOnce(testCase) % do not change function name
function teardownOnce(testCase)
rmpath('../.'); % Remove repository from Matlab Path
rmpath('./test_utils'); % Remove utils from Matlab Path
......@@ -47,7 +47,7 @@ rng(s)
end
function teardown(~) % do not change function name
function teardown(~)
close all; % Close all figures that would have been openend
......
% TEST_SANITIZESTRING Unitary tests for the sanitizestring sub-function
% Note:
% The function needs to be imported at each test, because apparently Matlab
% does not pass it from setupOnce() or setup().
% For some reasons, the whole package needs to be imported, not just the
% function under testing.
% Matlab package functionality is not very test-framework friendly.
% Either the whole package has to be imported in EVERY SINGLE TEST or the
% functions must be called as <package>.function() everytime.
% The second option is preferred for the tests as it has the smaller scope.
% -----
% Copyright 2022 Thomas Lambert <t.lambert@uliege.be>
......@@ -24,28 +24,28 @@ end
%% Setup and teardown
function setupOnce(testCase) % do not change function name
function setupOnce(testCase)
addpath('../.'); % Add repository to Matlab Path
addpath('./test_utils'); % Add utils to Matlab Path
% Set random number generator settings.
% Set random number generator settings
testCase.TestData.currentRNG = rng;
end
function teardownOnce(testCase) % do not change function name
function teardownOnce(testCase)
rmpath('../.'); % Remove repository from Matlab Path
rmpath('./test_utils'); % Remove utils from Matlab Path
% Restore the random number generator settings.
% Restore the random number generator settings
s = testCase.TestData.currentRNG;
rng(s)
end
function teardown(~) % do not change function name
function teardown(~)
close all; % Close all figures that would have been openend
......@@ -55,48 +55,50 @@ end
%% Test for correct output
function test_removeLeadingSpaces(testCase)
% Ensure that the function trims the leading spaces
import af_tools.utils.*
% Leading spaces must be trimmed
string = ' test';
verifyEqual(testCase, sanitizestring(string), 'test')
verifyEqual(testCase, af_tools.utils.sanitizestring(string), 'test')
end
function test_removeTrailingSpaces(testCase)
% Ensure that the function trims the trailing spaces
import af_tools.utils.*
% Trailing spaces must be trimmed
string = 'test ';
verifyEqual(testCase, sanitizestring(string), 'test')
verifyEqual(testCase, af_tools.utils.sanitizestring(string), 'test')
end
function test_rmMultiSpaces(testCase)
% Ensure that the function removes contiguous spaces and change them by underscore
function test_replaceSingleSpaces(testCase)
% Single spaces should be replaced by an underscore
string = 'str1 str2 str3';
import af_tools.utils.*
verifyEqual(testCase, af_tools.utils.sanitizestring(string), 'str1_str2_str3')
end
function test_replaceMultiSpaces(testCase)
% Multi spaces should be replaced by a single underscore
string = 'str1 str2 str3';
verifyEqual(testCase, sanitizestring(string), 'str1_str2_str3')
verifyEqual(testCase, af_tools.utils.sanitizestring(string), 'str1_str2_str3')
end
function test_changePC(testCase)
% Ensure that the function changes % symbol
import af_tools.utils.*
function test_replacePercent(testCase)
% The % symbol should be replaced by 'pc'
string = '0%';
verifyEqual(testCase, sanitizestring(string), '0pc')
verifyEqual(testCase, af_tools.utils.sanitizestring(string), '0pc')
end
\ No newline at end of file
end
% TEST_TRIMTORANGE Unitary tests for the trimtorange sub-function
% Note:
% The function needs to be imported at each test, because apparently Matlab
% does not pass it from setupOnce() or setup().
% For some reasons, the whole package needs to be imported, not just the
% function under testing.
% Matlab package functionality is not very test-framework friendly.
% Either the whole package has to be imported in EVERY SINGLE TEST or the
% functions must be called as <package>.function() everytime.
% The second option is preferred for the tests as it has the smaller scope.
% -----
% Copyright 2022 Thomas Lambert <t.lambert@uliege.be>
......@@ -24,28 +24,28 @@ end
%% Setup and teardown
function setupOnce(testCase) % do not change function name
function setupOnce(testCase)
addpath('../.'); % Add repository to Matlab Path
addpath('./test_utils'); % Add utils to Matlab Path
% Set random number generator settings.
% Set random number generator settings
testCase.TestData.currentRNG = rng;
end
function teardownOnce(testCase) % do not change function name
function teardownOnce(testCase)
rmpath('../.'); % Remove repository from Matlab Path
rmpath('./test_utils'); % Remove utils from Matlab Path
% Restore the random number generator settings.
% Restore the random number generator settings
s = testCase.TestData.currentRNG;
rng(s)
end
function teardown(~) % do not change function name
function teardown(~)
close all; % Close all figures that would have been openend
......@@ -55,15 +55,12 @@ end
%% Arguments and error tests
function test_nargout(testCase)
% Ensure that the function throws an error too many output
import af_tools.utils.*
% Ensure that the function throws an error when too many output
range = [-5;5];
dummy = -10:10;
[~, ~] = verifyError(testCase, @() trimtorange(range, dummy), 'MATLAB:nargoutchk:tooManyOutputs');
[~, ~, ~] = verifyError(testCase, @() trimtorange(range, dummy, dummy), 'MATLAB:nargoutchk:tooManyOutputs');
[~, ~] = verifyError(testCase, @() af_tools.utils.trimtorange(range, dummy), 'MATLAB:nargoutchk:tooManyOutputs');
end
......@@ -71,9 +68,7 @@ end
%% Test for correct output
function test_rangeInAOA(testCase)
% Ensure proper output if range is fully contained in alpha
import af_tools.utils.*
% Trim properly if range is fully contained in alpha
range = [-1,1]';
alpha = (-10:10)';
......@@ -81,7 +76,7 @@ cl = alpha + 10;
cd = alpha - 10;
expAlphaTrim = (-1:1)';
[alphaTrim, clTrim, cdTrim] = trimtorange(range, alpha, cl, cd);
[alphaTrim, clTrim, cdTrim] = af_tools.utils.trimtorange(range, alpha, cl, cd);
verifyEqual(testCase, alphaTrim, expAlphaTrim);
verifyEqual(testCase, clTrim, expAlphaTrim+10);
......@@ -93,14 +88,12 @@ end
function test_rangeOutAOA(testCase)
% If range is all out of AOA, just return the input
import af_tools.utils.*
range = [-20,-15]';
alpha = (-10:10)';
cl = alpha + 10;
cd = alpha - 10;
[alphaTrim, clTrim, cdTrim] = trimtorange(range, alpha, cl, cd);
[alphaTrim, clTrim, cdTrim] = af_tools.utils.trimtorange(range, alpha, cl, cd);
verifyEqual(testCase, alphaTrim, alpha);
verifyEqual(testCase, clTrim, cl);
......@@ -111,9 +104,7 @@ end
function test_rangePartOutAOA(testCase)
% Ensure proper output if range is partially out of alpha
import af_tools.utils.*
% Trim properly if partially out of alpha
range = (-20:0)';
alpha = (-10:10)';
......@@ -121,7 +112,7 @@ cl = alpha + 10;
cd = alpha - 10;
expAlphaTrim = (-10:0)';
[alphaTrim, clTrim, cdTrim] = trimtorange(range, alpha, cl, cd);
[alphaTrim, clTrim, cdTrim] = af_tools.utils.trimtorange(range, alpha, cl, cd);
verifyEqual(testCase, alphaTrim, expAlphaTrim);
verifyEqual(testCase, clTrim, expAlphaTrim+10);
......@@ -131,16 +122,14 @@ end
function test_rangeEqualAOA(testCase)
% Ensure proper output if range is equal to alpha
import af_tools.utils.*
% Trim properly if range is equal to alpha
alpha = (-10:10)';
range = (-10:10)';
cl = (20:40)';
cd = (-20:0)';
[alphaTrim, clTrim, cdTrim] = trimtorange(range, alpha, cl, cd);
[alphaTrim, clTrim, cdTrim] = af_tools.utils.trimtorange(range, alpha, cl, cd);
verifyEqual(testCase, alphaTrim, alpha);
verifyEqual(testCase, clTrim, cl);
......@@ -150,17 +139,15 @@ end
function test_scalarRange(testCase)
% Ensure proper output if range is a scalar
import af_tools.utils.*
% Trim properly if range is a scalar
range = (0)';
range = 0;
alpha = (-10:10)';
cl = alpha + 10;
cd = alpha - 10;
expAlphaTrim = 0;
[alphaTrim, clTrim, cdTrim] = trimtorange(range, alpha, cl, cd);
[alphaTrim, clTrim, cdTrim] = af_tools.utils.trimtorange(range, alpha, cl, cd);
verifyEqual(testCase, alphaTrim, expAlphaTrim);
verifyEqual(testCase, clTrim, expAlphaTrim+10);
......
% TEST_VECTTOCOL Unitary tests for the vecttocol sub-function
% Note:
% The function needs to be imported at each test, because apparently Matlab
% does not pass it from setupOnce() or setup().
% For some reasons, the whole package needs to be imported, not just the
% function under testing.
% Matlab package functionality is not very test-framework friendly.
% Either the whole package has to be imported in EVERY SINGLE TEST or the
% functions must be called as <package>.function() everytime.
% The second option is preferred for the tests as it has the smaller scope.
% -----
% Copyright 2022 Thomas Lambert <t.lambert@uliege.be>
......@@ -24,28 +24,28 @@ end
%% Setup and teardown
function setupOnce(testCase) % do not change function name
function setupOnce(testCase)
addpath('../.'); % Add repository to Matlab Path
addpath('./test_utils'); % Add utils to Matlab Path
% Set random number generator settings.
% Set random number generator settings
testCase.TestData.currentRNG = rng;
end
function teardownOnce(testCase) % do not change function name
function teardownOnce(testCase)
rmpath('../.'); % Remove repository from Matlab Path
rmpath('./test_utils'); % Remove utils from Matlab Path
% Restore the random number generator settings.
% Restore the random number generator settings
s = testCase.TestData.currentRNG;
rng(s)
end
function teardown(~) % do not change function name
function teardown(~)
close all; % Close all figures that would have been openend
......@@ -55,49 +55,41 @@ end
%% Test for correct output
function test_scalar(testCase)
% Ensure that the function outputs the same scalar
import af_tools.utils.*
% If input is scalar, output should be same scalar
scal = 0;
verifyEqual(testCase, vecttocol(scal), scal)
verifyEqual(testCase, af_tools.utils.vecttocol(scal), scal)
end
function test_rowVector(testCase)
% Ensure that the function outputs a column vector if we enter a row one
import af_tools.utils.*
% If input is a row vector, output should be its transpose (column vector)
rowVect = 1:10;
verifyEqual(testCase, vecttocol(rowVect), rowVect')
verifyEqual(testCase, af_tools.utils.vecttocol(rowVect), rowVect')
end
function test_colVect(testCase)
% Ensure that the function outputs the same vector if we enter a column one
import af_tools.utils.*
% If input is a column vector, output should be the same vector
colVect = (1:10)';
verifyEqual(testCase, vecttocol(colVect), colVect)
verifyEqual(testCase, af_tools.utils.vecttocol(colVect), colVect)
end
function test_array(testCase)
% Ensure that the function outputs the same array if we enter an array
import af_tools.utils.*
% If input is an array, output should be the same array
array = rand(5);
verifyEqual(testCase, vecttocol(array), array)
verifyEqual(testCase, af_tools.utils.vecttocol(array), array)
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment