FIT1002 2006 1 FIT1002 Computer Programming Unit 19 Testing and Debugging.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Java Programming, 3e Concepts and Techniques Chapter 4 Decision Making and Repetition with Reusable Objects.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
1 CSE1301 Computer Programming: Lecture 15 Flowcharts and Debugging.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Loops Repeat after me …. Loops A loop is a control structure in which a statement or set of statements execute repeatedly How many times the statements.
FIT Objectives By the end of this lecture, students should: understand iteration statements understand the differences of for and while understand.
16/27/ :53 PM6/27/ :53 PM6/27/ :53 PMLogic Control Structures Arithmetic Expressions Used to do arithmetic. Operations consist of +,
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Testing an individual module
1 CSE1301 Computer Programming: Lecture 15 Flowcharts, Testing and Debugging.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 These courseware materials are to be used in conjunction with Software Engineering: A Practitioner’s Approach, 5/e and are provided with permission by.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Debugging Logic Errors CPS120 Introduction to Computer Science Lecture 6.
11 Chapter 4 LOOPS AND FILES. 22 THE INCREMENT AND DECREMENT OPERATORS To increment a variable means to increase its value by one. To decrement a variable.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
System/Software Testing
CS 501: Software Engineering Fall 1999 Lecture 16 Verification and Validation.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
1 Debugging and Testing Overview Defensive Programming The goal is to prevent failures Debugging The goal is to find cause of failures and fix it Testing.
Input, Output, and Processing
Agenda Introduction Overview of White-box testing Basis path testing
Debugging in Java. Common Bugs Compilation or syntactical errors are the first that you will encounter and the easiest to debug They are usually the result.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Testing. 2 Overview Testing and debugging are important activities in software development. Techniques and tools are introduced. Material borrowed here.
Flow of Control Part 1: Selection
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Fundamentals of Algorithms MCS - 2 Lecture # 5. Representation of Algorithms (continued) Flowcharts.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Flow of Control: Loops Module 4. Objectives Design a loop Use while, do, and for in a program Use the for-each with enumerations Use assertion checks.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Introduction to Computing Systems and Programming Programming.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 CSE1301 Computer Programming: Lecture 16 Flow Diagrams and Debugging.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
SOFTWARE TESTING LECTURE 9. OBSERVATIONS ABOUT TESTING “ Testing is the process of executing a program with the intention of finding errors. ” – Myers.
Debugging and Testing Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Repetition Statements
Testing Tutorial 7.
Software Testing.
CS1101X Programming Methodology
Testing and Debugging.
Loop Structures.
Lecture 2 Introduction to Programming
Numbering System TODAY AND TOMORROW 11th Edition
Java Software Structures: John Lewis & Joseph Chase
CSI 121 Structure Programming Language Lecture 10: Iteration (Part 1)
Outline Altering flow of control Boolean expressions
Software Testing (Lecture 11-a)
Chapter 2- Visual Basic Schneider
Assist.Prof.Dr. Nükhet ÖZBEK Ege University
CSE 1020:Software Development
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Review of Previous Lesson
CHAPTER 6 Testing and Debugging.
Defensive Programming
Presentation transcript:

FIT FIT1002 Computer Programming Unit 19 Testing and Debugging

FIT Objectives By the end of this lecture, students should: understand how to perform basic testing of a program understand how to apply a flow chart to determine test cases understand the concept of interactive source-level debugging design a simple test strategy use the debugger in BlueJ

FIT Software Development Process  Define the problem clearly  Analyze the problem  Design an algorithm design top-down / implement bottom-up  Document the system  Code (Implement) the algorithm  Test the code

FIT Development Cycle AnalysisDesignImplementTest debugging and testing

FIT Debugging and Testing Debugging: the process of finding and correcting errors (a.k.a “bugs”) Testing: executing the program on a test data set

FIT Types of Errors syntactic: how instructions are written semantic: what they represent while (i=0; i < 5; i++) { j += i; } Example 1: n=0; …; n=6; … for (i=n; i != 5; i++) { System.out.println(i); } Example 2: if (choice == ‘Q’) { throw new Error(); return 0; } Example 3:

FIT Example: BestMark Problem: Write a program which reads a list of marks, and prints out the best mark Example: Input: Output: Best mark is 96

