VerifyEqual within a time interval

Hello, I have been assigned a series of tests to be completed on several floating point variables. The assignment requires me to check whether the Expected value is the same as the Actual value. However, it may take a few milliseconds for the Actual value to reach the same status as the Expected value, and the verifyEqual function does not seem to consider this scenario.
I'd like to know if there is some sort of workaround for this situation. Basically, if A is the Actual value and E the Expected value, I'd like to modify (somehow) the verifyEqual function so that it returns a passing test if A == E with a time delay of approx. 3 milliseconds.
Thank you for your cooperation.

5 Comments

Hi @Marco Montanaro,

Here's an example script that demonstrates how to implement this custom verification delay in Matlab:

function customVerifyEqual(A, E)

    % Set the time delay in milliseconds
    delayTime = 3; % 3 milliseconds
    % Check if A is equal to E after a time delay
    tic; % Start timer
    while toc * 1000 < delayTime
        if isequal(A, E)
            disp('Test Passed: A is equal to E within the time delay.');
            return;
        end
    end
    % If A is not equal to E within the time delay
    error('Test Failed: A is not equal to E within the time delay.');

end

% Example usage

A = 10.5; % Actual value

E = 10.5; % Expected value

% Call the custom verification function

customVerifyEqual(A, E);

So, in this script, the verifyfunction takes two input arguments A (Actual value) and E (Expected value) and compares the values and wait for the specified time delay using a while loop and the tic and toc functions. If the values match within the time delay, it displays a message indicating the test passed and if they do not match within the time delay, it throws an error indicating the test failed. You can adjust the delayTime variable to set the desired time delay in milliseconds according to your requirements. Hope, this answers your question. Please let me know if you have any further questions. Please see attached results.

Thank you so much for your help, you're a savior if I've ever seen one
@Marco Montanaro,
No problem, glad to help out. If this answers your question, please hit accept answer. If you have any further questions, please feel free to reach out.
But you did not answer the question but instead made a comment @Umar :P
@Aquatris, it’s weird that when I hit Answer this question button on my cell phone, it defaults to form and also layout of form is old version plus I am a poor guy who can’t afford all fancy things like Matlab toolbox. Just going through some hardships but my main goal is to make difference in people lives just like Jesus did. Sorry, for getting exaggerating too much.

Sign in to comment.

Answers (1)

Yes, you can do this in the MATLAB unit testing framework. I assume the context is something along the lines of either A or E being a property of an object or the result of a function call. In that case, use verifyThat and the Eventually constraint to perform your testing using another constraint (in the case you described probably IsEqualTo, though for this example I'm going to use IsGreaterThan.) Give verifyThat a function handle it can evaluate repeatedly to poll whether or not the constraint you use inside Eventually is satisfied.
In this case, I want to check that after calling tic that the toc function will eventually return a value greater than 5, indicating that it's been 5 seconds. For this example that does happen after 5 seconds.
% Setup phase -- create the test object and import the constraints
testcase = matlab.unittest.TestCase.forInteractiveUse;
import matlab.unittest.constraints.Eventually
import matlab.unittest.constraints.IsGreaterThan
% Start the exercise phase
tic
% Verify that the toc function eventually returns a value greater than 5
verifyThat(testcase, @() toc, Eventually(IsGreaterThan(5)))
Verification passed.
toc % Should be a little over 5 seconds
Elapsed time is 5.143284 seconds.
% No teardown required
If I specify a timeout in the Eventually constraint and the condition isn't satisfied by the time that timeout is reached, the verifyThat verification will fail.
tic
verifyThat(testcase, @() toc, Eventually(IsGreaterThan(5), "WithTimeoutOf", 4))
Verification failed. --------------------- Framework Diagnostic: --------------------- Eventually failed. --> The constraint never passed with a timeout of 4 second(s). --> IsGreaterThan failed. --> The value must be greater than the minimum value. Actual Value: 4.009260000000000 Minimum Value (Exclusive): 5 Evaluated Function: function_handle with value: @()toc ------------------ Stack Information: ------------------ In /tmp/Editor_zhfgp/LiveEditorEvaluationHelperEeditorId.m (LiveEditorEvaluationHelperEeditorId) at 12 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalMatlab.p (fevalMatlab) at 0 In /MATLAB/toolbox/matlab/connector2/interpreter/+connector/+internal/fevalJSON.p (fevalJSON) at 0
In this case, I wanted to see if toc said that more than 5 seconds had elapsed since the tic call, but I only waited for 4 seconds. The default timeout is 20 seconds which is why the first verifyThat test passed.

Products

Release

R2024a

Asked:

on 1 Aug 2024

Commented:

on 1 Aug 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!