1 /*
2  * Copyright (C) 2014 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 import java.lang.reflect.Method;
18 
19 public class Main {
20 
main(String[] args)21   public static void main(String[] args) throws Exception {
22     cmpLong();
23     cmpFloat();
24     cmpDouble();
25   }
26 
cmpLong()27   private static void cmpLong() throws Exception {
28     expectLt(3L, 5L);
29     expectGt(5L, 3L);
30     expectLt(Long.MIN_VALUE, Long.MAX_VALUE);
31     expectGt(Long.MAX_VALUE, Long.MIN_VALUE);
32 
33     expectEquals(0, smaliCmpLong(0L, 0L));
34     expectEquals(0, smaliCmpLong(1L, 1L));
35     expectEquals(-1, smaliCmpLong(1L, 2L));
36     expectEquals(1, smaliCmpLong(2L, 1L));
37     expectEquals(-1, smaliCmpLong(Long.MIN_VALUE, Long.MAX_VALUE));
38     expectEquals(1, smaliCmpLong(Long.MAX_VALUE, Long.MIN_VALUE));
39     expectEquals(0, smaliCmpLong(Long.MIN_VALUE, Long.MIN_VALUE));
40     expectEquals(0, smaliCmpLong(Long.MAX_VALUE, Long.MAX_VALUE));
41   }
42 
cmpFloat()43   private static void cmpFloat() throws Exception {
44     expectEq(0F, 0F);
45     expectEq(-0F, 0F);
46     expectEq(0F, -0F);
47     expectEq(-0F, -0F);
48     expectLt(3.1F, 5.1F);
49     expectGt(5.1F, 3.1F);
50     expectLt(Float.MIN_VALUE, Float.MAX_VALUE);
51     expectGt(Float.MAX_VALUE, Float.MIN_VALUE);
52     expectFalse(3.1F, Float.NaN);
53     expectFalse(Float.NaN, 3.1F);
54 
55     expectEquals(0, smaliCmpGtFloat(0F, 0F));
56     expectEquals(0, smaliCmpGtFloat(-0F, 0F));
57     expectEquals(0, smaliCmpGtFloat(0F, -0F));
58     expectEquals(0, smaliCmpGtFloat(-0F, -0F));
59     expectEquals(0, smaliCmpGtFloat(1F, 1F));
60     expectEquals(-1, smaliCmpGtFloat(1.1F, 2.1F));
61     expectEquals(1, smaliCmpGtFloat(2.1F, 1.1F));
62     expectEquals(-1, smaliCmpGtFloat(Float.MIN_VALUE, Float.MAX_VALUE));
63     expectEquals(1, smaliCmpGtFloat(Float.MAX_VALUE, Float.MIN_VALUE));
64     expectEquals(0, smaliCmpGtFloat(Float.MIN_VALUE, Float.MIN_VALUE));
65     expectEquals(0, smaliCmpGtFloat(Float.MAX_VALUE, Float.MAX_VALUE));
66     expectEquals(1, smaliCmpGtFloat(5F, Float.NaN));
67     expectEquals(1, smaliCmpGtFloat(Float.NaN, 5F));
68 
69     expectEquals(0, smaliCmpLtFloat(0F, 0F));
70     expectEquals(0, smaliCmpLtFloat(-0F, 0F));
71     expectEquals(0, smaliCmpLtFloat(0F, -0F));
72     expectEquals(0, smaliCmpLtFloat(-0F, -0F));
73     expectEquals(0, smaliCmpLtFloat(1F, 1F));
74     expectEquals(-1, smaliCmpLtFloat(1.1F, 2.1F));
75     expectEquals(1, smaliCmpLtFloat(2.1F, 1.1F));
76     expectEquals(-1, smaliCmpLtFloat(Float.MIN_VALUE, Float.MAX_VALUE));
77     expectEquals(1, smaliCmpLtFloat(Float.MAX_VALUE, Float.MIN_VALUE));
78     expectEquals(0, smaliCmpLtFloat(Float.MIN_VALUE, Float.MIN_VALUE));
79     expectEquals(0, smaliCmpLtFloat(Float.MAX_VALUE, Float.MAX_VALUE));
80     expectEquals(-1, smaliCmpLtFloat(5F, Float.NaN));
81     expectEquals(-1, smaliCmpLtFloat(Float.NaN, 5F));
82   }
83 
cmpDouble()84   private static void cmpDouble() throws Exception {
85     expectEq(0D, 0D);
86     expectEq(-0D, 0D);
87     expectEq(0D, -0D);
88     expectEq(-0D, -0D);
89     expectLt(3.1D, 5.1D);
90     expectGt(5.1D, 3.1D);
91     expectLt(Double.MIN_VALUE, Double.MAX_VALUE);
92     expectGt(Double.MAX_VALUE, Double.MIN_VALUE);
93     expectFalse(3.1D, Double.NaN);
94     expectFalse(Double.NaN, 3.1D);
95 
96     expectEquals(0, smaliCmpGtDouble(0D, 0D));
97     expectEquals(0, smaliCmpGtDouble(-0D, 0D));
98     expectEquals(0, smaliCmpGtDouble(0D, -0D));
99     expectEquals(0, smaliCmpGtDouble(-0D, -0D));
100     expectEquals(0, smaliCmpGtDouble(1D, 1D));
101     expectEquals(-1, smaliCmpGtDouble(1.1D, 2.1D));
102     expectEquals(1, smaliCmpGtDouble(2.1D, 1.1D));
103     expectEquals(-1, smaliCmpGtDouble(Double.MIN_VALUE, Double.MAX_VALUE));
104     expectEquals(1, smaliCmpGtDouble(Double.MAX_VALUE, Double.MIN_VALUE));
105     expectEquals(0, smaliCmpGtDouble(Double.MIN_VALUE, Double.MIN_VALUE));
106     expectEquals(0, smaliCmpGtDouble(Double.MAX_VALUE, Double.MAX_VALUE));
107     expectEquals(1, smaliCmpGtDouble(5D, Double.NaN));
108     expectEquals(1, smaliCmpGtDouble(Double.NaN, 5D));
109 
110     expectEquals(0, smaliCmpLtDouble(0D, 0D));
111     expectEquals(0, smaliCmpLtDouble(-0D, 0D));
112     expectEquals(0, smaliCmpLtDouble(0D, -0D));
113     expectEquals(0, smaliCmpLtDouble(-0D, -0D));
114     expectEquals(0, smaliCmpLtDouble(1D, 1D));
115     expectEquals(-1, smaliCmpLtDouble(1.1D, 2.1D));
116     expectEquals(1, smaliCmpLtDouble(2.1D, 1.1D));
117     expectEquals(-1, smaliCmpLtDouble(Double.MIN_VALUE, Double.MAX_VALUE));
118     expectEquals(1, smaliCmpLtDouble(Double.MAX_VALUE, Double.MIN_VALUE));
119     expectEquals(0, smaliCmpLtDouble(Double.MIN_VALUE, Double.MIN_VALUE));
120     expectEquals(0, smaliCmpLtDouble(Double.MAX_VALUE, Double.MAX_VALUE));
121     expectEquals(-1, smaliCmpLtDouble(5D, Double.NaN));
122     expectEquals(-1, smaliCmpLtDouble(Float.NaN, 5D));
123   }
124 
$opt$eq(float a, float b)125   static boolean $opt$eq(float a, float b) {
126     return a == b;
127   }
128 
$opt$eq(double a, double b)129   static boolean $opt$eq(double a, double b) {
130     return a == b;
131   }
132 
$opt$lt(long a, long b)133   static boolean $opt$lt(long a, long b) {
134     return a < b;
135   }
136 
$opt$lt(float a, float b)137   static boolean $opt$lt(float a, float b) {
138     return a < b;
139   }
140 
$opt$lt(double a, double b)141   static boolean $opt$lt(double a, double b) {
142     return a < b;
143   }
144 
$opt$gt(long a, long b)145   static boolean $opt$gt(long a, long b) {
146     return a > b;
147   }
148 
$opt$gt(float a, float b)149   static boolean $opt$gt(float a, float b) {
150     return a > b;
151   }
152 
$opt$gt(double a, double b)153   static boolean $opt$gt(double a, double b) {
154     return a > b;
155   }
156 
157   // Wrappers around methods located in file cmp.smali.
158 
smaliCmpLong(long a, long b)159   private static int smaliCmpLong(long a, long b) throws Exception {
160     Class<?> c = Class.forName("TestCmp");
161     Method m = c.getMethod("$opt$CmpLong", long.class, long.class);
162     int result = (Integer)m.invoke(null, a, b);
163     return result;
164   }
165 
smaliCmpGtFloat(float a, float b)166   private static int smaliCmpGtFloat(float a, float b) throws Exception {
167     Class<?> c = Class.forName("TestCmp");
168     Method m = c.getMethod("$opt$CmpGtFloat", float.class, float.class);
169     int result = (Integer)m.invoke(null, a, b);
170     return result;
171   }
172 
smaliCmpLtFloat(float a, float b)173   private static int smaliCmpLtFloat(float a, float b) throws Exception {
174     Class<?> c = Class.forName("TestCmp");
175     Method m = c.getMethod("$opt$CmpLtFloat", float.class, float.class);
176     int result = (Integer)m.invoke(null, a, b);
177     return result;
178   }
179 
smaliCmpGtDouble(double a, double b)180   private static int smaliCmpGtDouble(double a, double b) throws Exception {
181     Class<?> c = Class.forName("TestCmp");
182     Method m = c.getMethod("$opt$CmpGtDouble", double.class, double.class);
183     int result = (Integer)m.invoke(null, a, b);
184     return result;
185   }
186 
smaliCmpLtDouble(double a, double b)187   private static int smaliCmpLtDouble(double a, double b) throws Exception {
188     Class<?> c = Class.forName("TestCmp");
189     Method m = c.getMethod("$opt$CmpLtDouble", double.class, double.class);
190     int result = (Integer)m.invoke(null, a, b);
191     return result;
192   }
193 
expectEquals(int expected, int result)194     public static void expectEquals(int expected, int result) {
195     if (expected != result) {
196       throw new Error("Expected: " + expected + ", found: " + result);
197     }
198   }
199 
expectLt(long a, long b)200   public static void expectLt(long a, long b) {
201     if (!$opt$lt(a, b)) {
202       throw new Error("Expected: " + a + " < " + b);
203     }
204   }
205 
expectGt(long a, long b)206   public static void expectGt(long a, long b) {
207     if (!$opt$gt(a, b)) {
208       throw new Error("Expected: " + a + " > " + b);
209     }
210   }
211 
expectEq(float a, float b)212   public static void expectEq(float a, float b) {
213     if (!$opt$eq(a, b)) {
214       throw new Error("Expected: " + a + " == " + b);
215     }
216   }
217 
expectLt(float a, float b)218   public static void expectLt(float a, float b) {
219     if (!$opt$lt(a, b)) {
220       throw new Error("Expected: " + a + " < " + b);
221     }
222   }
223 
expectGt(float a, float b)224   public static void expectGt(float a, float b) {
225     if (!$opt$gt(a, b)) {
226       throw new Error("Expected: " + a + " > " + b);
227     }
228   }
229 
expectFalse(float a, float b)230   public static void expectFalse(float a, float b) {
231     if ($opt$lt(a, b)) {
232       throw new Error("Not expecting: " + a + " < " + b);
233     }
234     if ($opt$gt(a, b)) {
235       throw new Error("Not expecting: " + a + " > " + b);
236     }
237   }
238 
expectEq(double a, double b)239   public static void expectEq(double a, double b) {
240     if (!$opt$eq(a, b)) {
241       throw new Error("Expected: " + a + " == " + b);
242     }
243   }
244 
expectLt(double a, double b)245   public static void expectLt(double a, double b) {
246     if (!$opt$lt(a, b)) {
247       throw new Error("Expected: " + a + " < " + b);
248     }
249   }
250 
expectGt(double a, double b)251   public static void expectGt(double a, double b) {
252     if (!$opt$gt(a, b)) {
253       throw new Error("Expected: " + a + " > " + b);
254     }
255   }
256 
expectFalse(double a, double b)257   public static void expectFalse(double a, double b) {
258     if ($opt$lt(a, b)) {
259       throw new Error("Not expecting: " + a + " < " + b);
260     }
261     if ($opt$gt(a, b)) {
262       throw new Error("Not expecting: " + a + " > " + b);
263     }
264   }
265 
266 }
267