blob: f412034de26669c3f1b317a423c8fb24d61ba66f [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();
37 test_Integer_reverse();
38 test_Long_reverse();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010039 test_StrictMath_abs_I();
40 test_StrictMath_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010041 test_StrictMath_min_I();
42 test_StrictMath_max_I();
43 test_StrictMath_min_J();
44 test_StrictMath_max_J();
45 test_StrictMath_min_F();
46 test_StrictMath_max_F();
47 test_StrictMath_min_D();
48 test_StrictMath_max_D();
Elliott Hughes28c384b2012-06-15 16:46:25 -070049 test_String_charAt();
50 test_String_compareTo();
51 test_String_indexOf();
52 test_String_isEmpty();
53 test_String_length();
Andreas Gampe7a949612014-07-08 11:03:59 -070054 test_Thread_currentThread();
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070055 initSupportMethodsForPeekPoke();
56 test_Memory_peekByte();
57 test_Memory_peekShort();
58 test_Memory_peekInt();
59 test_Memory_peekLong();
60 test_Memory_pokeByte();
61 test_Memory_pokeShort();
62 test_Memory_pokeInt();
63 test_Memory_pokeLong();
Andreas Gampe0cbfd442014-07-08 21:33:47 -070064 test_AtomicBoolean_compareAndSet();
65 test_AtomicInteger_compareAndSet();
66 test_AtomicLong_compareAndSet();
Elliott Hughes28c384b2012-06-15 16:46:25 -070067 }
68
Serban Constantinescu23abec92014-07-02 16:13:38 +010069 /*
70 * Determine if two floating point numbers are approximately equal.
71 *
72 * (Assumes that floating point is generally working, so we can't use
73 * this for the first set of tests.)
74 */
75 static boolean approxEqual(float a, float b, float maxDelta) {
76 if (a > b)
77 return (a - b) < maxDelta;
78 else
79 return (b - a) < maxDelta;
80 }
81 static boolean approxEqual(double a, double b, double maxDelta) {
82 if (a > b)
83 return (a - b) < maxDelta;
84 else
85 return (b - a) < maxDelta;
86 }
87
Andreas Gampe7a949612014-07-08 11:03:59 -070088 /**
89 * Will test inlining Thread.currentThread().
90 */
91 public static void test_Thread_currentThread() {
92 // 1. Do not use result.
93 Thread.currentThread();
94
95 // 2. Result should not be null.
96 Assert.assertNotNull(Thread.currentThread());
97 }
98
Andreas Gampe0cbfd442014-07-08 21:33:47 -070099 /**
100 * Will test inlining CAS, by inclusion of AtomicBoolean in core.oat.
101 */
102 public static void test_AtomicBoolean_compareAndSet() {
103 java.util.concurrent.atomic.AtomicBoolean ab = new java.util.concurrent.atomic.AtomicBoolean();
104 Assert.assertEquals(ab.compareAndSet(false, false), true);
105 Assert.assertEquals(ab.compareAndSet(true, false), false);
106 Assert.assertEquals(ab.compareAndSet(true, true), false);
107 Assert.assertEquals(ab.compareAndSet(false, true), true);
108 Assert.assertEquals(ab.compareAndSet(false, true), false);
109 Assert.assertEquals(ab.compareAndSet(false, false), false);
110 Assert.assertEquals(ab.compareAndSet(true, true), true);
111 Assert.assertEquals(ab.compareAndSet(true, false), true);
112 Assert.assertEquals(ab.compareAndSet(true, false), false);
113 Assert.assertEquals(ab.compareAndSet(true, true), false);
114 Assert.assertEquals(ab.compareAndSet(false, false), true);
115 }
116
117 /**
118 * Will test inlining CAS, by inclusion of AtomicInteger in core.oat.
119 */
120 public static void test_AtomicInteger_compareAndSet() {
121 java.util.concurrent.atomic.AtomicInteger ab = new java.util.concurrent.atomic.AtomicInteger();
122 Assert.assertEquals(ab.compareAndSet(0, 0), true);
123 Assert.assertEquals(ab.compareAndSet(0x12345678, 0), false);
124 Assert.assertEquals(ab.compareAndSet(0x12345678, 0x12345678), false);
125 Assert.assertEquals(ab.compareAndSet(0, 0x12345678), true);
126 Assert.assertEquals(ab.compareAndSet(0, 0x12345678), false);
127 Assert.assertEquals(ab.compareAndSet(0, 0), false);
128 Assert.assertEquals(ab.compareAndSet(0x12345678, 0x12345678), true);
129 Assert.assertEquals(ab.compareAndSet(0x12345678, 0), true);
130 Assert.assertEquals(ab.compareAndSet(0x12345678, 0), false);
131 Assert.assertEquals(ab.compareAndSet(0x12345678, 0x12345678), false);
132 Assert.assertEquals(ab.compareAndSet(0, 0), true);
133 }
134
135 /**
136 * Will test inlining CAS, by inclusion of AtomicLong in core.oat.
137 */
138 public static void test_AtomicLong_compareAndSet() {
139 java.util.concurrent.atomic.AtomicLong ab = new java.util.concurrent.atomic.AtomicLong();
140 Assert.assertEquals(ab.compareAndSet(0l, 0l), true);
141 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0l), false);
142 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0x1234567890l), false);
143 Assert.assertEquals(ab.compareAndSet(0l, 0x1234567890l), true);
144 Assert.assertEquals(ab.compareAndSet(0l, 0x1234567890l), false);
145 Assert.assertEquals(ab.compareAndSet(0l, 0l), false);
146 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0x1234567890l), true);
147 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0l), true);
148 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0l), false);
149 Assert.assertEquals(ab.compareAndSet(0x1234567890l, 0x1234567890l), false);
150 Assert.assertEquals(ab.compareAndSet(0l, 0l), true);
151 }
152
Elliott Hughes28c384b2012-06-15 16:46:25 -0700153 public static void test_String_length() {
154 String str0 = "";
155 String str1 = "x";
156 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
157
158 Assert.assertEquals(str0.length(), 0);
159 Assert.assertEquals(str1.length(), 1);
160 Assert.assertEquals(str80.length(), 80);
161
162 String strNull = null;
163 try {
164 strNull.length();
165 Assert.fail();
166 } catch (NullPointerException expected) {
167 }
168 }
169
170 public static void test_String_isEmpty() {
171 String str0 = "";
172 String str1 = "x";
173
174 Assert.assertTrue(str0.isEmpty());
175 Assert.assertFalse(str1.isEmpty());
176
177 String strNull = null;
178 try {
179 strNull.isEmpty();
180 Assert.fail();
181 } catch (NullPointerException expected) {
182 }
183 }
184
185 public static void test_String_charAt() {
186 String testStr = "Now is the time";
187
188 Assert.assertEquals('N', testStr.charAt(0));
189 Assert.assertEquals('o', testStr.charAt(1));
190 Assert.assertEquals(' ', testStr.charAt(10));
191 Assert.assertEquals('e', testStr.charAt(testStr.length()-1));
192
193 try {
194 testStr.charAt(-1);
195 Assert.fail();
196 } catch (StringIndexOutOfBoundsException expected) {
197 }
198 try {
199 testStr.charAt(80);
200 Assert.fail();
201 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700202 }
203
Elliott Hughes28c384b2012-06-15 16:46:25 -0700204 String strNull = null;
205 try {
206 strNull.charAt(0);
207 Assert.fail();
208 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700209 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700210 }
jeffhao5d1ac922011-09-29 17:41:15 -0700211
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700212 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700213 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700214 public static void test_String_indexOf() {
215 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700216 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700217 String str3 = "abc";
218 String str10 = "abcdefghij";
219 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700220
Elliott Hughes28c384b2012-06-15 16:46:25 -0700221 int supplementaryChar = 0x20b9f;
222 String surrogatePair = "\ud842\udf9f";
223 String stringWithSurrogates = "hello " + surrogatePair + " world";
jeffhao5d1ac922011-09-29 17:41:15 -0700224
Elliott Hughes28c384b2012-06-15 16:46:25 -0700225 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
226 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
227 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
228 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
jeffhao5d1ac922011-09-29 17:41:15 -0700229
Elliott Hughes28c384b2012-06-15 16:46:25 -0700230 Assert.assertEquals(str0.indexOf('a'), -1);
231 Assert.assertEquals(str3.indexOf('a'), 0);
232 Assert.assertEquals(str3.indexOf('b'), 1);
233 Assert.assertEquals(str3.indexOf('c'), 2);
234 Assert.assertEquals(str10.indexOf('j'), 9);
235 Assert.assertEquals(str40.indexOf('a'), 0);
236 Assert.assertEquals(str40.indexOf('b'), 38);
237 Assert.assertEquals(str40.indexOf('c'), 39);
238 Assert.assertEquals(str0.indexOf('a',20), -1);
239 Assert.assertEquals(str0.indexOf('a',0), -1);
240 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700241 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700242 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700243 Assert.assertEquals(str3.indexOf('a',0), 0);
244 Assert.assertEquals(str3.indexOf('a',1), -1);
245 Assert.assertEquals(str3.indexOf('a',1234), -1);
246 Assert.assertEquals(str3.indexOf('b',0), 1);
247 Assert.assertEquals(str3.indexOf('b',1), 1);
248 Assert.assertEquals(str3.indexOf('c',2), 2);
249 Assert.assertEquals(str10.indexOf('j',5), 9);
250 Assert.assertEquals(str10.indexOf('j',9), 9);
251 Assert.assertEquals(str40.indexOf('a',10), 10);
252 Assert.assertEquals(str40.indexOf('b',40), -1);
253
254 String strNull = null;
255 try {
256 strNull.indexOf('a');
257 Assert.fail();
258 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700259 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700260 try {
261 strNull.indexOf('a', 0);
262 Assert.fail();
263 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700264 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700265 try {
266 strNull.indexOf('a', -1);
267 Assert.fail();
268 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700269 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700270 }
jeffhao5d1ac922011-09-29 17:41:15 -0700271
Elliott Hughes28c384b2012-06-15 16:46:25 -0700272 public static void test_String_compareTo() {
273 String test = "0123456789";
274 String test1 = new String("0123456789"); // different object
275 String test2 = new String("0123456780"); // different value
276 String offset = new String("xxx0123456789yyy");
277 String sub = offset.substring(3, 13);
278 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
279 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
280 String lc = "abcdefg";
281 String uc = "ABCDEFG";
282 Object blah = new Object();
283
284 Assert.assertTrue(lc.toUpperCase().equals(uc));
285
286 Assert.assertEquals(str32.compareTo(str33), -1);
287 Assert.assertEquals(str33.compareTo(str32), 1);
288
289 Assert.assertTrue(test.equals(test));
290 Assert.assertTrue(test.equals(test1));
291 Assert.assertFalse(test.equals(test2));
292
293 Assert.assertEquals(test.compareTo(test1), 0);
294 Assert.assertTrue(test1.compareTo(test2) > 0);
295 Assert.assertTrue(test2.compareTo(test1) < 0);
296
297 // Compare string with a nonzero offset, in left/right side.
298 Assert.assertEquals(test.compareTo(sub), 0);
299 Assert.assertEquals(sub.compareTo(test), 0);
300 Assert.assertTrue(test.equals(sub));
301 Assert.assertTrue(sub.equals(test));
302 // Same base, one is a substring.
303 Assert.assertFalse(offset.equals(sub));
304 Assert.assertFalse(sub.equals(offset));
305 // Wrong class.
306 Assert.assertFalse(test.equals(blah));
307
308 // Null lhs - throw.
309 try {
310 test.compareTo(null);
311 Assert.fail("didn't get expected npe");
312 } catch (NullPointerException npe) {
313 }
314 // Null rhs - okay.
315 Assert.assertFalse(test.equals(null));
316
317 test = test.substring(1);
318 Assert.assertTrue(test.equals("123456789"));
319 Assert.assertFalse(test.equals(test1));
320
321 test = test.substring(1);
322 Assert.assertTrue(test.equals("23456789"));
323
324 test = test.substring(1);
325 Assert.assertTrue(test.equals("3456789"));
326
327 test = test.substring(1);
328 Assert.assertTrue(test.equals("456789"));
329
330 test = test.substring(3,5);
331 Assert.assertTrue(test.equals("78"));
332
333 test = "this/is/a/path";
334 String[] strings = test.split("/");
335 Assert.assertEquals(4, strings.length);
336
337 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
338 Assert.assertEquals("this is a path", test.replace("/", " "));
339 }
340
341 public static void test_Math_abs_I() {
342 Assert.assertEquals(Math.abs(0), 0);
343 Assert.assertEquals(Math.abs(123), 123);
344 Assert.assertEquals(Math.abs(-123), 123);
345 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
346 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
347 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100348 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700349 }
350
351 public static void test_Math_abs_J() {
352 Assert.assertEquals(Math.abs(0L), 0L);
353 Assert.assertEquals(Math.abs(123L), 123L);
354 Assert.assertEquals(Math.abs(-123L), 123L);
355 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
356 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
357 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
358 }
359
Serban Constantinescu23abec92014-07-02 16:13:38 +0100360 public static void test_Math_min_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700361 Assert.assertEquals(Math.min(0, 0), 0);
362 Assert.assertEquals(Math.min(1, 0), 0);
363 Assert.assertEquals(Math.min(0, 1), 0);
364 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
365 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
366 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
367 }
368
Serban Constantinescu23abec92014-07-02 16:13:38 +0100369 public static void test_Math_max_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700370 Assert.assertEquals(Math.max(0, 0), 0);
371 Assert.assertEquals(Math.max(1, 0), 1);
372 Assert.assertEquals(Math.max(0, 1), 1);
373 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
374 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
375 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
376 }
377
Serban Constantinescu23abec92014-07-02 16:13:38 +0100378 public static void test_Math_min_J() {
379 Assert.assertEquals(Math.min(0L, 0L), 0L);
380 Assert.assertEquals(Math.min(1L, 0L), 0L);
381 Assert.assertEquals(Math.min(0L, 1L), 0L);
382 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
383 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
384 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
385 }
386
387 public static void test_Math_max_J() {
388 Assert.assertEquals(Math.max(0L, 0L), 0L);
389 Assert.assertEquals(Math.max(1L, 0L), 1L);
390 Assert.assertEquals(Math.max(0L, 1L), 1L);
391 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
392 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
393 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
394 }
395
396 public static void test_Math_min_F() {
397 Assert.assertTrue(approxEqual(Math.min(0.0f, 0.0f), 0.0f, 0.001f));
398 Assert.assertTrue(approxEqual(Math.min(1.0f, 0.0f), 0.0f, 0.001f));
399 Assert.assertTrue(approxEqual(Math.min(0.0f, 1.0f), 0.0f, 0.001f));
400 Assert.assertTrue(approxEqual(Math.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f));
401 Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f));
402 Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f));
403 }
404
405 public static void test_Math_max_F() {
406 Assert.assertTrue(approxEqual(Math.max(0.0f, 0.0f), 0.0f, 0.001f));
407 Assert.assertTrue(approxEqual(Math.max(1.0f, 0.0f), 1.0f, 0.001f));
408 Assert.assertTrue(approxEqual(Math.max(0.0f, 1.0f), 1.0f, 0.001f));
409 Assert.assertTrue(approxEqual(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
410 Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f));
411 Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
412 }
413
414 public static void test_Math_min_D() {
415 Assert.assertTrue(approxEqual(Math.min(0.0d, 0.0d), 0.0d, 0.001d));
416 Assert.assertTrue(approxEqual(Math.min(1.0d, 0.0d), 0.0d, 0.001d));
417 Assert.assertTrue(approxEqual(Math.min(0.0d, 1.0d), 0.0d, 0.001d));
418 Assert.assertTrue(approxEqual(Math.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d));
419 Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d));
420 Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d));
421 }
422
423 public static void test_Math_max_D() {
424 Assert.assertTrue(approxEqual(Math.max(0.0d, 0.0d), 0.0d, 0.001d));
425 Assert.assertTrue(approxEqual(Math.max(1.0d, 0.0d), 1.0d, 0.001d));
426 Assert.assertTrue(approxEqual(Math.max(0.0d, 1.0d), 1.0d, 0.001d));
427 Assert.assertTrue(approxEqual(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
428 Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d));
429 Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
430 }
431
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100432 public static void test_StrictMath_abs_I() {
433 Assert.assertEquals(StrictMath.abs(0), 0);
434 Assert.assertEquals(StrictMath.abs(123), 123);
435 Assert.assertEquals(StrictMath.abs(-123), 123);
436 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
437 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
438 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
439 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
440 }
441
442 public static void test_StrictMath_abs_J() {
443 Assert.assertEquals(StrictMath.abs(0L), 0L);
444 Assert.assertEquals(StrictMath.abs(123L), 123L);
445 Assert.assertEquals(StrictMath.abs(-123L), 123L);
446 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
447 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
448 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
449 }
450
Serban Constantinescu23abec92014-07-02 16:13:38 +0100451 public static void test_StrictMath_min_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100452 Assert.assertEquals(StrictMath.min(0, 0), 0);
453 Assert.assertEquals(StrictMath.min(1, 0), 0);
454 Assert.assertEquals(StrictMath.min(0, 1), 0);
455 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
456 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
457 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
458 }
459
Serban Constantinescu23abec92014-07-02 16:13:38 +0100460 public static void test_StrictMath_max_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100461 Assert.assertEquals(StrictMath.max(0, 0), 0);
462 Assert.assertEquals(StrictMath.max(1, 0), 1);
463 Assert.assertEquals(StrictMath.max(0, 1), 1);
464 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
465 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
466 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
467 }
468
Serban Constantinescu23abec92014-07-02 16:13:38 +0100469 public static void test_StrictMath_min_J() {
470 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
471 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
472 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
473 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
474 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
475 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
476 }
477
478 public static void test_StrictMath_max_J() {
479 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
480 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
481 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
482 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
483 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
484 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
485 }
486
487 public static void test_StrictMath_min_F() {
488 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 0.0f), 0.0f, 0.001f));
489 Assert.assertTrue(approxEqual(StrictMath.min(1.0f, 0.0f), 0.0f, 0.001f));
490 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 1.0f), 0.0f, 0.001f));
491 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f));
492 Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f));
493 Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f));
494 }
495
496 public static void test_StrictMath_max_F() {
497 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 0.0f), 0.0f, 0.001f));
498 Assert.assertTrue(approxEqual(StrictMath.max(1.0f, 0.0f), 1.0f, 0.001f));
499 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 1.0f), 1.0f, 0.001f));
500 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
501 Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f));
502 Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
503 }
504
505 public static void test_StrictMath_min_D() {
506 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 0.0d), 0.0d, 0.001d));
507 Assert.assertTrue(approxEqual(StrictMath.min(1.0d, 0.0d), 0.0d, 0.001d));
508 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 1.0d), 0.0d, 0.001d));
509 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d));
510 Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d));
511 Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d));
512 }
513
514 public static void test_StrictMath_max_D() {
515 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 0.0d), 0.0d, 0.001d));
516 Assert.assertTrue(approxEqual(StrictMath.max(1.0d, 0.0d), 1.0d, 0.001d));
517 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 1.0d), 1.0d, 0.001d));
518 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
519 Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d));
520 Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
521 }
522
Elliott Hughes28c384b2012-06-15 16:46:25 -0700523 public static void test_Float_floatToRawIntBits() {
524 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
525 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
526 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
527 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
528 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
529 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
530 }
531
532 public static void test_Float_intBitsToFloat() {
533 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
534 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
535 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
536 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
537 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
538 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
539 }
540
541 public static void test_Double_doubleToRawLongBits() {
542 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
543 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
544 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
545 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
546 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
547 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
548 }
549
550 public static void test_Double_longBitsToDouble() {
551 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
552 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
553 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
554 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
555 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
556 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
557 }
Serban Constantinescu23abec92014-07-02 16:13:38 +0100558
559 public static void test_Integer_reverse() {
560 Assert.assertEquals(Integer.reverse(1), 0x80000000);
561 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
562 Assert.assertEquals(Integer.reverse(0), 0);
563 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
564 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
565 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
566 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
567 }
568
569 public static void test_Long_reverse() {
570 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
571 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
572 Assert.assertEquals(Long.reverse(0L), 0L);
573 // FIXME: This asserts fail with or without this patch. I have collected
574 // the expected results on my host machine.
575 // Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
576 // Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
577 // Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
578 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
579 }
580
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700581 static Object runtime;
582 static Method address_of;
583 static Method peek_byte;
584 static Method peek_short;
585 static Method peek_int;
586 static Method peek_long;
587 static Method poke_byte;
588 static Method poke_short;
589 static Method poke_int;
590 static Method poke_long;
591
592 public static void initSupportMethodsForPeekPoke() throws Exception {
593 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
594 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
595 runtime = get_runtime.invoke(null);
596 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
597
598 Class<?> io_memory = Class.forName("libcore.io.Memory");
599 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
600 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
601 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
602 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
603 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
604 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
605 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
606 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
607 }
608
609 public static void test_Memory_peekByte() throws Exception {
610 byte[] b = new byte [2];
611 b[0] = 0x12;
612 b[1] = 0x11;
613 long address = (long)address_of.invoke(runtime, b);
614 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
615 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
616 }
617
618 public static void test_Memory_peekShort() throws Exception {
619 byte[] b = new byte [3];
620 b[0] = 0x13;
621 b[1] = 0x12;
622 b[2] = 0x11;
623 long address = (long)address_of.invoke(runtime, b);
624 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
625 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
626 }
627
628 public static void test_Memory_peekInt() throws Exception {
629 byte[] b = new byte [5];
630 b[0] = 0x15;
631 b[1] = 0x14;
632 b[2] = 0x13;
633 b[3] = 0x12;
634 b[4] = 0x11;
635 long address = (long)address_of.invoke(runtime, b);
636 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
637 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
638 }
639
640 public static void test_Memory_peekLong() throws Exception {
641 byte[] b = new byte [9];
642 b[0] = 0x19;
643 b[1] = 0x18;
644 b[2] = 0x17;
645 b[3] = 0x16;
646 b[4] = 0x15;
647 b[5] = 0x14;
648 b[6] = 0x13;
649 b[7] = 0x12;
650 b[8] = 0x11;
651 long address = (long)address_of.invoke(runtime, b);
652 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
653 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
654 }
655
656 public static void test_Memory_pokeByte() throws Exception {
657 byte[] r = {0x11, 0x12};
658 byte[] b = new byte [2];
659 long address = (long)address_of.invoke(runtime, b);
660 poke_byte.invoke(null, address, (byte)0x11);
661 poke_byte.invoke(null, address + 1, (byte)0x12);
662 Assert.assertTrue(Arrays.equals(r, b));
663 }
664
665 public static void test_Memory_pokeShort() throws Exception {
666 byte[] ra = {0x12, 0x11, 0x13};
667 byte[] ru = {0x12, 0x22, 0x21};
668 byte[] b = new byte [3];
669 long address = (long)address_of.invoke(runtime, b);
670
671 // Aligned write
672 b[2] = 0x13;
673 poke_short.invoke(null, address, (short)0x1112, false);
674 Assert.assertTrue(Arrays.equals(ra, b));
675
676 // Unaligned write
677 poke_short.invoke(null, address + 1, (short)0x2122, false);
678 Assert.assertTrue(Arrays.equals(ru, b));
679 }
680
681 public static void test_Memory_pokeInt() throws Exception {
682 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
683 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
684 byte[] b = new byte [5];
685 long address = (long)address_of.invoke(runtime, b);
686
687 b[4] = 0x15;
688 poke_int.invoke(null, address, (int)0x11121314, false);
689 Assert.assertTrue(Arrays.equals(ra, b));
690
691 poke_int.invoke(null, address + 1, (int)0x21222324, false);
692 Assert.assertTrue(Arrays.equals(ru, b));
693 }
694
695 public static void test_Memory_pokeLong() throws Exception {
696 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
697 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
698 byte[] b = new byte [9];
699 long address = (long)address_of.invoke(runtime, b);
700
701 b[8] = 0x19;
702 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
703 Assert.assertTrue(Arrays.equals(ra, b));
704
705 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
706 Assert.assertTrue(Arrays.equals(ru, b));
707 }
jeffhao5d1ac922011-09-29 17:41:15 -0700708}