blob: 9aaed9d589ad8b5f80440ea9331b109270a7cf6d [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();
Chao-ying Fuff87d7b2015-01-19 15:51:57 -080037 test_Math_sqrt();
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010038 test_Math_ceil();
39 test_Math_floor();
40 test_Math_rint();
41 test_Math_round_D();
42 test_Math_round_F();
Chris Larsen2714fe62016-02-11 14:23:53 -080043 test_Math_isNaN_D();
44 test_Math_isNaN_F();
45 test_Math_isInfinite_D();
46 test_Math_isInfinite_F();
Zheng Xua3fe7422014-07-09 14:03:15 +080047 test_Short_reverseBytes();
48 test_Integer_reverseBytes();
49 test_Long_reverseBytes();
Serban Constantinescu23abec92014-07-02 16:13:38 +010050 test_Integer_reverse();
51 test_Long_reverse();
Scott Wakeling611d3392015-07-10 11:42:06 +010052 test_Integer_numberOfLeadingZeros();
53 test_Long_numberOfLeadingZeros();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010054 test_StrictMath_abs_I();
55 test_StrictMath_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010056 test_StrictMath_min_I();
57 test_StrictMath_max_I();
58 test_StrictMath_min_J();
59 test_StrictMath_max_J();
60 test_StrictMath_min_F();
61 test_StrictMath_max_F();
62 test_StrictMath_min_D();
63 test_StrictMath_max_D();
Chao-ying Fuff87d7b2015-01-19 15:51:57 -080064 test_StrictMath_sqrt();
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010065 test_StrictMath_ceil();
66 test_StrictMath_floor();
67 test_StrictMath_rint();
68 test_StrictMath_round_D();
69 test_StrictMath_round_F();
Elliott Hughes28c384b2012-06-15 16:46:25 -070070 test_String_charAt();
71 test_String_compareTo();
72 test_String_indexOf();
73 test_String_isEmpty();
74 test_String_length();
Andreas Gampe7a949612014-07-08 11:03:59 -070075 test_Thread_currentThread();
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070076 initSupportMethodsForPeekPoke();
77 test_Memory_peekByte();
78 test_Memory_peekShort();
79 test_Memory_peekInt();
80 test_Memory_peekLong();
81 test_Memory_pokeByte();
82 test_Memory_pokeShort();
83 test_Memory_pokeInt();
84 test_Memory_pokeLong();
Scott Wakeling9ee23f42015-07-23 10:44:35 +010085 test_Integer_numberOfTrailingZeros();
86 test_Long_numberOfTrailingZeros();
87 test_Integer_rotateRight();
88 test_Long_rotateRight();
89 test_Integer_rotateLeft();
90 test_Long_rotateLeft();
91 test_Integer_rotateRightLeft();
92 test_Long_rotateRightLeft();
Elliott Hughes28c384b2012-06-15 16:46:25 -070093 }
94
Andreas Gampe7a949612014-07-08 11:03:59 -070095 /**
96 * Will test inlining Thread.currentThread().
97 */
98 public static void test_Thread_currentThread() {
99 // 1. Do not use result.
100 Thread.currentThread();
101
102 // 2. Result should not be null.
103 Assert.assertNotNull(Thread.currentThread());
104 }
105
Elliott Hughes28c384b2012-06-15 16:46:25 -0700106 public static void test_String_length() {
107 String str0 = "";
108 String str1 = "x";
109 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
110
111 Assert.assertEquals(str0.length(), 0);
112 Assert.assertEquals(str1.length(), 1);
113 Assert.assertEquals(str80.length(), 80);
114
115 String strNull = null;
116 try {
117 strNull.length();
118 Assert.fail();
119 } catch (NullPointerException expected) {
120 }
121 }
122
123 public static void test_String_isEmpty() {
124 String str0 = "";
125 String str1 = "x";
126
127 Assert.assertTrue(str0.isEmpty());
128 Assert.assertFalse(str1.isEmpty());
129
130 String strNull = null;
131 try {
132 strNull.isEmpty();
133 Assert.fail();
134 } catch (NullPointerException expected) {
135 }
136 }
137
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800138 // Break up the charAt tests. The optimizing compiler doesn't optimize methods with try-catch yet,
139 // so we need to separate out the tests that are expected to throw exception
140
Elliott Hughes28c384b2012-06-15 16:46:25 -0700141 public static void test_String_charAt() {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800142 String testStr = "Now is the time to test some stuff";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700143
Andreas Gampe878d58c2015-01-15 23:24:00 -0800144 Assert.assertEquals(testStr.length() - 1, 33); // 33 = testStr.length()-1 as a constant.
145 Assert.assertEquals('f', testStr.charAt(33));
Elliott Hughes28c384b2012-06-15 16:46:25 -0700146
Andreas Gampe878d58c2015-01-15 23:24:00 -0800147 test_String_charAt(testStr, 'N', 'o', ' ', 'f');
148 test_String_charAt(testStr.substring(3,15), ' ', 'i', 'm', 'e');
149 }
150 public static void test_String_charAt(String testStr, char a, char b, char c, char d) {
151 Assert.assertEquals(a, testStr.charAt(0));
152 Assert.assertEquals(b, testStr.charAt(1));
153 Assert.assertEquals(c, testStr.charAt(10));
154 Assert.assertEquals(d, testStr.charAt(testStr.length()-1));
155
156 test_String_charAtExc(testStr);
157 test_String_charAtExc2(testStr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800158 }
159
Andreas Gampe878d58c2015-01-15 23:24:00 -0800160 private static void test_String_charAtExc(String testStr) {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700161 try {
162 testStr.charAt(-1);
163 Assert.fail();
164 } catch (StringIndexOutOfBoundsException expected) {
165 }
166 try {
167 testStr.charAt(80);
168 Assert.fail();
169 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700170 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000171 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800172 if (testStr.length() == 34) {
173 testStr.charAt(34); // 34 = "Now is the time to test some stuff".length()
174 } else {
175 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length()
176 testStr.charAt(12);
177 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000178 Assert.fail();
179 } catch (StringIndexOutOfBoundsException expected) {
180 }
181 try {
182 test_String_charAt_inner(testStr, -1);
183 Assert.fail();
184 } catch (StringIndexOutOfBoundsException expected) {
185 }
186 try {
187 test_String_charAt_inner(testStr, 80);
188 Assert.fail();
189 } catch (StringIndexOutOfBoundsException expected) {
190 }
191 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800192 if (testStr.length() == 34) {
193 // 34 = "Now is the time to test some stuff".length()
194 test_String_charAt_inner(testStr, 34);
195 } else {
196 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length()
197 test_String_charAt_inner(testStr, 12);
198 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000199 Assert.fail();
200 } catch (StringIndexOutOfBoundsException expected) {
201 }
202
203 String strEmpty = "";
204 try {
205 strEmpty.charAt(0);
206 Assert.fail();
207 } catch (StringIndexOutOfBoundsException expected) {
208 }
jeffhao5d1ac922011-09-29 17:41:15 -0700209
Elliott Hughes28c384b2012-06-15 16:46:25 -0700210 String strNull = null;
211 try {
212 strNull.charAt(0);
213 Assert.fail();
214 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700215 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700216 }
jeffhao5d1ac922011-09-29 17:41:15 -0700217
Vladimir Marko00ca8472015-01-26 14:06:46 +0000218 private static char test_String_charAt_inner(String s, int index) {
219 // Using non-constant index here (assuming that this method wasn't inlined).
220 return s.charAt(index);
221 }
222
Andreas Gampe878d58c2015-01-15 23:24:00 -0800223 private static void test_String_charAtExc2(String testStr) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800224 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800225 test_String_charAtExc3(testStr);
226 Assert.fail();
227 } catch (StringIndexOutOfBoundsException expected) {
228 }
229 try {
230 test_String_charAtExc4(testStr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800231 Assert.fail();
232 } catch (StringIndexOutOfBoundsException expected) {
233 }
234 }
235
Andreas Gampe878d58c2015-01-15 23:24:00 -0800236 private static void test_String_charAtExc3(String testStr) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800237 Assert.assertEquals('N', testStr.charAt(-1));
238 }
239
Andreas Gampe878d58c2015-01-15 23:24:00 -0800240 private static void test_String_charAtExc4(String testStr) {
241 Assert.assertEquals('N', testStr.charAt(100));
242 }
243
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700244 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700245 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700246 public static void test_String_indexOf() {
247 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700248 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700249 String str3 = "abc";
250 String str10 = "abcdefghij";
251 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700252
Elliott Hughes28c384b2012-06-15 16:46:25 -0700253 Assert.assertEquals(str0.indexOf('a'), -1);
254 Assert.assertEquals(str3.indexOf('a'), 0);
255 Assert.assertEquals(str3.indexOf('b'), 1);
256 Assert.assertEquals(str3.indexOf('c'), 2);
257 Assert.assertEquals(str10.indexOf('j'), 9);
258 Assert.assertEquals(str40.indexOf('a'), 0);
259 Assert.assertEquals(str40.indexOf('b'), 38);
260 Assert.assertEquals(str40.indexOf('c'), 39);
261 Assert.assertEquals(str0.indexOf('a',20), -1);
262 Assert.assertEquals(str0.indexOf('a',0), -1);
263 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700264 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700265 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700266 Assert.assertEquals(str3.indexOf('a',0), 0);
267 Assert.assertEquals(str3.indexOf('a',1), -1);
268 Assert.assertEquals(str3.indexOf('a',1234), -1);
269 Assert.assertEquals(str3.indexOf('b',0), 1);
270 Assert.assertEquals(str3.indexOf('b',1), 1);
271 Assert.assertEquals(str3.indexOf('c',2), 2);
272 Assert.assertEquals(str10.indexOf('j',5), 9);
273 Assert.assertEquals(str10.indexOf('j',9), 9);
274 Assert.assertEquals(str40.indexOf('a',10), 10);
275 Assert.assertEquals(str40.indexOf('b',40), -1);
276
Andreas Gampe678e6952015-05-07 16:44:58 -0700277 testIndexOfNull();
278
Andreas Gampe21030dd2015-05-07 14:46:15 -0700279 // Same data as above, but stored so it's not a literal in the next test. -2 stands for
280 // indexOf(I) instead of indexOf(II).
281 start--;
282 int[][] searchData = {
283 { 'a', -2, -1 },
284 { 'a', -2, 0 },
285 { 'b', -2, 1 },
286 { 'c', -2, 2 },
287 { 'j', -2, 9 },
288 { 'a', -2, 0 },
289 { 'b', -2, 38 },
290 { 'c', -2, 39 },
291 { 'a', 20, -1 },
292 { 'a', 0, -1 },
293 { 'a', -1, -1 },
294 { '/', ++start, -1 },
295 { 'a', negIndex[0], -1 },
296 { 'a', 0, 0 },
297 { 'a', 1, -1 },
298 { 'a', 1234, -1 },
299 { 'b', 0, 1 },
300 { 'b', 1, 1 },
301 { 'c', 2, 2 },
302 { 'j', 5, 9 },
303 { 'j', 9, 9 },
304 { 'a', 10, 10 },
305 { 'b', 40, -1 },
306 };
307 testStringIndexOfChars(searchData);
308
Andreas Gampe678e6952015-05-07 16:44:58 -0700309 testSurrogateIndexOf();
310 }
311
Andreas Gampe21030dd2015-05-07 14:46:15 -0700312 private static void testStringIndexOfChars(int[][] searchData) {
313 // Use a try-catch to avoid inlining.
314 try {
315 testStringIndexOfCharsImpl(searchData);
316 } catch (Exception e) {
317 System.out.println("Unexpected exception");
318 }
319 }
320
321 private static void testStringIndexOfCharsImpl(int[][] searchData) {
322 String str0 = "";
323 String str1 = "/";
324 String str3 = "abc";
325 String str10 = "abcdefghij";
326 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
327
328 Assert.assertEquals(str0.indexOf(searchData[0][0]), searchData[0][2]);
329 Assert.assertEquals(str3.indexOf(searchData[1][0]), searchData[1][2]);
330 Assert.assertEquals(str3.indexOf(searchData[2][0]), searchData[2][2]);
331 Assert.assertEquals(str3.indexOf(searchData[3][0]), searchData[3][2]);
332 Assert.assertEquals(str10.indexOf(searchData[4][0]), searchData[4][2]);
333 Assert.assertEquals(str40.indexOf(searchData[5][0]), searchData[5][2]);
334 Assert.assertEquals(str40.indexOf(searchData[6][0]), searchData[6][2]);
335 Assert.assertEquals(str40.indexOf(searchData[7][0]), searchData[7][2]);
336 Assert.assertEquals(str0.indexOf(searchData[8][0], searchData[8][1]), searchData[8][2]);
337 Assert.assertEquals(str0.indexOf(searchData[9][0], searchData[9][1]), searchData[9][2]);
338 Assert.assertEquals(str0.indexOf(searchData[10][0], searchData[10][1]), searchData[10][2]);
339 Assert.assertEquals(str1.indexOf(searchData[11][0], searchData[11][1]), searchData[11][2]);
340 Assert.assertEquals(str1.indexOf(searchData[12][0], searchData[12][1]), searchData[12][2]);
341 Assert.assertEquals(str3.indexOf(searchData[13][0], searchData[13][1]), searchData[13][2]);
342 Assert.assertEquals(str3.indexOf(searchData[14][0], searchData[14][1]), searchData[14][2]);
343 Assert.assertEquals(str3.indexOf(searchData[15][0], searchData[15][1]), searchData[15][2]);
344 Assert.assertEquals(str3.indexOf(searchData[16][0], searchData[16][1]), searchData[16][2]);
345 Assert.assertEquals(str3.indexOf(searchData[17][0], searchData[17][1]), searchData[17][2]);
346 Assert.assertEquals(str3.indexOf(searchData[18][0], searchData[18][1]), searchData[18][2]);
347 Assert.assertEquals(str10.indexOf(searchData[19][0], searchData[19][1]), searchData[19][2]);
348 Assert.assertEquals(str10.indexOf(searchData[20][0], searchData[20][1]), searchData[20][2]);
349 Assert.assertEquals(str40.indexOf(searchData[21][0], searchData[21][1]), searchData[21][2]);
350 Assert.assertEquals(str40.indexOf(searchData[22][0], searchData[22][1]), searchData[22][2]);
351 }
352
Andreas Gampe678e6952015-05-07 16:44:58 -0700353 private static void testSurrogateIndexOf() {
354 int supplementaryChar = 0x20b9f;
355 String surrogatePair = "\ud842\udf9f";
356 String stringWithSurrogates = "hello " + surrogatePair + " world";
357
358 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
359 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
360 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
361 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -0700362
363 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar - 0x10000), -1);
364 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar | 0x80000000), -1);
Andreas Gampe678e6952015-05-07 16:44:58 -0700365 }
366
367 private static void testIndexOfNull() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700368 String strNull = null;
369 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700370 testNullIndex(strNull, 'a');
Elliott Hughes28c384b2012-06-15 16:46:25 -0700371 Assert.fail();
372 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700373 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700374 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700375 testNullIndex(strNull, 'a', 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700376 Assert.fail();
377 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700378 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700379 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700380 testNullIndex(strNull, 'a', -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700381 Assert.fail();
382 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700383 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700384 }
jeffhao5d1ac922011-09-29 17:41:15 -0700385
Andreas Gampe678e6952015-05-07 16:44:58 -0700386 private static int testNullIndex(String strNull, int c) {
387 return strNull.indexOf(c);
388 }
389
390 private static int testNullIndex(String strNull, int c, int startIndex) {
391 return strNull.indexOf(c, startIndex);
392 }
393
Elliott Hughes28c384b2012-06-15 16:46:25 -0700394 public static void test_String_compareTo() {
395 String test = "0123456789";
396 String test1 = new String("0123456789"); // different object
397 String test2 = new String("0123456780"); // different value
398 String offset = new String("xxx0123456789yyy");
399 String sub = offset.substring(3, 13);
400 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
401 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
402 String lc = "abcdefg";
403 String uc = "ABCDEFG";
404 Object blah = new Object();
405
406 Assert.assertTrue(lc.toUpperCase().equals(uc));
407
408 Assert.assertEquals(str32.compareTo(str33), -1);
409 Assert.assertEquals(str33.compareTo(str32), 1);
410
411 Assert.assertTrue(test.equals(test));
412 Assert.assertTrue(test.equals(test1));
413 Assert.assertFalse(test.equals(test2));
414
415 Assert.assertEquals(test.compareTo(test1), 0);
416 Assert.assertTrue(test1.compareTo(test2) > 0);
417 Assert.assertTrue(test2.compareTo(test1) < 0);
418
419 // Compare string with a nonzero offset, in left/right side.
420 Assert.assertEquals(test.compareTo(sub), 0);
421 Assert.assertEquals(sub.compareTo(test), 0);
422 Assert.assertTrue(test.equals(sub));
423 Assert.assertTrue(sub.equals(test));
424 // Same base, one is a substring.
425 Assert.assertFalse(offset.equals(sub));
426 Assert.assertFalse(sub.equals(offset));
427 // Wrong class.
428 Assert.assertFalse(test.equals(blah));
429
430 // Null lhs - throw.
431 try {
432 test.compareTo(null);
433 Assert.fail("didn't get expected npe");
434 } catch (NullPointerException npe) {
435 }
436 // Null rhs - okay.
437 Assert.assertFalse(test.equals(null));
438
439 test = test.substring(1);
440 Assert.assertTrue(test.equals("123456789"));
441 Assert.assertFalse(test.equals(test1));
442
443 test = test.substring(1);
444 Assert.assertTrue(test.equals("23456789"));
445
446 test = test.substring(1);
447 Assert.assertTrue(test.equals("3456789"));
448
449 test = test.substring(1);
450 Assert.assertTrue(test.equals("456789"));
451
452 test = test.substring(3,5);
453 Assert.assertTrue(test.equals("78"));
454
455 test = "this/is/a/path";
456 String[] strings = test.split("/");
457 Assert.assertEquals(4, strings.length);
458
459 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
460 Assert.assertEquals("this is a path", test.replace("/", " "));
461 }
462
463 public static void test_Math_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800464 Math.abs(-1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700465 Assert.assertEquals(Math.abs(0), 0);
466 Assert.assertEquals(Math.abs(123), 123);
467 Assert.assertEquals(Math.abs(-123), 123);
468 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
469 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
470 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100471 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700472 }
473
474 public static void test_Math_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800475 Math.abs(-1L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700476 Assert.assertEquals(Math.abs(0L), 0L);
477 Assert.assertEquals(Math.abs(123L), 123L);
478 Assert.assertEquals(Math.abs(-123L), 123L);
479 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
480 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
481 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800482 Assert.assertEquals(Math.abs(2147483648L), 2147483648L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700483 }
484
Serban Constantinescu23abec92014-07-02 16:13:38 +0100485 public static void test_Math_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800486 Math.min(1, 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700487 Assert.assertEquals(Math.min(0, 0), 0);
488 Assert.assertEquals(Math.min(1, 0), 0);
489 Assert.assertEquals(Math.min(0, 1), 0);
490 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
491 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
492 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
493 }
494
Serban Constantinescu23abec92014-07-02 16:13:38 +0100495 public static void test_Math_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800496 Math.max(1, 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700497 Assert.assertEquals(Math.max(0, 0), 0);
498 Assert.assertEquals(Math.max(1, 0), 1);
499 Assert.assertEquals(Math.max(0, 1), 1);
500 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
501 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
502 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
503 }
504
Serban Constantinescu23abec92014-07-02 16:13:38 +0100505 public static void test_Math_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800506 Math.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100507 Assert.assertEquals(Math.min(0L, 0L), 0L);
508 Assert.assertEquals(Math.min(1L, 0L), 0L);
509 Assert.assertEquals(Math.min(0L, 1L), 0L);
510 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
511 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
512 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
513 }
514
515 public static void test_Math_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800516 Math.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100517 Assert.assertEquals(Math.max(0L, 0L), 0L);
518 Assert.assertEquals(Math.max(1L, 0L), 1L);
519 Assert.assertEquals(Math.max(0L, 1L), 1L);
520 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
521 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
522 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
523 }
524
525 public static void test_Math_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800526 Math.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700527 Assert.assertTrue(Float.isNaN(Math.min(1.0f, Float.NaN)));
528 Assert.assertTrue(Float.isNaN(Math.min(Float.NaN, 1.0f)));
529 Assert.assertEquals(Math.min(-0.0f, 0.0f), -0.0f);
530 Assert.assertEquals(Math.min(0.0f, -0.0f), -0.0f);
531 Assert.assertEquals(Math.min(-0.0f, -0.0f), -0.0f);
532 Assert.assertEquals(Math.min(0.0f, 0.0f), 0.0f);
533 Assert.assertEquals(Math.min(1.0f, 0.0f), 0.0f);
534 Assert.assertEquals(Math.min(0.0f, 1.0f), 0.0f);
535 Assert.assertEquals(Math.min(0.0f, Float.MAX_VALUE), 0.0f);
536 Assert.assertEquals(Math.min(Float.MIN_VALUE, 0.0f), 0.0f);
537 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100538 }
539
540 public static void test_Math_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800541 Math.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700542 Assert.assertTrue(Float.isNaN(Math.max(1.0f, Float.NaN)));
543 Assert.assertTrue(Float.isNaN(Math.max(Float.NaN, 1.0f)));
544 Assert.assertEquals(Math.max(-0.0f, 0.0f), 0.0f);
545 Assert.assertEquals(Math.max(0.0f, -0.0f), 0.0f);
546 Assert.assertEquals(Math.max(-0.0f, -0.0f), -0.0f);
547 Assert.assertEquals(Math.max(0.0f, 0.0f), 0.0f);
548 Assert.assertEquals(Math.max(1.0f, 0.0f), 1.0f);
549 Assert.assertEquals(Math.max(0.0f, 1.0f), 1.0f);
550 Assert.assertEquals(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
551 Assert.assertEquals(Math.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
552 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100553 }
554
555 public static void test_Math_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800556 Math.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700557 Assert.assertTrue(Double.isNaN(Math.min(1.0d, Double.NaN)));
558 Assert.assertTrue(Double.isNaN(Math.min(Double.NaN, 1.0d)));
559 Assert.assertEquals(Math.min(-0.0d, 0.0d), -0.0d);
560 Assert.assertEquals(Math.min(0.0d, -0.0d), -0.0d);
561 Assert.assertEquals(Math.min(-0.0d, -0.0d), -0.0d);
562 Assert.assertEquals(Math.min(0.0d, 0.0d), 0.0d);
563 Assert.assertEquals(Math.min(1.0d, 0.0d), 0.0d);
564 Assert.assertEquals(Math.min(0.0d, 1.0d), 0.0d);
565 Assert.assertEquals(Math.min(0.0d, Double.MAX_VALUE), 0.0d);
566 Assert.assertEquals(Math.min(Double.MIN_VALUE, 0.0d), 0.0d);
567 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100568 }
569
570 public static void test_Math_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800571 Math.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700572 Assert.assertTrue(Double.isNaN(Math.max(1.0d, Double.NaN)));
573 Assert.assertTrue(Double.isNaN(Math.max(Double.NaN, 1.0d)));
574 Assert.assertEquals(Math.max(-0.0d, 0.0d), 0.0d);
575 Assert.assertEquals(Math.max(0.0d, -0.0d), 0.0d);
576 Assert.assertEquals(Math.max(-0.0d, -0.0d), -0.0d);
577 Assert.assertEquals(Math.max(0.0d, 0.0d), 0.0d);
578 Assert.assertEquals(Math.max(1.0d, 0.0d), 1.0d);
579 Assert.assertEquals(Math.max(0.0d, 1.0d), 1.0d);
580 Assert.assertEquals(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
581 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
582 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100583 }
584
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800585 public static void test_Math_sqrt() {
586 Math.sqrt(+4.0);
587 Assert.assertEquals(Math.sqrt(+4.0), +2.0d, 0.0);
588 Assert.assertEquals(Math.sqrt(+49.0), +7.0d, 0.0);
589 Assert.assertEquals(Math.sqrt(+1.44), +1.2d, 0.0);
590 }
591
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100592 public static void test_Math_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800593 Math.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100594 Assert.assertEquals(Math.ceil(+0.0), +0.0d, 0.0);
595 Assert.assertEquals(Math.ceil(-0.0), -0.0d, 0.0);
596 Assert.assertEquals(Math.ceil(-0.9), -0.0d, 0.0);
597 Assert.assertEquals(Math.ceil(-0.5), -0.0d, 0.0);
598 Assert.assertEquals(Math.ceil(0.0), -0.0d, 0.0);
599 Assert.assertEquals(Math.ceil(+2.0), +2.0d, 0.0);
600 Assert.assertEquals(Math.ceil(+2.1), +3.0d, 0.0);
601 Assert.assertEquals(Math.ceil(+2.5), +3.0d, 0.0);
602 Assert.assertEquals(Math.ceil(+2.9), +3.0d, 0.0);
603 Assert.assertEquals(Math.ceil(+3.0), +3.0d, 0.0);
604 Assert.assertEquals(Math.ceil(-2.0), -2.0d, 0.0);
605 Assert.assertEquals(Math.ceil(-2.1), -2.0d, 0.0);
606 Assert.assertEquals(Math.ceil(-2.5), -2.0d, 0.0);
607 Assert.assertEquals(Math.ceil(-2.9), -2.0d, 0.0);
608 Assert.assertEquals(Math.ceil(-3.0), -3.0d, 0.0);
Chris Larsen82831092015-08-25 09:06:58 -0700609 // 2^52 - 1.5
610 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
611 Double.longBitsToDouble(0x432FFFFFFFFFFFFEl), 0.0);
612 // 2^52 - 0.5
613 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
614 Double.longBitsToDouble(0x4330000000000000l), 0.0);
615 // 2^52
616 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x4330000000000000l)),
617 Double.longBitsToDouble(0x4330000000000000l), 0.0);
618 // 2^53 - 1
619 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
620 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
621 // 2^53
622 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x4340000000000000l)),
623 Double.longBitsToDouble(0x4340000000000000l), 0.0);
624 // 2^63 - 2^10
625 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
626 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
627 // 2^63
628 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43E0000000000000l)),
629 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
630 // 2^64
631 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43F0000000000000l)),
632 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
633 // -(2^52 - 1.5)
634 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
635 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0);
636 // -(2^52 - 0.5)
637 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
638 Double.longBitsToDouble(0xC32FFFFFFFFFFFFEl), 0.0);
639 // -2^52
640 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC330000000000000l)),
641 Double.longBitsToDouble(0xC330000000000000l), 0.0);
642 // -(2^53 - 1)
643 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
644 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
645 // -2^53
646 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC340000000000000l)),
647 Double.longBitsToDouble(0xC340000000000000l), 0.0);
648 // -(2^63 - 2^10)
649 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
650 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
651 // -2^63
652 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3E0000000000000l)),
653 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
654 // -2^64
655 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3F0000000000000l)),
656 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100657 Assert.assertEquals(Math.ceil(Double.NaN), Double.NaN, 0.0);
658 Assert.assertEquals(Math.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
659 Assert.assertEquals(Math.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
660 }
661
662 public static void test_Math_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800663 Math.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100664 Assert.assertEquals(Math.floor(+0.0), +0.0d, 0.0);
665 Assert.assertEquals(Math.floor(-0.0), -0.0d, 0.0);
666 Assert.assertEquals(Math.floor(+2.0), +2.0d, 0.0);
667 Assert.assertEquals(Math.floor(+2.1), +2.0d, 0.0);
668 Assert.assertEquals(Math.floor(+2.5), +2.0d, 0.0);
669 Assert.assertEquals(Math.floor(+2.9), +2.0d, 0.0);
670 Assert.assertEquals(Math.floor(+3.0), +3.0d, 0.0);
671 Assert.assertEquals(Math.floor(-2.0), -2.0d, 0.0);
672 Assert.assertEquals(Math.floor(-2.1), -3.0d, 0.0);
673 Assert.assertEquals(Math.floor(-2.5), -3.0d, 0.0);
674 Assert.assertEquals(Math.floor(-2.9), -3.0d, 0.0);
675 Assert.assertEquals(Math.floor(-3.0), -3.0d, 0.0);
Chris Larsen82831092015-08-25 09:06:58 -0700676 // 2^52 - 1.5
677 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
678 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0);
679 // 2^52 - 0.5
680 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
681 Double.longBitsToDouble(0x432FFFFFFFFFFFFEl), 0.0);
682 // 2^52
683 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x4330000000000000l)),
684 Double.longBitsToDouble(0x4330000000000000l), 0.0);
685 // 2^53 - 1
686 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
687 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
688 // 2^53
689 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x4340000000000000l)),
690 Double.longBitsToDouble(0x4340000000000000l), 0.0);
691 // 2^63 - 2^10
692 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
693 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
694 // 2^63
695 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43E0000000000000l)),
696 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
697 // 2^64
698 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43F0000000000000l)),
699 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
700 // -(2^52 - 1.5)
701 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
702 Double.longBitsToDouble(0xC32FFFFFFFFFFFFEl), 0.0);
703 // -(2^52 - 0.5)
704 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
705 Double.longBitsToDouble(0xC330000000000000l), 0.0);
706 // -2^52
707 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC330000000000000l)),
708 Double.longBitsToDouble(0xC330000000000000l), 0.0);
709 // -(2^53 - 1)
710 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
711 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
712 // -2^53
713 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC340000000000000l)),
714 Double.longBitsToDouble(0xC340000000000000l), 0.0);
715 // -(2^63 - 2^10)
716 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
717 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
718 // -2^63
719 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3E0000000000000l)),
720 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
721 // -2^64
722 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3F0000000000000l)),
723 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100724 Assert.assertEquals(Math.floor(Double.NaN), Double.NaN, 0.0);
725 Assert.assertEquals(Math.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
726 Assert.assertEquals(Math.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
727 }
728
729 public static void test_Math_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800730 Math.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100731 Assert.assertEquals(Math.rint(+0.0), +0.0d, 0.0);
732 Assert.assertEquals(Math.rint(-0.0), -0.0d, 0.0);
733 Assert.assertEquals(Math.rint(+2.0), +2.0d, 0.0);
734 Assert.assertEquals(Math.rint(+2.1), +2.0d, 0.0);
735 Assert.assertEquals(Math.rint(+2.5), +2.0d, 0.0);
736 Assert.assertEquals(Math.rint(+2.9), +3.0d, 0.0);
737 Assert.assertEquals(Math.rint(+3.0), +3.0d, 0.0);
738 Assert.assertEquals(Math.rint(-2.0), -2.0d, 0.0);
739 Assert.assertEquals(Math.rint(-2.1), -2.0d, 0.0);
740 Assert.assertEquals(Math.rint(-2.5), -2.0d, 0.0);
741 Assert.assertEquals(Math.rint(-2.9), -3.0d, 0.0);
742 Assert.assertEquals(Math.rint(-3.0), -3.0d, 0.0);
Chris Larsen82831092015-08-25 09:06:58 -0700743 // 2^52 - 1.5
744 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
745 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0);
746 // 2^52 - 0.5
747 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
748 Double.longBitsToDouble(0x4330000000000000l), 0.0);
749 // 2^52
750 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4330000000000000l)),
751 Double.longBitsToDouble(0x4330000000000000l), 0.0);
752 // 2^53 - 1
753 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
754 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
755 // 2^53
756 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4340000000000000l)),
757 Double.longBitsToDouble(0x4340000000000000l), 0.0);
758 // 2^63 - 2^10
759 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
760 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
761 // 2^63
762 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43E0000000000000l)),
763 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
764 // 2^64
765 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43F0000000000000l)),
766 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
767 // -(2^52 - 1.5)
768 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
769 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0);
770 // -(2^52 - 0.5)
771 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
772 Double.longBitsToDouble(0xC330000000000000l), 0.0);
773 // -2^52
774 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC330000000000000l)),
775 Double.longBitsToDouble(0xC330000000000000l), 0.0);
776 // -(2^53 - 1)
777 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
778 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
779 // -2^53
780 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC340000000000000l)),
781 Double.longBitsToDouble(0xC340000000000000l), 0.0);
782 // -(2^63 - 2^10)
783 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
784 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
785 // -2^63
786 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3E0000000000000l)),
787 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
788 // -2^64
789 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3F0000000000000l)),
790 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100791 Assert.assertEquals(Math.rint(Double.NaN), Double.NaN, 0.0);
792 Assert.assertEquals(Math.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
793 Assert.assertEquals(Math.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
794 }
795
796 public static void test_Math_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800797 Math.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100798 Assert.assertEquals(Math.round(+0.0d), (long)+0.0);
799 Assert.assertEquals(Math.round(-0.0d), (long)+0.0);
800 Assert.assertEquals(Math.round(2.0d), 2l);
801 Assert.assertEquals(Math.round(2.1d), 2l);
802 Assert.assertEquals(Math.round(2.5d), 3l);
803 Assert.assertEquals(Math.round(2.9d), 3l);
804 Assert.assertEquals(Math.round(3.0d), 3l);
805 Assert.assertEquals(Math.round(-2.0d), -2l);
806 Assert.assertEquals(Math.round(-2.1d), -2l);
807 Assert.assertEquals(Math.round(-2.5d), -2l);
808 Assert.assertEquals(Math.round(-2.9d), -3l);
809 Assert.assertEquals(Math.round(-3.0d), -3l);
Yi Kong879ca672015-11-18 14:11:44 +0000810 Assert.assertEquals(Math.round(0.49999999999999994d), 0l);
Hans Boehm92d4f0e2016-02-17 12:14:03 -0800811 Assert.assertEquals(Math.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100812 Assert.assertEquals(Math.round(Double.NaN), (long)+0.0d);
813 Assert.assertEquals(Math.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
814 Assert.assertEquals(Math.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
815 Assert.assertEquals(Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
816 Assert.assertEquals(Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
817 }
818
819 public static void test_Math_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800820 Math.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100821 Assert.assertEquals(Math.round(+0.0f), (int)+0.0);
822 Assert.assertEquals(Math.round(-0.0f), (int)+0.0);
823 Assert.assertEquals(Math.round(2.0f), 2);
824 Assert.assertEquals(Math.round(2.1f), 2);
825 Assert.assertEquals(Math.round(2.5f), 3);
826 Assert.assertEquals(Math.round(2.9f), 3);
827 Assert.assertEquals(Math.round(3.0f), 3);
828 Assert.assertEquals(Math.round(-2.0f), -2);
829 Assert.assertEquals(Math.round(-2.1f), -2);
830 Assert.assertEquals(Math.round(-2.5f), -2);
831 Assert.assertEquals(Math.round(-2.9f), -3);
832 Assert.assertEquals(Math.round(-3.0f), -3);
Chris Larsenb74353a2015-11-20 09:07:09 -0800833 // 0.4999999701976776123046875
834 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f);
Hans Boehm92d4f0e2016-02-17 12:14:03 -0800835 Assert.assertEquals(Math.round(16777215.0f), 16777215); // 2^24 - 1
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100836 Assert.assertEquals(Math.round(Float.NaN), (int)+0.0f);
837 Assert.assertEquals(Math.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
838 Assert.assertEquals(Math.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
839 Assert.assertEquals(Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
840 Assert.assertEquals(Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
841 }
842
Chris Larsen2714fe62016-02-11 14:23:53 -0800843 public static void test_Math_isNaN_D() {
844 // Quiet NaN.
845 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF4000000000000l)));
846 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF4000000000000l)));
847 // Signaling NaN.
848 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF8000000000000l)));
849 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF8000000000000l)));
850 // Distinct from +/- infinity.
851 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FF0000000000000l)));
852 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFF0000000000000l)));
853 // Distinct from normal numbers.
854 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FE0000000000000l)));
855 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFE0000000000000l)));
856 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0010000000000000l)));
857 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8010000000000000l)));
858 // Distinct from +/- zero.
859 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000000l)));
860 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000000l)));
861 // Distinct from subnormal numbers.
862 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0008000000000000l)));
863 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8008000000000000l)));
864 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000001l)));
865 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000001l)));
866 }
867
868 public static void test_Math_isNaN_F() {
869 // Quiet NaN.
870 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FA00000)));
871 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFA00000)));
872 // Signaling NaN.
873 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FC00000)));
874 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFC00000)));
875 // Distinct from +/- infinity.
876 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F800000)));
877 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF800000)));
878 // Distinct from normal numbers.
879 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F000000)));
880 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF000000)));
881 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00800000)));
882 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80800000)));
883 // Distinct from +/- zero.
884 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000000)));
885 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000000)));
886 // Distinct from subnormal numbers.
887 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00400000)));
888 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80400000)));
889 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000001)));
890 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000001)));
891 }
892
893 public static void test_Math_isInfinite_D() {
894 // Distinct from Quiet NaN.
895 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF4000000000000l)));
896 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF4000000000000l)));
897 // Distinct from Signaling NaN.
898 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF8000000000000l)));
899 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF8000000000000l)));
900 // +/- infinity.
901 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0x7FF0000000000000l)));
902 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0xFFF0000000000000l)));
903 // Distinct from normal numbers.
904 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FE0000000000000l)));
905 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFE0000000000000l)));
906 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0010000000000000l)));
907 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8010000000000000l)));
908 // Distinct from +/- zero.
909 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000000l)));
910 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000000l)));
911 // Distinct from subnormal numbers.
912 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0008000000000000l)));
913 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8008000000000000l)));
914 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000001l)));
915 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000001l)));
916 }
917
918 public static void test_Math_isInfinite_F() {
919 // Distinct from Quiet NaN.
920 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FA00000)));
921 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFA00000)));
922 // Distinct from Signaling NaN.
923 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FC00000)));
924 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFC00000)));
925 // +/- infinity.
926 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0x7F800000)));
927 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0xFF800000)));
928 // Distinct from normal numbers.
929 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7F000000)));
930 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFF000000)));
931 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00800000)));
932 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80800000)));
933 // Distinct from +/- zero.
934 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000000)));
935 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000000)));
936 // Distinct from subnormal numbers.
937 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00400000)));
938 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80400000)));
939 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000001)));
940 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000001)));
941 }
942
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100943 public static void test_StrictMath_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800944 StrictMath.abs(-1);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100945 Assert.assertEquals(StrictMath.abs(0), 0);
946 Assert.assertEquals(StrictMath.abs(123), 123);
947 Assert.assertEquals(StrictMath.abs(-123), 123);
948 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
949 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
950 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
951 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
952 }
953
954 public static void test_StrictMath_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800955 StrictMath.abs(-1L);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100956 Assert.assertEquals(StrictMath.abs(0L), 0L);
957 Assert.assertEquals(StrictMath.abs(123L), 123L);
958 Assert.assertEquals(StrictMath.abs(-123L), 123L);
959 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
960 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
961 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
962 }
963
Serban Constantinescu23abec92014-07-02 16:13:38 +0100964 public static void test_StrictMath_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800965 StrictMath.min(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100966 Assert.assertEquals(StrictMath.min(0, 0), 0);
967 Assert.assertEquals(StrictMath.min(1, 0), 0);
968 Assert.assertEquals(StrictMath.min(0, 1), 0);
969 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
970 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
971 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
972 }
973
Serban Constantinescu23abec92014-07-02 16:13:38 +0100974 public static void test_StrictMath_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800975 StrictMath.max(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100976 Assert.assertEquals(StrictMath.max(0, 0), 0);
977 Assert.assertEquals(StrictMath.max(1, 0), 1);
978 Assert.assertEquals(StrictMath.max(0, 1), 1);
979 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
980 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
981 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
982 }
983
Serban Constantinescu23abec92014-07-02 16:13:38 +0100984 public static void test_StrictMath_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800985 StrictMath.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100986 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
987 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
988 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
989 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
990 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
991 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
992 }
993
994 public static void test_StrictMath_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800995 StrictMath.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100996 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
997 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
998 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
999 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
1000 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
1001 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
1002 }
1003
1004 public static void test_StrictMath_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001005 StrictMath.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001006 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN)));
1007 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f)));
1008 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f);
1009 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f);
1010 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f);
1011 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f);
1012 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f);
1013 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f);
1014 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f);
1015 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f);
1016 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001017 }
1018
1019 public static void test_StrictMath_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001020 StrictMath.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001021 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN)));
1022 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f)));
1023 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f);
1024 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f);
1025 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f);
1026 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f);
1027 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f);
1028 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f);
1029 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
1030 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
1031 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001032 }
1033
1034 public static void test_StrictMath_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001035 StrictMath.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001036 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN)));
1037 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d)));
1038 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d);
1039 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d);
1040 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d);
1041 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d);
1042 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d);
1043 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d);
1044 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d);
1045 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d);
1046 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001047 }
1048
1049 public static void test_StrictMath_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001050 StrictMath.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001051 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN)));
1052 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d)));
1053 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d);
1054 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d);
1055 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d);
1056 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d);
1057 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d);
1058 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d);
1059 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
1060 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
1061 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001062 }
1063
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001064 public static void test_StrictMath_sqrt() {
1065 StrictMath.sqrt(+4.0);
1066 Assert.assertEquals(StrictMath.sqrt(+4.0), +2.0d, 0.0);
1067 Assert.assertEquals(StrictMath.sqrt(+49.0), +7.0d, 0.0);
1068 Assert.assertEquals(StrictMath.sqrt(+1.44), +1.2d, 0.0);
1069 }
1070
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001071 public static void test_StrictMath_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001072 StrictMath.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001073 Assert.assertEquals(StrictMath.ceil(+0.0), +0.0d, 0.0);
1074 Assert.assertEquals(StrictMath.ceil(-0.0), -0.0d, 0.0);
1075 Assert.assertEquals(StrictMath.ceil(-0.9), -0.0d, 0.0);
1076 Assert.assertEquals(StrictMath.ceil(-0.5), -0.0d, 0.0);
1077 Assert.assertEquals(StrictMath.ceil(0.0), -0.0d, 0.0);
1078 Assert.assertEquals(StrictMath.ceil(+2.0), +2.0d, 0.0);
1079 Assert.assertEquals(StrictMath.ceil(+2.1), +3.0d, 0.0);
1080 Assert.assertEquals(StrictMath.ceil(+2.5), +3.0d, 0.0);
1081 Assert.assertEquals(StrictMath.ceil(+2.9), +3.0d, 0.0);
1082 Assert.assertEquals(StrictMath.ceil(+3.0), +3.0d, 0.0);
1083 Assert.assertEquals(StrictMath.ceil(-2.0), -2.0d, 0.0);
1084 Assert.assertEquals(StrictMath.ceil(-2.1), -2.0d, 0.0);
1085 Assert.assertEquals(StrictMath.ceil(-2.5), -2.0d, 0.0);
1086 Assert.assertEquals(StrictMath.ceil(-2.9), -2.0d, 0.0);
1087 Assert.assertEquals(StrictMath.ceil(-3.0), -3.0d, 0.0);
1088 Assert.assertEquals(StrictMath.ceil(Double.NaN), Double.NaN, 0.0);
1089 Assert.assertEquals(StrictMath.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1090 Assert.assertEquals(StrictMath.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1091 }
1092
1093 public static void test_StrictMath_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001094 StrictMath.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001095 Assert.assertEquals(StrictMath.floor(+0.0), +0.0d, 0.0);
1096 Assert.assertEquals(StrictMath.floor(-0.0), -0.0d, 0.0);
1097 Assert.assertEquals(StrictMath.floor(+2.0), +2.0d, 0.0);
1098 Assert.assertEquals(StrictMath.floor(+2.1), +2.0d, 0.0);
1099 Assert.assertEquals(StrictMath.floor(+2.5), +2.0d, 0.0);
1100 Assert.assertEquals(StrictMath.floor(+2.9), +2.0d, 0.0);
1101 Assert.assertEquals(StrictMath.floor(+3.0), +3.0d, 0.0);
1102 Assert.assertEquals(StrictMath.floor(-2.0), -2.0d, 0.0);
1103 Assert.assertEquals(StrictMath.floor(-2.1), -3.0d, 0.0);
1104 Assert.assertEquals(StrictMath.floor(-2.5), -3.0d, 0.0);
1105 Assert.assertEquals(StrictMath.floor(-2.9), -3.0d, 0.0);
1106 Assert.assertEquals(StrictMath.floor(-3.0), -3.0d, 0.0);
1107 Assert.assertEquals(StrictMath.floor(Double.NaN), Double.NaN, 0.0);
1108 Assert.assertEquals(StrictMath.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1109 Assert.assertEquals(StrictMath.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1110 }
1111
1112 public static void test_StrictMath_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001113 StrictMath.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001114 Assert.assertEquals(StrictMath.rint(+0.0), +0.0d, 0.0);
1115 Assert.assertEquals(StrictMath.rint(-0.0), -0.0d, 0.0);
1116 Assert.assertEquals(StrictMath.rint(+2.0), +2.0d, 0.0);
1117 Assert.assertEquals(StrictMath.rint(+2.1), +2.0d, 0.0);
1118 Assert.assertEquals(StrictMath.rint(+2.5), +2.0d, 0.0);
1119 Assert.assertEquals(StrictMath.rint(+2.9), +3.0d, 0.0);
1120 Assert.assertEquals(StrictMath.rint(+3.0), +3.0d, 0.0);
1121 Assert.assertEquals(StrictMath.rint(-2.0), -2.0d, 0.0);
1122 Assert.assertEquals(StrictMath.rint(-2.1), -2.0d, 0.0);
1123 Assert.assertEquals(StrictMath.rint(-2.5), -2.0d, 0.0);
1124 Assert.assertEquals(StrictMath.rint(-2.9), -3.0d, 0.0);
1125 Assert.assertEquals(StrictMath.rint(-3.0), -3.0d, 0.0);
1126 Assert.assertEquals(StrictMath.rint(Double.NaN), Double.NaN, 0.0);
1127 Assert.assertEquals(StrictMath.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1128 Assert.assertEquals(StrictMath.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1129 }
1130
1131 public static void test_StrictMath_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001132 StrictMath.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001133 Assert.assertEquals(StrictMath.round(+0.0d), (long)+0.0);
1134 Assert.assertEquals(StrictMath.round(-0.0d), (long)+0.0);
1135 Assert.assertEquals(StrictMath.round(2.0d), 2l);
1136 Assert.assertEquals(StrictMath.round(2.1d), 2l);
1137 Assert.assertEquals(StrictMath.round(2.5d), 3l);
1138 Assert.assertEquals(StrictMath.round(2.9d), 3l);
1139 Assert.assertEquals(StrictMath.round(3.0d), 3l);
1140 Assert.assertEquals(StrictMath.round(-2.0d), -2l);
1141 Assert.assertEquals(StrictMath.round(-2.1d), -2l);
1142 Assert.assertEquals(StrictMath.round(-2.5d), -2l);
1143 Assert.assertEquals(StrictMath.round(-2.9d), -3l);
1144 Assert.assertEquals(StrictMath.round(-3.0d), -3l);
Yi Kong879ca672015-11-18 14:11:44 +00001145 Assert.assertEquals(StrictMath.round(0.49999999999999994d), 0l);
Vladimir Marko9c1c06a2016-02-25 17:50:41 +00001146 Assert.assertEquals(StrictMath.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001147 Assert.assertEquals(StrictMath.round(Double.NaN), (long)+0.0d);
1148 Assert.assertEquals(StrictMath.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
1149 Assert.assertEquals(StrictMath.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
1150 Assert.assertEquals(StrictMath.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
1151 Assert.assertEquals(StrictMath.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
1152 }
1153
1154 public static void test_StrictMath_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001155 StrictMath.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001156 Assert.assertEquals(StrictMath.round(+0.0f), (int)+0.0);
1157 Assert.assertEquals(StrictMath.round(-0.0f), (int)+0.0);
1158 Assert.assertEquals(StrictMath.round(2.0f), 2);
1159 Assert.assertEquals(StrictMath.round(2.1f), 2);
1160 Assert.assertEquals(StrictMath.round(2.5f), 3);
1161 Assert.assertEquals(StrictMath.round(2.9f), 3);
1162 Assert.assertEquals(StrictMath.round(3.0f), 3);
1163 Assert.assertEquals(StrictMath.round(-2.0f), -2);
1164 Assert.assertEquals(StrictMath.round(-2.1f), -2);
1165 Assert.assertEquals(StrictMath.round(-2.5f), -2);
1166 Assert.assertEquals(StrictMath.round(-2.9f), -3);
1167 Assert.assertEquals(StrictMath.round(-3.0f), -3);
Chris Larsenb74353a2015-11-20 09:07:09 -08001168 // 0.4999999701976776123046875
1169 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f);
Vladimir Marko9c1c06a2016-02-25 17:50:41 +00001170 Assert.assertEquals(StrictMath.round(16777215.0f), 16777215); // 2^24 - 1
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001171 Assert.assertEquals(StrictMath.round(Float.NaN), (int)+0.0f);
1172 Assert.assertEquals(StrictMath.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
1173 Assert.assertEquals(StrictMath.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
1174 Assert.assertEquals(StrictMath.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
1175 Assert.assertEquals(StrictMath.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
1176 }
1177
Elliott Hughes28c384b2012-06-15 16:46:25 -07001178 public static void test_Float_floatToRawIntBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001179 Float.floatToRawIntBits(-1.0f);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001180 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
1181 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
1182 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
1183 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
1184 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
1185 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
1186 }
1187
1188 public static void test_Float_intBitsToFloat() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001189 Float.intBitsToFloat(0xbf800000);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001190 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
1191 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
1192 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
1193 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
1194 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
1195 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
1196 }
1197
1198 public static void test_Double_doubleToRawLongBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001199 Double.doubleToRawLongBits(-1.0);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001200 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
1201 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
1202 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
1203 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
1204 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
1205 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
1206 }
1207
1208 public static void test_Double_longBitsToDouble() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001209 Double.longBitsToDouble(0xbff0000000000000L);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001210 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
1211 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
1212 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
1213 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
1214 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
1215 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
1216 }
Serban Constantinescu23abec92014-07-02 16:13:38 +01001217
Zheng Xua3fe7422014-07-09 14:03:15 +08001218 public static void test_Short_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001219 Short.reverseBytes((short)0x1357);
Zheng Xua3fe7422014-07-09 14:03:15 +08001220 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
1221 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
1222 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
1223 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
1224 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
1225 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
1226 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
1227 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
1228 }
1229
1230 public static void test_Integer_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001231 Integer.reverseBytes(0x13579bdf);
Zheng Xua3fe7422014-07-09 14:03:15 +08001232 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
1233 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
1234 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
1235 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
1236 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
1237 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
1238 }
1239
1240 public static void test_Long_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001241 Long.reverseBytes(0x13579bdf2468ace0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001242 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
1243 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
1244 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
1245 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
1246 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
1247 }
1248
Serban Constantinescu23abec92014-07-02 16:13:38 +01001249 public static void test_Integer_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001250 Integer.reverse(0x12345678);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001251 Assert.assertEquals(Integer.reverse(1), 0x80000000);
1252 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
1253 Assert.assertEquals(Integer.reverse(0), 0);
1254 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
1255 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
1256 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
1257 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
1258 }
1259
1260 public static void test_Long_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001261 Long.reverse(0x1234567812345678L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001262 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
1263 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
1264 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001265 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
1266 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
1267 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001268 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
Andreas Gampe575422f2015-07-07 13:25:58 -07001269
1270 Assert.assertEquals(test_Long_reverse_b22324327(0xaaaaaaaaaaaaaaaaL, 0x5555555555555555L),
1271 157472205507277347L);
1272 }
1273
1274 // A bit more complicated than the above. Use local variables to stress register allocation.
1275 private static long test_Long_reverse_b22324327(long l1, long l2) {
1276 // A couple of local integers. Use them in a loop, so they get promoted.
1277 int i1 = 0, i2 = 1, i3 = 2, i4 = 3, i5 = 4, i6 = 5, i7 = 6, i8 = 7;
1278 for (int k = 0; k < 10; k++) {
1279 i1 += 1;
1280 i2 += 2;
1281 i3 += 3;
1282 i4 += 4;
1283 i5 += 5;
1284 i6 += 6;
1285 i7 += 7;
1286 i8 += 8;
1287 }
1288
1289 // Do the Long.reverse() calls, save the results.
1290 long r1 = Long.reverse(l1);
1291 long r2 = Long.reverse(l2);
1292
1293 // Some more looping with the ints.
1294 for (int k = 0; k < 10; k++) {
1295 i1 += 1;
1296 i2 += 2;
1297 i3 += 3;
1298 i4 += 4;
1299 i5 += 5;
1300 i6 += 6;
1301 i7 += 7;
1302 i8 += 8;
1303 }
1304
1305 // Include everything in the result, so things are kept live. Try to be a little bit clever to
1306 // avoid things being folded somewhere.
1307 return (r1 / i1) + (r2 / i2) + i3 + i4 + i5 + i6 + i7 + i8;
Serban Constantinescu23abec92014-07-02 16:13:38 +01001308 }
1309
Mark Mendelld5897672015-08-12 21:16:41 -04001310 public static boolean doThrow = false;
1311
1312 public static int $noinline$return_int_zero() {
1313 if (doThrow) {
1314 throw new Error();
1315 }
1316 return 0;
1317 }
1318
Scott Wakeling611d3392015-07-10 11:42:06 +01001319 public static void test_Integer_numberOfLeadingZeros() {
1320 Assert.assertEquals(Integer.numberOfLeadingZeros(0), Integer.SIZE);
Mark Mendelld5897672015-08-12 21:16:41 -04001321 Assert.assertEquals(Integer.numberOfLeadingZeros(1), Integer.SIZE - 1);
1322 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << (Integer.SIZE-1)), 0);
1323 Assert.assertEquals(Integer.numberOfLeadingZeros($noinline$return_int_zero()), Integer.SIZE);
Scott Wakeling611d3392015-07-10 11:42:06 +01001324 for (int i = 0; i < Integer.SIZE; i++) {
1325 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << i), Integer.SIZE - 1 - i);
1326 Assert.assertEquals(Integer.numberOfLeadingZeros((1 << i) | 1), Integer.SIZE - 1 - i);
1327 Assert.assertEquals(Integer.numberOfLeadingZeros(0xFFFFFFFF >>> i), i);
1328 }
1329 }
1330
Mark Mendelld5897672015-08-12 21:16:41 -04001331 public static long $noinline$return_long_zero() {
1332 if (doThrow) {
1333 throw new Error();
1334 }
1335 return 0;
1336 }
1337
Scott Wakeling611d3392015-07-10 11:42:06 +01001338 public static void test_Long_numberOfLeadingZeros() {
1339 Assert.assertEquals(Long.numberOfLeadingZeros(0L), Long.SIZE);
Mark Mendelld5897672015-08-12 21:16:41 -04001340 Assert.assertEquals(Long.numberOfLeadingZeros(1L), Long.SIZE - 1);
1341 Assert.assertEquals(Long.numberOfLeadingZeros(1L << ((Long.SIZE/2)-1)), Long.SIZE/2);
1342 Assert.assertEquals(Long.numberOfLeadingZeros(1L << (Long.SIZE-1)), 0);
1343 Assert.assertEquals(Long.numberOfLeadingZeros($noinline$return_long_zero()), Long.SIZE);
Scott Wakeling611d3392015-07-10 11:42:06 +01001344 for (int i = 0; i < Long.SIZE; i++) {
1345 Assert.assertEquals(Long.numberOfLeadingZeros(1L << i), Long.SIZE - 1 - i);
1346 Assert.assertEquals(Long.numberOfLeadingZeros((1L << i) | 1L), Long.SIZE - 1 - i);
1347 Assert.assertEquals(Long.numberOfLeadingZeros(0xFFFFFFFFFFFFFFFFL >>> i), i);
1348 }
1349 }
1350
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001351 static Object runtime;
1352 static Method address_of;
Zuo Wangf37a88b2014-07-10 04:26:41 -07001353 static Method new_non_movable_array;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001354 static Method peek_byte;
1355 static Method peek_short;
1356 static Method peek_int;
1357 static Method peek_long;
1358 static Method poke_byte;
1359 static Method poke_short;
1360 static Method poke_int;
1361 static Method poke_long;
1362
1363 public static void initSupportMethodsForPeekPoke() throws Exception {
1364 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
1365 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
1366 runtime = get_runtime.invoke(null);
1367 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
Zuo Wangf37a88b2014-07-10 04:26:41 -07001368 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001369
1370 Class<?> io_memory = Class.forName("libcore.io.Memory");
1371 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
1372 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
1373 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
1374 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
1375 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
1376 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
1377 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
1378 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
1379 }
1380
1381 public static void test_Memory_peekByte() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001382 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001383 b[0] = 0x12;
1384 b[1] = 0x11;
1385 long address = (long)address_of.invoke(runtime, b);
1386 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
1387 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
1388 }
1389
1390 public static void test_Memory_peekShort() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001391 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001392 b[0] = 0x13;
1393 b[1] = 0x12;
1394 b[2] = 0x11;
1395 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001396 peek_short.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001397 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
1398 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
1399 }
1400
1401 public static void test_Memory_peekInt() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001402 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001403 b[0] = 0x15;
1404 b[1] = 0x14;
1405 b[2] = 0x13;
1406 b[3] = 0x12;
1407 b[4] = 0x11;
1408 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001409 peek_int.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001410 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
1411 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
1412 }
1413
1414 public static void test_Memory_peekLong() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001415 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001416 b[0] = 0x19;
1417 b[1] = 0x18;
1418 b[2] = 0x17;
1419 b[3] = 0x16;
1420 b[4] = 0x15;
1421 b[5] = 0x14;
1422 b[6] = 0x13;
1423 b[7] = 0x12;
1424 b[8] = 0x11;
1425 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001426 peek_long.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001427 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
1428 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
1429 }
1430
1431 public static void test_Memory_pokeByte() throws Exception {
1432 byte[] r = {0x11, 0x12};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001433 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001434 long address = (long)address_of.invoke(runtime, b);
1435 poke_byte.invoke(null, address, (byte)0x11);
1436 poke_byte.invoke(null, address + 1, (byte)0x12);
1437 Assert.assertTrue(Arrays.equals(r, b));
1438 }
1439
1440 public static void test_Memory_pokeShort() throws Exception {
1441 byte[] ra = {0x12, 0x11, 0x13};
1442 byte[] ru = {0x12, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001443 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001444 long address = (long)address_of.invoke(runtime, b);
1445
1446 // Aligned write
1447 b[2] = 0x13;
1448 poke_short.invoke(null, address, (short)0x1112, false);
1449 Assert.assertTrue(Arrays.equals(ra, b));
1450
1451 // Unaligned write
1452 poke_short.invoke(null, address + 1, (short)0x2122, false);
1453 Assert.assertTrue(Arrays.equals(ru, b));
1454 }
1455
1456 public static void test_Memory_pokeInt() throws Exception {
1457 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
1458 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001459 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001460 long address = (long)address_of.invoke(runtime, b);
1461
1462 b[4] = 0x15;
1463 poke_int.invoke(null, address, (int)0x11121314, false);
1464 Assert.assertTrue(Arrays.equals(ra, b));
1465
1466 poke_int.invoke(null, address + 1, (int)0x21222324, false);
1467 Assert.assertTrue(Arrays.equals(ru, b));
1468 }
1469
1470 public static void test_Memory_pokeLong() throws Exception {
1471 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
1472 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001473 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001474 long address = (long)address_of.invoke(runtime, b);
1475
1476 b[8] = 0x19;
1477 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
1478 Assert.assertTrue(Arrays.equals(ra, b));
1479
1480 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
1481 Assert.assertTrue(Arrays.equals(ru, b));
1482 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +01001483
1484 public static void test_Integer_numberOfTrailingZeros() {
1485 Assert.assertEquals(Integer.numberOfTrailingZeros(0), Integer.SIZE);
1486 for (int i = 0; i < Integer.SIZE; i++) {
1487 Assert.assertEquals(
1488 Integer.numberOfTrailingZeros(0x80000000 >> i),
1489 Integer.SIZE - 1 - i);
1490 Assert.assertEquals(
1491 Integer.numberOfTrailingZeros((0x80000000 >> i) | 0x80000000),
1492 Integer.SIZE - 1 - i);
1493 Assert.assertEquals(Integer.numberOfTrailingZeros(1 << i), i);
1494 }
1495 }
1496
1497 public static void test_Long_numberOfTrailingZeros() {
1498 Assert.assertEquals(Long.numberOfTrailingZeros(0), Long.SIZE);
1499 for (int i = 0; i < Long.SIZE; i++) {
1500 Assert.assertEquals(
1501 Long.numberOfTrailingZeros(0x8000000000000000L >> i),
1502 Long.SIZE - 1 - i);
1503 Assert.assertEquals(
1504 Long.numberOfTrailingZeros((0x8000000000000000L >> i) | 0x8000000000000000L),
1505 Long.SIZE - 1 - i);
1506 Assert.assertEquals(Long.numberOfTrailingZeros(1L << i), i);
1507 }
1508 }
1509
1510 public static void test_Integer_rotateRight() throws Exception {
1511 Assert.assertEquals(Integer.rotateRight(0x11, 0), 0x11);
1512
1513 Assert.assertEquals(Integer.rotateRight(0x11, 1), 0x80000008);
1514 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE - 1), 0x22);
1515 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE), 0x11);
1516 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE + 1), 0x80000008);
1517
1518 Assert.assertEquals(Integer.rotateRight(0x11, -1), 0x22);
1519 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE - 1)), 0x80000008);
1520 Assert.assertEquals(Integer.rotateRight(0x11, -Integer.SIZE), 0x11);
1521 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE + 1)), 0x22);
1522
1523 Assert.assertEquals(Integer.rotateRight(0x80000000, 1), 0x40000000);
1524
1525 for (int i = 0; i < Integer.SIZE; i++) {
1526 Assert.assertEquals(
1527 Integer.rotateRight(0xBBAAAADD, i),
1528 (0xBBAAAADD >>> i) | (0xBBAAAADD << (Integer.SIZE - i)));
1529 }
1530 }
1531
1532 public static void test_Long_rotateRight() throws Exception {
1533 Assert.assertEquals(Long.rotateRight(0x11, 0), 0x11);
1534
1535 Assert.assertEquals(Long.rotateRight(0x11, 1), 0x8000000000000008L);
1536 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE - 1), 0x22);
1537 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE), 0x11);
1538 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE + 1), 0x8000000000000008L);
1539
1540 Assert.assertEquals(Long.rotateRight(0x11, -1), 0x22);
1541 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE - 1)), 0x8000000000000008L);
1542 Assert.assertEquals(Long.rotateRight(0x11, -Long.SIZE), 0x11);
1543 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE + 1)), 0x22);
1544
1545 Assert.assertEquals(Long.rotateRight(0x8000000000000000L, 1), 0x4000000000000000L);
1546
1547 for (int i = 0; i < Long.SIZE; i++) {
1548 Assert.assertEquals(
1549 Long.rotateRight(0xBBAAAADDFF0000DDL, i),
1550 (0xBBAAAADDFF0000DDL >>> i) | (0xBBAAAADDFF0000DDL << (Long.SIZE - i)));
1551 }
1552 }
1553
1554 public static void test_Integer_rotateLeft() throws Exception {
1555 Assert.assertEquals(Integer.rotateLeft(0x11, 0), 0x11);
1556
1557 Assert.assertEquals(Integer.rotateLeft(0x11, 1), 0x22);
1558 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE - 1), 0x80000008);
1559 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE), 0x11);
1560 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE + 1), 0x22);
1561
1562 Assert.assertEquals(Integer.rotateLeft(0x11, -1), 0x80000008);
1563 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE - 1)), 0x22);
1564 Assert.assertEquals(Integer.rotateLeft(0x11, -Integer.SIZE), 0x11);
1565 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE + 1)), 0x80000008);
1566
1567 Assert.assertEquals(Integer.rotateLeft(0xC0000000, 1), 0x80000001);
1568
1569 for (int i = 0; i < Integer.SIZE; i++) {
1570 Assert.assertEquals(
1571 Integer.rotateLeft(0xBBAAAADD, i),
1572 (0xBBAAAADD << i) | (0xBBAAAADD >>> (Integer.SIZE - i)));
1573 }
1574 }
1575
1576 public static void test_Long_rotateLeft() throws Exception {
1577 Assert.assertEquals(Long.rotateLeft(0x11, 0), 0x11);
1578
1579 Assert.assertEquals(Long.rotateLeft(0x11, 1), 0x22);
1580 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE - 1), 0x8000000000000008L);
1581 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE), 0x11);
1582 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE + 1), 0x22);
1583
1584 Assert.assertEquals(Long.rotateLeft(0x11, -1), 0x8000000000000008L);
1585 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE - 1)), 0x22);
1586 Assert.assertEquals(Long.rotateLeft(0x11, -Long.SIZE), 0x11);
1587 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE + 1)), 0x8000000000000008L);
1588
1589 Assert.assertEquals(Long.rotateLeft(0xC000000000000000L, 1), 0x8000000000000001L);
1590
1591 for (int i = 0; i < Long.SIZE; i++) {
1592 Assert.assertEquals(
1593 Long.rotateLeft(0xBBAAAADDFF0000DDL, i),
1594 (0xBBAAAADDFF0000DDL << i) | (0xBBAAAADDFF0000DDL >>> (Long.SIZE - i)));
1595 }
1596 }
1597
1598 public static void test_Integer_rotateRightLeft() throws Exception {
1599 for (int i = 0; i < Integer.SIZE * 2; i++) {
1600 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, i),
1601 Integer.rotateRight(0xBBAAAADD, -i));
1602 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, -i),
1603 Integer.rotateRight(0xBBAAAADD, i));
1604 }
1605 }
1606
1607 public static void test_Long_rotateRightLeft() throws Exception {
1608 for (int i = 0; i < Long.SIZE * 2; i++) {
1609 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, i),
1610 Long.rotateRight(0xBBAAAADDFF0000DDL, -i));
1611 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, -i),
1612 Long.rotateRight(0xBBAAAADDFF0000DDL, i));
1613 }
1614 }
jeffhao5d1ac922011-09-29 17:41:15 -07001615}