MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Create Basic Parameterized Test


This example shows how to create a basic parameterized test.

Create Function to Test

In your working folder, create a function in the file sierpinski.m. This function returns
a matrix representing an image of a Sierpinski carpet fractal. It takes as input the fractal
level and an optional data type.

function carpet = sierpinski(nLevels,classname)
if nargin == 1
classname = 'single';
end

mSize = 3^nLevels;
carpet = ones(mSize,classname);

cutCarpet(1,1,mSize,nLevels) % begin recursion

function cutCarpet(x,y,s,cL)
if cL
ss = s/3; % define subsize
for lx = 0:2
for ly = 0:2
if lx == 1 && ly == 1
% remove center square
carpet(x+ss:x+2*ss-1,y+ss:y+2*ss-1) = 0;
else
% recurse
cutCarpet(x + lx*ss, y + ly*ss, ss, cL-1)
end
end
end
end
end
end

Create TestCarpet Test Class

In a file in your working folder, create a new class, TestCarpet, to test the sierpinski
function.

classdef TestCarpet < matlab.unittest.TestCase

33 Unit Testing

Free download pdf