1/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/**
18 * Test comparison operators.
19 */
20public class Compare {
21
22    /*
23     * Test the integer comparisons in various ways.
24     */
25    static void testIntCompare(int minus, int plus, int plus2, int zero) {
26        System.out.println("IntMath.testIntCompare");
27
28        if (minus > plus)
29            Main.assertTrue(false);
30        if (minus >= plus)
31            Main.assertTrue(false);
32        if (plus < minus)
33            Main.assertTrue(false);
34        if (plus <= minus)
35            Main.assertTrue(false);
36        if (plus == minus)
37            Main.assertTrue(false);
38        if (plus != plus2)
39            Main.assertTrue(false);
40
41        /* try a branch-taken */
42        if (plus != minus) {
43            Main.assertTrue(true);
44        } else {
45            Main.assertTrue(false);
46        }
47
48        if (minus > 0)
49            Main.assertTrue(false);
50        if (minus >= 0)
51            Main.assertTrue(false);
52        if (plus < 0)
53            Main.assertTrue(false);
54        if (plus <= 0)
55            Main.assertTrue(false);
56        if (plus == 0)
57            Main.assertTrue(false);
58        if (zero != 0)
59            Main.assertTrue(false);
60
61        if (zero == 0) {
62            Main.assertTrue(true);
63        } else {
64            Main.assertTrue(false);
65        }
66    }
67
68    /*
69     * Test cmp-long.
70     *
71     * minus=-5, alsoMinus=0xFFFFFFFF00000009, plus=4, alsoPlus=8
72     */
73    static void testLongCompare(long minus, long alsoMinus, long plus,
74        long alsoPlus) {
75
76        System.out.println("IntMath.testLongCompare");
77        if (minus > plus)
78            Main.assertTrue(false);
79        if (plus < minus)
80            Main.assertTrue(false);
81        if (plus == minus)
82            Main.assertTrue(false);
83
84        if (plus >= plus+1)
85            Main.assertTrue(false);
86        if (minus >= minus+1)
87            Main.assertTrue(false);
88
89        /* try a branch-taken */
90        if (plus != minus) {
91            Main.assertTrue(true);
92        } else {
93            Main.assertTrue(false);
94        }
95
96        /* compare when high words are equal but low words differ */
97        if (plus > alsoPlus)
98            Main.assertTrue(false);
99        if (alsoPlus < plus)
100            Main.assertTrue(false);
101        if (alsoPlus == plus)
102            Main.assertTrue(false);
103
104        /* high words are equal, low words have apparently different signs */
105        if (minus < alsoMinus)      // bug!
106            Main.assertTrue(false);
107        if (alsoMinus > minus)
108            Main.assertTrue(false);
109        if (alsoMinus == minus)
110            Main.assertTrue(false);
111    }
112
113    /*
114     * Test cmpl-float and cmpg-float.
115     */
116    static void testFloatCompare(float minus, float plus, float plus2,
117        float nan) {
118
119        System.out.println("IntMath.testFloatCompare");
120        if (minus > plus)
121            Main.assertTrue(false);
122        if (plus < minus)
123            Main.assertTrue(false);
124        if (plus == minus)
125            Main.assertTrue(false);
126        if (plus != plus2)
127            Main.assertTrue(false);
128
129        if (plus <= nan)
130            Main.assertTrue(false);
131        if (plus >= nan)
132            Main.assertTrue(false);
133        if (minus <= nan)
134            Main.assertTrue(false);
135        if (minus >= nan)
136            Main.assertTrue(false);
137        if (nan >= plus)
138            Main.assertTrue(false);
139        if (nan <= plus)
140            Main.assertTrue(false);
141
142        if (nan == nan)
143            Main.assertTrue(false);
144    }
145
146    static void testDoubleCompare(double minus, double plus, double plus2,
147        double nan) {
148
149        System.out.println("IntMath.testDoubleCompare");
150        if (minus > plus)
151            Main.assertTrue(false);
152        if (plus < minus)
153            Main.assertTrue(false);
154        if (plus == minus)
155            Main.assertTrue(false);
156        if (plus != plus2)
157            Main.assertTrue(false);
158
159        if (plus <= nan)
160            Main.assertTrue(false);
161        if (plus >= nan)
162            Main.assertTrue(false);
163        if (minus <= nan)
164            Main.assertTrue(false);
165        if (minus >= nan)
166            Main.assertTrue(false);
167        if (nan >= plus)
168            Main.assertTrue(false);
169        if (nan <= plus)
170            Main.assertTrue(false);
171
172        if (nan == nan)
173            Main.assertTrue(false);
174    }
175
176    public static void run() {
177        testIntCompare(-5, 4, 4, 0);
178        testLongCompare(-5L, -4294967287L, 4L, 8L);
179
180        testFloatCompare(-5.0f, 4.0f, 4.0f, (1.0f/0.0f) / (1.0f/0.0f));
181        testDoubleCompare(-5.0, 4.0, 4.0, (1.0/0.0) / (1.0/0.0));
182    }
183}
184