previous | start | next

File RootApproximatorTest6.java

1 import java.util.Random;
2
3 /**
4     This program verifies the computation of square root values
5     by checking a mathematical property of square roots.
6 */
7 public class RootApproximatorTest6
8 {  
9    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 = 1.0E6 * generator.nextDouble();
20          RootApproximator r = new RootApproximator(x);
21          double y = r.getRoot();
22          System.out.println("square root of " + x
23             + " = " + y);
24
25          // check that test value fulfills square property
26
27          if (Numeric.approxEqual(y * y, x))
28          {
29             System.out.println("Test passed.");
30             passcount++;
31          }
32          else
33          {
34             System.out.println("Test failed.");
35             failcount++;
36          }
37       }
38       System.out.println("Pass: " + passcount);
39       System.out.println("Fail: " + failcount);
40    }
41 }


previous | start | next