Testing and Debugging


Example

Write a program to get an employee ID and a password from a user.

An ID is valid if it is a 7 digit number (and the first digit can't be 0).

A password is valid if it meets the following two criteria:

  1. it is at least 6 but no more than 10 character long
  2. the first character is a letter or an underscore

First draft of code:

done = 0;
id = input('Enter your employee ID: ');
while ~done
    if id < 1000000 || id > 9999999
        id = input('Invalid ID. Enter your ID: ');
    else
        done = 1;
    end
end

done = 0;
while ~done
    passwd = input('Enter your password: ', 's');
    if length(passwd) < 6 || length(passwd) > 10
        disp('invalid – bad length')
    elseif ~( isletter(passwd(1)) || passwd(1) == '_' )
        disp('invalid – must start with letter or _')
    else
        done = 1;
    end
end  

Testing

What user input should be used to have adequate test coverage?

 

 

 


Types of errors

Syntax errors

 

 

 

 

Semantic errors

 

 

 

Side note: warnings vs errors in MATLAB

 

 

 


Debugging

Using a debugger

See also screenshot of MATLAB's debugger in use