blob: fad8a9f100bf4518c01b666006efcedc3050e3b2 [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);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100733 Assert.assertEquals(Math.rint(+0.5), +0.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100734 Assert.assertEquals(Math.rint(+2.0), +2.0d, 0.0);
735 Assert.assertEquals(Math.rint(+2.1), +2.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100736 Assert.assertEquals(Math.rint(+2.5), +2.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100737 Assert.assertEquals(Math.rint(+2.9), +3.0d, 0.0);
738 Assert.assertEquals(Math.rint(+3.0), +3.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100739 Assert.assertEquals(Math.rint(+3.5), +4.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100740 Assert.assertEquals(Math.rint(-2.0), -2.0d, 0.0);
741 Assert.assertEquals(Math.rint(-2.1), -2.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100742 Assert.assertEquals(Math.rint(-2.5), -2.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100743 Assert.assertEquals(Math.rint(-2.9), -3.0d, 0.0);
744 Assert.assertEquals(Math.rint(-3.0), -3.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100745 Assert.assertEquals(Math.rint(-3.5), -4.0d, 0.0); // expects tie-to-even
Chris Larsen82831092015-08-25 09:06:58 -0700746 // 2^52 - 1.5
747 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
748 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0);
749 // 2^52 - 0.5
750 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
751 Double.longBitsToDouble(0x4330000000000000l), 0.0);
752 // 2^52
753 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4330000000000000l)),
754 Double.longBitsToDouble(0x4330000000000000l), 0.0);
755 // 2^53 - 1
756 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
757 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
758 // 2^53
759 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4340000000000000l)),
760 Double.longBitsToDouble(0x4340000000000000l), 0.0);
761 // 2^63 - 2^10
762 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
763 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
764 // 2^63
765 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43E0000000000000l)),
766 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
767 // 2^64
768 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43F0000000000000l)),
769 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
770 // -(2^52 - 1.5)
771 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
772 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0);
773 // -(2^52 - 0.5)
774 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
775 Double.longBitsToDouble(0xC330000000000000l), 0.0);
776 // -2^52
777 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC330000000000000l)),
778 Double.longBitsToDouble(0xC330000000000000l), 0.0);
779 // -(2^53 - 1)
780 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
781 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
782 // -2^53
783 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC340000000000000l)),
784 Double.longBitsToDouble(0xC340000000000000l), 0.0);
785 // -(2^63 - 2^10)
786 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
787 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
788 // -2^63
789 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3E0000000000000l)),
790 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
791 // -2^64
792 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3F0000000000000l)),
793 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100794 Assert.assertEquals(Math.rint(Double.NaN), Double.NaN, 0.0);
795 Assert.assertEquals(Math.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
796 Assert.assertEquals(Math.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
797 }
798
799 public static void test_Math_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800800 Math.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100801 Assert.assertEquals(Math.round(+0.0d), (long)+0.0);
802 Assert.assertEquals(Math.round(-0.0d), (long)+0.0);
803 Assert.assertEquals(Math.round(2.0d), 2l);
804 Assert.assertEquals(Math.round(2.1d), 2l);
805 Assert.assertEquals(Math.round(2.5d), 3l);
806 Assert.assertEquals(Math.round(2.9d), 3l);
807 Assert.assertEquals(Math.round(3.0d), 3l);
808 Assert.assertEquals(Math.round(-2.0d), -2l);
809 Assert.assertEquals(Math.round(-2.1d), -2l);
810 Assert.assertEquals(Math.round(-2.5d), -2l);
811 Assert.assertEquals(Math.round(-2.9d), -3l);
812 Assert.assertEquals(Math.round(-3.0d), -3l);
Yi Kong879ca672015-11-18 14:11:44 +0000813 Assert.assertEquals(Math.round(0.49999999999999994d), 0l);
Chris Larsen7adaab02016-04-21 14:49:20 -0700814 Assert.assertEquals(Math.round(4503599627370495.0d), 4503599627370495l); // 2^52 - 1
815 Assert.assertEquals(Math.round(4503599627370495.5d), 4503599627370496l); // 2^52 - 0.5
816 Assert.assertEquals(Math.round(4503599627370496.0d), 4503599627370496l); // 2^52
817 Assert.assertEquals(Math.round(-4503599627370495.0d), -4503599627370495l); // -(2^52 - 1)
818 Assert.assertEquals(Math.round(-4503599627370495.5d), -4503599627370495l); // -(2^52 - 0.5)
819 Assert.assertEquals(Math.round(-4503599627370496.0d), -4503599627370496l); // -2^52
Hans Boehm92d4f0e2016-02-17 12:14:03 -0800820 Assert.assertEquals(Math.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -0700821 Assert.assertEquals(Math.round(-9007199254740991.0d), -9007199254740991l); // -(2^53 - 1)
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100822 Assert.assertEquals(Math.round(Double.NaN), (long)+0.0d);
823 Assert.assertEquals(Math.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
824 Assert.assertEquals(Math.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -0700825 Assert.assertEquals(Math.round(Double.longBitsToDouble(0x43F0000000000000l)),
826 Long.MAX_VALUE); // 2^64
827 Assert.assertEquals(Math.round(Double.longBitsToDouble(0xC3F0000000000000l)),
828 Long.MIN_VALUE); // -2^64
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100829 Assert.assertEquals(Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
830 Assert.assertEquals(Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
831 }
832
833 public static void test_Math_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800834 Math.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100835 Assert.assertEquals(Math.round(+0.0f), (int)+0.0);
836 Assert.assertEquals(Math.round(-0.0f), (int)+0.0);
837 Assert.assertEquals(Math.round(2.0f), 2);
838 Assert.assertEquals(Math.round(2.1f), 2);
839 Assert.assertEquals(Math.round(2.5f), 3);
840 Assert.assertEquals(Math.round(2.9f), 3);
841 Assert.assertEquals(Math.round(3.0f), 3);
842 Assert.assertEquals(Math.round(-2.0f), -2);
843 Assert.assertEquals(Math.round(-2.1f), -2);
844 Assert.assertEquals(Math.round(-2.5f), -2);
845 Assert.assertEquals(Math.round(-2.9f), -3);
846 Assert.assertEquals(Math.round(-3.0f), -3);
Chris Larsenb74353a2015-11-20 09:07:09 -0800847 // 0.4999999701976776123046875
848 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f);
Chris Larsenf09d5322016-04-22 12:06:34 -0700849 Assert.assertEquals(Math.round(8388607.0f), 8388607); // 2^23 - 1
850 Assert.assertEquals(Math.round(8388607.5f), 8388608); // 2^23 - 0.5
851 Assert.assertEquals(Math.round(8388608.0f), 8388608); // 2^23
852 Assert.assertEquals(Math.round(-8388607.0f), -8388607); // -(2^23 - 1)
853 Assert.assertEquals(Math.round(-8388607.5f), -8388607); // -(2^23 - 0.5)
854 Assert.assertEquals(Math.round(-8388608.0f), -8388608); // -2^23
Hans Boehm92d4f0e2016-02-17 12:14:03 -0800855 Assert.assertEquals(Math.round(16777215.0f), 16777215); // 2^24 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -0700856 Assert.assertEquals(Math.round(16777216.0f), 16777216); // 2^24
857 Assert.assertEquals(Math.round(-16777215.0f), -16777215); // -(2^24 - 1)
858 Assert.assertEquals(Math.round(-16777216.0f), -16777216); // -2^24
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100859 Assert.assertEquals(Math.round(Float.NaN), (int)+0.0f);
860 Assert.assertEquals(Math.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
861 Assert.assertEquals(Math.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -0700862 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x4F800000)),
863 Integer.MAX_VALUE); // 2^32
864 Assert.assertEquals(Math.round(Float.intBitsToFloat(0xCF800000)),
865 Integer.MIN_VALUE); // -2^32
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100866 Assert.assertEquals(Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
867 Assert.assertEquals(Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
868 }
869
Chris Larsen2714fe62016-02-11 14:23:53 -0800870 public static void test_Math_isNaN_D() {
871 // Quiet NaN.
872 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF4000000000000l)));
873 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF4000000000000l)));
874 // Signaling NaN.
875 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF8000000000000l)));
876 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF8000000000000l)));
877 // Distinct from +/- infinity.
878 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FF0000000000000l)));
879 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFF0000000000000l)));
880 // Distinct from normal numbers.
881 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FE0000000000000l)));
882 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFE0000000000000l)));
883 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0010000000000000l)));
884 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8010000000000000l)));
885 // Distinct from +/- zero.
886 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000000l)));
887 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000000l)));
888 // Distinct from subnormal numbers.
889 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0008000000000000l)));
890 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8008000000000000l)));
891 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000001l)));
892 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000001l)));
893 }
894
895 public static void test_Math_isNaN_F() {
896 // Quiet NaN.
897 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FA00000)));
898 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFA00000)));
899 // Signaling NaN.
900 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FC00000)));
901 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFC00000)));
902 // Distinct from +/- infinity.
903 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F800000)));
904 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF800000)));
905 // Distinct from normal numbers.
906 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F000000)));
907 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF000000)));
908 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00800000)));
909 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80800000)));
910 // Distinct from +/- zero.
911 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000000)));
912 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000000)));
913 // Distinct from subnormal numbers.
914 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00400000)));
915 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80400000)));
916 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000001)));
917 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000001)));
918 }
919
920 public static void test_Math_isInfinite_D() {
921 // Distinct from Quiet NaN.
922 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF4000000000000l)));
923 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF4000000000000l)));
924 // Distinct from Signaling NaN.
925 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF8000000000000l)));
926 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF8000000000000l)));
927 // +/- infinity.
928 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0x7FF0000000000000l)));
929 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0xFFF0000000000000l)));
930 // Distinct from normal numbers.
931 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FE0000000000000l)));
932 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFE0000000000000l)));
933 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0010000000000000l)));
934 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8010000000000000l)));
935 // Distinct from +/- zero.
936 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000000l)));
937 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000000l)));
938 // Distinct from subnormal numbers.
939 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0008000000000000l)));
940 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8008000000000000l)));
941 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000001l)));
942 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000001l)));
943 }
944
945 public static void test_Math_isInfinite_F() {
946 // Distinct from Quiet NaN.
947 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FA00000)));
948 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFA00000)));
949 // Distinct from Signaling NaN.
950 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FC00000)));
951 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFC00000)));
952 // +/- infinity.
953 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0x7F800000)));
954 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0xFF800000)));
955 // Distinct from normal numbers.
956 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7F000000)));
957 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFF000000)));
958 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00800000)));
959 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80800000)));
960 // Distinct from +/- zero.
961 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000000)));
962 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000000)));
963 // Distinct from subnormal numbers.
964 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00400000)));
965 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80400000)));
966 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000001)));
967 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000001)));
968 }
969
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100970 public static void test_StrictMath_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800971 StrictMath.abs(-1);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100972 Assert.assertEquals(StrictMath.abs(0), 0);
973 Assert.assertEquals(StrictMath.abs(123), 123);
974 Assert.assertEquals(StrictMath.abs(-123), 123);
975 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
976 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
977 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
978 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
979 }
980
981 public static void test_StrictMath_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800982 StrictMath.abs(-1L);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100983 Assert.assertEquals(StrictMath.abs(0L), 0L);
984 Assert.assertEquals(StrictMath.abs(123L), 123L);
985 Assert.assertEquals(StrictMath.abs(-123L), 123L);
986 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
987 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
988 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
989 }
990
Serban Constantinescu23abec92014-07-02 16:13:38 +0100991 public static void test_StrictMath_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800992 StrictMath.min(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100993 Assert.assertEquals(StrictMath.min(0, 0), 0);
994 Assert.assertEquals(StrictMath.min(1, 0), 0);
995 Assert.assertEquals(StrictMath.min(0, 1), 0);
996 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
997 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
998 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
999 }
1000
Serban Constantinescu23abec92014-07-02 16:13:38 +01001001 public static void test_StrictMath_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001002 StrictMath.max(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +01001003 Assert.assertEquals(StrictMath.max(0, 0), 0);
1004 Assert.assertEquals(StrictMath.max(1, 0), 1);
1005 Assert.assertEquals(StrictMath.max(0, 1), 1);
1006 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
1007 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
1008 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
1009 }
1010
Serban Constantinescu23abec92014-07-02 16:13:38 +01001011 public static void test_StrictMath_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001012 StrictMath.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001013 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
1014 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
1015 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
1016 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
1017 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
1018 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
1019 }
1020
1021 public static void test_StrictMath_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001022 StrictMath.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001023 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
1024 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
1025 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
1026 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
1027 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
1028 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
1029 }
1030
1031 public static void test_StrictMath_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001032 StrictMath.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001033 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN)));
1034 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f)));
1035 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f);
1036 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f);
1037 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f);
1038 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f);
1039 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f);
1040 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f);
1041 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f);
1042 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f);
1043 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001044 }
1045
1046 public static void test_StrictMath_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001047 StrictMath.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001048 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN)));
1049 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f)));
1050 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f);
1051 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f);
1052 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f);
1053 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f);
1054 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f);
1055 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f);
1056 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
1057 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
1058 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001059 }
1060
1061 public static void test_StrictMath_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001062 StrictMath.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001063 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN)));
1064 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d)));
1065 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d);
1066 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d);
1067 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d);
1068 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d);
1069 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d);
1070 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d);
1071 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d);
1072 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d);
1073 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001074 }
1075
1076 public static void test_StrictMath_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001077 StrictMath.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001078 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN)));
1079 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d)));
1080 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d);
1081 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d);
1082 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d);
1083 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d);
1084 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d);
1085 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d);
1086 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
1087 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
1088 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001089 }
1090
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001091 public static void test_StrictMath_sqrt() {
1092 StrictMath.sqrt(+4.0);
1093 Assert.assertEquals(StrictMath.sqrt(+4.0), +2.0d, 0.0);
1094 Assert.assertEquals(StrictMath.sqrt(+49.0), +7.0d, 0.0);
1095 Assert.assertEquals(StrictMath.sqrt(+1.44), +1.2d, 0.0);
1096 }
1097
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001098 public static void test_StrictMath_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001099 StrictMath.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001100 Assert.assertEquals(StrictMath.ceil(+0.0), +0.0d, 0.0);
1101 Assert.assertEquals(StrictMath.ceil(-0.0), -0.0d, 0.0);
1102 Assert.assertEquals(StrictMath.ceil(-0.9), -0.0d, 0.0);
1103 Assert.assertEquals(StrictMath.ceil(-0.5), -0.0d, 0.0);
1104 Assert.assertEquals(StrictMath.ceil(0.0), -0.0d, 0.0);
1105 Assert.assertEquals(StrictMath.ceil(+2.0), +2.0d, 0.0);
1106 Assert.assertEquals(StrictMath.ceil(+2.1), +3.0d, 0.0);
1107 Assert.assertEquals(StrictMath.ceil(+2.5), +3.0d, 0.0);
1108 Assert.assertEquals(StrictMath.ceil(+2.9), +3.0d, 0.0);
1109 Assert.assertEquals(StrictMath.ceil(+3.0), +3.0d, 0.0);
1110 Assert.assertEquals(StrictMath.ceil(-2.0), -2.0d, 0.0);
1111 Assert.assertEquals(StrictMath.ceil(-2.1), -2.0d, 0.0);
1112 Assert.assertEquals(StrictMath.ceil(-2.5), -2.0d, 0.0);
1113 Assert.assertEquals(StrictMath.ceil(-2.9), -2.0d, 0.0);
1114 Assert.assertEquals(StrictMath.ceil(-3.0), -3.0d, 0.0);
1115 Assert.assertEquals(StrictMath.ceil(Double.NaN), Double.NaN, 0.0);
1116 Assert.assertEquals(StrictMath.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1117 Assert.assertEquals(StrictMath.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1118 }
1119
1120 public static void test_StrictMath_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001121 StrictMath.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001122 Assert.assertEquals(StrictMath.floor(+0.0), +0.0d, 0.0);
1123 Assert.assertEquals(StrictMath.floor(-0.0), -0.0d, 0.0);
1124 Assert.assertEquals(StrictMath.floor(+2.0), +2.0d, 0.0);
1125 Assert.assertEquals(StrictMath.floor(+2.1), +2.0d, 0.0);
1126 Assert.assertEquals(StrictMath.floor(+2.5), +2.0d, 0.0);
1127 Assert.assertEquals(StrictMath.floor(+2.9), +2.0d, 0.0);
1128 Assert.assertEquals(StrictMath.floor(+3.0), +3.0d, 0.0);
1129 Assert.assertEquals(StrictMath.floor(-2.0), -2.0d, 0.0);
1130 Assert.assertEquals(StrictMath.floor(-2.1), -3.0d, 0.0);
1131 Assert.assertEquals(StrictMath.floor(-2.5), -3.0d, 0.0);
1132 Assert.assertEquals(StrictMath.floor(-2.9), -3.0d, 0.0);
1133 Assert.assertEquals(StrictMath.floor(-3.0), -3.0d, 0.0);
1134 Assert.assertEquals(StrictMath.floor(Double.NaN), Double.NaN, 0.0);
1135 Assert.assertEquals(StrictMath.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1136 Assert.assertEquals(StrictMath.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1137 }
1138
1139 public static void test_StrictMath_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001140 StrictMath.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001141 Assert.assertEquals(StrictMath.rint(+0.0), +0.0d, 0.0);
1142 Assert.assertEquals(StrictMath.rint(-0.0), -0.0d, 0.0);
1143 Assert.assertEquals(StrictMath.rint(+2.0), +2.0d, 0.0);
1144 Assert.assertEquals(StrictMath.rint(+2.1), +2.0d, 0.0);
1145 Assert.assertEquals(StrictMath.rint(+2.5), +2.0d, 0.0);
1146 Assert.assertEquals(StrictMath.rint(+2.9), +3.0d, 0.0);
1147 Assert.assertEquals(StrictMath.rint(+3.0), +3.0d, 0.0);
1148 Assert.assertEquals(StrictMath.rint(-2.0), -2.0d, 0.0);
1149 Assert.assertEquals(StrictMath.rint(-2.1), -2.0d, 0.0);
1150 Assert.assertEquals(StrictMath.rint(-2.5), -2.0d, 0.0);
1151 Assert.assertEquals(StrictMath.rint(-2.9), -3.0d, 0.0);
1152 Assert.assertEquals(StrictMath.rint(-3.0), -3.0d, 0.0);
1153 Assert.assertEquals(StrictMath.rint(Double.NaN), Double.NaN, 0.0);
1154 Assert.assertEquals(StrictMath.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1155 Assert.assertEquals(StrictMath.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1156 }
1157
1158 public static void test_StrictMath_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001159 StrictMath.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001160 Assert.assertEquals(StrictMath.round(+0.0d), (long)+0.0);
1161 Assert.assertEquals(StrictMath.round(-0.0d), (long)+0.0);
1162 Assert.assertEquals(StrictMath.round(2.0d), 2l);
1163 Assert.assertEquals(StrictMath.round(2.1d), 2l);
1164 Assert.assertEquals(StrictMath.round(2.5d), 3l);
1165 Assert.assertEquals(StrictMath.round(2.9d), 3l);
1166 Assert.assertEquals(StrictMath.round(3.0d), 3l);
1167 Assert.assertEquals(StrictMath.round(-2.0d), -2l);
1168 Assert.assertEquals(StrictMath.round(-2.1d), -2l);
1169 Assert.assertEquals(StrictMath.round(-2.5d), -2l);
1170 Assert.assertEquals(StrictMath.round(-2.9d), -3l);
1171 Assert.assertEquals(StrictMath.round(-3.0d), -3l);
Yi Kong879ca672015-11-18 14:11:44 +00001172 Assert.assertEquals(StrictMath.round(0.49999999999999994d), 0l);
Chris Larsen7adaab02016-04-21 14:49:20 -07001173 Assert.assertEquals(StrictMath.round(4503599627370495.0d), 4503599627370495l); // 2^52 - 1
1174 Assert.assertEquals(StrictMath.round(4503599627370495.5d), 4503599627370496l); // 2^52 - 0.5
1175 Assert.assertEquals(StrictMath.round(4503599627370496.0d), 4503599627370496l); // 2^52
1176 Assert.assertEquals(StrictMath.round(-4503599627370495.0d), -4503599627370495l); // -(2^52 - 1)
1177 Assert.assertEquals(StrictMath.round(-4503599627370495.5d), -4503599627370495l); // -(2^52 - 0.5)
1178 Assert.assertEquals(StrictMath.round(-4503599627370496.0d), -4503599627370496l); // -2^52
Vladimir Marko9c1c06a2016-02-25 17:50:41 +00001179 Assert.assertEquals(StrictMath.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -07001180 Assert.assertEquals(StrictMath.round(-9007199254740991.0d), -9007199254740991l); // -(2^53 - 1)
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001181 Assert.assertEquals(StrictMath.round(Double.NaN), (long)+0.0d);
1182 Assert.assertEquals(StrictMath.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
1183 Assert.assertEquals(StrictMath.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -07001184 Assert.assertEquals(StrictMath.round(Double.longBitsToDouble(0x43F0000000000000l)),
1185 Long.MAX_VALUE); // 2^64
1186 Assert.assertEquals(StrictMath.round(Double.longBitsToDouble(0xC3F0000000000000l)),
1187 Long.MIN_VALUE); // -2^64
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001188 Assert.assertEquals(StrictMath.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
1189 Assert.assertEquals(StrictMath.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
1190 }
1191
1192 public static void test_StrictMath_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001193 StrictMath.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001194 Assert.assertEquals(StrictMath.round(+0.0f), (int)+0.0);
1195 Assert.assertEquals(StrictMath.round(-0.0f), (int)+0.0);
1196 Assert.assertEquals(StrictMath.round(2.0f), 2);
1197 Assert.assertEquals(StrictMath.round(2.1f), 2);
1198 Assert.assertEquals(StrictMath.round(2.5f), 3);
1199 Assert.assertEquals(StrictMath.round(2.9f), 3);
1200 Assert.assertEquals(StrictMath.round(3.0f), 3);
1201 Assert.assertEquals(StrictMath.round(-2.0f), -2);
1202 Assert.assertEquals(StrictMath.round(-2.1f), -2);
1203 Assert.assertEquals(StrictMath.round(-2.5f), -2);
1204 Assert.assertEquals(StrictMath.round(-2.9f), -3);
1205 Assert.assertEquals(StrictMath.round(-3.0f), -3);
Chris Larsenb74353a2015-11-20 09:07:09 -08001206 // 0.4999999701976776123046875
1207 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f);
Chris Larsenf09d5322016-04-22 12:06:34 -07001208 Assert.assertEquals(StrictMath.round(8388607.0f), 8388607); // 2^23 - 1
1209 Assert.assertEquals(StrictMath.round(8388607.5f), 8388608); // 2^23 - 0.5
1210 Assert.assertEquals(StrictMath.round(8388608.0f), 8388608); // 2^23
1211 Assert.assertEquals(StrictMath.round(-8388607.0f), -8388607); // -(2^23 - 1)
1212 Assert.assertEquals(StrictMath.round(-8388607.5f), -8388607); // -(2^23 - 0.5)
1213 Assert.assertEquals(StrictMath.round(-8388608.0f), -8388608); // -2^23
Vladimir Marko9c1c06a2016-02-25 17:50:41 +00001214 Assert.assertEquals(StrictMath.round(16777215.0f), 16777215); // 2^24 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -07001215 Assert.assertEquals(StrictMath.round(16777216.0f), 16777216); // 2^24
1216 Assert.assertEquals(StrictMath.round(-16777215.0f), -16777215); // -(2^24 - 1)
1217 Assert.assertEquals(StrictMath.round(-16777216.0f), -16777216); // -2^24
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001218 Assert.assertEquals(StrictMath.round(Float.NaN), (int)+0.0f);
1219 Assert.assertEquals(StrictMath.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
1220 Assert.assertEquals(StrictMath.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -07001221 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x4F800000)),
1222 Integer.MAX_VALUE); // 2^32
1223 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0xCF800000)),
1224 Integer.MIN_VALUE); // -2^32
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001225 Assert.assertEquals(StrictMath.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
1226 Assert.assertEquals(StrictMath.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
1227 }
1228
Elliott Hughes28c384b2012-06-15 16:46:25 -07001229 public static void test_Float_floatToRawIntBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001230 Float.floatToRawIntBits(-1.0f);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001231 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
1232 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
1233 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
1234 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
1235 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
1236 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
1237 }
1238
1239 public static void test_Float_intBitsToFloat() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001240 Float.intBitsToFloat(0xbf800000);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001241 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
1242 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
1243 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
1244 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
1245 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
1246 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
1247 }
1248
1249 public static void test_Double_doubleToRawLongBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001250 Double.doubleToRawLongBits(-1.0);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001251 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
1252 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
1253 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
1254 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
1255 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
1256 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
1257 }
1258
1259 public static void test_Double_longBitsToDouble() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001260 Double.longBitsToDouble(0xbff0000000000000L);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001261 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
1262 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
1263 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
1264 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
1265 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
1266 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
1267 }
Serban Constantinescu23abec92014-07-02 16:13:38 +01001268
Zheng Xua3fe7422014-07-09 14:03:15 +08001269 public static void test_Short_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001270 Short.reverseBytes((short)0x1357);
Zheng Xua3fe7422014-07-09 14:03:15 +08001271 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
1272 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
1273 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
1274 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
1275 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
1276 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
1277 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
1278 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
1279 }
1280
1281 public static void test_Integer_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001282 Integer.reverseBytes(0x13579bdf);
Zheng Xua3fe7422014-07-09 14:03:15 +08001283 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
1284 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
1285 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
1286 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
1287 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
1288 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
1289 }
1290
1291 public static void test_Long_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001292 Long.reverseBytes(0x13579bdf2468ace0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001293 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
1294 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
1295 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
1296 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
1297 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
1298 }
1299
Serban Constantinescu23abec92014-07-02 16:13:38 +01001300 public static void test_Integer_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001301 Integer.reverse(0x12345678);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001302 Assert.assertEquals(Integer.reverse(1), 0x80000000);
1303 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
1304 Assert.assertEquals(Integer.reverse(0), 0);
1305 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
1306 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
1307 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
1308 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
1309 }
1310
1311 public static void test_Long_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001312 Long.reverse(0x1234567812345678L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001313 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
1314 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
1315 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001316 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
1317 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
1318 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001319 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
Andreas Gampe575422f2015-07-07 13:25:58 -07001320
1321 Assert.assertEquals(test_Long_reverse_b22324327(0xaaaaaaaaaaaaaaaaL, 0x5555555555555555L),
1322 157472205507277347L);
1323 }
1324
1325 // A bit more complicated than the above. Use local variables to stress register allocation.
1326 private static long test_Long_reverse_b22324327(long l1, long l2) {
1327 // A couple of local integers. Use them in a loop, so they get promoted.
1328 int i1 = 0, i2 = 1, i3 = 2, i4 = 3, i5 = 4, i6 = 5, i7 = 6, i8 = 7;
1329 for (int k = 0; k < 10; k++) {
1330 i1 += 1;
1331 i2 += 2;
1332 i3 += 3;
1333 i4 += 4;
1334 i5 += 5;
1335 i6 += 6;
1336 i7 += 7;
1337 i8 += 8;
1338 }
1339
1340 // Do the Long.reverse() calls, save the results.
1341 long r1 = Long.reverse(l1);
1342 long r2 = Long.reverse(l2);
1343
1344 // Some more looping with the ints.
1345 for (int k = 0; k < 10; k++) {
1346 i1 += 1;
1347 i2 += 2;
1348 i3 += 3;
1349 i4 += 4;
1350 i5 += 5;
1351 i6 += 6;
1352 i7 += 7;
1353 i8 += 8;
1354 }
1355
1356 // Include everything in the result, so things are kept live. Try to be a little bit clever to
1357 // avoid things being folded somewhere.
1358 return (r1 / i1) + (r2 / i2) + i3 + i4 + i5 + i6 + i7 + i8;
Serban Constantinescu23abec92014-07-02 16:13:38 +01001359 }
1360
Mark Mendelld5897672015-08-12 21:16:41 -04001361 public static boolean doThrow = false;
1362
1363 public static int $noinline$return_int_zero() {
1364 if (doThrow) {
1365 throw new Error();
1366 }
1367 return 0;
1368 }
1369
Scott Wakeling611d3392015-07-10 11:42:06 +01001370 public static void test_Integer_numberOfLeadingZeros() {
1371 Assert.assertEquals(Integer.numberOfLeadingZeros(0), Integer.SIZE);
Mark Mendelld5897672015-08-12 21:16:41 -04001372 Assert.assertEquals(Integer.numberOfLeadingZeros(1), Integer.SIZE - 1);
1373 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << (Integer.SIZE-1)), 0);
1374 Assert.assertEquals(Integer.numberOfLeadingZeros($noinline$return_int_zero()), Integer.SIZE);
Scott Wakeling611d3392015-07-10 11:42:06 +01001375 for (int i = 0; i < Integer.SIZE; i++) {
1376 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << i), Integer.SIZE - 1 - i);
1377 Assert.assertEquals(Integer.numberOfLeadingZeros((1 << i) | 1), Integer.SIZE - 1 - i);
1378 Assert.assertEquals(Integer.numberOfLeadingZeros(0xFFFFFFFF >>> i), i);
1379 }
1380 }
1381
Mark Mendelld5897672015-08-12 21:16:41 -04001382 public static long $noinline$return_long_zero() {
1383 if (doThrow) {
1384 throw new Error();
1385 }
1386 return 0;
1387 }
1388
Scott Wakeling611d3392015-07-10 11:42:06 +01001389 public static void test_Long_numberOfLeadingZeros() {
1390 Assert.assertEquals(Long.numberOfLeadingZeros(0L), Long.SIZE);
Mark Mendelld5897672015-08-12 21:16:41 -04001391 Assert.assertEquals(Long.numberOfLeadingZeros(1L), Long.SIZE - 1);
1392 Assert.assertEquals(Long.numberOfLeadingZeros(1L << ((Long.SIZE/2)-1)), Long.SIZE/2);
1393 Assert.assertEquals(Long.numberOfLeadingZeros(1L << (Long.SIZE-1)), 0);
1394 Assert.assertEquals(Long.numberOfLeadingZeros($noinline$return_long_zero()), Long.SIZE);
Scott Wakeling611d3392015-07-10 11:42:06 +01001395 for (int i = 0; i < Long.SIZE; i++) {
1396 Assert.assertEquals(Long.numberOfLeadingZeros(1L << i), Long.SIZE - 1 - i);
1397 Assert.assertEquals(Long.numberOfLeadingZeros((1L << i) | 1L), Long.SIZE - 1 - i);
1398 Assert.assertEquals(Long.numberOfLeadingZeros(0xFFFFFFFFFFFFFFFFL >>> i), i);
1399 }
1400 }
1401
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001402 static Object runtime;
1403 static Method address_of;
Zuo Wangf37a88b2014-07-10 04:26:41 -07001404 static Method new_non_movable_array;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001405 static Method peek_byte;
1406 static Method peek_short;
1407 static Method peek_int;
1408 static Method peek_long;
1409 static Method poke_byte;
1410 static Method poke_short;
1411 static Method poke_int;
1412 static Method poke_long;
1413
1414 public static void initSupportMethodsForPeekPoke() throws Exception {
1415 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
1416 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
1417 runtime = get_runtime.invoke(null);
1418 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
Zuo Wangf37a88b2014-07-10 04:26:41 -07001419 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001420
1421 Class<?> io_memory = Class.forName("libcore.io.Memory");
1422 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
1423 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
1424 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
1425 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
1426 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
1427 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
1428 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
1429 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
1430 }
1431
1432 public static void test_Memory_peekByte() throws Exception {
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 b[0] = 0x12;
1435 b[1] = 0x11;
1436 long address = (long)address_of.invoke(runtime, b);
1437 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
1438 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
1439 }
1440
1441 public static void test_Memory_peekShort() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001442 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001443 b[0] = 0x13;
1444 b[1] = 0x12;
1445 b[2] = 0x11;
1446 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001447 peek_short.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001448 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
1449 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
1450 }
1451
1452 public static void test_Memory_peekInt() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001453 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001454 b[0] = 0x15;
1455 b[1] = 0x14;
1456 b[2] = 0x13;
1457 b[3] = 0x12;
1458 b[4] = 0x11;
1459 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001460 peek_int.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001461 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
1462 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
1463 }
1464
1465 public static void test_Memory_peekLong() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001466 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001467 b[0] = 0x19;
1468 b[1] = 0x18;
1469 b[2] = 0x17;
1470 b[3] = 0x16;
1471 b[4] = 0x15;
1472 b[5] = 0x14;
1473 b[6] = 0x13;
1474 b[7] = 0x12;
1475 b[8] = 0x11;
1476 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001477 peek_long.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001478 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
1479 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
1480 }
1481
1482 public static void test_Memory_pokeByte() throws Exception {
1483 byte[] r = {0x11, 0x12};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001484 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001485 long address = (long)address_of.invoke(runtime, b);
1486 poke_byte.invoke(null, address, (byte)0x11);
1487 poke_byte.invoke(null, address + 1, (byte)0x12);
1488 Assert.assertTrue(Arrays.equals(r, b));
1489 }
1490
1491 public static void test_Memory_pokeShort() throws Exception {
1492 byte[] ra = {0x12, 0x11, 0x13};
1493 byte[] ru = {0x12, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001494 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001495 long address = (long)address_of.invoke(runtime, b);
1496
1497 // Aligned write
1498 b[2] = 0x13;
1499 poke_short.invoke(null, address, (short)0x1112, false);
1500 Assert.assertTrue(Arrays.equals(ra, b));
1501
1502 // Unaligned write
1503 poke_short.invoke(null, address + 1, (short)0x2122, false);
1504 Assert.assertTrue(Arrays.equals(ru, b));
1505 }
1506
1507 public static void test_Memory_pokeInt() throws Exception {
1508 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
1509 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001510 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001511 long address = (long)address_of.invoke(runtime, b);
1512
1513 b[4] = 0x15;
1514 poke_int.invoke(null, address, (int)0x11121314, false);
1515 Assert.assertTrue(Arrays.equals(ra, b));
1516
1517 poke_int.invoke(null, address + 1, (int)0x21222324, false);
1518 Assert.assertTrue(Arrays.equals(ru, b));
1519 }
1520
1521 public static void test_Memory_pokeLong() throws Exception {
1522 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
1523 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001524 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001525 long address = (long)address_of.invoke(runtime, b);
1526
1527 b[8] = 0x19;
1528 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
1529 Assert.assertTrue(Arrays.equals(ra, b));
1530
1531 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
1532 Assert.assertTrue(Arrays.equals(ru, b));
1533 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +01001534
1535 public static void test_Integer_numberOfTrailingZeros() {
1536 Assert.assertEquals(Integer.numberOfTrailingZeros(0), Integer.SIZE);
1537 for (int i = 0; i < Integer.SIZE; i++) {
1538 Assert.assertEquals(
1539 Integer.numberOfTrailingZeros(0x80000000 >> i),
1540 Integer.SIZE - 1 - i);
1541 Assert.assertEquals(
1542 Integer.numberOfTrailingZeros((0x80000000 >> i) | 0x80000000),
1543 Integer.SIZE - 1 - i);
1544 Assert.assertEquals(Integer.numberOfTrailingZeros(1 << i), i);
1545 }
1546 }
1547
1548 public static void test_Long_numberOfTrailingZeros() {
1549 Assert.assertEquals(Long.numberOfTrailingZeros(0), Long.SIZE);
1550 for (int i = 0; i < Long.SIZE; i++) {
1551 Assert.assertEquals(
1552 Long.numberOfTrailingZeros(0x8000000000000000L >> i),
1553 Long.SIZE - 1 - i);
1554 Assert.assertEquals(
1555 Long.numberOfTrailingZeros((0x8000000000000000L >> i) | 0x8000000000000000L),
1556 Long.SIZE - 1 - i);
1557 Assert.assertEquals(Long.numberOfTrailingZeros(1L << i), i);
1558 }
1559 }
1560
1561 public static void test_Integer_rotateRight() throws Exception {
1562 Assert.assertEquals(Integer.rotateRight(0x11, 0), 0x11);
1563
1564 Assert.assertEquals(Integer.rotateRight(0x11, 1), 0x80000008);
1565 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE - 1), 0x22);
1566 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE), 0x11);
1567 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE + 1), 0x80000008);
1568
1569 Assert.assertEquals(Integer.rotateRight(0x11, -1), 0x22);
1570 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE - 1)), 0x80000008);
1571 Assert.assertEquals(Integer.rotateRight(0x11, -Integer.SIZE), 0x11);
1572 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE + 1)), 0x22);
1573
1574 Assert.assertEquals(Integer.rotateRight(0x80000000, 1), 0x40000000);
1575
1576 for (int i = 0; i < Integer.SIZE; i++) {
1577 Assert.assertEquals(
1578 Integer.rotateRight(0xBBAAAADD, i),
1579 (0xBBAAAADD >>> i) | (0xBBAAAADD << (Integer.SIZE - i)));
1580 }
1581 }
1582
1583 public static void test_Long_rotateRight() throws Exception {
1584 Assert.assertEquals(Long.rotateRight(0x11, 0), 0x11);
1585
1586 Assert.assertEquals(Long.rotateRight(0x11, 1), 0x8000000000000008L);
1587 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE - 1), 0x22);
1588 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE), 0x11);
1589 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE + 1), 0x8000000000000008L);
1590
1591 Assert.assertEquals(Long.rotateRight(0x11, -1), 0x22);
1592 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE - 1)), 0x8000000000000008L);
1593 Assert.assertEquals(Long.rotateRight(0x11, -Long.SIZE), 0x11);
1594 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE + 1)), 0x22);
1595
1596 Assert.assertEquals(Long.rotateRight(0x8000000000000000L, 1), 0x4000000000000000L);
1597
1598 for (int i = 0; i < Long.SIZE; i++) {
1599 Assert.assertEquals(
1600 Long.rotateRight(0xBBAAAADDFF0000DDL, i),
1601 (0xBBAAAADDFF0000DDL >>> i) | (0xBBAAAADDFF0000DDL << (Long.SIZE - i)));
1602 }
1603 }
1604
1605 public static void test_Integer_rotateLeft() throws Exception {
1606 Assert.assertEquals(Integer.rotateLeft(0x11, 0), 0x11);
1607
1608 Assert.assertEquals(Integer.rotateLeft(0x11, 1), 0x22);
1609 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE - 1), 0x80000008);
1610 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE), 0x11);
1611 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE + 1), 0x22);
1612
1613 Assert.assertEquals(Integer.rotateLeft(0x11, -1), 0x80000008);
1614 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE - 1)), 0x22);
1615 Assert.assertEquals(Integer.rotateLeft(0x11, -Integer.SIZE), 0x11);
1616 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE + 1)), 0x80000008);
1617
1618 Assert.assertEquals(Integer.rotateLeft(0xC0000000, 1), 0x80000001);
1619
1620 for (int i = 0; i < Integer.SIZE; i++) {
1621 Assert.assertEquals(
1622 Integer.rotateLeft(0xBBAAAADD, i),
1623 (0xBBAAAADD << i) | (0xBBAAAADD >>> (Integer.SIZE - i)));
1624 }
1625 }
1626
1627 public static void test_Long_rotateLeft() throws Exception {
1628 Assert.assertEquals(Long.rotateLeft(0x11, 0), 0x11);
1629
1630 Assert.assertEquals(Long.rotateLeft(0x11, 1), 0x22);
1631 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE - 1), 0x8000000000000008L);
1632 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE), 0x11);
1633 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE + 1), 0x22);
1634
1635 Assert.assertEquals(Long.rotateLeft(0x11, -1), 0x8000000000000008L);
1636 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE - 1)), 0x22);
1637 Assert.assertEquals(Long.rotateLeft(0x11, -Long.SIZE), 0x11);
1638 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE + 1)), 0x8000000000000008L);
1639
1640 Assert.assertEquals(Long.rotateLeft(0xC000000000000000L, 1), 0x8000000000000001L);
1641
1642 for (int i = 0; i < Long.SIZE; i++) {
1643 Assert.assertEquals(
1644 Long.rotateLeft(0xBBAAAADDFF0000DDL, i),
1645 (0xBBAAAADDFF0000DDL << i) | (0xBBAAAADDFF0000DDL >>> (Long.SIZE - i)));
1646 }
1647 }
1648
1649 public static void test_Integer_rotateRightLeft() throws Exception {
1650 for (int i = 0; i < Integer.SIZE * 2; i++) {
1651 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, i),
1652 Integer.rotateRight(0xBBAAAADD, -i));
1653 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, -i),
1654 Integer.rotateRight(0xBBAAAADD, i));
1655 }
1656 }
1657
1658 public static void test_Long_rotateRightLeft() throws Exception {
1659 for (int i = 0; i < Long.SIZE * 2; i++) {
1660 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, i),
1661 Long.rotateRight(0xBBAAAADDFF0000DDL, -i));
1662 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, -i),
1663 Long.rotateRight(0xBBAAAADDFF0000DDL, i));
1664 }
1665 }
jeffhao5d1ac922011-09-29 17:41:15 -07001666}