1// Copyright (c) 1999, Silicon Graphics, Inc. -- ALL RIGHTS RESERVED
2//
3// Permission is granted free of charge to copy, modify, use and distribute
4// this software  provided you include the entirety of this notice in all
5// copies made.
6//
7// THIS SOFTWARE IS PROVIDED ON AN AS IS BASIS, WITHOUT WARRANTY OF ANY
8// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
9// WARRANTIES THAT THE SUBJECT SOFTWARE IS FREE OF DEFECTS, MERCHANTABLE, FIT
10// FOR A PARTICULAR PURPOSE OR NON-INFRINGING.   SGI ASSUMES NO RISK AS TO THE
11// QUALITY AND PERFORMANCE OF THE SOFTWARE.   SHOULD THE SOFTWARE PROVE
12// DEFECTIVE IN ANY RESPECT, SGI ASSUMES NO COST OR LIABILITY FOR ANY
13// SERVICING, REPAIR OR CORRECTION.  THIS DISCLAIMER OF WARRANTY CONSTITUTES
14// AN ESSENTIAL PART OF THIS LICENSE. NO USE OF ANY SUBJECT SOFTWARE IS
15// AUTHORIZED HEREUNDER EXCEPT UNDER THIS DISCLAIMER.
16//
17// UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, WHETHER TORT (INCLUDING,
18// WITHOUT LIMITATION, NEGLIGENCE OR STRICT LIABILITY), CONTRACT, OR
19// OTHERWISE, SHALL SGI BE LIABLE FOR ANY DIRECT, INDIRECT, SPECIAL,
20// INCIDENTAL, OR CONSEQUENTIAL DAMAGES OF ANY CHARACTER WITH RESPECT TO THE
21// SOFTWARE INCLUDING, WITHOUT LIMITATION, DAMAGES FOR LOSS OF GOODWILL, WORK
22// STOPPAGE, LOSS OF DATA, COMPUTER FAILURE OR MALFUNCTION, OR ANY AND ALL
23// OTHER COMMERCIAL DAMAGES OR LOSSES, EVEN IF SGI SHALL HAVE BEEN INFORMED OF
24// THE POSSIBILITY OF SUCH DAMAGES.  THIS LIMITATION OF LIABILITY SHALL NOT
25// APPLY TO LIABILITY RESULTING FROM SGI's NEGLIGENCE TO THE EXTENT APPLICABLE
26// LAW PROHIBITS SUCH LIMITATION.  SOME JURISDICTIONS DO NOT ALLOW THE
27// EXCLUSION OR LIMITATION OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO THAT
28// EXCLUSION AND LIMITATION MAY NOT APPLY TO YOU.
29//
30// These license terms shall be governed by and construed in accordance with
31// the laws of the United States and the State of California as applied to
32// agreements entered into and to be performed entirely within California
33// between California residents.  Any litigation relating to these license
34// terms shall be subject to the exclusive jurisdiction of the Federal Courts
35// of the Northern District of California (or, absent subject matter
36// jurisdiction in such courts, the courts of the State of California), with
37// venue lying exclusively in Santa Clara County, California.
38
39// Superficial sanity test for the constructive reals package.
40
41// Added test for division by negative number.  Hans_Boehm@hp.com, 8/13/01
42// Modified to use AssertionFailedError. hboehm@google.com, 6/6/14
43// Modified further for Android/JUnit testing framework, 12/15/14
44// Added basic asin and acos tests, improved messages,
45//        hboehm@google.com, 5/22/15
46
47package com.hp.creals;
48
49import java.math.BigInteger;
50import junit.framework.AssertionFailedError;
51import junit.framework.TestCase;
52
53public class CRTest extends TestCase {
54    private static void check(boolean x, String s) {
55        if (!x) throw new AssertionFailedError(s);
56    }
57    private static void check_eq(CR x, CR y, String s) {
58        if (x.compareTo(y, -50) != 0) {
59            throw new AssertionFailedError(s + "(" + x + " vs. " + y + ")");
60        }
61    }
62    private static void check_appr_eq(double x, double y, String s) {
63        if (Math.abs(x - y) > 0.000001) {
64            throw new AssertionFailedError(s + "(" + x + " vs. " + y + ")");
65        }
66    }
67    // TODO: Break this up into meaningful smaller tests.
68    public void testCR() {
69        CR zero = CR.valueOf(0);
70        CR one = CR.valueOf(1);
71        CR two = CR.valueOf(2);
72        check(one.signum() == 1, "signum(1) failed");
73        check(one.negate().signum() == -1, "signum(-1) failed");
74        check(zero.signum(-100) == 0, "signum(0) failed");
75        check(one.compareTo(two, -10) == -1, "comparison failed");
76        check(two.toString(4).compareTo("2.0000") == 0, "toString failed");
77        check_eq(one.shiftLeft(1),two, "shiftLeft failed");
78        check_eq(two.shiftRight(1),one, "shiftRight failed");
79        check_eq(one.add(one),two, "add failed 1");
80        check_eq(one.max(two),two, "max failed");
81        check_eq(one.min(two),one, "min failed");
82        check_eq(one.abs(),one, "abs failed 1");
83        check_eq(one.negate().abs(),one, "abs failed 2");
84        CR three = two.add(one);
85        CR four = two.add(two);
86        check_eq(CR.valueOf(4), four, "2 + 2 failed");
87        check_eq(CR.valueOf(3), three, "2 + 1 failed");
88        check_eq(one.negate().add(two), one, "negate failed");
89        check(one.negate().signum() == -1, "signum(-1) failed");
90        check_eq(two.multiply(two), four, "multiply failed");
91        check_eq(one.divide(four).shiftLeft(4), four, "divide failed 1");
92        check_eq(two.divide(one.negate()), two.negate(), "divide(neg) failed");
93        CR thirteen = CR.valueOf(13);
94        check_eq(one.divide(thirteen).multiply(thirteen), one,
95                 "divide failed 2");
96        check(thirteen.floatValue() == 13.0, "floatValue failed");
97        check(thirteen.intValue() == 13, "intValue failed");
98        check(thirteen.longValue() == 13, "longValue failed");
99        check(thirteen.doubleValue() == 13.0, "doubleValue failed");
100        check_eq(zero.exp(), one, "exp(0) failed");
101        CR e = one.exp();
102        check(e.toString(20).substring(0,17)
103                            .compareTo("2.718281828459045") == 0,
104              "exp(1) failed");
105        check_eq(e.ln(), one, "ln(e) failed");
106        CR half_pi = CR.PI.divide(two);
107        CR half = one.divide(two);
108        BigInteger million = BigInteger.valueOf(1000*1000);
109        BigInteger thousand = BigInteger.valueOf(1000);
110        CR huge = CR.valueOf(million.multiply(million).multiply(thousand));
111        UnaryCRFunction asin = UnaryCRFunction.asinFunction;
112        UnaryCRFunction acos = UnaryCRFunction.acosFunction;
113        UnaryCRFunction atan = UnaryCRFunction.atanFunction;
114        UnaryCRFunction tan = UnaryCRFunction.tanFunction;
115        check_eq(half_pi.sin(), one, "sin(pi/2) failed");
116        check_eq(asin.execute(one),half_pi, "asin(1) failed");
117        check_eq(asin.execute(one.negate()),
118                              half_pi.negate(), "asin(-1) failed");
119        check_eq(asin.execute(zero), zero, "asin(0) failed");
120        check_eq(asin.execute(half.sin()), half, "asin(sin(0.5)) failed");
121        UnaryCRFunction cosine = UnaryCRFunction.sinFunction
122                                        .monotoneDerivative(zero, CR.PI);
123        check_eq(cosine.execute(one), one.cos(), "monotoneDerivative failed");
124        check_eq(cosine.execute(three), three.cos(),
125                 "monotoneDerivative failed 2");
126        check_eq(asin.execute(one.sin()), one, "asin(sin(1) failed");
127        check_eq(acos.execute(one.cos()), one, "acos(cos(1) failed");
128        check_eq(atan.execute(tan.execute(one)), one, "atan(tan(1) failed");
129        check_eq(atan.execute(tan.execute(one.negate())), one.negate(),
130                 "atan(tan(-1) failed");
131        check_eq(tan.execute(atan.execute(huge)), huge,
132                 "tan(atan(10**15)) failed");
133        CR sqrt13 = thirteen.sqrt();
134        check_eq(sqrt13.multiply(sqrt13), thirteen, "sqrt(13)*sqrt(13) failed");
135        CR tmp = CR.PI.add(CR.valueOf(-123).exp());
136        CR tmp2 = tmp.subtract(CR.PI);
137        check(tmp2.ln().intValue() == -123, "intValue(...) failed");
138        check(tmp2.ln().longValue() == -123, "longValue(...) failed");
139        check(tmp2.ln().floatValue() == -123.0, "floatValue(...) failed");
140        check(tmp2.ln().doubleValue() == -123.0, "doubleValue(...) failed");
141        for (double n = -10.0; n < 10.0; n += 2.0) {
142            check_appr_eq(Math.sin(n), CR.valueOf(n).sin().doubleValue(),
143                          "sin failed at " + n);
144            check_appr_eq(Math.cos(n), CR.valueOf(n).cos().doubleValue(),
145                          "cos failed at " + n);
146            check_appr_eq(Math.exp(n), CR.valueOf(n).exp().doubleValue(),
147                          "exp failed at " + n);
148            check_appr_eq(Math.asin(0.1*n),
149                          CR.valueOf(0.1*n).asin().doubleValue(),
150                          "asin failed at " + 0.1*n);
151            check_appr_eq(Math.acos(0.1*n),
152                          CR.valueOf(0.1*n).acos().doubleValue(),
153                          "acos failed at " + 0.1*n);
154            if (n > 0.0) {
155              check_appr_eq(Math.log(n), CR.valueOf(n).ln().doubleValue(),
156                          "ln failed at " + n);
157            }
158        }
159        check_appr_eq(Math.cos(12345678.0),
160                      CR.valueOf(12345678).cos().doubleValue(),
161                      "cos failed at " + 12345678);
162    }
163}
164