• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     expectLt(3.1F, 5.1F);
45     expectGt(5.1F, 3.1F);
46     expectLt(Float.MIN_VALUE, Float.MAX_VALUE);
47     expectGt(Float.MAX_VALUE, Float.MIN_VALUE);
48     expectFalse(3.1F, Float.NaN);
49     expectFalse(Float.NaN, 3.1F);
50 
51     expectEquals(0, smaliCmpGtFloat(0F, 0F));
52     expectEquals(0, smaliCmpGtFloat(1F, 1F));
53     expectEquals(-1, smaliCmpGtFloat(1.1F, 2.1F));
54     expectEquals(1, smaliCmpGtFloat(2.1F, 1.1F));
55     expectEquals(-1, smaliCmpGtFloat(Float.MIN_VALUE, Float.MAX_VALUE));
56     expectEquals(1, smaliCmpGtFloat(Float.MAX_VALUE, Float.MIN_VALUE));
57     expectEquals(0, smaliCmpGtFloat(Float.MIN_VALUE, Float.MIN_VALUE));
58     expectEquals(0, smaliCmpGtFloat(Float.MAX_VALUE, Float.MAX_VALUE));
59     expectEquals(1, smaliCmpGtFloat(5F, Float.NaN));
60     expectEquals(1, smaliCmpGtFloat(Float.NaN, 5F));
61 
62     expectEquals(0, smaliCmpLtFloat(0F, 0F));
63     expectEquals(0, smaliCmpLtFloat(1F, 1F));
64     expectEquals(-1, smaliCmpLtFloat(1.1F, 2.1F));
65     expectEquals(1, smaliCmpLtFloat(2.1F, 1.1F));
66     expectEquals(-1, smaliCmpLtFloat(Float.MIN_VALUE, Float.MAX_VALUE));
67     expectEquals(1, smaliCmpLtFloat(Float.MAX_VALUE, Float.MIN_VALUE));
68     expectEquals(0, smaliCmpLtFloat(Float.MIN_VALUE, Float.MIN_VALUE));
69     expectEquals(0, smaliCmpLtFloat(Float.MAX_VALUE, Float.MAX_VALUE));
70     expectEquals(-1, smaliCmpLtFloat(5F, Float.NaN));
71     expectEquals(-1, smaliCmpLtFloat(Float.NaN, 5F));
72   }
73 
cmpDouble()74   private static void cmpDouble() throws Exception {
75     expectLt(3.1D, 5.1D);
76     expectGt(5.1D, 3.1D);
77     expectLt(Double.MIN_VALUE, Double.MAX_VALUE);
78     expectGt(Double.MAX_VALUE, Double.MIN_VALUE);
79     expectFalse(3.1D, Double.NaN);
80     expectFalse(Double.NaN, 3.1D);
81 
82     expectEquals(0, smaliCmpGtDouble(0D, 0D));
83     expectEquals(0, smaliCmpGtDouble(1D, 1D));
84     expectEquals(-1, smaliCmpGtDouble(1.1D, 2.1D));
85     expectEquals(1, smaliCmpGtDouble(2.1D, 1.1D));
86     expectEquals(-1, smaliCmpGtDouble(Double.MIN_VALUE, Double.MAX_VALUE));
87     expectEquals(1, smaliCmpGtDouble(Double.MAX_VALUE, Double.MIN_VALUE));
88     expectEquals(0, smaliCmpGtDouble(Double.MIN_VALUE, Double.MIN_VALUE));
89     expectEquals(0, smaliCmpGtDouble(Double.MAX_VALUE, Double.MAX_VALUE));
90     expectEquals(1, smaliCmpGtDouble(5D, Double.NaN));
91     expectEquals(1, smaliCmpGtDouble(Double.NaN, 5D));
92 
93     expectEquals(0, smaliCmpLtDouble(0D, 0D));
94     expectEquals(0, smaliCmpLtDouble(1D, 1D));
95     expectEquals(-1, smaliCmpLtDouble(1.1D, 2.1D));
96     expectEquals(1, smaliCmpLtDouble(2.1D, 1.1D));
97     expectEquals(-1, smaliCmpLtDouble(Double.MIN_VALUE, Double.MAX_VALUE));
98     expectEquals(1, smaliCmpLtDouble(Double.MAX_VALUE, Double.MIN_VALUE));
99     expectEquals(0, smaliCmpLtDouble(Double.MIN_VALUE, Double.MIN_VALUE));
100     expectEquals(0, smaliCmpLtDouble(Double.MAX_VALUE, Double.MAX_VALUE));
101     expectEquals(-1, smaliCmpLtDouble(5D, Double.NaN));
102     expectEquals(-1, smaliCmpLtDouble(Float.NaN, 5D));
103   }
104 
$opt$lt(long a, long b)105  static boolean $opt$lt(long a, long b) {
106     return a < b;
107   }
108 
$opt$lt(float a, float b)109   static boolean $opt$lt(float a, float b) {
110     return a < b;
111   }
112 
$opt$lt(double a, double b)113   static boolean $opt$lt(double a, double b) {
114     return a < b;
115   }
116 
$opt$gt(long a, long b)117   static boolean $opt$gt(long a, long b) {
118     return a > b;
119   }
120 
$opt$gt(float a, float b)121   static boolean $opt$gt(float a, float b) {
122     return a > b;
123   }
124 
$opt$gt(double a, double b)125   static boolean $opt$gt(double a, double b) {
126     return a > b;
127   }
128 
129   // Wrappers around methods located in file cmp.smali.
130 
smaliCmpLong(long a, long b)131   private static int smaliCmpLong(long a, long b) throws Exception {
132     Class<?> c = Class.forName("TestCmp");
133     Method m = c.getMethod("$opt$CmpLong", long.class, long.class);
134     int result = (Integer)m.invoke(null, a, b);
135     return result;
136   }
137 
smaliCmpGtFloat(float a, float b)138   private static int smaliCmpGtFloat(float a, float b) throws Exception {
139     Class<?> c = Class.forName("TestCmp");
140     Method m = c.getMethod("$opt$CmpGtFloat", float.class, float.class);
141     int result = (Integer)m.invoke(null, a, b);
142     return result;
143   }
144 
smaliCmpLtFloat(float a, float b)145   private static int smaliCmpLtFloat(float a, float b) throws Exception {
146     Class<?> c = Class.forName("TestCmp");
147     Method m = c.getMethod("$opt$CmpLtFloat", float.class, float.class);
148     int result = (Integer)m.invoke(null, a, b);
149     return result;
150   }
151 
smaliCmpGtDouble(double a, double b)152   private static int smaliCmpGtDouble(double a, double b) throws Exception {
153     Class<?> c = Class.forName("TestCmp");
154     Method m = c.getMethod("$opt$CmpGtDouble", double.class, double.class);
155     int result = (Integer)m.invoke(null, a, b);
156     return result;
157   }
158 
smaliCmpLtDouble(double a, double b)159   private static int smaliCmpLtDouble(double a, double b) throws Exception {
160     Class<?> c = Class.forName("TestCmp");
161     Method m = c.getMethod("$opt$CmpLtDouble", double.class, double.class);
162     int result = (Integer)m.invoke(null, a, b);
163     return result;
164   }
165 
expectEquals(int expected, int result)166     public static void expectEquals(int expected, int result) {
167     if (expected != result) {
168       throw new Error("Expected: " + expected + ", found: " + result);
169     }
170   }
171 
expectLt(long a, long b)172   public static void expectLt(long a, long b) {
173     if (!$opt$lt(a, b)) {
174       throw new Error("Expected: " + a + " < " + b);
175     }
176   }
177 
expectGt(long a, long b)178   public static void expectGt(long a, long b) {
179     if (!$opt$gt(a, b)) {
180       throw new Error("Expected: " + a + " > " + b);
181     }
182   }
183 
expectLt(float a, float b)184   public static void expectLt(float a, float b) {
185     if (!$opt$lt(a, b)) {
186       throw new Error("Expected: " + a + " < " + b);
187     }
188   }
189 
expectGt(float a, float b)190   public static void expectGt(float a, float b) {
191     if (!$opt$gt(a, b)) {
192       throw new Error("Expected: " + a + " > " + b);
193     }
194   }
195 
expectFalse(float a, float b)196   public static void expectFalse(float a, float b) {
197     if ($opt$lt(a, b)) {
198       throw new Error("Not expecting: " + a + " < " + b);
199     }
200     if ($opt$gt(a, b)) {
201       throw new Error("Not expecting: " + a + " > " + b);
202     }
203   }
204 
expectLt(double a, double b)205   public static void expectLt(double a, double b) {
206     if (!$opt$lt(a, b)) {
207       throw new Error("Expected: " + a + " < " + b);
208     }
209   }
210 
expectGt(double a, double b)211   public static void expectGt(double a, double b) {
212     if (!$opt$gt(a, b)) {
213       throw new Error("Expected: " + a + " > " + b);
214     }
215   }
216 
expectFalse(double a, double b)217   public static void expectFalse(double a, double b) {
218     if ($opt$lt(a, b)) {
219       throw new Error("Not expecting: " + a + " < " + b);
220     }
221     if ($opt$gt(a, b)) {
222       throw new Error("Not expecting: " + a + " > " + b);
223     }
224   }
225 
226 }
227 
228