MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
that returns a StringDiagnostic. Tolerance classes must implement a
getDiagosticFor method.

methods
function diag = getDiagnosticFor(tolerance, actual, expected)
import matlab.unittest.diagnostics.StringDiagnostic

if ~isSameSize(actual.Sequence, expected.Sequence)
str = 'The DNA sequences must be the same length.';
else
str = sprintf('%s%d.\n%s%d.', ...
'The DNA sequences have a Hamming distance of ', ...
hammingDistance(actual.Sequence, expected.Sequence), ...
'The allowable distance is ', ...
tolerance.Value);
end
diag = StringDiagnostic(str);
end
end

HammingDistance Class Definition Summary

classdef HammingDistance < matlab.unittest.constraints.Tolerance
properties
Value
end

methods
function tolerance = HammingDistance(value)
tolerance.Value = value;
end

function tf = supports(~, value)
tf = isa(value, '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 diag = getDiagnosticFor(tolerance, actual, expected)

33 Unit Testing

Free download pdf