Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e Chapter 8: Testing and Debugging 1 Chapter 8 Testing and Debugging."— Presentation transcript:

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

2 ©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

3 ©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

4 ©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)

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

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

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

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

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

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

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

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

13 ©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

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

15 ©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

16 ©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

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

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

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

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

21 ©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 // 3 @param n any positive integer // 4 @return 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

22 ©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

23 ©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 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; }

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

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

26 ©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

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


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

Similar presentations


Ads by Google