Download presentation
Presentation is loading. Please wait.
Published byCameron Burke Modified over 9 years ago
1
cosc175/testing1 Testing Software errors are costly GIGO Preventing Errors –Echo checking –Range and limit checking –Defensive programming
2
cosc175/testing2 Testing Precondition – assertion that must be true for an algorithm to work correctly Postcondition – assertion that should be true after the module has executed
3
cosc175/testing3 //****************************************************** 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; }
4
cosc175/testing4 Deskcheck Code walkthrough Trace –Write variable names across top of page –Trace each line of code and indicate new value in variable
5
cosc175/testing5 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; } abc --- 916 661 18 0
6
cosc175/testing6 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
7
cosc175/testing7 Testing strategy Start with values with known result Test all branches of conditionals Check limits of loops Logic errors - bug
8
cosc175/testing8 Debugging Breakpoint Stepping
9
cosc175/testing9 To start debugging 1.Click Start Debug on the Build menu. 2.Click Go, Step Into, or Run to Cursor.
10
cosc175/testing10 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.
11
cosc175/testing11 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 )
12
cosc175/testing12 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
13
cosc175/testing13 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.
14
cosc175/testing14 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."
15
cosc175/testing15 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 Print biggest.
16
cosc175/testing16 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
17
cosc175/testing17 Exercises 1.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. 2.Have the user enter 8 numbers from the keyboard and print out the smallest and the largest of the numbers. 3.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. 4.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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.