Chapter 10 Testing and Debugging. Chapter Goals ► To learn how to carry out unit tests ► To understand the principles of test case selection and evaluation.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

FIT FIT1002 Computer Programming Unit 19 Testing and Debugging.
Testing & Debugging CSC 171 FALL 2004 LECTURE 13.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
Chapter 6 Iteration Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Classes, methods, and conditional statements We’re past the basics. These are the roots.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 5 Lecture 5-1: while Loops, Fencepost Loops, and Sentinel Loops reading: 4.1, 5.1.
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.
Chapter 8 Testing and Debugging Goals To learn how to carry out unit tests To understand the principles of test case selection and evaluation To learn.
16/27/2015 3:38 AM6/27/2015 3:38 AM6/27/2015 3:38 AMTesting and Debugging Testing The process of verifying the software performs to the specifications.
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
Chapter 6 Iteration.  Executes a block of code repeatedly  A condition controls how often the loop is executed while (condition) statement  Most commonly,
Chapter 6: Iteration Part 2. Create triangle pattern [] [][] [][][] [][][][] Loop through rows for (int i = 1; i
Iteration. Adding CDs to Vic Stack In many of the programs you write, you would like to have a CD on the stack before the program runs. To do this, you.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Week 5 - Wednesday.  What did we talk about last time?  Exam 1!  And before that?  Review!  And before that?  if and switch statements.
1 Principles of Computer Science I Prof. Nadeem Abdul Hamid CSC 120 – Fall 2005 Lecture Unit 10 - Testing.
Chapter 10 Testing and Debugging. Chapter Goals To learn how to carry out unit tests To understand the principles of test case selection and evaluation.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Chapter 10 – Testing and Debugging. Chapter Goals ► Learn techniques to test your code ► Learn to carry out unit tests ► Understand principles of test.
Fall 2006Adapted from Java Concepts Companion Slides1 Testing and Debugging Advanced Programming ICOM 4015 Lecture 9 Reading: Java Concepts Chapter 10.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. Review 2.
ICOM 4015 Fall 2008 Big Java by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved. ICOM 4015: Advanced Programming Lecture 6 Chapter.
The while Loop Syntax while (condition) { statements } As long condition is true, the statements in the while loop execute.
Problem of the Day  Why are manhole covers round?
Repetition & Loops. One of the BIG advantages of a computer: ­It can perform tasks over and over again, without getting bored or making mistakes (assuming.
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.
Neil Ghani Software testing. 2 Introduction In a perfect world all programs fully verified testing thus redundant Back in the real.
Problem of the Day  Why are manhole covers round?
COMP 121 Week 1: Testing and Debugging. Testing Program testing can be used to show the presence of bugs, but never to show their absence! ~ Edsger Dijkstra.
Chapter 5 Repetition and Loop Statements J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 6: Iteration Part 1. To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To understand.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
1 while loops. 2 Definite loops definite loop: A loop that executes a known number of times.  The for loops we have seen so far are definite loops. We.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. Chapter 6 – Iteration.
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Logic Our programs will have to make decisions on what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if and if-else.
Chapter 5: Control Structures II
Review, Pseudocode, Flow Charting, and Storyboarding.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Week 14 Introduction to Computer Science and Object-Oriented Programming COMP 111 George Basham.
CSC 1010 Programming for All Lecture 4 Loops Some material based on material from Marty Stepp, Instructor, University of Washington.
1 Chapter 8: Testing and Debugging  Chapter Goals To learn how to design test harnesses for testing components of your programs in isolationTo learn how.
Chapter 6 Iteration. Chapter Goals To be able to program loops with the while, for, and do statements To avoid infinite loops and off-by-one errors To.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Chapter 10 – Testing and Debugging. Goals Learn techniques to test your code Learn techniques to test your code Learn to carry out unit tests Learn to.
Loops, Part II IT108 George Mason University. Indefinite Loop Don’t always have access to the number of iterations ahead of time If a condition (user-response,
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Loops ( while and for ) CSE 1310 – Introduction to Computers and Programming Alexandra Stefan 1.
Information and Computer Sciences University of Hawaii, Manoa
Chapter Six: Iteration
Loop Structures.
Selected Topics From Chapter 6 Iteration
Chapter 10 Testing and Debugging
Repetition Chapter 6 12/06/16 & 12/07/16 1 1
Language Constructs Construct means to build or put together. Language constructs refers to those parts which make up a high level programming language.
CSE 1020:Software Development
Review for Midterm 3.
Corresponds with Chapter 5
Presentation transcript:

Chapter 10 Testing and Debugging

Chapter Goals ► To learn how to carry out unit tests ► To understand the principles of test case selection and evaluation ► To learn how to use logging ► To become familiar with the debugger ► To learn strategies for effective debugging

The First Bug

Unit Test  Test classes in isolation, outside the program in which they are used  Test one class at a time  Supply test inputs through test harness  Test harness = program that feeds test inputs to a class

Root Approximator ► Example program to illustrate testing: square root approximator Algorithm known to the ancient Greeks (Heron) ► Task: to compute the square root of a ► Given: a guess x (we start with x=a)  The actual square root lies between a/x and x

► Take midpoint (x + a/x) / 2 as a better guess Method converges rapidly. ► Square root of 100: Guess #1: 50.5 Guess #2: Guess #3: Guess #4: Guess #5: Guess #6: Guess #7: Guess #8: 10.0 All the other guesses from this point on, return the same value

File RootApproximator.java 1 /** 2 Computes approximations to the square root 3 of a number, using Heron's algorithm 4 */ 5 public class RootApproximator 6 { 7 /** 8 Constructs a root approximator for a given number aNumber the number from which to extract the square root 10 (Precondition: aNumber >= 0) 11 */ 12 public RootApproximator(double aNumber) 13 {a = aNumber; 15 xold = 1; 16 xnew = a; 17 }

18 19 /** 20 Compute a better guess from the current guess. the next guess 22 */ 23 public double nextGuess() 24 { xold = xnew; 26 if (xold != 0) 27 xnew = (xold + a / xold) / 2; 28 return xnew; 29 } 30

31 /** 32 Compute the root by repeatedly improving 33 the current guess until two successive guesses are approximately equal. the computed value for the square root 35 */ 36 public double getRoot() 37 { 38 while (!Numeric.approxEqual(xnew, xold)) 39 nextGuess(); 40 return xnew; 41 } private double a; // the number whose square root is computed 44 private double xnew; // the current guess 45 private double xold; // the old guess 46 }

File RootApproximatorTest.java 1 import javax.swing.JOptionPane; 2 /** This program prints ten approximations for a square root. 3 */ 4 public class RootApproximatorTest 5 { 6 public static void main(String[] args) 7 { 8 String input = JOptionPane.showInputDialog("Enter a number"); 9 double x = Double.parseDouble(input); 10 RootApproximator r = new RootApproximator(x); 11 final int MAX_TRIES = 10; 12 for (int tries = 1; tries <= MAX_TRIES; tries++) 13{ double y = r.nextGuess(); 14 System.out.println("Guess #" + tries + ": " + y); 15 } 16 System.exit(0); 17 } 18 }

Unit Test with BlueJ

File RootApproximatorTest2 1 import javax.swing.JOptionPane; 2 /** This program computes square roots of user-supplied inputs. 3 */ 4 public class RootApproximatorTest2 5 { public static void main(String[] args) 6 { 7 boolean done = false; 8 while (!done) 9 { String input = JOptionPane.showInputDialog("Enter a number, C to quit"); 10 if (input == null) 12 done = true; 13 else 14 { double x = Double.parseDouble(input); 16 RootApproximator r = new RootApproximator(x); 17 double y = r.getRoot(); 18 System.out.println("square root of " + x + " = " + y); 20 } 21 } 22 System.exit(0); 23 } 24 }

Sources of Test Data ► Provided by humans RootApproximatorTest2 ► Computer-generated sequence RootApproximatorTest3 ► Random sequence RootApproximatorTest4

File RootApproximatorTest3.java 1 /** This program computes square roots of input values 2 computer generated by a loop. */ 3 public class RootApproximatorTest3 4 { public static void main(String[] args) 5 { 6 final double MIN = 1; 7final double MAX = 10; 8 final double INCREMENT = 0.5; 9 for (double x = MIN; x <= MAX; x = x + INCREMENT) 10 { 11 RootApproximator r = new RootApproximator(x); 12 double y = r.getRoot(); 13 System.out.println("square root of " + x + " = " + y); 14 } } }

File RootApproximatorTest4.java 1 import java.util.Random; 2 /** This program computes square roots of random inputs. */ 3 public class RootApproximatorTest4 4 { public static void main(String[] args) 5 { final double SAMPLES = 100; 6 Random generator = new Random(); 7 for (int i = 1; i <= SAMPLES; i++) 8 { // generate random test value 9 double x = 1.0E6 * generator.nextDouble(); 10 RootApproximator r = new RootApproximator(x); 11 double y = r.getRoot(); 12 System.out.println("square root of " + x + " = " + y); 13 } 14 } 15 }

Test Cases  Positive test case: expect positive outcome E.g. square root of 100  Negative test case: expect negative outcome E.g. square root of 100  Boundary test case: at boundary of domain Frequent cause for errors E.g. square root of 0

Test Case Evaluation  Manual E.g. You know the result and you verify if the program has produced the correct result  Check property of result E.g. square root squared = original value  Oracle = slower way of computing answer E.g. square root of x = x 1/2

Regression Testing ► Save test cases Automate testing ► java Program test1.out java Program test2.out java Program test3.out ► Repeat test whenever program changes  Test suite = collection of test cases  Cycling = bug that is fixed but reappears in later version  Regression testing = testing against past failures

Test Coverage ► Black-box testing: test functionality without understanding internal structure ► White-box testing: take internal structure into account when designing tests ► Test coverage: the code that is actually executed during test cases ► Easy to overlook error branches during testing  Make sure to execute each branch in at least one test case

Program Trace  Output statements in your program for diagnostic purposes if (status == SINGLE) { System.out.println("status is SINGLE");... }...

Logging 1.Get global logger object: Logger logger = Logger.getLogger("global"); 2.Log a message logger.info("status is SINGLE"); 3.By default, logged messages are printed. Turn them off with logger.setLevel(Level.OFF);  Remember to import java.util.logging.Logger

The Debugger  Debugger = program to run your program, interrupt it, and inspect variables  Three key commands:  Set Breakpoint  Single Step  Inspect Variable  Two variations of Single Step  Step Over = don't enter method call  Step Inside = enter method call

The Debugger Stopping at a Breakpoint

Inspecting Variables

Sample Debugging Session  Word class counts syllables in a word  Each group of adjacent vowels (aeiouy) is a syllable  However, an e at the end of a word doesn't count  If algorithm gives count of 0, increment to 1  Constructor removes non-letters at beginning and end  Buggy output:  Syllables in hello: 1 Syllables in regal: 1 Syllables in real: 1

File Word.java 1 public class Word 2 { 3 /** Constructs a word by removing leading and trailing 4 non-letter characters, such as punctuation marks. s the input string */ 6 public Word(String s) 7 { int i = 0; 8 while (i < s.length() && !Character.isLetter(s.charAt(i))) 9 i++; 10 int j = s.length() - 1; 11 while (j > i && !Character.isLetter(s.charAt(j))) 12 j--; 13 text = s.substring(i, j + 1); 14 } 15

16 /** Returns the text of the word, after removal of 17 the leading and trailing non-letter characters. the text of the word */ 19 public String getText() 20 { return text; } /** Counts the syllables in the word. the syllable count */ 24 public int countSyllables() 25 { int count = 0; 26 int end = text.length() - 1; 27 if (end < 0) return 0; // the empty string has no syllables // An e at the end of the word doesn't count as a vowel 30 char ch = Character.toLowerCase(text.charAt(end)); 31 if (ch == 'e') end--;

32 boolean insideVowelGroup = false; 33 for (int i = 0; i <= end; i++) 34 { ch = Character.toLowerCase(text.charAt(i)); 35 if ("aeiouy".indexOf(ch) >= 0) 36 { // ch is a vowel 37 if (!insideVowelGroup) 38 { // start of new vowel group 39 count++; 40 insideVowelGroup = true; 41 } 42 } // Every word has at least one syllable 45 if (count == 0) 46 count = 1; 47 return count; 48 } 49 private String text; 50 }

File WordTest.java 1 import java.util.StringTokenizer; 2 import javax.swing.JOptionPane; 3 public class WordTest 4 { public static void main(String[] args) 5 { 6 String input = JOptionPane.showInputDialog("Enter a sentence"); 7 StringTokenizer tokenizer = new StringTokenizer(input); 8 while (tokenizer.hasMoreTokens()) 9 { 10 String token = tokenizer.nextToken(); 11 Word w = new Word(token); 12 int syllables = w.countSyllables(); 13 System.out.println("Syllables in " + w.getText() + ": " + syllables); 14 } 15 System.exit(0); 16 } }

Final Letter Test is Not Correct  Set breakpoint in line 35 (first line of countSyllables)  Start program, supply input hello  Method checks if final letter is 'e'  Run to line 41  Inspect variable ch  Should contain final letter but contains 'l'

Debugging the countSyllables Method

The Current Values of the Local and Instance Variables

More Problems Found  end is set to 3, not 4  text contains "hell", not "hello"  No wonder countSyllables returns 1  Culprit is elsewhere  Can't go back in time  Restart and set breakpoint in Word constructor

Debugging the Word Constructor  Supply "hello" input again  Break past the end of second loop in constructor  Inspect i and j  They are 0 and 4--makes sense since the input consists of letters  Why is text set to "hell"?  Off-by-one error: Second parameter of substring is the first position not to include  text = substring(i, j); should be text = substring(i, j + 1);

Debugging the Word Constructor

Another Error  Fix the error  Recompile  Test again:  Syllables in hello: 1 Syllables in regal: 1 Syllables in real: 1  Oh no, it's still not right  Start debugger  Erase all old breakpoints  Supply input "regal"

Debugging countSyllables (again)  Break in the beginning of countSyllables  Single-step through loop  First iteration ('r'): skips test for vowel  Second iteration ('e'): passes test, increments count  Third iteration ('g'): skips test  Fourth iteration ('a'): passes test, but doesn't increment count  insideVowelGroup was never reset to false  Fix and retest: All test cases pass  Is the program now bug-free? The debugger can't answer that.