Title: CSE1301 Computer Programming: Lecture 12 Debugging and Testing
1CSE1301 Computer Programming Lecture
12Debugging and Testing
2Topics
- How to find errors in your program?
- Methods for debugging
- Methods for testing
3Debugging Basics
- Know the (C) language well.
Examples
float x, y, z 3.5
printf(d\n, num)
scanf(f, x)
scanf(s, name)
if (i lt N) scanf(d\n, i)
4Debugging Basics (cont.)
- Pay attention to compiler error and warning
messages.
Examples
Possible incorrect assignment.
if (ch Q) break
Possible use of N before declaration.
scanf(d, N)
5Tracing
- Trace execution of program
- location in the program
- status/contents of variables
- Tools
- programming environment
- eg. step, breakpoints, watch
- debugging statements
- eg. output values of variables, markers at
specific locations, etc.
6Example Debugging Statements
int debugging 1 ... for (i0 iltN
i) scanf(s, name) if (debugging)
printf("for id, names\n", i,
name)
TIP make debugging statements conditional on a
boolean variable.
7Methods for Testing
- Test data set should fully test program.
- Every line of code is executed at least once
(i.e., all logical paths of program are
traversed). - Use the design, represented by flow diagram.
TIP build your programs incrementally, testing
small components as you go along.
8Example BestMark (Alg 9)
- module getNextMark
-
- input nextMark
- return nextMark
-
9Classes of Test data
- Valid data
- Valid boundary data
- Special or unusual cases
- Invalid data
10Test Data Valid Data
- What would valid data be?
- What does it depend on?
- Example BestMark
- What the test is out of?
- If mark is out of 100 valid test data
- 75, 65, 55.
11Test Data Valid Boundary Data
- What are the extremes?
- Example BestMark
- minimum of 0
- maximum of 100
- Test selection conditions
- Test iteration exit conditions
- Test first and last elements of an array (lecture
23)
12Test Data Special Cases
- Example BestMark
- What if someone is absent or the mark is withheld
(special consideration)?
loop input nextMark if nextMark is Abs''
or WH'' output No mark for this
student'' else return nextMark
13Test Data Invalid Data
- What is invalid data?
- Incorrect type of data.
- Data not in expected range.
- Use features of the programming language to
ensure correct data type. - Example BestMark
- mark can be restricted to an integer
- int mark
- scanf(d, mark)
14Test Data Invalid Data (cont.)
- loop
-
- input nextMark
- if nextMark is Abs'' or WH''
-
- output No mark for this student''
-
- else if nextMark lt 0 or nextMark gt 100
-
- output invalid mark not 0 - 100''
-
- else
- return nextMark
-
15Example
- int x,y
- scanf("d d", x, y)
- if (x gt 2)
- while (x gt y)
- printf("S1,")
- x--
-
- printf("S2,")
-
- else if (x lt y)
-
- printf("S3,")
-
- printf("S4")
16Example Valid Data
- What lines of code indicates what is valid data?
- int x
- int y
- scanf("d d\n", x, y)
- Valid data is any integer. Positive, negative,
or zero.
17Test data for all logical paths
- int x,y
- scanf("d d", x, y)
- if (x gt 2)
- while (x gt y)
- printf("S1,")
- x--
-
- printf("S2,")
-
- else if (x lt y)
-
- printf("S3,")
-
- printf("S4")
- What is done for every input?
- What does this say about the output?
- S4 must be output at the end every time.
18Test data for all logical paths
- int x,y
- scanf("d d", x, y)
- if (x gt 2)
- while (x gt y)
- printf("S1,")
- x--
-
- printf("S2,")
-
- else if (x lt y)
-
- printf("S3,")
-
- printf("S4")
19Test data for all logical paths
TO DO Test all paths from Input to Output S4
20Choice Points
Input x,y
xgt2?
NO
NO
xlty
xgty?
Paths are determined by choice points.
YES
YES
output S3
output S1
output S2
decrement x
output S4
21Choice Points
- int x,y
- scanf("d d", x, y)
- if (x gt 2)
- while (x gt y)
- printf("S1,")
- x--
-
- printf("S2,")
-
- else if (x lt y)
-
- printf("S3,")
-
- printf("S4")
- What are the highest level choice points?
22Choice Points
Input x,y
- What are the highest level choice points?
xgt2?
NO
NO
xlty
xgty?
YES
YES
output S3
output S1
output S2
decrement x
output S4
23Choice Points
Input x,y
Test data Case 1 NOT (xgt2), NOT (xlty)
xgt2?
Specific Values x2, y 2
NO
NO
xlty
xgty?
Output S4
YES
YES
output S3
output S1
output S2
decrement x
output S4
24Choice Points
Input x,y
Test data Case 2 NOT (xgt2), xlty
xgt2?
Specific Values x2, y 3
NO
NO
xlty
xgty?
Output S3, S4
YES
YES
output S3
output S1
output S2
decrement x
output S4
25Choice Points
Input x,y
- int x,y
- scanf("d d", x, y)
- if (x gt 2)
- while (x gt y)
- printf("S1,")
- x--
-
- printf("S2,")
-
- else if (x lt y)
-
- printf("S3,")
-
- printf("S4")
xgt2?
NO
NO
xlty
xgty?
YES
YES
output S3
output S1
output S2
decrement x
output S4
26Choice Points
Input x,y
Test data Case 3 (Loop body not executed) x gt
2, NOT(x gt y)
xgt2?
NO
NO
xlty
xgty?
Specific Values x3, y 4
YES
YES
Output S2, S4
output S3
output S1
output S2
decrement x
output S4
27Choice Points
Input x,y
Test data Case 4 (Loop body executed) x gt 2, x
gt y
xgt2?
NO
NO
xlty
xgty?
YES
Specific Values x5, y 4
YES
output S3
output S1
Output S1, S2, S4
output S2
decrement x
output S4
28Notes on Loop Tests
- Is it possible that the loop never terminates?
- only if algorithm is incorrect
- Example
- x gt y, AND decrementing x each step.
29Example Change in Algorithm
- Provide a set of test data
- valid
- valid boundary
- invalid
/ Step 1 / while (x gt 0) / Step 2 /
if (y 2) / Step 2a /
else / Step 2b / / Step 3
/ / Step 4 /
- How would you ensure that the loop will always
terminate?
30Summary
- Testing is an important part of the software
development process. - Considering all the test data cases can lead to
change in the algorithm. - Flow Diagrams can be used to design the test data
set.