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

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

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

Page 1: ©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 Debugging1

Chapter 8

Testing and Debugging

Page 2: ©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 Debugging2

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 filesKeep test procedures in scripts

Page 3: ©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 Debugging3

Test case inputs

• Typed in by hand• Generated randomly• Read from a file

Page 4: ©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 Debugging4

Evaluating test case outputs

• Independent knowledge• Check properties

e.g. sqrt(x)2 = x• Oracle: less efficient method that yields the

same answere.g. sqrt(x) = pow(x, 0.5)

Page 5: ©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 Debugging5

Figure 1Approximation of the SquareRoot

Page 6: ©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 Debugging6

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); } } }}

Page 7: ©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 Debugging7

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); } }}

Page 8: ©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 Debugging8

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); } }}

Page 9: ©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 Debugging9

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.");

Page 10: ©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 Debugging10

System.out.println("square root of ” + x + ” = ” + y); } } }

Page 11: ©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 Debugging11

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);

Page 12: ©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 Debugging12

// 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); } }}

Page 13: ©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 Debugging13

File test.bat

java Program < test1.injava Program < test2.injava Program < test3.in

Page 14: ©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 Debugging14

File test.bat

java %1 < test1.injava %1 < test2.injava %1 < test3.in

Page 15: ©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 Debugging15

File test.bat

for %%f in (test*.in) do java %1 < %%f

Page 16: ©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 Debugging16

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

Page 17: ©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 Debugging17

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);

Page 18: ©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 Debugging18

Figure 2The First Bug

Page 19: ©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 Debugging19

Figure 3Debugger Stopped at Selected Line

Page 20: ©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 Debugging20

Figure 4Inspecting Variables

Page 21: ©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 Debugging21

Program PrimeBug.java

public class PrimeBug // 1{ /** // 2 Tests whether an integer is prime // 3 @param n any positive integer // 4 @return true iff n is a prime // 5 **/ // 6 // 7public 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

Page 22: ©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 Debugging22

// 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

Page 23: ©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 Debugging23

Program GoodPrime.java

public class GoodPrime{ /** Tests whether an integer is a prime @param n any positive integer @return 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; }

Page 24: ©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 Debugging24

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); } }}

Page 25: ©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 Debugging25

Figure 5Call Stack Display

Page 26: ©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 Debugging26

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

Page 27: ©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 Debugging27

Figure 6Typical Therac-25 Facility