1 |
import java.util.Random; |
2 |
|
3 |
/** |
4 |
This program verifies the computation of square root values |
5 |
by using an oracle. |
6 |
*/ |
7 |
public class RootApproximatorTest7 |
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 |
double oracleValue = Math.pow(x, 0.5); |
26 |
|
27 |
// check that test value approximately equals oracle value |
28 |
|
29 |
if (Numeric.approxEqual(y, oracleValue)) |
30 |
{ |
31 |
System.out.println("Test passed."); |
32 |
passcount++; |
33 |
} |
34 |
else |
35 |
{ |
36 |
System.out.println("Test failed."); |
37 |
failcount++; |
38 |
} |
39 |
} |
40 |
System.out.println("Pass: " + passcount); |
41 |
System.out.println("Fail: " + failcount); |
42 |
} |
43 |
} |