01: import java.util.Random;
02: 
03: /**
04:    This program verifies the computation of square root values
05:    by checking a mathematical property of square roots.
06: */
07: public class RootApproximatorHarness5
08: {  
09:    public static void main(String[] args)
10:    {  
11:       final double SAMPLES = 100;
12:       int passcount = 0;
13:       int failcount = 0;
14:       Random generator = new Random();
15:       for (int i = 1; i <= SAMPLES; i++)
16:       {  
17:          // Generate random test value
18: 
19:          double x = 1000 * generator.nextDouble();
20:          RootApproximator r = new RootApproximator(x);
21:          double y = r.getRoot();
22: 
23:          // Check that test value fulfills square property
24: 
25:          if (Numeric.approxEqual(y * y, x))
26:          {
27:             System.out.print("Test passed: ");
28:             passcount++;
29:          }
30:          else
31:          {
32:             System.out.print("Test failed: ");
33:             failcount++;
34:          }
35: 
36:          System.out.println("x = " + x
37:                + ", root squared = " + y * y);
38:       }
39:       System.out.println("Pass: " + passcount);
40:       System.out.println("Fail: " + failcount);
41:    }
42: }