CSE1301 Computer Programming: Lecture 12 Debugging and Testing - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CSE1301 Computer Programming: Lecture 12 Debugging and Testing

Description:

TIP: make debugging statements conditional on a boolean variable. Methods ... TIP: build your programs incrementally, testing small components as you go along. ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 31
Provided by: AnnNichols
Category:

less

Transcript and Presenter's Notes

Title: CSE1301 Computer Programming: Lecture 12 Debugging and Testing


1
CSE1301 Computer Programming Lecture
12Debugging and Testing
2
Topics
  • How to find errors in your program?
  • Methods for debugging
  • Methods for testing

3
Debugging 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)
4
Debugging 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)
5
Tracing
  • 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.

6
Example 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.
7
Methods 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.
8
Example BestMark (Alg 9)
  • module getNextMark
  • input nextMark
  • return nextMark

9
Classes of Test data
  • Valid data
  • Valid boundary data
  • Special or unusual cases
  • Invalid data

10
Test 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.

11
Test 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)

12
Test 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
13
Test 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)

14
Test 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

15
Example
  • 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")

16
Example 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.

17
Test 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.

18
Test 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")

19
Test data for all logical paths
TO DO Test all paths from Input to Output S4
20
Choice 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
21
Choice 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?

22
Choice 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
23
Choice 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
24
Choice 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
25
Choice 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
26
Choice 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
27
Choice 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
28
Notes 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.

29
Example 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?

30
Summary
  • 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.
Write a Comment
User Comments (0)
About PowerShow.com