blob: cb7b0b395ad57b8c13a5ad9f88bff1facc572e5f [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2007 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
jeffhao5d1ac922011-09-29 17:41:15 -070017import junit.framework.Assert;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070018import java.util.Arrays;
19import java.lang.reflect.Method;
jeffhao5d1ac922011-09-29 17:41:15 -070020
21public class Main {
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070022 public static void main(String args[]) throws Exception {
Elliott Hughes28c384b2012-06-15 16:46:25 -070023 test_Double_doubleToRawLongBits();
24 test_Double_longBitsToDouble();
25 test_Float_floatToRawIntBits();
26 test_Float_intBitsToFloat();
27 test_Math_abs_I();
28 test_Math_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010029 test_Math_min_I();
30 test_Math_max_I();
31 test_Math_min_J();
32 test_Math_max_J();
33 test_Math_min_F();
34 test_Math_max_F();
35 test_Math_min_D();
36 test_Math_max_D();
Zheng Xua3fe7422014-07-09 14:03:15 +080037 test_Short_reverseBytes();
38 test_Integer_reverseBytes();
39 test_Long_reverseBytes();
Serban Constantinescu23abec92014-07-02 16:13:38 +010040 test_Integer_reverse();
41 test_Long_reverse();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010042 test_StrictMath_abs_I();
43 test_StrictMath_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010044 test_StrictMath_min_I();
45 test_StrictMath_max_I();
46 test_StrictMath_min_J();
47 test_StrictMath_max_J();
48 test_StrictMath_min_F();
49 test_StrictMath_max_F();
50 test_StrictMath_min_D();
51 test_StrictMath_max_D();
Elliott Hughes28c384b2012-06-15 16:46:25 -070052 test_String_charAt();
53 test_String_compareTo();
54 test_String_indexOf();
55 test_String_isEmpty();
56 test_String_length();
Andreas Gampe7a949612014-07-08 11:03:59 -070057 test_Thread_currentThread();
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070058 initSupportMethodsForPeekPoke();
59 test_Memory_peekByte();
60 test_Memory_peekShort();
61 test_Memory_peekInt();
62 test_Memory_peekLong();
63 test_Memory_pokeByte();
64 test_Memory_pokeShort();
65 test_Memory_pokeInt();
66 test_Memory_pokeLong();
Andreas Gampe0cbfd442014-07-08 21:33:47 -070067 test_AtomicBoolean_compareAndSet();
68 test_AtomicInteger_compareAndSet();
69 test_AtomicLong_compareAndSet();
Elliott Hughes28c384b2012-06-15 16:46:25 -070070 }
71
Serban Constantinescu23abec92014-07-02 16:13:38 +010072 /*
73 * Determine if two floating point numbers are approximately equal.
74 *
75 * (Assumes that floating point is generally working, so we can't use
76 * this for the first set of tests.)
77 */
78 static boolean approxEqual(float a, float b, float maxDelta) {
79 if (a > b)
80 return (a - b) < maxDelta;
81 else
82 return (b - a) < maxDelta;
83 }
84 static boolean approxEqual(double a, double b, double maxDelta) {
85 if (a > b)
86 return (a - b) < maxDelta;
87 else
88 return (b - a) < maxDelta;
89 }
90
Andreas Gampe7a949612014-07-08 11:03:59 -070091 /**
92 * Will test inlining Thread.currentThread().
93 */
94 public static void test_Thread_currentThread() {
95 // 1. Do not use result.
96 Thread.currentThread();
97
98 // 2. Result should not be null.
99 Assert.assertNotNull(Thread.currentThread());
100 }
101
Andreas Gampe0cbfd442014-07-08 21:33:47 -0700102 /**
103 * Will test inlining CAS, by inclusion of AtomicBoolean in core.oat.
104 */
105 public static void test_AtomicBoolean_compareAndSet() {
106 java.util.concurrent.atomic.AtomicBoolean ab = new java.util.concurrent.atomic.AtomicBoolean();
107 Assert.assertEquals(ab.compareAndSet(false, false), true);
108 Assert.assertEquals(ab.compareAndSet(true, false), false);
109 Assert.assertEquals(ab.compareAndSet(true, true), false);
110 Assert.assertEquals(ab.compareAndSet(false, true), true);
111 Assert.assertEquals(ab.compareAndSet(false, true), false);
112 Assert.assertEquals(ab.compareAndSet(false, false), false);
113 Assert.assertEquals(ab.compareAndSet(true, true), true);
114 Assert.assertEquals(ab.compareAndSet(true, false), true);
115 Assert.assertEquals(ab.compareAndSet(true, false), false);
116 Assert.assertEquals(ab.compareAndSet(true, true), false);
117 Assert.assertEquals(ab.compareAndSet(false, false), true);
118 }
119
120 /**
121 * Will test inlining CAS, by inclusion of AtomicInteger in core.oat.
122 */
123 public static void test_AtomicInteger_compareAndSet() {
124 java.util.concurrent.atomic.AtomicInteger ab = new java.util.concurrent.atomic.AtomicInteger();
125 Assert.assertEquals(ab.compareAndSet(0, 0), true);
126 Assert.assertEquals(ab.compareAndSet(0x12345678, 0), false);
127 Assert.assertEquals(ab.compareAndSet(0x12345678, 0x12345678), false);
128 Assert.assertEquals(ab.compareAndSet(0, 0x12345678), true);
129 Assert.assertEquals(ab.compareAndSet(0, 0x12345678), false);
130 Assert.assertEquals(ab.compareAndSet(0, 0), false);
131 Assert.assertEquals(ab.compareAndSet(0x12345678, 0x12345678), true);
132 Assert.assertEquals(ab.compareAndSet(0x12345678, 0), true);
133 Assert.assertEquals(ab.compareAndSet(0x12345678, 0), false);
134 Assert.assertEquals(ab.compareAndSet(0x12345678, 0x12345678), false);
135 Assert.assertEquals(ab.compareAndSet(0, 0), true);
136 }
137
138 /**
139 * Will test inlining CAS, by inclusion of AtomicLong in core.oat.
140 */
141 public static void test_AtomicLong_compareAndSet() {
142 java.util.concurrent.atomic.AtomicLong ab = new java.util.concurrent.atomic.AtomicLong();
143 Assert.assertEquals(ab.compareAndSet(0l, 0l), true);
144 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0l), false);
145 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0x1234567890l), false);
146 Assert.assertEquals(ab.compareAndSet(0l, 0x1234567890l), true);
147 Assert.assertEquals(ab.compareAndSet(0l, 0x1234567890l), false);
148 Assert.assertEquals(ab.compareAndSet(0l, 0l), false);
149 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0x1234567890l), true);
150 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0l), true);
151 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0l), false);
152 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0x1234567890l), false);
153 Assert.assertEquals(ab.compareAndSet(0l, 0l), true);
154 }
155
Elliott Hughes28c384b2012-06-15 16:46:25 -0700156 public static void test_String_length() {
157 String str0 = "";
158 String str1 = "x";
159 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
160
161 Assert.assertEquals(str0.length(), 0);
162 Assert.assertEquals(str1.length(), 1);
163 Assert.assertEquals(str80.length(), 80);
164
165 String strNull = null;
166 try {
167 strNull.length();
168 Assert.fail();
169 } catch (NullPointerException expected) {
170 }
171 }
172
173 public static void test_String_isEmpty() {
174 String str0 = "";
175 String str1 = "x";
176
177 Assert.assertTrue(str0.isEmpty());
178 Assert.assertFalse(str1.isEmpty());
179
180 String strNull = null;
181 try {
182 strNull.isEmpty();
183 Assert.fail();
184 } catch (NullPointerException expected) {
185 }
186 }
187
188 public static void test_String_charAt() {
189 String testStr = "Now is the time";
190
191 Assert.assertEquals('N', testStr.charAt(0));
192 Assert.assertEquals('o', testStr.charAt(1));
193 Assert.assertEquals(' ', testStr.charAt(10));
194 Assert.assertEquals('e', testStr.charAt(testStr.length()-1));
195
196 try {
197 testStr.charAt(-1);
198 Assert.fail();
199 } catch (StringIndexOutOfBoundsException expected) {
200 }
201 try {
202 testStr.charAt(80);
203 Assert.fail();
204 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700205 }
206
Elliott Hughes28c384b2012-06-15 16:46:25 -0700207 String strNull = null;
208 try {
209 strNull.charAt(0);
210 Assert.fail();
211 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700212 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700213 }
jeffhao5d1ac922011-09-29 17:41:15 -0700214
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700215 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700216 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700217 public static void test_String_indexOf() {
218 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700219 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700220 String str3 = "abc";
221 String str10 = "abcdefghij";
222 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700223
Elliott Hughes28c384b2012-06-15 16:46:25 -0700224 int supplementaryChar = 0x20b9f;
225 String surrogatePair = "\ud842\udf9f";
226 String stringWithSurrogates = "hello " + surrogatePair + " world";
jeffhao5d1ac922011-09-29 17:41:15 -0700227
Elliott Hughes28c384b2012-06-15 16:46:25 -0700228 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
229 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
230 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
231 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
jeffhao5d1ac922011-09-29 17:41:15 -0700232
Elliott Hughes28c384b2012-06-15 16:46:25 -0700233 Assert.assertEquals(str0.indexOf('a'), -1);
234 Assert.assertEquals(str3.indexOf('a'), 0);
235 Assert.assertEquals(str3.indexOf('b'), 1);
236 Assert.assertEquals(str3.indexOf('c'), 2);
237 Assert.assertEquals(str10.indexOf('j'), 9);
238 Assert.assertEquals(str40.indexOf('a'), 0);
239 Assert.assertEquals(str40.indexOf('b'), 38);
240 Assert.assertEquals(str40.indexOf('c'), 39);
241 Assert.assertEquals(str0.indexOf('a',20), -1);
242 Assert.assertEquals(str0.indexOf('a',0), -1);
243 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700244 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700245 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700246 Assert.assertEquals(str3.indexOf('a',0), 0);
247 Assert.assertEquals(str3.indexOf('a',1), -1);
248 Assert.assertEquals(str3.indexOf('a',1234), -1);
249 Assert.assertEquals(str3.indexOf('b',0), 1);
250 Assert.assertEquals(str3.indexOf('b',1), 1);
251 Assert.assertEquals(str3.indexOf('c',2), 2);
252 Assert.assertEquals(str10.indexOf('j',5), 9);
253 Assert.assertEquals(str10.indexOf('j',9), 9);
254 Assert.assertEquals(str40.indexOf('a',10), 10);
255 Assert.assertEquals(str40.indexOf('b',40), -1);
256
257 String strNull = null;
258 try {
259 strNull.indexOf('a');
260 Assert.fail();
261 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700262 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700263 try {
264 strNull.indexOf('a', 0);
265 Assert.fail();
266 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700267 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700268 try {
269 strNull.indexOf('a', -1);
270 Assert.fail();
271 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700272 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700273 }
jeffhao5d1ac922011-09-29 17:41:15 -0700274
Elliott Hughes28c384b2012-06-15 16:46:25 -0700275 public static void test_String_compareTo() {
276 String test = "0123456789";
277 String test1 = new String("0123456789"); // different object
278 String test2 = new String("0123456780"); // different value
279 String offset = new String("xxx0123456789yyy");
280 String sub = offset.substring(3, 13);
281 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
282 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
283 String lc = "abcdefg";
284 String uc = "ABCDEFG";
285 Object blah = new Object();
286
287 Assert.assertTrue(lc.toUpperCase().equals(uc));
288
289 Assert.assertEquals(str32.compareTo(str33), -1);
290 Assert.assertEquals(str33.compareTo(str32), 1);
291
292 Assert.assertTrue(test.equals(test));
293 Assert.assertTrue(test.equals(test1));
294 Assert.assertFalse(test.equals(test2));
295
296 Assert.assertEquals(test.compareTo(test1), 0);
297 Assert.assertTrue(test1.compareTo(test2) > 0);
298 Assert.assertTrue(test2.compareTo(test1) < 0);
299
300 // Compare string with a nonzero offset, in left/right side.
301 Assert.assertEquals(test.compareTo(sub), 0);
302 Assert.assertEquals(sub.compareTo(test), 0);
303 Assert.assertTrue(test.equals(sub));
304 Assert.assertTrue(sub.equals(test));
305 // Same base, one is a substring.
306 Assert.assertFalse(offset.equals(sub));
307 Assert.assertFalse(sub.equals(offset));
308 // Wrong class.
309 Assert.assertFalse(test.equals(blah));
310
311 // Null lhs - throw.
312 try {
313 test.compareTo(null);
314 Assert.fail("didn't get expected npe");
315 } catch (NullPointerException npe) {
316 }
317 // Null rhs - okay.
318 Assert.assertFalse(test.equals(null));
319
320 test = test.substring(1);
321 Assert.assertTrue(test.equals("123456789"));
322 Assert.assertFalse(test.equals(test1));
323
324 test = test.substring(1);
325 Assert.assertTrue(test.equals("23456789"));
326
327 test = test.substring(1);
328 Assert.assertTrue(test.equals("3456789"));
329
330 test = test.substring(1);
331 Assert.assertTrue(test.equals("456789"));
332
333 test = test.substring(3,5);
334 Assert.assertTrue(test.equals("78"));
335
336 test = "this/is/a/path";
337 String[] strings = test.split("/");
338 Assert.assertEquals(4, strings.length);
339
340 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
341 Assert.assertEquals("this is a path", test.replace("/", " "));
342 }
343
344 public static void test_Math_abs_I() {
345 Assert.assertEquals(Math.abs(0), 0);
346 Assert.assertEquals(Math.abs(123), 123);
347 Assert.assertEquals(Math.abs(-123), 123);
348 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
349 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
350 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100351 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700352 }
353
354 public static void test_Math_abs_J() {
355 Assert.assertEquals(Math.abs(0L), 0L);
356 Assert.assertEquals(Math.abs(123L), 123L);
357 Assert.assertEquals(Math.abs(-123L), 123L);
358 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
359 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
360 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
361 }
362
Serban Constantinescu23abec92014-07-02 16:13:38 +0100363 public static void test_Math_min_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700364 Assert.assertEquals(Math.min(0, 0), 0);
365 Assert.assertEquals(Math.min(1, 0), 0);
366 Assert.assertEquals(Math.min(0, 1), 0);
367 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
368 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
369 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
370 }
371
Serban Constantinescu23abec92014-07-02 16:13:38 +0100372 public static void test_Math_max_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700373 Assert.assertEquals(Math.max(0, 0), 0);
374 Assert.assertEquals(Math.max(1, 0), 1);
375 Assert.assertEquals(Math.max(0, 1), 1);
376 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
377 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
378 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
379 }
380
Serban Constantinescu23abec92014-07-02 16:13:38 +0100381 public static void test_Math_min_J() {
382 Assert.assertEquals(Math.min(0L, 0L), 0L);
383 Assert.assertEquals(Math.min(1L, 0L), 0L);
384 Assert.assertEquals(Math.min(0L, 1L), 0L);
385 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
386 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
387 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
388 }
389
390 public static void test_Math_max_J() {
391 Assert.assertEquals(Math.max(0L, 0L), 0L);
392 Assert.assertEquals(Math.max(1L, 0L), 1L);
393 Assert.assertEquals(Math.max(0L, 1L), 1L);
394 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
395 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
396 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
397 }
398
399 public static void test_Math_min_F() {
400 Assert.assertTrue(approxEqual(Math.min(0.0f, 0.0f), 0.0f, 0.001f));
401 Assert.assertTrue(approxEqual(Math.min(1.0f, 0.0f), 0.0f, 0.001f));
402 Assert.assertTrue(approxEqual(Math.min(0.0f, 1.0f), 0.0f, 0.001f));
403 Assert.assertTrue(approxEqual(Math.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f));
404 Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f));
405 Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f));
406 }
407
408 public static void test_Math_max_F() {
409 Assert.assertTrue(approxEqual(Math.max(0.0f, 0.0f), 0.0f, 0.001f));
410 Assert.assertTrue(approxEqual(Math.max(1.0f, 0.0f), 1.0f, 0.001f));
411 Assert.assertTrue(approxEqual(Math.max(0.0f, 1.0f), 1.0f, 0.001f));
412 Assert.assertTrue(approxEqual(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
413 Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f));
414 Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
415 }
416
417 public static void test_Math_min_D() {
418 Assert.assertTrue(approxEqual(Math.min(0.0d, 0.0d), 0.0d, 0.001d));
419 Assert.assertTrue(approxEqual(Math.min(1.0d, 0.0d), 0.0d, 0.001d));
420 Assert.assertTrue(approxEqual(Math.min(0.0d, 1.0d), 0.0d, 0.001d));
421 Assert.assertTrue(approxEqual(Math.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d));
422 Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d));
423 Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d));
424 }
425
426 public static void test_Math_max_D() {
427 Assert.assertTrue(approxEqual(Math.max(0.0d, 0.0d), 0.0d, 0.001d));
428 Assert.assertTrue(approxEqual(Math.max(1.0d, 0.0d), 1.0d, 0.001d));
429 Assert.assertTrue(approxEqual(Math.max(0.0d, 1.0d), 1.0d, 0.001d));
430 Assert.assertTrue(approxEqual(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
431 Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d));
432 Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
433 }
434
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100435 public static void test_StrictMath_abs_I() {
436 Assert.assertEquals(StrictMath.abs(0), 0);
437 Assert.assertEquals(StrictMath.abs(123), 123);
438 Assert.assertEquals(StrictMath.abs(-123), 123);
439 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
440 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
441 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
442 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
443 }
444
445 public static void test_StrictMath_abs_J() {
446 Assert.assertEquals(StrictMath.abs(0L), 0L);
447 Assert.assertEquals(StrictMath.abs(123L), 123L);
448 Assert.assertEquals(StrictMath.abs(-123L), 123L);
449 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
450 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
451 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
452 }
453
Serban Constantinescu23abec92014-07-02 16:13:38 +0100454 public static void test_StrictMath_min_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100455 Assert.assertEquals(StrictMath.min(0, 0), 0);
456 Assert.assertEquals(StrictMath.min(1, 0), 0);
457 Assert.assertEquals(StrictMath.min(0, 1), 0);
458 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
459 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
460 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
461 }
462
Serban Constantinescu23abec92014-07-02 16:13:38 +0100463 public static void test_StrictMath_max_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100464 Assert.assertEquals(StrictMath.max(0, 0), 0);
465 Assert.assertEquals(StrictMath.max(1, 0), 1);
466 Assert.assertEquals(StrictMath.max(0, 1), 1);
467 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
468 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
469 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
470 }
471
Serban Constantinescu23abec92014-07-02 16:13:38 +0100472 public static void test_StrictMath_min_J() {
473 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
474 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
475 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
476 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
477 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
478 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
479 }
480
481 public static void test_StrictMath_max_J() {
482 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
483 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
484 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
485 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
486 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
487 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
488 }
489
490 public static void test_StrictMath_min_F() {
491 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 0.0f), 0.0f, 0.001f));
492 Assert.assertTrue(approxEqual(StrictMath.min(1.0f, 0.0f), 0.0f, 0.001f));
493 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 1.0f), 0.0f, 0.001f));
494 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f));
495 Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f));
496 Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f));
497 }
498
499 public static void test_StrictMath_max_F() {
500 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 0.0f), 0.0f, 0.001f));
501 Assert.assertTrue(approxEqual(StrictMath.max(1.0f, 0.0f), 1.0f, 0.001f));
502 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 1.0f), 1.0f, 0.001f));
503 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
504 Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f));
505 Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
506 }
507
508 public static void test_StrictMath_min_D() {
509 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 0.0d), 0.0d, 0.001d));
510 Assert.assertTrue(approxEqual(StrictMath.min(1.0d, 0.0d), 0.0d, 0.001d));
511 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 1.0d), 0.0d, 0.001d));
512 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d));
513 Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d));
514 Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d));
515 }
516
517 public static void test_StrictMath_max_D() {
518 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 0.0d), 0.0d, 0.001d));
519 Assert.assertTrue(approxEqual(StrictMath.max(1.0d, 0.0d), 1.0d, 0.001d));
520 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 1.0d), 1.0d, 0.001d));
521 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
522 Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d));
523 Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
524 }
525
Elliott Hughes28c384b2012-06-15 16:46:25 -0700526 public static void test_Float_floatToRawIntBits() {
527 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
528 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
529 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
530 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
531 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
532 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
533 }
534
535 public static void test_Float_intBitsToFloat() {
536 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
537 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
538 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
539 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
540 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
541 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
542 }
543
544 public static void test_Double_doubleToRawLongBits() {
545 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
546 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
547 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
548 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
549 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
550 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
551 }
552
553 public static void test_Double_longBitsToDouble() {
554 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
555 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
556 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
557 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
558 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
559 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
560 }
Serban Constantinescu23abec92014-07-02 16:13:38 +0100561
Zheng Xua3fe7422014-07-09 14:03:15 +0800562 public static void test_Short_reverseBytes() {
563 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
564 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
565 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
566 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
567 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
568 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
569 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
570 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
571 }
572
573 public static void test_Integer_reverseBytes() {
574 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
575 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
576 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
577 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
578 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
579 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
580 }
581
582 public static void test_Long_reverseBytes() {
583 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
584 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
585 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
586 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
587 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
588 }
589
Serban Constantinescu23abec92014-07-02 16:13:38 +0100590 public static void test_Integer_reverse() {
591 Assert.assertEquals(Integer.reverse(1), 0x80000000);
592 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
593 Assert.assertEquals(Integer.reverse(0), 0);
594 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
595 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
596 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
597 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
598 }
599
600 public static void test_Long_reverse() {
601 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
602 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
603 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +0800604 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
605 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
606 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100607 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
608 }
609
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700610 static Object runtime;
611 static Method address_of;
612 static Method peek_byte;
613 static Method peek_short;
614 static Method peek_int;
615 static Method peek_long;
616 static Method poke_byte;
617 static Method poke_short;
618 static Method poke_int;
619 static Method poke_long;
620
621 public static void initSupportMethodsForPeekPoke() throws Exception {
622 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
623 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
624 runtime = get_runtime.invoke(null);
625 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
626
627 Class<?> io_memory = Class.forName("libcore.io.Memory");
628 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
629 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
630 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
631 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
632 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
633 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
634 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
635 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
636 }
637
638 public static void test_Memory_peekByte() throws Exception {
639 byte[] b = new byte [2];
640 b[0] = 0x12;
641 b[1] = 0x11;
642 long address = (long)address_of.invoke(runtime, b);
643 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
644 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
645 }
646
647 public static void test_Memory_peekShort() throws Exception {
648 byte[] b = new byte [3];
649 b[0] = 0x13;
650 b[1] = 0x12;
651 b[2] = 0x11;
652 long address = (long)address_of.invoke(runtime, b);
653 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
654 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
655 }
656
657 public static void test_Memory_peekInt() throws Exception {
658 byte[] b = new byte [5];
659 b[0] = 0x15;
660 b[1] = 0x14;
661 b[2] = 0x13;
662 b[3] = 0x12;
663 b[4] = 0x11;
664 long address = (long)address_of.invoke(runtime, b);
665 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
666 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
667 }
668
669 public static void test_Memory_peekLong() throws Exception {
670 byte[] b = new byte [9];
671 b[0] = 0x19;
672 b[1] = 0x18;
673 b[2] = 0x17;
674 b[3] = 0x16;
675 b[4] = 0x15;
676 b[5] = 0x14;
677 b[6] = 0x13;
678 b[7] = 0x12;
679 b[8] = 0x11;
680 long address = (long)address_of.invoke(runtime, b);
681 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
682 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
683 }
684
685 public static void test_Memory_pokeByte() throws Exception {
686 byte[] r = {0x11, 0x12};
687 byte[] b = new byte [2];
688 long address = (long)address_of.invoke(runtime, b);
689 poke_byte.invoke(null, address, (byte)0x11);
690 poke_byte.invoke(null, address + 1, (byte)0x12);
691 Assert.assertTrue(Arrays.equals(r, b));
692 }
693
694 public static void test_Memory_pokeShort() throws Exception {
695 byte[] ra = {0x12, 0x11, 0x13};
696 byte[] ru = {0x12, 0x22, 0x21};
697 byte[] b = new byte [3];
698 long address = (long)address_of.invoke(runtime, b);
699
700 // Aligned write
701 b[2] = 0x13;
702 poke_short.invoke(null, address, (short)0x1112, false);
703 Assert.assertTrue(Arrays.equals(ra, b));
704
705 // Unaligned write
706 poke_short.invoke(null, address + 1, (short)0x2122, false);
707 Assert.assertTrue(Arrays.equals(ru, b));
708 }
709
710 public static void test_Memory_pokeInt() throws Exception {
711 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
712 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
713 byte[] b = new byte [5];
714 long address = (long)address_of.invoke(runtime, b);
715
716 b[4] = 0x15;
717 poke_int.invoke(null, address, (int)0x11121314, false);
718 Assert.assertTrue(Arrays.equals(ra, b));
719
720 poke_int.invoke(null, address + 1, (int)0x21222324, false);
721 Assert.assertTrue(Arrays.equals(ru, b));
722 }
723
724 public static void test_Memory_pokeLong() throws Exception {
725 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
726 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
727 byte[] b = new byte [9];
728 long address = (long)address_of.invoke(runtime, b);
729
730 b[8] = 0x19;
731 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
732 Assert.assertTrue(Arrays.equals(ra, b));
733
734 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
735 Assert.assertTrue(Arrays.equals(ru, b));
736 }
jeffhao5d1ac922011-09-29 17:41:15 -0700737}