©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging.

Slides:



Advertisements
Similar presentations
Testing & Debugging CSC 171 FALL 2004 LECTURE 13.
Advertisements

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 6: Iteration 1 Chapter 6 Iteration.
1 CS2200 Software Development Lecture: Testing and Design A. O’Riordan, 2008 K. Brown,
1 CS2200 Software Development Lecture 27: More Testing A. O’Riordan, 2008 K. Brown,
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
1 Repetition structures Overview while statement for statement do while statement.
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
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.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
Relational Operators Control structures Decisions using “if” statements  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course.
Loops Chapter 4. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while” structures.
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 7: More about Methods 1 Chapter 7 More about Methods.
16-Aug-15 Java Puzzlers From the book Java Puzzlers by Joshua Bloch and Neal Gafter.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
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.
Testing CSE 140 University of Washington 1. Testing Programming to analyze data is powerful It’s useless if the results are not correct Correctness is.
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.
CSC 204 Programming I Loop I The while statement.
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.
Lecture 2: Classes and Objects, using Scanner and String.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Debugging Dwight Deugo Nesa Matic
Debugging. 2 © 2003, Espirity Inc. Module Road Map 1.Eclipse Debugging  Debug Perspective  Debug Session  Breakpoint  Debug Views  Breakpoint Types.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 11: Arrays and Vectors 1 Chapter 11 Arrays and Vectors.
1 Debugging. 2 A Lot of Time is Spent Debugging Programs Debugging. Cyclic process of editing, compiling, and fixing errors. n Always a logical explanation.
Chapter 5 Loops.
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.
Chapter 15: Algorithms 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 15 Algorithms.
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.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 5: Decisions 1 Chapter 5 Decisions.
©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 2: Fundamental Data Types 1 Chapter 2 Fundamental Data Types.
Chapter 5: Control Structures II
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Java Basics Hussein Suleman March 2007 UCT Department of Computer Science Computer Science 1015F.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Review TEST 2 Chapters 4,5,7. QUESTION For which type of operands does the == operator always work correctly: (a) int, (b) double, or (c) String?
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
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.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Copyright © 2014 by John Wiley & Sons. All rights reserved.1 Decisions and Iterations.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
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.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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,
Chapter 7 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.
Testing and Debugging UCT Department of Computer Science Computer Science 1015F Hussein Suleman March 2009.
Testing Tutorial 7.
Elementary Programming
Introduction to Computer Science / Procedural – 67130
Important terms Black-box testing White-box testing Regression testing
Chapter 10 Testing and Debugging
SELECTION STATEMENTS (1)
Important terms Black-box testing White-box testing Regression testing
Java so far Week 7.
The Basics of Recursion
COMPUTER 2430 Object Oriented Programming and Data Structures I
class PrintOnetoTen { public static void main(String args[]) {
CSE 1020:Software Development
6.2 for Loops Example: for ( int i = 1; i
CSS161: Fundamentals of Computing
Presentation transcript:

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 2 Unit Tests Unit test: Test one method in isolation Test harness: Program whose purpose is to test one or more methods Boundary cases: At the boundary of defined values (e.g. Sqrt(0)) Regression testing: Retest after each change. Keep test cases in files Keep test procedures in scripts

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 3 Test case inputs Typed in by hand Generated randomly Read from a file

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 4 Evaluating test case outputs Independent knowledge Check properties e.g. sqrt(x) 2 = x Oracle: less efficient method that yields the same answer e.g. sqrt(x) = pow(x, 0.5)

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 5 Figure 1 Approximation of the Square Root

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 6 Program SqrtTest1.java public class SqrtTest1 { public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); boolean done = false; while (!done) { String inputLine = console.readLine(); if (inputLine == null) done = true; else { double x = Double.parseDouble(inputLine); double y = MathAlgs.sqrt(x); System.out.println("square root of ” + x + ” = ” + y); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 7 Program SqrtTest2.java public class SqrtTest2 { public static void main(String[] args) { for (double x = 0; x <= 10; x = x + 0.5) { double y = MathAlgs.sqrt(x); System.out.println("square root of ” + x + ” = ” + y); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 8 Program SqrtTest3.java import java.util.Random; public class SqrtTest3 { public static void main(String[] args) { Random generator = new Random(); for (int i = 1; i <= 100; i++) { // generate random test value double x = 1.0E6 * generator.nextDouble(); double y = MathAlgs.sqrt(x); System.out.println("square root of ” + x + ” = ” + y); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 9 Program SqrtTest4.java import java.util.Random; public class SqrtTest4 { public static void main(String[] args) { Random generator = new Random(); for (int i = 1; i <= 100; i++) { // generate random test value double x = 1.0E6 * generator.nextDouble(); double y = MathAlgs.sqrt(x); // check that test value fulfills square property if (Numeric.approxEqual(y * y, x)) System.out.println("Test passed."); else System.out.println("Test failed.");

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 10 System.out.println("square root of ” + x + ” = ” + y); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 11 Program SqrtTest5.java import java.util.Random; public class SqrtTest5 { public static void main(String[] args) { Random generator = new Random(); for (int i = 1; i <= 100; i++) { // generate random test value double x = 1.0E6 * generator.nextDouble(); double y = MathAlgs.sqrt(x);

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 12 // compare against oracle if (Numeric.approxEqual(y, Math.pow(x, 0.5))) System.out.println("Test passed. "); else System.out.println("Test failed. "); System.out.println("square root of ” + x + ” = ” + y); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 13 File test.bat java Program < test1.in java Program < test2.in java Program < test3.in

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 14 File test.bat java %1 < test1.in java %1 < test2.in java %1 < test3.in

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 15 File test.bat for %f in (test*.in) do java %1 < %f

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 16 Program Trace Add trace messages public static void sqrt(double a) { System.out.println("Entering sqrt");... System.out.println("Exiting sqrt"); } Disadvantage: Need to remove trace messages when program is (hopefully) correct Better: Use a debugger

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 17 Assertions public class Assertion { public static void check(boolean b) { if (!b) { System.out.println ("Assertion failed."); new Throwable().printStackTrace(); System.exit(1); } } Assert.check(a >= 0); b = sqrt(a);

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 18 Figure 2 The First Bug

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 19 Figure 3 Debugger Stopped at Selected Line

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 20 Figure 4 Inspecting Variables

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 21 Program PrimeBug.java public class PrimeBug // 1 { /** // 2 Tests whether an integer is prime // n any positive integer // true iff n is a prime // 5 **/ // 6 // 7 public static boolean isPrime(int n) // 8 { if (n == 2) return true; // 9 if (n % 2 == 0) return false; // 10 int k = 3; // 11 while (k * k < n) // 12 { if (n % k == 0) return false; // 13 k = k + 2; // 14 } // 15 return true; // 16 } // 17

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 22 // 18 public static void main(String[] args) // 19 { ConsoleReader console // 20 = new ConsoleReader(System.in); // 21 System.out.println // 22 ("Please enter the upper bound:"); // 23 int n = console.readInt(); // 24 // 25 for (int i = 1; i <= n; i = i + 2) // 26 { if (isPrime(i)) // 27 System.out.println(i); // 28 } // 29 } // 30 } // 31

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 23 Program GoodPrime.java public class GoodPrime { /** Tests whether an integer is a n any positive true iff n is a prime */ public static boolean isPrime(int n) { if (n == 1) return false; if (n == 2) return true; if (n % 2 == 0) return false; int k = 3; while (k * k <= n) { if (n % k == 0) return false; k = k + 2; } return true; }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 24 public static void main(String[] args) { ConsoleReader console = new ConsoleReader(System.in); System.out.println ("Please enter the upper bound:"); int n = console.readInt(); if (n >= 2) System.out.println(2); for (int i = 1; i <= n; i = i + 2) { if (isPrime(i)) System.out.println(i); }

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 25 Figure 5 Call Stack Display

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 26 Debugging Strategies Reproduce the error Divide and conquer Know what your program should do Look at all details Understand each error before you fix it

©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 27 Figure 6 Typical Therac-25 Facility