Transcript Testing
Testing
• Software errors are costly
• GIGO
• Preventing Errors
– Echo checking
– Range and limit checking
– Defensive programming
cosc175/testing
1
Testing
• Precondition
– assertion that must be true for an algorithm to
work correctly
• Postcondition
– assertion that should be true after the module
has executed
cosc175/testing
2
//******************************************************
float Cube( /* in */ float x )
// Precondition:
//
The absolute value of x cubed does not exceed the
//
machine's maximum float value
// Postcondition:
//
Function value == x cubed
{
return x * x * x;
}
cosc175/testing
3
Deskcheck
• Code walkthrough
• Trace
– Write variable names across top of page
– Trace each line of code and indicate new value
in variable
cosc175/testing
4
Trace
const int X = 5;
int main()
{
int a,b,c;
b = 1;
c = X + b;
a = X + 4;
a = c;
b = c;
a = a + b + c;
c = c % X;
c = c * a;
a = a % b;
cout << a << b << c;
return 0;
a
9
6
18
0
b
1
6
c
6
1
18
}
cosc175/testing
5
trace the following with the
values 90, 80, 40
Procedure PrintActivity( int temp )
Print "The recommended activity is ";
if (temp > 85)AND (temp < 95)
Print "swimming."
else if (temp > 70)
Print "tennis."
else if (temp > 32)
Print "golf."
else if (temp > 0)
Print "skiing."
else
Print "Chinese Checkers."
End PrintActivity
cosc175/testing
6
Testing strategy
•
•
•
•
Start with values with known result
Test all branches of conditionals
Check limits of loops
Logic errors - bug
cosc175/testing
7
Debugging
• Breakpoint
• Stepping
cosc175/testing
8
To start debugging
1.Click Start Debug on the Build
menu.
2.Click Go, Step Into, or Run to
Cursor.
cosc175/testing
9
To run the program and
execute the next statement
(Step Into)
1. While the program is paused in break mode
(program is waiting for user input after
completing a debugging command), click Step
Into from the Debug menu. The debugger
executes the next statement, then pauses
execution in break mode. If the next statement
is a function call, the debugger steps into that
function, then pauses execution at the
beginning of the function.
2. Repeat step 1 to continue executing the
program one statement at a time.
cosc175/testing
10
• Write a procedure that will input a y or n from the
user and repeatedly print an error message and
input another character until y or n is received.
• void GetYesOrNo(out char response )
Write a procedure that will input and validate an age
for an employee. Write the postcondition
• void InputAge(out integer age)
cosc175/testing
11
Use the following data sets:
Set 1: 10,3,8,0 Set 2: 0 Set 3:
20,20,0
// calculate the average age
While (age > 0)
Input age
total = total + age
numAges = numAges + 1
End while
Average = total + numAges
cosc175/testing
12
Example 1:
• Ulam's Conjecture: Start with any number, n. If n
is even, divide it by 2. If n is odd, multiply it by
three and add 1. Repeat this process as long as n
is greater than 1. Ulam's Conjecture is that n
always becomes 1 eventually. Write a loop that
tests this Conjecture for a specific value of n input
by the user. Print each number in the series and
at the end tell how many numbers were printed.
cosc175/testing
13
Get number.
count = 0.
While (number > 1)
If (number mod 2 = 0)
number = number / 2.
Else
number = number * 3 + 1.
endif
Print number.
count = count + 1
End while.
Print "count = " count
Print "the Conjecture held."
cosc175/testing
14
Example 2:Have the user enter 8 numbers from the
keyboard and print out the largest of the numbers.
Get number.
biggest = number.
count = 1
While count < 8
Get number.
If number > biggest
biggest = number.
count = count + 1
End while
cosc175/testing
Print biggest.
15
Exercises
1.
Perform a trace of example 1 using the following data
sets:
a. Set1: 10
b. Set2: 15
2. Perform a trace of example 2 from this weeks lecture
notes using the following data sets:
a. Set1: 5,10,15,20,25,30,35,0
b. Set2: 1,2,3,4,5,4,3,2
c. Set3: 0,0,0,0,5,5,5,5
cosc175/testing
16
Exercises
1.
2.
3.
4.
Write the pseudocode to perform the following: You go to the local
burger joint and order a burger, fries and a drink. You pay for your meal
with a $5 bill. The total cost of your meal is $X (X is in dollars and
cents - i.e., it's a float). Build a program that the cashier can use to help
him/her give you your change. The program should tell the cashier how
many half-dollars, quarters, dimes, nickels, and pennies (s)he should
give you.
Have the user enter 8 numbers from the keyboard and print out the
smallest and the largest of the numbers.
Build a predicate (a Boolean-valued function) that accepts a year. Its
value is TRUE if the year is a leap year and is FALSE otherwise. A leap
year is one that is evenly divisible by four, except for centuries - which
are usually not, unless they are also evenly divisible by 400. So, 1996 is
a leap year, 1900 is not a leap year, and 2000 is a leap year.
Build a function that tells the winner of the paper-rock-scissors game.
Paper covers rock; rock breaks scissors; scissors cuts paper. Your
function should accept the choices made by two players, and it should
return the winner - or Nobody, in case the two players make the same
selection.
cosc175/testing
17