blob: 5597947cfc44fdf92fbf4d50a491a2f7b164fc98 [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();
Andra Danciudc787f42020-07-07 14:28:56 +000085 test_Integer_divideUnsigned();
Artem Serova3bd4ec2020-08-27 16:26:17 +010086 test_Long_divideUnsigned();
Scott Wakeling9ee23f42015-07-23 10:44:35 +010087 test_Integer_numberOfTrailingZeros();
88 test_Long_numberOfTrailingZeros();
89 test_Integer_rotateRight();
90 test_Long_rotateRight();
91 test_Integer_rotateLeft();
92 test_Long_rotateLeft();
93 test_Integer_rotateRightLeft();
94 test_Long_rotateRightLeft();
Elliott Hughes28c384b2012-06-15 16:46:25 -070095 }
96
Andreas Gampe7a949612014-07-08 11:03:59 -070097 /**
98 * Will test inlining Thread.currentThread().
99 */
100 public static void test_Thread_currentThread() {
101 // 1. Do not use result.
102 Thread.currentThread();
103
104 // 2. Result should not be null.
105 Assert.assertNotNull(Thread.currentThread());
106 }
107
Elliott Hughes28c384b2012-06-15 16:46:25 -0700108 public static void test_String_length() {
109 String str0 = "";
110 String str1 = "x";
111 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
112
113 Assert.assertEquals(str0.length(), 0);
114 Assert.assertEquals(str1.length(), 1);
115 Assert.assertEquals(str80.length(), 80);
116
117 String strNull = null;
118 try {
119 strNull.length();
120 Assert.fail();
121 } catch (NullPointerException expected) {
122 }
123 }
124
125 public static void test_String_isEmpty() {
126 String str0 = "";
127 String str1 = "x";
128
129 Assert.assertTrue(str0.isEmpty());
130 Assert.assertFalse(str1.isEmpty());
131
132 String strNull = null;
133 try {
134 strNull.isEmpty();
135 Assert.fail();
136 } catch (NullPointerException expected) {
137 }
138 }
139
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800140 // Break up the charAt tests. The optimizing compiler doesn't optimize methods with try-catch yet,
141 // so we need to separate out the tests that are expected to throw exception
142
Elliott Hughes28c384b2012-06-15 16:46:25 -0700143 public static void test_String_charAt() {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800144 String testStr = "Now is the time to test some stuff";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700145
Andreas Gampe878d58c2015-01-15 23:24:00 -0800146 Assert.assertEquals(testStr.length() - 1, 33); // 33 = testStr.length()-1 as a constant.
147 Assert.assertEquals('f', testStr.charAt(33));
Elliott Hughes28c384b2012-06-15 16:46:25 -0700148
Andreas Gampe878d58c2015-01-15 23:24:00 -0800149 test_String_charAt(testStr, 'N', 'o', ' ', 'f');
150 test_String_charAt(testStr.substring(3,15), ' ', 'i', 'm', 'e');
151 }
152 public static void test_String_charAt(String testStr, char a, char b, char c, char d) {
153 Assert.assertEquals(a, testStr.charAt(0));
154 Assert.assertEquals(b, testStr.charAt(1));
155 Assert.assertEquals(c, testStr.charAt(10));
156 Assert.assertEquals(d, testStr.charAt(testStr.length()-1));
157
158 test_String_charAtExc(testStr);
159 test_String_charAtExc2(testStr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800160 }
161
Andreas Gampe878d58c2015-01-15 23:24:00 -0800162 private static void test_String_charAtExc(String testStr) {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700163 try {
164 testStr.charAt(-1);
165 Assert.fail();
166 } catch (StringIndexOutOfBoundsException expected) {
167 }
168 try {
169 testStr.charAt(80);
170 Assert.fail();
171 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700172 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000173 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800174 if (testStr.length() == 34) {
175 testStr.charAt(34); // 34 = "Now is the time to test some stuff".length()
176 } else {
177 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length()
178 testStr.charAt(12);
179 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000180 Assert.fail();
181 } catch (StringIndexOutOfBoundsException expected) {
182 }
183 try {
184 test_String_charAt_inner(testStr, -1);
185 Assert.fail();
186 } catch (StringIndexOutOfBoundsException expected) {
187 }
188 try {
189 test_String_charAt_inner(testStr, 80);
190 Assert.fail();
191 } catch (StringIndexOutOfBoundsException expected) {
192 }
193 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800194 if (testStr.length() == 34) {
195 // 34 = "Now is the time to test some stuff".length()
196 test_String_charAt_inner(testStr, 34);
197 } else {
198 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length()
199 test_String_charAt_inner(testStr, 12);
200 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000201 Assert.fail();
202 } catch (StringIndexOutOfBoundsException expected) {
203 }
204
205 String strEmpty = "";
206 try {
207 strEmpty.charAt(0);
208 Assert.fail();
209 } catch (StringIndexOutOfBoundsException expected) {
210 }
jeffhao5d1ac922011-09-29 17:41:15 -0700211
Elliott Hughes28c384b2012-06-15 16:46:25 -0700212 String strNull = null;
213 try {
214 strNull.charAt(0);
215 Assert.fail();
216 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700217 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700218 }
jeffhao5d1ac922011-09-29 17:41:15 -0700219
Vladimir Marko00ca8472015-01-26 14:06:46 +0000220 private static char test_String_charAt_inner(String s, int index) {
221 // Using non-constant index here (assuming that this method wasn't inlined).
222 return s.charAt(index);
223 }
224
Andreas Gampe878d58c2015-01-15 23:24:00 -0800225 private static void test_String_charAtExc2(String testStr) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800226 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800227 test_String_charAtExc3(testStr);
228 Assert.fail();
229 } catch (StringIndexOutOfBoundsException expected) {
230 }
231 try {
232 test_String_charAtExc4(testStr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800233 Assert.fail();
234 } catch (StringIndexOutOfBoundsException expected) {
235 }
236 }
237
Andreas Gampe878d58c2015-01-15 23:24:00 -0800238 private static void test_String_charAtExc3(String testStr) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800239 Assert.assertEquals('N', testStr.charAt(-1));
240 }
241
Andreas Gampe878d58c2015-01-15 23:24:00 -0800242 private static void test_String_charAtExc4(String testStr) {
243 Assert.assertEquals('N', testStr.charAt(100));
244 }
245
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700246 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700247 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700248 public static void test_String_indexOf() {
249 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700250 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700251 String str3 = "abc";
252 String str10 = "abcdefghij";
253 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700254
Elliott Hughes28c384b2012-06-15 16:46:25 -0700255 Assert.assertEquals(str0.indexOf('a'), -1);
256 Assert.assertEquals(str3.indexOf('a'), 0);
257 Assert.assertEquals(str3.indexOf('b'), 1);
258 Assert.assertEquals(str3.indexOf('c'), 2);
259 Assert.assertEquals(str10.indexOf('j'), 9);
260 Assert.assertEquals(str40.indexOf('a'), 0);
261 Assert.assertEquals(str40.indexOf('b'), 38);
262 Assert.assertEquals(str40.indexOf('c'), 39);
263 Assert.assertEquals(str0.indexOf('a',20), -1);
264 Assert.assertEquals(str0.indexOf('a',0), -1);
265 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700266 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700267 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700268 Assert.assertEquals(str3.indexOf('a',0), 0);
269 Assert.assertEquals(str3.indexOf('a',1), -1);
270 Assert.assertEquals(str3.indexOf('a',1234), -1);
271 Assert.assertEquals(str3.indexOf('b',0), 1);
272 Assert.assertEquals(str3.indexOf('b',1), 1);
273 Assert.assertEquals(str3.indexOf('c',2), 2);
274 Assert.assertEquals(str10.indexOf('j',5), 9);
275 Assert.assertEquals(str10.indexOf('j',9), 9);
276 Assert.assertEquals(str40.indexOf('a',10), 10);
277 Assert.assertEquals(str40.indexOf('b',40), -1);
278
Andreas Gampe678e6952015-05-07 16:44:58 -0700279 testIndexOfNull();
280
Andreas Gampe21030dd2015-05-07 14:46:15 -0700281 // Same data as above, but stored so it's not a literal in the next test. -2 stands for
282 // indexOf(I) instead of indexOf(II).
283 start--;
284 int[][] searchData = {
285 { 'a', -2, -1 },
286 { 'a', -2, 0 },
287 { 'b', -2, 1 },
288 { 'c', -2, 2 },
289 { 'j', -2, 9 },
290 { 'a', -2, 0 },
291 { 'b', -2, 38 },
292 { 'c', -2, 39 },
293 { 'a', 20, -1 },
294 { 'a', 0, -1 },
295 { 'a', -1, -1 },
296 { '/', ++start, -1 },
297 { 'a', negIndex[0], -1 },
298 { 'a', 0, 0 },
299 { 'a', 1, -1 },
300 { 'a', 1234, -1 },
301 { 'b', 0, 1 },
302 { 'b', 1, 1 },
303 { 'c', 2, 2 },
304 { 'j', 5, 9 },
305 { 'j', 9, 9 },
306 { 'a', 10, 10 },
307 { 'b', 40, -1 },
308 };
309 testStringIndexOfChars(searchData);
310
Andreas Gampe678e6952015-05-07 16:44:58 -0700311 testSurrogateIndexOf();
312 }
313
Andreas Gampe21030dd2015-05-07 14:46:15 -0700314 private static void testStringIndexOfChars(int[][] searchData) {
315 // Use a try-catch to avoid inlining.
316 try {
317 testStringIndexOfCharsImpl(searchData);
318 } catch (Exception e) {
319 System.out.println("Unexpected exception");
320 }
321 }
322
323 private static void testStringIndexOfCharsImpl(int[][] searchData) {
324 String str0 = "";
325 String str1 = "/";
326 String str3 = "abc";
327 String str10 = "abcdefghij";
328 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
329
330 Assert.assertEquals(str0.indexOf(searchData[0][0]), searchData[0][2]);
331 Assert.assertEquals(str3.indexOf(searchData[1][0]), searchData[1][2]);
332 Assert.assertEquals(str3.indexOf(searchData[2][0]), searchData[2][2]);
333 Assert.assertEquals(str3.indexOf(searchData[3][0]), searchData[3][2]);
334 Assert.assertEquals(str10.indexOf(searchData[4][0]), searchData[4][2]);
335 Assert.assertEquals(str40.indexOf(searchData[5][0]), searchData[5][2]);
336 Assert.assertEquals(str40.indexOf(searchData[6][0]), searchData[6][2]);
337 Assert.assertEquals(str40.indexOf(searchData[7][0]), searchData[7][2]);
338 Assert.assertEquals(str0.indexOf(searchData[8][0], searchData[8][1]), searchData[8][2]);
339 Assert.assertEquals(str0.indexOf(searchData[9][0], searchData[9][1]), searchData[9][2]);
340 Assert.assertEquals(str0.indexOf(searchData[10][0], searchData[10][1]), searchData[10][2]);
341 Assert.assertEquals(str1.indexOf(searchData[11][0], searchData[11][1]), searchData[11][2]);
342 Assert.assertEquals(str1.indexOf(searchData[12][0], searchData[12][1]), searchData[12][2]);
343 Assert.assertEquals(str3.indexOf(searchData[13][0], searchData[13][1]), searchData[13][2]);
344 Assert.assertEquals(str3.indexOf(searchData[14][0], searchData[14][1]), searchData[14][2]);
345 Assert.assertEquals(str3.indexOf(searchData[15][0], searchData[15][1]), searchData[15][2]);
346 Assert.assertEquals(str3.indexOf(searchData[16][0], searchData[16][1]), searchData[16][2]);
347 Assert.assertEquals(str3.indexOf(searchData[17][0], searchData[17][1]), searchData[17][2]);
348 Assert.assertEquals(str3.indexOf(searchData[18][0], searchData[18][1]), searchData[18][2]);
349 Assert.assertEquals(str10.indexOf(searchData[19][0], searchData[19][1]), searchData[19][2]);
350 Assert.assertEquals(str10.indexOf(searchData[20][0], searchData[20][1]), searchData[20][2]);
351 Assert.assertEquals(str40.indexOf(searchData[21][0], searchData[21][1]), searchData[21][2]);
352 Assert.assertEquals(str40.indexOf(searchData[22][0], searchData[22][1]), searchData[22][2]);
353 }
354
Andreas Gampe678e6952015-05-07 16:44:58 -0700355 private static void testSurrogateIndexOf() {
356 int supplementaryChar = 0x20b9f;
357 String surrogatePair = "\ud842\udf9f";
358 String stringWithSurrogates = "hello " + surrogatePair + " world";
359
360 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
361 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
362 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
363 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -0700364
365 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar - 0x10000), -1);
366 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar | 0x80000000), -1);
Andreas Gampe678e6952015-05-07 16:44:58 -0700367 }
368
369 private static void testIndexOfNull() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700370 String strNull = null;
371 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700372 testNullIndex(strNull, 'a');
Elliott Hughes28c384b2012-06-15 16:46:25 -0700373 Assert.fail();
374 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700375 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700376 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700377 testNullIndex(strNull, 'a', 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700378 Assert.fail();
379 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700380 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700381 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700382 testNullIndex(strNull, 'a', -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700383 Assert.fail();
384 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700385 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700386 }
jeffhao5d1ac922011-09-29 17:41:15 -0700387
Andreas Gampe678e6952015-05-07 16:44:58 -0700388 private static int testNullIndex(String strNull, int c) {
389 return strNull.indexOf(c);
390 }
391
392 private static int testNullIndex(String strNull, int c, int startIndex) {
393 return strNull.indexOf(c, startIndex);
394 }
395
Elliott Hughes28c384b2012-06-15 16:46:25 -0700396 public static void test_String_compareTo() {
397 String test = "0123456789";
398 String test1 = new String("0123456789"); // different object
399 String test2 = new String("0123456780"); // different value
400 String offset = new String("xxx0123456789yyy");
401 String sub = offset.substring(3, 13);
402 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
403 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
404 String lc = "abcdefg";
405 String uc = "ABCDEFG";
406 Object blah = new Object();
407
408 Assert.assertTrue(lc.toUpperCase().equals(uc));
409
410 Assert.assertEquals(str32.compareTo(str33), -1);
411 Assert.assertEquals(str33.compareTo(str32), 1);
412
413 Assert.assertTrue(test.equals(test));
414 Assert.assertTrue(test.equals(test1));
415 Assert.assertFalse(test.equals(test2));
416
417 Assert.assertEquals(test.compareTo(test1), 0);
418 Assert.assertTrue(test1.compareTo(test2) > 0);
419 Assert.assertTrue(test2.compareTo(test1) < 0);
420
421 // Compare string with a nonzero offset, in left/right side.
422 Assert.assertEquals(test.compareTo(sub), 0);
423 Assert.assertEquals(sub.compareTo(test), 0);
424 Assert.assertTrue(test.equals(sub));
425 Assert.assertTrue(sub.equals(test));
426 // Same base, one is a substring.
427 Assert.assertFalse(offset.equals(sub));
428 Assert.assertFalse(sub.equals(offset));
429 // Wrong class.
430 Assert.assertFalse(test.equals(blah));
431
432 // Null lhs - throw.
433 try {
434 test.compareTo(null);
435 Assert.fail("didn't get expected npe");
436 } catch (NullPointerException npe) {
437 }
438 // Null rhs - okay.
439 Assert.assertFalse(test.equals(null));
440
441 test = test.substring(1);
442 Assert.assertTrue(test.equals("123456789"));
443 Assert.assertFalse(test.equals(test1));
444
445 test = test.substring(1);
446 Assert.assertTrue(test.equals("23456789"));
447
448 test = test.substring(1);
449 Assert.assertTrue(test.equals("3456789"));
450
451 test = test.substring(1);
452 Assert.assertTrue(test.equals("456789"));
453
454 test = test.substring(3,5);
455 Assert.assertTrue(test.equals("78"));
456
457 test = "this/is/a/path";
458 String[] strings = test.split("/");
459 Assert.assertEquals(4, strings.length);
460
461 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
462 Assert.assertEquals("this is a path", test.replace("/", " "));
463 }
464
465 public static void test_Math_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800466 Math.abs(-1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700467 Assert.assertEquals(Math.abs(0), 0);
468 Assert.assertEquals(Math.abs(123), 123);
469 Assert.assertEquals(Math.abs(-123), 123);
470 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
471 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
472 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100473 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700474 }
475
476 public static void test_Math_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800477 Math.abs(-1L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700478 Assert.assertEquals(Math.abs(0L), 0L);
479 Assert.assertEquals(Math.abs(123L), 123L);
480 Assert.assertEquals(Math.abs(-123L), 123L);
481 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
482 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
483 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800484 Assert.assertEquals(Math.abs(2147483648L), 2147483648L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700485 }
486
Serban Constantinescu23abec92014-07-02 16:13:38 +0100487 public static void test_Math_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800488 Math.min(1, 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700489 Assert.assertEquals(Math.min(0, 0), 0);
490 Assert.assertEquals(Math.min(1, 0), 0);
491 Assert.assertEquals(Math.min(0, 1), 0);
492 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
493 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
494 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
495 }
496
Serban Constantinescu23abec92014-07-02 16:13:38 +0100497 public static void test_Math_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800498 Math.max(1, 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700499 Assert.assertEquals(Math.max(0, 0), 0);
500 Assert.assertEquals(Math.max(1, 0), 1);
501 Assert.assertEquals(Math.max(0, 1), 1);
502 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
503 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
504 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
505 }
506
Serban Constantinescu23abec92014-07-02 16:13:38 +0100507 public static void test_Math_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800508 Math.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100509 Assert.assertEquals(Math.min(0L, 0L), 0L);
510 Assert.assertEquals(Math.min(1L, 0L), 0L);
511 Assert.assertEquals(Math.min(0L, 1L), 0L);
512 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
513 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
514 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
515 }
516
517 public static void test_Math_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800518 Math.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100519 Assert.assertEquals(Math.max(0L, 0L), 0L);
520 Assert.assertEquals(Math.max(1L, 0L), 1L);
521 Assert.assertEquals(Math.max(0L, 1L), 1L);
522 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
523 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
524 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
525 }
526
527 public static void test_Math_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800528 Math.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700529 Assert.assertTrue(Float.isNaN(Math.min(1.0f, Float.NaN)));
530 Assert.assertTrue(Float.isNaN(Math.min(Float.NaN, 1.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(-0.0f, -0.0f), -0.0f);
534 Assert.assertEquals(Math.min(0.0f, 0.0f), 0.0f);
535 Assert.assertEquals(Math.min(1.0f, 0.0f), 0.0f);
536 Assert.assertEquals(Math.min(0.0f, 1.0f), 0.0f);
537 Assert.assertEquals(Math.min(0.0f, Float.MAX_VALUE), 0.0f);
538 Assert.assertEquals(Math.min(Float.MIN_VALUE, 0.0f), 0.0f);
539 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
xueliang.zhongc032e742016-03-28 16:44:32 +0100540 // Should not have flush-to-zero behavior.
541 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MIN_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100542 }
543
544 public static void test_Math_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800545 Math.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700546 Assert.assertTrue(Float.isNaN(Math.max(1.0f, Float.NaN)));
547 Assert.assertTrue(Float.isNaN(Math.max(Float.NaN, 1.0f)));
548 Assert.assertEquals(Math.max(-0.0f, 0.0f), 0.0f);
549 Assert.assertEquals(Math.max(0.0f, -0.0f), 0.0f);
550 Assert.assertEquals(Math.max(-0.0f, -0.0f), -0.0f);
551 Assert.assertEquals(Math.max(0.0f, 0.0f), 0.0f);
552 Assert.assertEquals(Math.max(1.0f, 0.0f), 1.0f);
553 Assert.assertEquals(Math.max(0.0f, 1.0f), 1.0f);
554 Assert.assertEquals(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700555 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
xueliang.zhongc032e742016-03-28 16:44:32 +0100556 // Should not have flush-to-zero behavior.
557 Assert.assertEquals(Math.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
558 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MIN_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100559 }
560
561 public static void test_Math_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800562 Math.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700563 Assert.assertTrue(Double.isNaN(Math.min(1.0d, Double.NaN)));
564 Assert.assertTrue(Double.isNaN(Math.min(Double.NaN, 1.0d)));
565 Assert.assertEquals(Math.min(-0.0d, 0.0d), -0.0d);
566 Assert.assertEquals(Math.min(0.0d, -0.0d), -0.0d);
567 Assert.assertEquals(Math.min(-0.0d, -0.0d), -0.0d);
568 Assert.assertEquals(Math.min(0.0d, 0.0d), 0.0d);
569 Assert.assertEquals(Math.min(1.0d, 0.0d), 0.0d);
570 Assert.assertEquals(Math.min(0.0d, 1.0d), 0.0d);
571 Assert.assertEquals(Math.min(0.0d, Double.MAX_VALUE), 0.0d);
572 Assert.assertEquals(Math.min(Double.MIN_VALUE, 0.0d), 0.0d);
573 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
xueliang.zhongc032e742016-03-28 16:44:32 +0100574 // Should not have flush-to-zero behavior.
575 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MIN_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100576 }
577
578 public static void test_Math_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800579 Math.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700580 Assert.assertTrue(Double.isNaN(Math.max(1.0d, Double.NaN)));
581 Assert.assertTrue(Double.isNaN(Math.max(Double.NaN, 1.0d)));
582 Assert.assertEquals(Math.max(-0.0d, 0.0d), 0.0d);
583 Assert.assertEquals(Math.max(0.0d, -0.0d), 0.0d);
584 Assert.assertEquals(Math.max(-0.0d, -0.0d), -0.0d);
585 Assert.assertEquals(Math.max(0.0d, 0.0d), 0.0d);
586 Assert.assertEquals(Math.max(1.0d, 0.0d), 1.0d);
587 Assert.assertEquals(Math.max(0.0d, 1.0d), 1.0d);
588 Assert.assertEquals(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
589 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
590 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
xueliang.zhongc032e742016-03-28 16:44:32 +0100591 // Should not have flush-to-zero behavior.
592 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
593 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MIN_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100594 }
595
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800596 public static void test_Math_sqrt() {
597 Math.sqrt(+4.0);
598 Assert.assertEquals(Math.sqrt(+4.0), +2.0d, 0.0);
599 Assert.assertEquals(Math.sqrt(+49.0), +7.0d, 0.0);
600 Assert.assertEquals(Math.sqrt(+1.44), +1.2d, 0.0);
601 }
602
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100603 public static void test_Math_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800604 Math.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100605 Assert.assertEquals(Math.ceil(+0.0), +0.0d, 0.0);
606 Assert.assertEquals(Math.ceil(-0.0), -0.0d, 0.0);
607 Assert.assertEquals(Math.ceil(-0.9), -0.0d, 0.0);
608 Assert.assertEquals(Math.ceil(-0.5), -0.0d, 0.0);
609 Assert.assertEquals(Math.ceil(0.0), -0.0d, 0.0);
610 Assert.assertEquals(Math.ceil(+2.0), +2.0d, 0.0);
611 Assert.assertEquals(Math.ceil(+2.1), +3.0d, 0.0);
612 Assert.assertEquals(Math.ceil(+2.5), +3.0d, 0.0);
613 Assert.assertEquals(Math.ceil(+2.9), +3.0d, 0.0);
614 Assert.assertEquals(Math.ceil(+3.0), +3.0d, 0.0);
615 Assert.assertEquals(Math.ceil(-2.0), -2.0d, 0.0);
616 Assert.assertEquals(Math.ceil(-2.1), -2.0d, 0.0);
617 Assert.assertEquals(Math.ceil(-2.5), -2.0d, 0.0);
618 Assert.assertEquals(Math.ceil(-2.9), -2.0d, 0.0);
619 Assert.assertEquals(Math.ceil(-3.0), -3.0d, 0.0);
Chris Larsen82831092015-08-25 09:06:58 -0700620 // 2^52 - 1.5
621 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
622 Double.longBitsToDouble(0x432FFFFFFFFFFFFEl), 0.0);
623 // 2^52 - 0.5
624 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
625 Double.longBitsToDouble(0x4330000000000000l), 0.0);
626 // 2^52
627 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x4330000000000000l)),
628 Double.longBitsToDouble(0x4330000000000000l), 0.0);
629 // 2^53 - 1
630 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
631 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
632 // 2^53
633 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x4340000000000000l)),
634 Double.longBitsToDouble(0x4340000000000000l), 0.0);
635 // 2^63 - 2^10
636 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
637 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
638 // 2^63
639 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43E0000000000000l)),
640 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
641 // 2^64
642 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0x43F0000000000000l)),
643 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
644 // -(2^52 - 1.5)
645 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
646 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0);
647 // -(2^52 - 0.5)
648 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
649 Double.longBitsToDouble(0xC32FFFFFFFFFFFFEl), 0.0);
650 // -2^52
651 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC330000000000000l)),
652 Double.longBitsToDouble(0xC330000000000000l), 0.0);
653 // -(2^53 - 1)
654 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
655 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
656 // -2^53
657 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC340000000000000l)),
658 Double.longBitsToDouble(0xC340000000000000l), 0.0);
659 // -(2^63 - 2^10)
660 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
661 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
662 // -2^63
663 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3E0000000000000l)),
664 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
665 // -2^64
666 Assert.assertEquals(Math.ceil(Double.longBitsToDouble(0xC3F0000000000000l)),
667 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100668 Assert.assertEquals(Math.ceil(Double.NaN), Double.NaN, 0.0);
669 Assert.assertEquals(Math.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
670 Assert.assertEquals(Math.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
671 }
672
673 public static void test_Math_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800674 Math.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100675 Assert.assertEquals(Math.floor(+0.0), +0.0d, 0.0);
676 Assert.assertEquals(Math.floor(-0.0), -0.0d, 0.0);
677 Assert.assertEquals(Math.floor(+2.0), +2.0d, 0.0);
678 Assert.assertEquals(Math.floor(+2.1), +2.0d, 0.0);
679 Assert.assertEquals(Math.floor(+2.5), +2.0d, 0.0);
680 Assert.assertEquals(Math.floor(+2.9), +2.0d, 0.0);
681 Assert.assertEquals(Math.floor(+3.0), +3.0d, 0.0);
682 Assert.assertEquals(Math.floor(-2.0), -2.0d, 0.0);
683 Assert.assertEquals(Math.floor(-2.1), -3.0d, 0.0);
684 Assert.assertEquals(Math.floor(-2.5), -3.0d, 0.0);
685 Assert.assertEquals(Math.floor(-2.9), -3.0d, 0.0);
686 Assert.assertEquals(Math.floor(-3.0), -3.0d, 0.0);
Chris Larsen82831092015-08-25 09:06:58 -0700687 // 2^52 - 1.5
688 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
689 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0);
690 // 2^52 - 0.5
691 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
692 Double.longBitsToDouble(0x432FFFFFFFFFFFFEl), 0.0);
693 // 2^52
694 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x4330000000000000l)),
695 Double.longBitsToDouble(0x4330000000000000l), 0.0);
696 // 2^53 - 1
697 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
698 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
699 // 2^53
700 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x4340000000000000l)),
701 Double.longBitsToDouble(0x4340000000000000l), 0.0);
702 // 2^63 - 2^10
703 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
704 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
705 // 2^63
706 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43E0000000000000l)),
707 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
708 // 2^64
709 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0x43F0000000000000l)),
710 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
711 // -(2^52 - 1.5)
712 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
713 Double.longBitsToDouble(0xC32FFFFFFFFFFFFEl), 0.0);
714 // -(2^52 - 0.5)
715 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
716 Double.longBitsToDouble(0xC330000000000000l), 0.0);
717 // -2^52
718 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC330000000000000l)),
719 Double.longBitsToDouble(0xC330000000000000l), 0.0);
720 // -(2^53 - 1)
721 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
722 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
723 // -2^53
724 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC340000000000000l)),
725 Double.longBitsToDouble(0xC340000000000000l), 0.0);
726 // -(2^63 - 2^10)
727 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
728 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
729 // -2^63
730 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3E0000000000000l)),
731 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
732 // -2^64
733 Assert.assertEquals(Math.floor(Double.longBitsToDouble(0xC3F0000000000000l)),
734 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100735 Assert.assertEquals(Math.floor(Double.NaN), Double.NaN, 0.0);
736 Assert.assertEquals(Math.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
737 Assert.assertEquals(Math.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
738 }
739
740 public static void test_Math_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800741 Math.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100742 Assert.assertEquals(Math.rint(+0.0), +0.0d, 0.0);
743 Assert.assertEquals(Math.rint(-0.0), -0.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100744 Assert.assertEquals(Math.rint(+0.5), +0.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100745 Assert.assertEquals(Math.rint(+2.0), +2.0d, 0.0);
746 Assert.assertEquals(Math.rint(+2.1), +2.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100747 Assert.assertEquals(Math.rint(+2.5), +2.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100748 Assert.assertEquals(Math.rint(+2.9), +3.0d, 0.0);
749 Assert.assertEquals(Math.rint(+3.0), +3.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100750 Assert.assertEquals(Math.rint(+3.5), +4.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100751 Assert.assertEquals(Math.rint(-2.0), -2.0d, 0.0);
752 Assert.assertEquals(Math.rint(-2.1), -2.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100753 Assert.assertEquals(Math.rint(-2.5), -2.0d, 0.0); // expects tie-to-even
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100754 Assert.assertEquals(Math.rint(-2.9), -3.0d, 0.0);
755 Assert.assertEquals(Math.rint(-3.0), -3.0d, 0.0);
xueliang.zhong6099d5e2016-04-20 18:44:56 +0100756 Assert.assertEquals(Math.rint(-3.5), -4.0d, 0.0); // expects tie-to-even
Chris Larsen82831092015-08-25 09:06:58 -0700757 // 2^52 - 1.5
758 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFDl)),
759 Double.longBitsToDouble(0x432FFFFFFFFFFFFCl), 0.0);
760 // 2^52 - 0.5
761 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x432FFFFFFFFFFFFFl)),
762 Double.longBitsToDouble(0x4330000000000000l), 0.0);
763 // 2^52
764 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4330000000000000l)),
765 Double.longBitsToDouble(0x4330000000000000l), 0.0);
766 // 2^53 - 1
767 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x433FFFFFFFFFFFFFl)),
768 Double.longBitsToDouble(0x433FFFFFFFFFFFFFl), 0.0);
769 // 2^53
770 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x4340000000000000l)),
771 Double.longBitsToDouble(0x4340000000000000l), 0.0);
772 // 2^63 - 2^10
773 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl)),
774 Double.longBitsToDouble(0x43DFFFFFFFFFFFFFl), 0.0);
775 // 2^63
776 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43E0000000000000l)),
777 Double.longBitsToDouble(0x43E0000000000000l), 0.0);
778 // 2^64
779 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0x43F0000000000000l)),
780 Double.longBitsToDouble(0x43F0000000000000l), 0.0);
781 // -(2^52 - 1.5)
782 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFDl)),
783 Double.longBitsToDouble(0xC32FFFFFFFFFFFFCl), 0.0);
784 // -(2^52 - 0.5)
785 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC32FFFFFFFFFFFFFl)),
786 Double.longBitsToDouble(0xC330000000000000l), 0.0);
787 // -2^52
788 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC330000000000000l)),
789 Double.longBitsToDouble(0xC330000000000000l), 0.0);
790 // -(2^53 - 1)
791 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl)),
792 Double.longBitsToDouble(0xC33FFFFFFFFFFFFFl), 0.0);
793 // -2^53
794 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC340000000000000l)),
795 Double.longBitsToDouble(0xC340000000000000l), 0.0);
796 // -(2^63 - 2^10)
797 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl)),
798 Double.longBitsToDouble(0xC3DFFFFFFFFFFFFFl), 0.0);
799 // -2^63
800 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3E0000000000000l)),
801 Double.longBitsToDouble(0xC3E0000000000000l), 0.0);
802 // -2^64
803 Assert.assertEquals(Math.rint(Double.longBitsToDouble(0xC3F0000000000000l)),
804 Double.longBitsToDouble(0xC3F0000000000000l), 0.0);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100805 Assert.assertEquals(Math.rint(Double.NaN), Double.NaN, 0.0);
806 Assert.assertEquals(Math.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
807 Assert.assertEquals(Math.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
808 }
809
810 public static void test_Math_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800811 Math.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100812 Assert.assertEquals(Math.round(+0.0d), (long)+0.0);
813 Assert.assertEquals(Math.round(-0.0d), (long)+0.0);
814 Assert.assertEquals(Math.round(2.0d), 2l);
815 Assert.assertEquals(Math.round(2.1d), 2l);
816 Assert.assertEquals(Math.round(2.5d), 3l);
817 Assert.assertEquals(Math.round(2.9d), 3l);
818 Assert.assertEquals(Math.round(3.0d), 3l);
819 Assert.assertEquals(Math.round(-2.0d), -2l);
820 Assert.assertEquals(Math.round(-2.1d), -2l);
821 Assert.assertEquals(Math.round(-2.5d), -2l);
822 Assert.assertEquals(Math.round(-2.9d), -3l);
823 Assert.assertEquals(Math.round(-3.0d), -3l);
Yi Kong879ca672015-11-18 14:11:44 +0000824 Assert.assertEquals(Math.round(0.49999999999999994d), 0l);
Chris Larsen7adaab02016-04-21 14:49:20 -0700825 Assert.assertEquals(Math.round(4503599627370495.0d), 4503599627370495l); // 2^52 - 1
826 Assert.assertEquals(Math.round(4503599627370495.5d), 4503599627370496l); // 2^52 - 0.5
827 Assert.assertEquals(Math.round(4503599627370496.0d), 4503599627370496l); // 2^52
828 Assert.assertEquals(Math.round(-4503599627370495.0d), -4503599627370495l); // -(2^52 - 1)
829 Assert.assertEquals(Math.round(-4503599627370495.5d), -4503599627370495l); // -(2^52 - 0.5)
830 Assert.assertEquals(Math.round(-4503599627370496.0d), -4503599627370496l); // -2^52
Hans Boehm92d4f0e2016-02-17 12:14:03 -0800831 Assert.assertEquals(Math.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -0700832 Assert.assertEquals(Math.round(-9007199254740991.0d), -9007199254740991l); // -(2^53 - 1)
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100833 Assert.assertEquals(Math.round(Double.NaN), (long)+0.0d);
834 Assert.assertEquals(Math.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
835 Assert.assertEquals(Math.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -0700836 Assert.assertEquals(Math.round(Double.longBitsToDouble(0x43F0000000000000l)),
837 Long.MAX_VALUE); // 2^64
838 Assert.assertEquals(Math.round(Double.longBitsToDouble(0xC3F0000000000000l)),
839 Long.MIN_VALUE); // -2^64
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100840 Assert.assertEquals(Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
841 Assert.assertEquals(Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
842 }
843
844 public static void test_Math_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800845 Math.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100846 Assert.assertEquals(Math.round(+0.0f), (int)+0.0);
847 Assert.assertEquals(Math.round(-0.0f), (int)+0.0);
848 Assert.assertEquals(Math.round(2.0f), 2);
849 Assert.assertEquals(Math.round(2.1f), 2);
850 Assert.assertEquals(Math.round(2.5f), 3);
851 Assert.assertEquals(Math.round(2.9f), 3);
852 Assert.assertEquals(Math.round(3.0f), 3);
853 Assert.assertEquals(Math.round(-2.0f), -2);
854 Assert.assertEquals(Math.round(-2.1f), -2);
855 Assert.assertEquals(Math.round(-2.5f), -2);
856 Assert.assertEquals(Math.round(-2.9f), -3);
857 Assert.assertEquals(Math.round(-3.0f), -3);
Chris Larsenb74353a2015-11-20 09:07:09 -0800858 // 0.4999999701976776123046875
859 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f);
Chris Larsenf09d5322016-04-22 12:06:34 -0700860 Assert.assertEquals(Math.round(8388607.0f), 8388607); // 2^23 - 1
861 Assert.assertEquals(Math.round(8388607.5f), 8388608); // 2^23 - 0.5
862 Assert.assertEquals(Math.round(8388608.0f), 8388608); // 2^23
863 Assert.assertEquals(Math.round(-8388607.0f), -8388607); // -(2^23 - 1)
864 Assert.assertEquals(Math.round(-8388607.5f), -8388607); // -(2^23 - 0.5)
865 Assert.assertEquals(Math.round(-8388608.0f), -8388608); // -2^23
Hans Boehm92d4f0e2016-02-17 12:14:03 -0800866 Assert.assertEquals(Math.round(16777215.0f), 16777215); // 2^24 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -0700867 Assert.assertEquals(Math.round(16777216.0f), 16777216); // 2^24
868 Assert.assertEquals(Math.round(-16777215.0f), -16777215); // -(2^24 - 1)
869 Assert.assertEquals(Math.round(-16777216.0f), -16777216); // -2^24
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100870 Assert.assertEquals(Math.round(Float.NaN), (int)+0.0f);
871 Assert.assertEquals(Math.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
872 Assert.assertEquals(Math.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -0700873 Assert.assertEquals(Math.round(Float.intBitsToFloat(0x4F800000)),
874 Integer.MAX_VALUE); // 2^32
875 Assert.assertEquals(Math.round(Float.intBitsToFloat(0xCF800000)),
876 Integer.MIN_VALUE); // -2^32
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100877 Assert.assertEquals(Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
878 Assert.assertEquals(Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
879 }
880
Chris Larsen2714fe62016-02-11 14:23:53 -0800881 public static void test_Math_isNaN_D() {
882 // Quiet NaN.
883 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF4000000000000l)));
884 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF4000000000000l)));
885 // Signaling NaN.
886 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0x7FF8000000000000l)));
887 Assert.assertTrue(Double.isNaN(Double.longBitsToDouble(0xFFF8000000000000l)));
888 // Distinct from +/- infinity.
889 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FF0000000000000l)));
890 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFF0000000000000l)));
891 // Distinct from normal numbers.
892 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x7FE0000000000000l)));
893 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0xFFE0000000000000l)));
894 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0010000000000000l)));
895 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8010000000000000l)));
896 // Distinct from +/- zero.
897 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000000l)));
898 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000000l)));
899 // Distinct from subnormal numbers.
900 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0008000000000000l)));
901 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8008000000000000l)));
902 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x0000000000000001l)));
903 Assert.assertFalse(Double.isNaN(Double.longBitsToDouble(0x8000000000000001l)));
904 }
905
906 public static void test_Math_isNaN_F() {
907 // Quiet NaN.
908 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FA00000)));
909 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFA00000)));
910 // Signaling NaN.
911 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0x7FC00000)));
912 Assert.assertTrue(Float.isNaN(Float.intBitsToFloat(0xFFC00000)));
913 // Distinct from +/- infinity.
914 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F800000)));
915 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF800000)));
916 // Distinct from normal numbers.
917 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x7F000000)));
918 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0xFF000000)));
919 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00800000)));
920 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80800000)));
921 // Distinct from +/- zero.
922 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000000)));
923 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000000)));
924 // Distinct from subnormal numbers.
925 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00400000)));
926 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80400000)));
927 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x00000001)));
928 Assert.assertFalse(Float.isNaN(Float.intBitsToFloat(0x80000001)));
929 }
930
931 public static void test_Math_isInfinite_D() {
932 // Distinct from Quiet NaN.
933 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF4000000000000l)));
934 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF4000000000000l)));
935 // Distinct from Signaling NaN.
936 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FF8000000000000l)));
937 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFF8000000000000l)));
938 // +/- infinity.
939 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0x7FF0000000000000l)));
940 Assert.assertTrue(Double.isInfinite(Double.longBitsToDouble(0xFFF0000000000000l)));
941 // Distinct from normal numbers.
942 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x7FE0000000000000l)));
943 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0xFFE0000000000000l)));
944 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0010000000000000l)));
945 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8010000000000000l)));
946 // Distinct from +/- zero.
947 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000000l)));
948 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000000l)));
949 // Distinct from subnormal numbers.
950 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0008000000000000l)));
951 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8008000000000000l)));
952 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x0000000000000001l)));
953 Assert.assertFalse(Double.isInfinite(Double.longBitsToDouble(0x8000000000000001l)));
954 }
955
956 public static void test_Math_isInfinite_F() {
957 // Distinct from Quiet NaN.
958 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FA00000)));
959 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFA00000)));
960 // Distinct from Signaling NaN.
961 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7FC00000)));
962 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFFC00000)));
963 // +/- infinity.
964 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0x7F800000)));
965 Assert.assertTrue(Float.isInfinite(Float.intBitsToFloat(0xFF800000)));
966 // Distinct from normal numbers.
967 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x7F000000)));
968 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0xFF000000)));
969 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00800000)));
970 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80800000)));
971 // Distinct from +/- zero.
972 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000000)));
973 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000000)));
974 // Distinct from subnormal numbers.
975 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00400000)));
976 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80400000)));
977 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x00000001)));
978 Assert.assertFalse(Float.isInfinite(Float.intBitsToFloat(0x80000001)));
979 }
980
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100981 public static void test_StrictMath_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800982 StrictMath.abs(-1);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100983 Assert.assertEquals(StrictMath.abs(0), 0);
984 Assert.assertEquals(StrictMath.abs(123), 123);
985 Assert.assertEquals(StrictMath.abs(-123), 123);
986 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
987 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
988 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
989 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
990 }
991
992 public static void test_StrictMath_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800993 StrictMath.abs(-1L);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100994 Assert.assertEquals(StrictMath.abs(0L), 0L);
995 Assert.assertEquals(StrictMath.abs(123L), 123L);
996 Assert.assertEquals(StrictMath.abs(-123L), 123L);
997 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
998 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
999 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
1000 }
1001
Serban Constantinescu23abec92014-07-02 16:13:38 +01001002 public static void test_StrictMath_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001003 StrictMath.min(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +01001004 Assert.assertEquals(StrictMath.min(0, 0), 0);
1005 Assert.assertEquals(StrictMath.min(1, 0), 0);
1006 Assert.assertEquals(StrictMath.min(0, 1), 0);
1007 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
1008 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
1009 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
1010 }
1011
Serban Constantinescu23abec92014-07-02 16:13:38 +01001012 public static void test_StrictMath_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001013 StrictMath.max(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +01001014 Assert.assertEquals(StrictMath.max(0, 0), 0);
1015 Assert.assertEquals(StrictMath.max(1, 0), 1);
1016 Assert.assertEquals(StrictMath.max(0, 1), 1);
1017 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
1018 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
1019 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
1020 }
1021
Serban Constantinescu23abec92014-07-02 16:13:38 +01001022 public static void test_StrictMath_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001023 StrictMath.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001024 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
1025 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
1026 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
1027 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
1028 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
1029 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
1030 }
1031
1032 public static void test_StrictMath_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001033 StrictMath.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001034 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
1035 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
1036 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
1037 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
1038 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
1039 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
1040 }
1041
1042 public static void test_StrictMath_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001043 StrictMath.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001044 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN)));
1045 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f)));
1046 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f);
1047 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f);
1048 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f);
1049 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f);
1050 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f);
1051 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f);
1052 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f);
1053 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f);
1054 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001055 }
1056
1057 public static void test_StrictMath_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001058 StrictMath.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001059 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN)));
1060 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f)));
1061 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f);
1062 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f);
1063 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f);
1064 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f);
1065 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f);
1066 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f);
1067 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
1068 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
1069 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001070 }
1071
1072 public static void test_StrictMath_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001073 StrictMath.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001074 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN)));
1075 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d)));
1076 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d);
1077 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d);
1078 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d);
1079 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d);
1080 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d);
1081 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d);
1082 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d);
1083 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d);
1084 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001085 }
1086
1087 public static void test_StrictMath_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001088 StrictMath.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +07001089 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN)));
1090 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d)));
1091 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d);
1092 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d);
1093 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d);
1094 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d);
1095 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d);
1096 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d);
1097 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
1098 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
1099 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001100 }
1101
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001102 public static void test_StrictMath_sqrt() {
1103 StrictMath.sqrt(+4.0);
1104 Assert.assertEquals(StrictMath.sqrt(+4.0), +2.0d, 0.0);
1105 Assert.assertEquals(StrictMath.sqrt(+49.0), +7.0d, 0.0);
1106 Assert.assertEquals(StrictMath.sqrt(+1.44), +1.2d, 0.0);
1107 }
1108
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001109 public static void test_StrictMath_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001110 StrictMath.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001111 Assert.assertEquals(StrictMath.ceil(+0.0), +0.0d, 0.0);
1112 Assert.assertEquals(StrictMath.ceil(-0.0), -0.0d, 0.0);
1113 Assert.assertEquals(StrictMath.ceil(-0.9), -0.0d, 0.0);
1114 Assert.assertEquals(StrictMath.ceil(-0.5), -0.0d, 0.0);
1115 Assert.assertEquals(StrictMath.ceil(0.0), -0.0d, 0.0);
1116 Assert.assertEquals(StrictMath.ceil(+2.0), +2.0d, 0.0);
1117 Assert.assertEquals(StrictMath.ceil(+2.1), +3.0d, 0.0);
1118 Assert.assertEquals(StrictMath.ceil(+2.5), +3.0d, 0.0);
1119 Assert.assertEquals(StrictMath.ceil(+2.9), +3.0d, 0.0);
1120 Assert.assertEquals(StrictMath.ceil(+3.0), +3.0d, 0.0);
1121 Assert.assertEquals(StrictMath.ceil(-2.0), -2.0d, 0.0);
1122 Assert.assertEquals(StrictMath.ceil(-2.1), -2.0d, 0.0);
1123 Assert.assertEquals(StrictMath.ceil(-2.5), -2.0d, 0.0);
1124 Assert.assertEquals(StrictMath.ceil(-2.9), -2.0d, 0.0);
1125 Assert.assertEquals(StrictMath.ceil(-3.0), -3.0d, 0.0);
1126 Assert.assertEquals(StrictMath.ceil(Double.NaN), Double.NaN, 0.0);
1127 Assert.assertEquals(StrictMath.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1128 Assert.assertEquals(StrictMath.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1129 }
1130
1131 public static void test_StrictMath_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001132 StrictMath.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001133 Assert.assertEquals(StrictMath.floor(+0.0), +0.0d, 0.0);
1134 Assert.assertEquals(StrictMath.floor(-0.0), -0.0d, 0.0);
1135 Assert.assertEquals(StrictMath.floor(+2.0), +2.0d, 0.0);
1136 Assert.assertEquals(StrictMath.floor(+2.1), +2.0d, 0.0);
1137 Assert.assertEquals(StrictMath.floor(+2.5), +2.0d, 0.0);
1138 Assert.assertEquals(StrictMath.floor(+2.9), +2.0d, 0.0);
1139 Assert.assertEquals(StrictMath.floor(+3.0), +3.0d, 0.0);
1140 Assert.assertEquals(StrictMath.floor(-2.0), -2.0d, 0.0);
1141 Assert.assertEquals(StrictMath.floor(-2.1), -3.0d, 0.0);
1142 Assert.assertEquals(StrictMath.floor(-2.5), -3.0d, 0.0);
1143 Assert.assertEquals(StrictMath.floor(-2.9), -3.0d, 0.0);
1144 Assert.assertEquals(StrictMath.floor(-3.0), -3.0d, 0.0);
1145 Assert.assertEquals(StrictMath.floor(Double.NaN), Double.NaN, 0.0);
1146 Assert.assertEquals(StrictMath.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1147 Assert.assertEquals(StrictMath.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1148 }
1149
1150 public static void test_StrictMath_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001151 StrictMath.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001152 Assert.assertEquals(StrictMath.rint(+0.0), +0.0d, 0.0);
1153 Assert.assertEquals(StrictMath.rint(-0.0), -0.0d, 0.0);
1154 Assert.assertEquals(StrictMath.rint(+2.0), +2.0d, 0.0);
1155 Assert.assertEquals(StrictMath.rint(+2.1), +2.0d, 0.0);
1156 Assert.assertEquals(StrictMath.rint(+2.5), +2.0d, 0.0);
1157 Assert.assertEquals(StrictMath.rint(+2.9), +3.0d, 0.0);
1158 Assert.assertEquals(StrictMath.rint(+3.0), +3.0d, 0.0);
1159 Assert.assertEquals(StrictMath.rint(-2.0), -2.0d, 0.0);
1160 Assert.assertEquals(StrictMath.rint(-2.1), -2.0d, 0.0);
1161 Assert.assertEquals(StrictMath.rint(-2.5), -2.0d, 0.0);
1162 Assert.assertEquals(StrictMath.rint(-2.9), -3.0d, 0.0);
1163 Assert.assertEquals(StrictMath.rint(-3.0), -3.0d, 0.0);
1164 Assert.assertEquals(StrictMath.rint(Double.NaN), Double.NaN, 0.0);
1165 Assert.assertEquals(StrictMath.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
1166 Assert.assertEquals(StrictMath.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
1167 }
1168
1169 public static void test_StrictMath_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001170 StrictMath.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001171 Assert.assertEquals(StrictMath.round(+0.0d), (long)+0.0);
1172 Assert.assertEquals(StrictMath.round(-0.0d), (long)+0.0);
1173 Assert.assertEquals(StrictMath.round(2.0d), 2l);
1174 Assert.assertEquals(StrictMath.round(2.1d), 2l);
1175 Assert.assertEquals(StrictMath.round(2.5d), 3l);
1176 Assert.assertEquals(StrictMath.round(2.9d), 3l);
1177 Assert.assertEquals(StrictMath.round(3.0d), 3l);
1178 Assert.assertEquals(StrictMath.round(-2.0d), -2l);
1179 Assert.assertEquals(StrictMath.round(-2.1d), -2l);
1180 Assert.assertEquals(StrictMath.round(-2.5d), -2l);
1181 Assert.assertEquals(StrictMath.round(-2.9d), -3l);
1182 Assert.assertEquals(StrictMath.round(-3.0d), -3l);
Yi Kong879ca672015-11-18 14:11:44 +00001183 Assert.assertEquals(StrictMath.round(0.49999999999999994d), 0l);
Chris Larsen7adaab02016-04-21 14:49:20 -07001184 Assert.assertEquals(StrictMath.round(4503599627370495.0d), 4503599627370495l); // 2^52 - 1
1185 Assert.assertEquals(StrictMath.round(4503599627370495.5d), 4503599627370496l); // 2^52 - 0.5
1186 Assert.assertEquals(StrictMath.round(4503599627370496.0d), 4503599627370496l); // 2^52
1187 Assert.assertEquals(StrictMath.round(-4503599627370495.0d), -4503599627370495l); // -(2^52 - 1)
1188 Assert.assertEquals(StrictMath.round(-4503599627370495.5d), -4503599627370495l); // -(2^52 - 0.5)
1189 Assert.assertEquals(StrictMath.round(-4503599627370496.0d), -4503599627370496l); // -2^52
Vladimir Marko9c1c06a2016-02-25 17:50:41 +00001190 Assert.assertEquals(StrictMath.round(9007199254740991.0d), 9007199254740991l); // 2^53 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -07001191 Assert.assertEquals(StrictMath.round(-9007199254740991.0d), -9007199254740991l); // -(2^53 - 1)
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001192 Assert.assertEquals(StrictMath.round(Double.NaN), (long)+0.0d);
1193 Assert.assertEquals(StrictMath.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
1194 Assert.assertEquals(StrictMath.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -07001195 Assert.assertEquals(StrictMath.round(Double.longBitsToDouble(0x43F0000000000000l)),
1196 Long.MAX_VALUE); // 2^64
1197 Assert.assertEquals(StrictMath.round(Double.longBitsToDouble(0xC3F0000000000000l)),
1198 Long.MIN_VALUE); // -2^64
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001199 Assert.assertEquals(StrictMath.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
1200 Assert.assertEquals(StrictMath.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
1201 }
1202
1203 public static void test_StrictMath_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001204 StrictMath.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001205 Assert.assertEquals(StrictMath.round(+0.0f), (int)+0.0);
1206 Assert.assertEquals(StrictMath.round(-0.0f), (int)+0.0);
1207 Assert.assertEquals(StrictMath.round(2.0f), 2);
1208 Assert.assertEquals(StrictMath.round(2.1f), 2);
1209 Assert.assertEquals(StrictMath.round(2.5f), 3);
1210 Assert.assertEquals(StrictMath.round(2.9f), 3);
1211 Assert.assertEquals(StrictMath.round(3.0f), 3);
1212 Assert.assertEquals(StrictMath.round(-2.0f), -2);
1213 Assert.assertEquals(StrictMath.round(-2.1f), -2);
1214 Assert.assertEquals(StrictMath.round(-2.5f), -2);
1215 Assert.assertEquals(StrictMath.round(-2.9f), -3);
1216 Assert.assertEquals(StrictMath.round(-3.0f), -3);
Chris Larsenb74353a2015-11-20 09:07:09 -08001217 // 0.4999999701976776123046875
1218 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x3EFFFFFF)), (int)+0.0f);
Chris Larsenf09d5322016-04-22 12:06:34 -07001219 Assert.assertEquals(StrictMath.round(8388607.0f), 8388607); // 2^23 - 1
1220 Assert.assertEquals(StrictMath.round(8388607.5f), 8388608); // 2^23 - 0.5
1221 Assert.assertEquals(StrictMath.round(8388608.0f), 8388608); // 2^23
1222 Assert.assertEquals(StrictMath.round(-8388607.0f), -8388607); // -(2^23 - 1)
1223 Assert.assertEquals(StrictMath.round(-8388607.5f), -8388607); // -(2^23 - 0.5)
1224 Assert.assertEquals(StrictMath.round(-8388608.0f), -8388608); // -2^23
Vladimir Marko9c1c06a2016-02-25 17:50:41 +00001225 Assert.assertEquals(StrictMath.round(16777215.0f), 16777215); // 2^24 - 1
Chris Larsenf09d5322016-04-22 12:06:34 -07001226 Assert.assertEquals(StrictMath.round(16777216.0f), 16777216); // 2^24
1227 Assert.assertEquals(StrictMath.round(-16777215.0f), -16777215); // -(2^24 - 1)
1228 Assert.assertEquals(StrictMath.round(-16777216.0f), -16777216); // -2^24
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001229 Assert.assertEquals(StrictMath.round(Float.NaN), (int)+0.0f);
1230 Assert.assertEquals(StrictMath.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
1231 Assert.assertEquals(StrictMath.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
Chris Larsen7adaab02016-04-21 14:49:20 -07001232 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0x4F800000)),
1233 Integer.MAX_VALUE); // 2^32
1234 Assert.assertEquals(StrictMath.round(Float.intBitsToFloat(0xCF800000)),
1235 Integer.MIN_VALUE); // -2^32
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +01001236 Assert.assertEquals(StrictMath.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
1237 Assert.assertEquals(StrictMath.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
1238 }
1239
Elliott Hughes28c384b2012-06-15 16:46:25 -07001240 public static void test_Float_floatToRawIntBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001241 Float.floatToRawIntBits(-1.0f);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001242 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
1243 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
1244 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
1245 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
1246 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
1247 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
1248 }
1249
1250 public static void test_Float_intBitsToFloat() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001251 Float.intBitsToFloat(0xbf800000);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001252 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
1253 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
1254 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
1255 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
1256 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
1257 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
1258 }
1259
1260 public static void test_Double_doubleToRawLongBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001261 Double.doubleToRawLongBits(-1.0);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001262 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
1263 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
1264 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
1265 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
1266 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
1267 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
1268 }
1269
1270 public static void test_Double_longBitsToDouble() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001271 Double.longBitsToDouble(0xbff0000000000000L);
Elliott Hughes28c384b2012-06-15 16:46:25 -07001272 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
1273 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
1274 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
1275 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
1276 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
1277 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
1278 }
Serban Constantinescu23abec92014-07-02 16:13:38 +01001279
Zheng Xua3fe7422014-07-09 14:03:15 +08001280 public static void test_Short_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001281 Short.reverseBytes((short)0x1357);
Zheng Xua3fe7422014-07-09 14:03:15 +08001282 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
1283 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
1284 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
1285 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
1286 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
1287 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
1288 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
1289 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
1290 }
1291
1292 public static void test_Integer_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001293 Integer.reverseBytes(0x13579bdf);
Zheng Xua3fe7422014-07-09 14:03:15 +08001294 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
1295 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
1296 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
1297 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
1298 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
1299 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
1300 }
1301
1302 public static void test_Long_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001303 Long.reverseBytes(0x13579bdf2468ace0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001304 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
1305 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
1306 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
1307 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
1308 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
1309 }
1310
Serban Constantinescu23abec92014-07-02 16:13:38 +01001311 public static void test_Integer_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001312 Integer.reverse(0x12345678);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001313 Assert.assertEquals(Integer.reverse(1), 0x80000000);
1314 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
1315 Assert.assertEquals(Integer.reverse(0), 0);
1316 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
1317 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
1318 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
1319 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
1320 }
1321
1322 public static void test_Long_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001323 Long.reverse(0x1234567812345678L);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001324 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
1325 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
1326 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001327 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
1328 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
1329 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001330 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
Andreas Gampe575422f2015-07-07 13:25:58 -07001331
1332 Assert.assertEquals(test_Long_reverse_b22324327(0xaaaaaaaaaaaaaaaaL, 0x5555555555555555L),
1333 157472205507277347L);
1334 }
1335
1336 // A bit more complicated than the above. Use local variables to stress register allocation.
1337 private static long test_Long_reverse_b22324327(long l1, long l2) {
1338 // A couple of local integers. Use them in a loop, so they get promoted.
1339 int i1 = 0, i2 = 1, i3 = 2, i4 = 3, i5 = 4, i6 = 5, i7 = 6, i8 = 7;
1340 for (int k = 0; k < 10; k++) {
1341 i1 += 1;
1342 i2 += 2;
1343 i3 += 3;
1344 i4 += 4;
1345 i5 += 5;
1346 i6 += 6;
1347 i7 += 7;
1348 i8 += 8;
1349 }
1350
1351 // Do the Long.reverse() calls, save the results.
1352 long r1 = Long.reverse(l1);
1353 long r2 = Long.reverse(l2);
1354
1355 // Some more looping with the ints.
1356 for (int k = 0; k < 10; k++) {
1357 i1 += 1;
1358 i2 += 2;
1359 i3 += 3;
1360 i4 += 4;
1361 i5 += 5;
1362 i6 += 6;
1363 i7 += 7;
1364 i8 += 8;
1365 }
1366
1367 // Include everything in the result, so things are kept live. Try to be a little bit clever to
1368 // avoid things being folded somewhere.
1369 return (r1 / i1) + (r2 / i2) + i3 + i4 + i5 + i6 + i7 + i8;
Serban Constantinescu23abec92014-07-02 16:13:38 +01001370 }
1371
Mark Mendelld5897672015-08-12 21:16:41 -04001372 public static boolean doThrow = false;
1373
1374 public static int $noinline$return_int_zero() {
1375 if (doThrow) {
1376 throw new Error();
1377 }
1378 return 0;
1379 }
1380
Andra Danciudc787f42020-07-07 14:28:56 +00001381 public static void test_Integer_divideUnsigned() {
1382 Assert.assertEquals(Integer.divideUnsigned(100, 10), 10);
1383 Assert.assertEquals(Integer.divideUnsigned(100, 1), 100);
1384 Assert.assertEquals(Integer.divideUnsigned(1024, 128), 8);
1385 Assert.assertEquals(Integer.divideUnsigned(12345678, 264), 46763);
1386 Assert.assertEquals(Integer.divideUnsigned(13, 5), 2);
1387 Assert.assertEquals(Integer.divideUnsigned(-2, 2), Integer.MAX_VALUE);
1388 Assert.assertEquals(Integer.divideUnsigned(-1, 2), Integer.MAX_VALUE);
1389 Assert.assertEquals(Integer.divideUnsigned(100000, -1), 0);
1390 Assert.assertEquals(Integer.divideUnsigned(Integer.MAX_VALUE, -1), 0);
1391 Assert.assertEquals(Integer.divideUnsigned(-2, -1), 0);
Vladimir Marko8942b3a2020-07-20 10:42:15 +01001392 Assert.assertEquals(Integer.divideUnsigned(-1, -2), 1);
Andra Danciudc787f42020-07-07 14:28:56 +00001393 Assert.assertEquals(Integer.divideUnsigned(-173448, 13), 330368757);
1394 Assert.assertEquals(Integer.divideUnsigned(Integer.MIN_VALUE, 2), (1 << 30));
1395 Assert.assertEquals(Integer.divideUnsigned(-1, Integer.MIN_VALUE), 1);
1396 Assert.assertEquals(Integer.divideUnsigned(Integer.MAX_VALUE, Integer.MIN_VALUE), 0);
1397 Assert.assertEquals(Integer.divideUnsigned(Integer.MIN_VALUE, Integer.MAX_VALUE), 1);
1398
1399 try {
1400 Integer.divideUnsigned(1, 0);
Vladimir Markobe7fe3b2020-07-09 10:58:12 +01001401 Assert.fail("Unreachable");
1402 } catch (ArithmeticException expected) {
Andra Danciudc787f42020-07-07 14:28:56 +00001403 }
Andra Danciudc787f42020-07-07 14:28:56 +00001404 }
1405
Artem Serova3bd4ec2020-08-27 16:26:17 +01001406
1407 private static final long BIG_LONG_VALUE = 739287620162442240L;
1408
1409 public static void test_Long_divideUnsigned() {
1410 Assert.assertEquals(Long.divideUnsigned(100L, 10L), 10L);
1411 Assert.assertEquals(Long.divideUnsigned(100L, 1L), 100L);
1412 Assert.assertEquals(Long.divideUnsigned(1024L, 128L), 8L);
1413 Assert.assertEquals(Long.divideUnsigned(12345678L, 264L), 46763L);
1414 Assert.assertEquals(Long.divideUnsigned(13L, 5L), 2L);
1415 Assert.assertEquals(Long.divideUnsigned(-2L, 2L), Long.MAX_VALUE);
1416 Assert.assertEquals(Long.divideUnsigned(-1L, 2L), Long.MAX_VALUE);
1417 Assert.assertEquals(Long.divideUnsigned(100000L, -1L), 0L);
1418 Assert.assertEquals(Long.divideUnsigned(Long.MAX_VALUE, -1L), 0L);
1419 Assert.assertEquals(Long.divideUnsigned(-2L, -1L), 0L);
1420 Assert.assertEquals(Long.divideUnsigned(-1L, -2L), 1L);
1421 Assert.assertEquals(Long.divideUnsigned(-173448L, 13L), 1418980313362259859L);
1422 Assert.assertEquals(Long.divideUnsigned(Long.MIN_VALUE, 2L), (1L << 62));
1423 Assert.assertEquals(Long.divideUnsigned(-1L, Long.MIN_VALUE), 1L);
1424 Assert.assertEquals(Long.divideUnsigned(Long.MAX_VALUE, Long.MIN_VALUE), 0L);
1425 Assert.assertEquals(Long.divideUnsigned(Long.MIN_VALUE, Long.MAX_VALUE), 1L);
1426 Assert.assertEquals(Long.divideUnsigned(Long.MAX_VALUE, 1L), Long.MAX_VALUE);
1427 Assert.assertEquals(Long.divideUnsigned(Long.MIN_VALUE, 1L), Long.MIN_VALUE);
1428 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, BIG_LONG_VALUE), 1L);
1429 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 1L), BIG_LONG_VALUE);
1430 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 1024L), 721960566564885L);
1431 Assert.assertEquals(Long.divideUnsigned(BIG_LONG_VALUE, 0x1FFFFFFFFL), 86064406L);
1432
1433 try {
1434 Long.divideUnsigned(1L, 0L);
1435 Assert.fail("Unreachable");
1436 } catch (ArithmeticException expected) {
1437 }
1438 }
1439
Scott Wakeling611d3392015-07-10 11:42:06 +01001440 public static void test_Integer_numberOfLeadingZeros() {
1441 Assert.assertEquals(Integer.numberOfLeadingZeros(0), Integer.SIZE);
Mark Mendelld5897672015-08-12 21:16:41 -04001442 Assert.assertEquals(Integer.numberOfLeadingZeros(1), Integer.SIZE - 1);
1443 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << (Integer.SIZE-1)), 0);
1444 Assert.assertEquals(Integer.numberOfLeadingZeros($noinline$return_int_zero()), Integer.SIZE);
Scott Wakeling611d3392015-07-10 11:42:06 +01001445 for (int i = 0; i < Integer.SIZE; i++) {
1446 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << i), Integer.SIZE - 1 - i);
1447 Assert.assertEquals(Integer.numberOfLeadingZeros((1 << i) | 1), Integer.SIZE - 1 - i);
1448 Assert.assertEquals(Integer.numberOfLeadingZeros(0xFFFFFFFF >>> i), i);
1449 }
1450 }
1451
Mark Mendelld5897672015-08-12 21:16:41 -04001452 public static long $noinline$return_long_zero() {
1453 if (doThrow) {
1454 throw new Error();
1455 }
1456 return 0;
1457 }
1458
Scott Wakeling611d3392015-07-10 11:42:06 +01001459 public static void test_Long_numberOfLeadingZeros() {
1460 Assert.assertEquals(Long.numberOfLeadingZeros(0L), Long.SIZE);
Mark Mendelld5897672015-08-12 21:16:41 -04001461 Assert.assertEquals(Long.numberOfLeadingZeros(1L), Long.SIZE - 1);
1462 Assert.assertEquals(Long.numberOfLeadingZeros(1L << ((Long.SIZE/2)-1)), Long.SIZE/2);
1463 Assert.assertEquals(Long.numberOfLeadingZeros(1L << (Long.SIZE-1)), 0);
1464 Assert.assertEquals(Long.numberOfLeadingZeros($noinline$return_long_zero()), Long.SIZE);
Scott Wakeling611d3392015-07-10 11:42:06 +01001465 for (int i = 0; i < Long.SIZE; i++) {
1466 Assert.assertEquals(Long.numberOfLeadingZeros(1L << i), Long.SIZE - 1 - i);
1467 Assert.assertEquals(Long.numberOfLeadingZeros((1L << i) | 1L), Long.SIZE - 1 - i);
1468 Assert.assertEquals(Long.numberOfLeadingZeros(0xFFFFFFFFFFFFFFFFL >>> i), i);
1469 }
1470 }
1471
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001472 static Object runtime;
1473 static Method address_of;
Zuo Wangf37a88b2014-07-10 04:26:41 -07001474 static Method new_non_movable_array;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001475 static Method peek_byte;
1476 static Method peek_short;
1477 static Method peek_int;
1478 static Method peek_long;
1479 static Method poke_byte;
1480 static Method poke_short;
1481 static Method poke_int;
1482 static Method poke_long;
1483
1484 public static void initSupportMethodsForPeekPoke() throws Exception {
1485 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
1486 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
1487 runtime = get_runtime.invoke(null);
1488 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
Zuo Wangf37a88b2014-07-10 04:26:41 -07001489 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001490
1491 Class<?> io_memory = Class.forName("libcore.io.Memory");
1492 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
1493 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
1494 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
1495 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
1496 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
1497 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
1498 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
1499 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
1500 }
1501
1502 public static void test_Memory_peekByte() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001503 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001504 b[0] = 0x12;
1505 b[1] = 0x11;
1506 long address = (long)address_of.invoke(runtime, b);
1507 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
1508 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
1509 }
1510
1511 public static void test_Memory_peekShort() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001512 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001513 b[0] = 0x13;
1514 b[1] = 0x12;
1515 b[2] = 0x11;
1516 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001517 peek_short.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001518 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
1519 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
1520 }
1521
1522 public static void test_Memory_peekInt() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001523 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001524 b[0] = 0x15;
1525 b[1] = 0x14;
1526 b[2] = 0x13;
1527 b[3] = 0x12;
1528 b[4] = 0x11;
1529 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001530 peek_int.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001531 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
1532 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
1533 }
1534
1535 public static void test_Memory_peekLong() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001536 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001537 b[0] = 0x19;
1538 b[1] = 0x18;
1539 b[2] = 0x17;
1540 b[3] = 0x16;
1541 b[4] = 0x15;
1542 b[5] = 0x14;
1543 b[6] = 0x13;
1544 b[7] = 0x12;
1545 b[8] = 0x11;
1546 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001547 peek_long.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001548 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
1549 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
1550 }
1551
1552 public static void test_Memory_pokeByte() throws Exception {
1553 byte[] r = {0x11, 0x12};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001554 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001555 long address = (long)address_of.invoke(runtime, b);
1556 poke_byte.invoke(null, address, (byte)0x11);
1557 poke_byte.invoke(null, address + 1, (byte)0x12);
1558 Assert.assertTrue(Arrays.equals(r, b));
1559 }
1560
1561 public static void test_Memory_pokeShort() throws Exception {
1562 byte[] ra = {0x12, 0x11, 0x13};
1563 byte[] ru = {0x12, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001564 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001565 long address = (long)address_of.invoke(runtime, b);
1566
1567 // Aligned write
1568 b[2] = 0x13;
1569 poke_short.invoke(null, address, (short)0x1112, false);
1570 Assert.assertTrue(Arrays.equals(ra, b));
1571
1572 // Unaligned write
1573 poke_short.invoke(null, address + 1, (short)0x2122, false);
1574 Assert.assertTrue(Arrays.equals(ru, b));
1575 }
1576
1577 public static void test_Memory_pokeInt() throws Exception {
1578 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
1579 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001580 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001581 long address = (long)address_of.invoke(runtime, b);
1582
1583 b[4] = 0x15;
1584 poke_int.invoke(null, address, (int)0x11121314, false);
1585 Assert.assertTrue(Arrays.equals(ra, b));
1586
1587 poke_int.invoke(null, address + 1, (int)0x21222324, false);
1588 Assert.assertTrue(Arrays.equals(ru, b));
1589 }
1590
1591 public static void test_Memory_pokeLong() throws Exception {
1592 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
1593 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001594 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001595 long address = (long)address_of.invoke(runtime, b);
1596
1597 b[8] = 0x19;
1598 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
1599 Assert.assertTrue(Arrays.equals(ra, b));
1600
1601 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
1602 Assert.assertTrue(Arrays.equals(ru, b));
1603 }
Scott Wakeling9ee23f42015-07-23 10:44:35 +01001604
1605 public static void test_Integer_numberOfTrailingZeros() {
1606 Assert.assertEquals(Integer.numberOfTrailingZeros(0), Integer.SIZE);
1607 for (int i = 0; i < Integer.SIZE; i++) {
1608 Assert.assertEquals(
1609 Integer.numberOfTrailingZeros(0x80000000 >> i),
1610 Integer.SIZE - 1 - i);
1611 Assert.assertEquals(
1612 Integer.numberOfTrailingZeros((0x80000000 >> i) | 0x80000000),
1613 Integer.SIZE - 1 - i);
1614 Assert.assertEquals(Integer.numberOfTrailingZeros(1 << i), i);
1615 }
1616 }
1617
1618 public static void test_Long_numberOfTrailingZeros() {
1619 Assert.assertEquals(Long.numberOfTrailingZeros(0), Long.SIZE);
1620 for (int i = 0; i < Long.SIZE; i++) {
1621 Assert.assertEquals(
1622 Long.numberOfTrailingZeros(0x8000000000000000L >> i),
1623 Long.SIZE - 1 - i);
1624 Assert.assertEquals(
1625 Long.numberOfTrailingZeros((0x8000000000000000L >> i) | 0x8000000000000000L),
1626 Long.SIZE - 1 - i);
1627 Assert.assertEquals(Long.numberOfTrailingZeros(1L << i), i);
1628 }
1629 }
1630
1631 public static void test_Integer_rotateRight() throws Exception {
1632 Assert.assertEquals(Integer.rotateRight(0x11, 0), 0x11);
1633
1634 Assert.assertEquals(Integer.rotateRight(0x11, 1), 0x80000008);
1635 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE - 1), 0x22);
1636 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE), 0x11);
1637 Assert.assertEquals(Integer.rotateRight(0x11, Integer.SIZE + 1), 0x80000008);
1638
1639 Assert.assertEquals(Integer.rotateRight(0x11, -1), 0x22);
1640 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE - 1)), 0x80000008);
1641 Assert.assertEquals(Integer.rotateRight(0x11, -Integer.SIZE), 0x11);
1642 Assert.assertEquals(Integer.rotateRight(0x11, -(Integer.SIZE + 1)), 0x22);
1643
1644 Assert.assertEquals(Integer.rotateRight(0x80000000, 1), 0x40000000);
1645
1646 for (int i = 0; i < Integer.SIZE; i++) {
1647 Assert.assertEquals(
1648 Integer.rotateRight(0xBBAAAADD, i),
1649 (0xBBAAAADD >>> i) | (0xBBAAAADD << (Integer.SIZE - i)));
1650 }
1651 }
1652
1653 public static void test_Long_rotateRight() throws Exception {
1654 Assert.assertEquals(Long.rotateRight(0x11, 0), 0x11);
1655
1656 Assert.assertEquals(Long.rotateRight(0x11, 1), 0x8000000000000008L);
1657 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE - 1), 0x22);
1658 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE), 0x11);
1659 Assert.assertEquals(Long.rotateRight(0x11, Long.SIZE + 1), 0x8000000000000008L);
1660
1661 Assert.assertEquals(Long.rotateRight(0x11, -1), 0x22);
1662 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE - 1)), 0x8000000000000008L);
1663 Assert.assertEquals(Long.rotateRight(0x11, -Long.SIZE), 0x11);
1664 Assert.assertEquals(Long.rotateRight(0x11, -(Long.SIZE + 1)), 0x22);
1665
1666 Assert.assertEquals(Long.rotateRight(0x8000000000000000L, 1), 0x4000000000000000L);
1667
1668 for (int i = 0; i < Long.SIZE; i++) {
1669 Assert.assertEquals(
1670 Long.rotateRight(0xBBAAAADDFF0000DDL, i),
1671 (0xBBAAAADDFF0000DDL >>> i) | (0xBBAAAADDFF0000DDL << (Long.SIZE - i)));
1672 }
1673 }
1674
1675 public static void test_Integer_rotateLeft() throws Exception {
1676 Assert.assertEquals(Integer.rotateLeft(0x11, 0), 0x11);
1677
1678 Assert.assertEquals(Integer.rotateLeft(0x11, 1), 0x22);
1679 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE - 1), 0x80000008);
1680 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE), 0x11);
1681 Assert.assertEquals(Integer.rotateLeft(0x11, Integer.SIZE + 1), 0x22);
1682
1683 Assert.assertEquals(Integer.rotateLeft(0x11, -1), 0x80000008);
1684 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE - 1)), 0x22);
1685 Assert.assertEquals(Integer.rotateLeft(0x11, -Integer.SIZE), 0x11);
1686 Assert.assertEquals(Integer.rotateLeft(0x11, -(Integer.SIZE + 1)), 0x80000008);
1687
1688 Assert.assertEquals(Integer.rotateLeft(0xC0000000, 1), 0x80000001);
1689
1690 for (int i = 0; i < Integer.SIZE; i++) {
1691 Assert.assertEquals(
1692 Integer.rotateLeft(0xBBAAAADD, i),
1693 (0xBBAAAADD << i) | (0xBBAAAADD >>> (Integer.SIZE - i)));
1694 }
1695 }
1696
1697 public static void test_Long_rotateLeft() throws Exception {
1698 Assert.assertEquals(Long.rotateLeft(0x11, 0), 0x11);
1699
1700 Assert.assertEquals(Long.rotateLeft(0x11, 1), 0x22);
1701 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE - 1), 0x8000000000000008L);
1702 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE), 0x11);
1703 Assert.assertEquals(Long.rotateLeft(0x11, Long.SIZE + 1), 0x22);
1704
1705 Assert.assertEquals(Long.rotateLeft(0x11, -1), 0x8000000000000008L);
1706 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE - 1)), 0x22);
1707 Assert.assertEquals(Long.rotateLeft(0x11, -Long.SIZE), 0x11);
1708 Assert.assertEquals(Long.rotateLeft(0x11, -(Long.SIZE + 1)), 0x8000000000000008L);
1709
1710 Assert.assertEquals(Long.rotateLeft(0xC000000000000000L, 1), 0x8000000000000001L);
1711
1712 for (int i = 0; i < Long.SIZE; i++) {
1713 Assert.assertEquals(
1714 Long.rotateLeft(0xBBAAAADDFF0000DDL, i),
1715 (0xBBAAAADDFF0000DDL << i) | (0xBBAAAADDFF0000DDL >>> (Long.SIZE - i)));
1716 }
1717 }
1718
1719 public static void test_Integer_rotateRightLeft() throws Exception {
1720 for (int i = 0; i < Integer.SIZE * 2; i++) {
1721 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, i),
1722 Integer.rotateRight(0xBBAAAADD, -i));
1723 Assert.assertEquals(Integer.rotateLeft(0xBBAAAADD, -i),
1724 Integer.rotateRight(0xBBAAAADD, i));
1725 }
1726 }
1727
1728 public static void test_Long_rotateRightLeft() throws Exception {
1729 for (int i = 0; i < Long.SIZE * 2; i++) {
1730 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, i),
1731 Long.rotateRight(0xBBAAAADDFF0000DDL, -i));
1732 Assert.assertEquals(Long.rotateLeft(0xBBAAAADDFF0000DDL, -i),
1733 Long.rotateRight(0xBBAAAADDFF0000DDL, i));
1734 }
1735 }
jeffhao5d1ac922011-09-29 17:41:15 -07001736}