FIT Example: BestMark (cont) set bestMark to 0 loop { input mark if (end of input) then exit loop if (mark > bestMark) then { set bestMark to mark } output “Best mark is “, bestMark Algorithm

FIT Classes of Test Data 1. Valid data 2. Valid boundary data 3. Special or unusual cases 4. Invalid data

FIT Test Data: Valid Data Reasonable data for the problem Example: BestMark What is the test out of? If mark is out of 100, valid test data is 75, 65, 55

FIT Test Data: Valid Boundary Data Data with extreme values Example: BestMark minimum of 0 maximum of 100 Particularly use test data to: – Test iteration exit conditions – Test first and last elements of an array

FIT Test Data: Special Cases Example: BestMark What if someone is absent or the mark is withheld (special consideration)? input markEntered if (markEntered is “Abs” or “WH”) { output “No mark for this student” set mark to 0 } else { set mark to numerical value of markEntered }

FIT Test Data: Invalid Data Invalid data is of an incorrect type/class, or outside the expected range or not adhering to some other constraint on the data (eg, a String for a name containing special characters) Use data types in your program as restrictively as possible to make sure that such problems are automatically avoided. For example, it is much better to code the mark as an object with an instance variable of type int and a boolean field “withheld” instead of using a string to code a mark that could potentially be a “WH”.

FIT Testing Techniques Test data set should “fully” test the program All logical paths of the program should be traversed (i.e., every line of code should be executed at least once) Use the design represented by the flowchart TIP: build your programs incrementally, testing small components as you go along

FIT Flowcharts Represent flow of control of algorithms: sequences selection iteration Useful for: Finding semantic errors Determining test data set

FIT Flowchart: Sequence Step A: input number Step B: add 1 to number Step C: output number Instruction in rectangular box Order of execution indicated by arrows Represented by concatenating instructions (usually vertically)

FIT Step A: input number Step B: if number is negative, then add -1 to number else add 1 to number Step C: output number Example 2: an algorithm involving “selection” Step A: input number Step C: output number Sequence (cont)

FIT Flowchart: Selection Step A if ( condition C1 ) { } else { } Step C Step A C1 true? S2 Step C S1 YES NO Arrow labeled with result of condition test Condition test in diamond

FIT input number if number is negative, then add -1 to number else add 1 to number output number input number is number negative? add -1 to number YES NO add 1 to number output number Example: Algorithm to Flowchart

FIT Flowchart: Iteration (while loop) while ( condition C1 ) { } C1 true? S1 YES NO

FIT Flowchart: Iteration (for loop) for ( init ; condition C1 ; increment ) { } has to be treated like a while loop… C1 true? S1 YES NO init increment

FIT Example: Code to Flowchart (Spot the error!) for ( i=0; i<10; i++ ) { x=console.nextInt(); if ( x < 0 ) { break; } is i < 10 ? input value for x YES NO set i to 0 increment i is x < 0 ? NOYES

FIT Example: Code to Flowchart (correct version) for ( i=0; i<10; i++ ) { x=console.nextInt(); if ( x < 0 ) { break; } is i < 10 ? input value for x YES NO set i to 0 increment i is x < 0 ? NO YES

FIT Algorithm to Flowchart Example: Calculating Mean input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count input value for totalNumbers set sum to 0 set count to 0

FIT is count< totalNumbers? Algorithm to Flowchart Example: Calculating Mean input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count input value for nextNum YES NO increment count add nextNum to sum

FIT Algorithm to Flowchart Example: Calculating Mean input totalNumbers set sum to 0 set count to 0 while (count < totalNumbers) { input nextNum add nextNum to sum add 1 to count } output “Sum was” sum output “Mean was” sum/count output value for sum output value for sum/count

FIT Testing All Execution Paths int x; int y; x=console.nextInt(); y=console.nextInt(); if (x > 2) { while (x > y) { System.out.println("S1,"); x--; } System.out.println("S2,"); } else if (x < y) { System.out.println("S3,"); } System.out.println("S4"); x>2? Input x,y output S2 x<y? output S4 output S3 x>y? output S1 decrement x

FIT Example (cont) YESNO x>2? Input x,y int x; int y; x=console.nextInt(); y=console.nextInt(); if (x > 2) { while (x > y) { System.out.println("S1,"); x--; } System.out.println("S2,"); } else if (x < y) { System.out.println("S3,"); } System.out.println("S4");

FIT Example (cont) int x; int y; x=console.nextInt(); y=console.nextInt(); if (x > 2) { while (x > y) { System.out.println("S1,"); x--; } System.out.println("S2,"); } else if (x < y) { System.out.println("S3,"); } System.out.println("S4"); x>2? Input x,y x>y? output S1 decrement x YES NO YES

FIT Example (cont) int x; int y; x=console.nextInt(); y=console.nextInt(); if (x > 2) { while (x > y) { System.out.println("S1,"); x--; } System.out.println("S2,"); } else if (x < y) { System.out.println("S3,"); } System.out.println("S4"); YES NO x>2? Input x,y NO YES NO YES x>y? output S1 decrement x x<y? output S3

FIT YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x Example (cont) int x; int y; x=console.nextInt(); y=console.nextInt(); if (x > 2) { while (x > y) { System.out.println("S1,"); x--; } System.out.println("S2,"); } else if (x < y) { System.out.println("S3,"); } System.out.println("S4");

FIT Testing all logical execution paths What is done for every input? What does this say about the output? S4 must be output at the end every time int x; int y; x=console.nextInt(); y=console.nextInt(); if (x > 2) { while (x > y) { System.out.println("S1,"); x--; } System.out.println("S2,"); } else if (x < y) { System.out.println("S3,"); } System.out.println("S4");

FIT Example (cont): Choice Points YES NO x>2? Input x,y NO output S2 x<y? output S4 YES output S3 NO YES x>y? output S1 decrement x Paths are determined by choice points

FIT Test data for all logical paths YES NO x>2? Input x,y NO output S2 x<y? output S4 output S3 NO YES x>y? output S1 decrement x xy

FIT Debugging Testing is the process of checking whether the program behaves as expected. If it does not behave as expected we need to find the mistakes and remove them. This process is called debugging. The fundamental process of debugging is to make assumptions about what the state of the program should be at some intermediate point and to verify whether this is the case. This allows us to narrow down to the point where the problem originates.

FIT Example: Debugging Statements... for (i=n; i!=2*n; i+=k) { System.out.println(a[i]); } If this loop does not terminate correctly, we need to verify that i really reaches the value 2n, i.e. that n is divisible by k (assumption). Approach 1: Embed printing statements to check this

FIT Example: Debugging Statements (cont) TIP: make debugging statements conditional on a boolean variable … final boolean debugging = true; … if (debugging) System.out.println(“n=“+n+” k=“+k); for (i=n; i!=2*n; i+=k) { System.out.println(a[i]); if (debugging) System.out.println(“i=“+i+” k=“+k); }

FIT Assertions (advanced, optional) … assert n%k==0; … Java has a special mechanism to verify assumptions like “n is divisible by k” above called “assertions”. you “assert” a boolean condition Java checks this condition every time the assertion is encountered. If the condition is true nothing happens if it is false, an exception is thrown (and the program is terminated) You need to run your program from the command line with assertions enabled to use this checking mechanism: java -cp. -enableassertions MyProg

FIT Typical Bugs 1. Array index out of bounds (Array out of bounds exception) 2. Object variables and arrays unititalized (null pointer exception) 3. Infinite loops (termination condition wrong) 4. Incorrect boolean expressions (operator precedences) 5. Incorrect nesting of selection (dangling else)

FIT Source-Level Debugging Luckily modern interactive development environments offer much more comfortable means of debugging. In particular, they can indicate the current location of the execution in the source code. This is called source-level debugging. Details differ between IDEs. We will demonstrate source- level debugging in BlueJ interactively in the tutorials. Details of the BlueJ debugger can be found in the BlueJ manuals.

FIT Tracing The process of executing a program step by step and checking its state is called tracing. The debugger –indicates current execution point in the source code –allows to inspect current contents of variables –allows to inspect current state of objects You can set “breakpoints” on source-code statements. The trace can be performed –“stepping” from statement to statement or – “skipping” from breakpoint to breakpoint

FIT BlueJ Debugger