# Testing and Solving a Matlab Exam Solution

I was telling one group about how to make it easy for them self when solving an exam in Introductory Programming with Matlab, then the person next to us also wanted to know. Therefor, here is the answer.

The trial exam that all the students have been trying out is all about creating an image toolbox. This means some functions that takes an image, some parameters and returns an image. They are asked to create a driver script to test their toolbox, this have two meanings. To make them show that the toolbox works, and to show that they can handle user inputs to decide what functions to run.

```%% An outline of the driver script.

clear all;
close all;
clc;

%We want two figures such we can show original and new image.
original = figure(1);
newImage = figure(2);
%We can now swap between the figures with figure(original) and figure(newImage).

% Output some welcome text.

% Ends with an imread. (notice how you can get the colormap also('gif' images)).
[X Xcmap] = imread(....) %But our code only works with jpeg images!!!!

figure(original);
image(X), colormap(Xcmap)

% Now ask the user what he want to do.
% Create some logic that execute the function and gets the parameters
% from the user.
% Show the image.

switch(userOption)

case 5; % Lets say this is the frame function

%Handle this with proper statement.
inOrOut = input('Want to append or replace the image with a frame ...');
thickness = input('How thick?');
%ask for the rest of the stuff.

if(inOrOut)
Y = frame(X , ...) % Put in the rest of the code.
end

% Its assume that no matter what function the user picked, the new value is assigned to Y.
% You handle this assumtion by coding it properly.

figure(newImage); %Switch figure window.
image(newImage); % Done, or wrap with a loop for all of it such you can apply multiply functions.

```

At this point, when you are all new to programming, you properly never get your code correct in the first place. What I suggested the students last week was to not run this code every time they wanted to test if it works. Mainly because they have to type in all the information every time, like image path, function type and parameters.

Alternative, for every input replace them with an assigment statement.

```%thickness = input('Give Thinkness'); % becomes
thickness = 20; % And delete this line when you are positive that your function work.
```

However, what if you then forget to swap them back before you turn in? Instead, you should create a new test script TestFrame.m. In this file, you create the code that tests the frame function. Hardcoding all the parameters such it is fast to run. You possible also can set the parameters and run frame, then reset the parameters and run frame again and so forth. You now have N tests in one file. If you set these tests up to comply with what the assignment asks for, then you cannot fail.

```% Example TestFrame.m
Thickness = 10;
Color = [255, 0 0]
iOrOut = 1; %Inside
Y = frame(X,Thickness,iOrOut,Color);
disp('Does it look okay, press enter');
pause
Y = frame(X,Thickness,0,Color); % I have now tested both inside or outside frame.
Y = frame(X,Thickness,0);
Y = frame(X,Thickness,1); % I have now tested with optional color paramter.
```

Notice how I created four tests here to test every possible configuration. When this script runs I am, sure that my function works and if you document these tests in your reports, you surely would impress the TA's (But it also make you confident that you did a good job). You also have the option to extend the test. Many of you in your first go manage to add an 11-width border when you was supposed to do a 10-width border. How could you test this?.

```% Test for border width.
% Testing the right side of the image.

% Make sure that the end, lets say 100, - 10 = 90th column in the 50th row is not colored red.
assert(~(Y(50,end-10,1) = 255 && Y(10,end-10,2) == 0 && Y(10,end-10,3) == 0));

```

Off cause this test is very bold, what if the image had a red color at that position. If it is a natural image, the chance is low, but what if. Then you design your tests better, this is to show you how. `assert` will write a line out in red if the statement do not hold.

Hope you can use it.