matlab.unittest.constraints.Tolerance - Fundamental interface for tolerances - MATLAB (original) (raw)
Namespace: matlab.unittest.constraints
Fundamental interface for tolerances
Description
The matlab.unittest.constraints.Tolerance
class provides an interface for tolerances. Tolerances define a notion of approximate equality for given data types and can be applied to the IsEqualTo constraint as well as certain comparators using the Within
name-value argument.
The class has three abstract methods. To create a custom tolerance class, derive your class from matlab.unittest.constraints.Tolerance
and implement all the abstract methods.
Methods
supports | tf = supports(tolerance,expected)Determine if the tolerance supports the expected value. This method specifies which data types the tolerance supports.Input Arguments tolerance — Tolerance, specified as amatlab.unittest.constraints.Tolerance object.expected — Expected value.Output Arguments tf — Whether the tolerance supports the expected value, returned as a logical 1 (true) or 0 (false). |
---|---|
satisfiedBy | tf = satisfiedBy(tolerance,actual,expected)Determine if the values are within the tolerance. This method provides the tolerance definition.Input Arguments tolerance — Tolerance, specified as amatlab.unittest.constraints.Tolerance object.actual — Value to test.expected — Expected value.Output Arguments tf — Whether the actual and expected values are within the tolerance, returned as a logical 1 (true) or 0 (false). |
getDiagnosticFor | diagnostic = getDiagnosticFor(tolerance,actual,expected)Produce diagnostic information for the values being compared. This information is incorporated into the diagnostics provided by the IsEqualTo constraint.Input Arguments tolerance — Tolerance, specified as amatlab.unittest.constraints.Tolerance object.actual — Value to test.expected — Expected value.Output Arguments diagnostic — Diagnostic information to display, returned as a matlab.automation.diagnostics.Diagnostic object.Depending on the test runner configuration, the testing framework can display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics. |
Examples
Determine if two DNA sequences have a Hamming distance within a specified tolerance. For two DNA sequences of the same length, the Hamming distance is the number of positions in which the nucleotides (letters) of one sequence differ from the other.
Create DNA
Class
To represent DNA sequences, create the DNA
class in a file named DNA.m
in your current folder.
classdef DNA properties (SetAccess=immutable) Sequence char {mustHaveValidLetters} end
methods
function dna = DNA(sequence)
dna.Sequence = sequence;
end
end
end
function mustHaveValidLetters(sequence) validLetters = ... sequence == 'A' | ... sequence == 'C' | ... sequence == 'T' | ... sequence == 'G';
if ~all(validLetters,"all") error("Sequence contains one or more invalid letters.") end end
Create HammingDistance
Class
In a file named HammingDistance.m
in your current folder, create the HammingDistance
class by subclassing matlab.unittest.constraints.Tolerance
. Add a property Value
so that you can specify the maximum allowable Hamming distance.
Classes that derive from the Tolerance
class must implement the supports
, satisfiedBy
, and getDiagnosticFor
methods:
supports
method — Specify that the tolerance must support objects of theDNA
class.satisfiedBy
method — Specify that for the actual and expected values to be within the tolerance, they must be the same size and their Hamming distance must be less than or equal to the tolerance value.getDiagosticFor
method — Create and return aStringDiagnostic
object that contains diagnostic information about the comparison.
classdef HammingDistance < matlab.unittest.constraints.Tolerance properties Value end
methods
function tolerance = HammingDistance(value)
tolerance.Value = value;
end
function tf = supports(~,expected)
tf = isa(expected,"DNA");
end
function tf = satisfiedBy(tolerance,actual,expected)
if ~isSameSize(actual.Sequence,expected.Sequence)
tf = false;
return
end
tf = hammingDistance(actual.Sequence,expected.Sequence) <= ...
tolerance.Value;
end
function diagnostic = getDiagnosticFor(tolerance,actual,expected)
import matlab.automation.diagnostics.StringDiagnostic
if ~isSameSize(actual.Sequence,expected.Sequence)
str = "The DNA sequences have different lengths.";
else
str = "The DNA sequences have a Hamming distance of " ...
+ hammingDistance(actual.Sequence,expected.Sequence) ...
+ "." + newline + "The allowable distance is " ...
+ tolerance.Value + ".";
end
diagnostic = StringDiagnostic(str);
end
end
end
function tf = isSameSize(str1,str2) tf = isequal(size(str1),size(str2)); end
function distance = hammingDistance(str1,str2) distance = nnz(str1 ~= str2); end
Compare DNA Sequences
To compare DNA sequences using a tolerance, first import the necessary classes and create a test case for interactive testing.
import matlab.unittest.TestCase import matlab.unittest.constraints.IsEqualTo testCase = TestCase.forInteractiveUse;
Create two DNA
objects and compare them without specifying a tolerance. The test fails because the objects are not equal.
sampleA = DNA("ACCTGAGTA"); sampleB = DNA("ACCACAGTA"); testCase.verifyThat(sampleA,IsEqualTo(sampleB))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> ObjectComparator failed. --> The objects are not equal using "isequaln".
Actual Value:
DNA with properties:
Sequence: 'ACCTGAGTA'
Expected Value:
DNA with properties:
Sequence: 'ACCACAGTA'
------------------
Stack Information:
------------------
In C:\work\CreateCustomToleranceExample.m (CreateCustomToleranceExample) at 45
Verify that the DNA sequences are equal within a Hamming distance of 1
. The test fails and the testing framework displays additional diagnostic information produced by the getDiagnosticFor
method.
testCase.verifyThat(sampleA,IsEqualTo(sampleB,"Within",HammingDistance(1)))
Verification failed. --------------------- Framework Diagnostic: --------------------- IsEqualTo failed. --> ObjectComparator failed. --> The objects are not equal using "isequaln". --> The DNA sequences have a Hamming distance of 2. The allowable distance is 1.
Actual Value:
DNA with properties:
Sequence: 'ACCTGAGTA'
Expected Value:
DNA with properties:
Sequence: 'ACCACAGTA'
------------------
Stack Information:
------------------
In C:\work\CreateCustomToleranceExample.m (CreateCustomToleranceExample) at 51
Verify that the DNA sequences are equal within a Hamming distance of 2
. The test passes.
testCase.verifyThat(sampleA,IsEqualTo(sampleB,"Within",HammingDistance(2)))
Version History
Introduced in R2013a