blob: 77c1a99728a0bf2873d0a13d32780b5fef6e36d8 [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();
Zheng Xua3fe7422014-07-09 14:03:15 +080043 test_Short_reverseBytes();
44 test_Integer_reverseBytes();
45 test_Long_reverseBytes();
Serban Constantinescu23abec92014-07-02 16:13:38 +010046 test_Integer_reverse();
47 test_Long_reverse();
Scott Wakeling611d3392015-07-10 11:42:06 +010048 test_Integer_numberOfLeadingZeros();
49 test_Long_numberOfLeadingZeros();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010050 test_StrictMath_abs_I();
51 test_StrictMath_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010052 test_StrictMath_min_I();
53 test_StrictMath_max_I();
54 test_StrictMath_min_J();
55 test_StrictMath_max_J();
56 test_StrictMath_min_F();
57 test_StrictMath_max_F();
58 test_StrictMath_min_D();
59 test_StrictMath_max_D();
Chao-ying Fuff87d7b2015-01-19 15:51:57 -080060 test_StrictMath_sqrt();
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010061 test_StrictMath_ceil();
62 test_StrictMath_floor();
63 test_StrictMath_rint();
64 test_StrictMath_round_D();
65 test_StrictMath_round_F();
Elliott Hughes28c384b2012-06-15 16:46:25 -070066 test_String_charAt();
67 test_String_compareTo();
68 test_String_indexOf();
69 test_String_isEmpty();
70 test_String_length();
Andreas Gampe7a949612014-07-08 11:03:59 -070071 test_Thread_currentThread();
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070072 initSupportMethodsForPeekPoke();
73 test_Memory_peekByte();
74 test_Memory_peekShort();
75 test_Memory_peekInt();
76 test_Memory_peekLong();
77 test_Memory_pokeByte();
78 test_Memory_pokeShort();
79 test_Memory_pokeInt();
80 test_Memory_pokeLong();
Elliott Hughes28c384b2012-06-15 16:46:25 -070081 }
82
Andreas Gampe7a949612014-07-08 11:03:59 -070083 /**
84 * Will test inlining Thread.currentThread().
85 */
86 public static void test_Thread_currentThread() {
87 // 1. Do not use result.
88 Thread.currentThread();
89
90 // 2. Result should not be null.
91 Assert.assertNotNull(Thread.currentThread());
92 }
93
Elliott Hughes28c384b2012-06-15 16:46:25 -070094 public static void test_String_length() {
95 String str0 = "";
96 String str1 = "x";
97 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
98
99 Assert.assertEquals(str0.length(), 0);
100 Assert.assertEquals(str1.length(), 1);
101 Assert.assertEquals(str80.length(), 80);
102
103 String strNull = null;
104 try {
105 strNull.length();
106 Assert.fail();
107 } catch (NullPointerException expected) {
108 }
109 }
110
111 public static void test_String_isEmpty() {
112 String str0 = "";
113 String str1 = "x";
114
115 Assert.assertTrue(str0.isEmpty());
116 Assert.assertFalse(str1.isEmpty());
117
118 String strNull = null;
119 try {
120 strNull.isEmpty();
121 Assert.fail();
122 } catch (NullPointerException expected) {
123 }
124 }
125
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800126 // Break up the charAt tests. The optimizing compiler doesn't optimize methods with try-catch yet,
127 // so we need to separate out the tests that are expected to throw exception
128
Elliott Hughes28c384b2012-06-15 16:46:25 -0700129 public static void test_String_charAt() {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800130 String testStr = "Now is the time to test some stuff";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700131
Andreas Gampe878d58c2015-01-15 23:24:00 -0800132 Assert.assertEquals(testStr.length() - 1, 33); // 33 = testStr.length()-1 as a constant.
133 Assert.assertEquals('f', testStr.charAt(33));
Elliott Hughes28c384b2012-06-15 16:46:25 -0700134
Andreas Gampe878d58c2015-01-15 23:24:00 -0800135 test_String_charAt(testStr, 'N', 'o', ' ', 'f');
136 test_String_charAt(testStr.substring(3,15), ' ', 'i', 'm', 'e');
137 }
138 public static void test_String_charAt(String testStr, char a, char b, char c, char d) {
139 Assert.assertEquals(a, testStr.charAt(0));
140 Assert.assertEquals(b, testStr.charAt(1));
141 Assert.assertEquals(c, testStr.charAt(10));
142 Assert.assertEquals(d, testStr.charAt(testStr.length()-1));
143
144 test_String_charAtExc(testStr);
145 test_String_charAtExc2(testStr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800146 }
147
Andreas Gampe878d58c2015-01-15 23:24:00 -0800148 private static void test_String_charAtExc(String testStr) {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700149 try {
150 testStr.charAt(-1);
151 Assert.fail();
152 } catch (StringIndexOutOfBoundsException expected) {
153 }
154 try {
155 testStr.charAt(80);
156 Assert.fail();
157 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700158 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000159 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800160 if (testStr.length() == 34) {
161 testStr.charAt(34); // 34 = "Now is the time to test some stuff".length()
162 } else {
163 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length()
164 testStr.charAt(12);
165 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000166 Assert.fail();
167 } catch (StringIndexOutOfBoundsException expected) {
168 }
169 try {
170 test_String_charAt_inner(testStr, -1);
171 Assert.fail();
172 } catch (StringIndexOutOfBoundsException expected) {
173 }
174 try {
175 test_String_charAt_inner(testStr, 80);
176 Assert.fail();
177 } catch (StringIndexOutOfBoundsException expected) {
178 }
179 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800180 if (testStr.length() == 34) {
181 // 34 = "Now is the time to test some stuff".length()
182 test_String_charAt_inner(testStr, 34);
183 } else {
184 Assert.assertEquals(testStr.length(), 12); // 12 = " is the time".length()
185 test_String_charAt_inner(testStr, 12);
186 }
Vladimir Marko00ca8472015-01-26 14:06:46 +0000187 Assert.fail();
188 } catch (StringIndexOutOfBoundsException expected) {
189 }
190
191 String strEmpty = "";
192 try {
193 strEmpty.charAt(0);
194 Assert.fail();
195 } catch (StringIndexOutOfBoundsException expected) {
196 }
jeffhao5d1ac922011-09-29 17:41:15 -0700197
Elliott Hughes28c384b2012-06-15 16:46:25 -0700198 String strNull = null;
199 try {
200 strNull.charAt(0);
201 Assert.fail();
202 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700203 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700204 }
jeffhao5d1ac922011-09-29 17:41:15 -0700205
Vladimir Marko00ca8472015-01-26 14:06:46 +0000206 private static char test_String_charAt_inner(String s, int index) {
207 // Using non-constant index here (assuming that this method wasn't inlined).
208 return s.charAt(index);
209 }
210
Andreas Gampe878d58c2015-01-15 23:24:00 -0800211 private static void test_String_charAtExc2(String testStr) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800212 try {
Andreas Gampe878d58c2015-01-15 23:24:00 -0800213 test_String_charAtExc3(testStr);
214 Assert.fail();
215 } catch (StringIndexOutOfBoundsException expected) {
216 }
217 try {
218 test_String_charAtExc4(testStr);
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800219 Assert.fail();
220 } catch (StringIndexOutOfBoundsException expected) {
221 }
222 }
223
Andreas Gampe878d58c2015-01-15 23:24:00 -0800224 private static void test_String_charAtExc3(String testStr) {
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800225 Assert.assertEquals('N', testStr.charAt(-1));
226 }
227
Andreas Gampe878d58c2015-01-15 23:24:00 -0800228 private static void test_String_charAtExc4(String testStr) {
229 Assert.assertEquals('N', testStr.charAt(100));
230 }
231
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700232 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700233 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700234 public static void test_String_indexOf() {
235 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700236 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700237 String str3 = "abc";
238 String str10 = "abcdefghij";
239 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700240
Elliott Hughes28c384b2012-06-15 16:46:25 -0700241 Assert.assertEquals(str0.indexOf('a'), -1);
242 Assert.assertEquals(str3.indexOf('a'), 0);
243 Assert.assertEquals(str3.indexOf('b'), 1);
244 Assert.assertEquals(str3.indexOf('c'), 2);
245 Assert.assertEquals(str10.indexOf('j'), 9);
246 Assert.assertEquals(str40.indexOf('a'), 0);
247 Assert.assertEquals(str40.indexOf('b'), 38);
248 Assert.assertEquals(str40.indexOf('c'), 39);
249 Assert.assertEquals(str0.indexOf('a',20), -1);
250 Assert.assertEquals(str0.indexOf('a',0), -1);
251 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700252 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700253 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700254 Assert.assertEquals(str3.indexOf('a',0), 0);
255 Assert.assertEquals(str3.indexOf('a',1), -1);
256 Assert.assertEquals(str3.indexOf('a',1234), -1);
257 Assert.assertEquals(str3.indexOf('b',0), 1);
258 Assert.assertEquals(str3.indexOf('b',1), 1);
259 Assert.assertEquals(str3.indexOf('c',2), 2);
260 Assert.assertEquals(str10.indexOf('j',5), 9);
261 Assert.assertEquals(str10.indexOf('j',9), 9);
262 Assert.assertEquals(str40.indexOf('a',10), 10);
263 Assert.assertEquals(str40.indexOf('b',40), -1);
264
Andreas Gampe678e6952015-05-07 16:44:58 -0700265 testIndexOfNull();
266
Andreas Gampe21030dd2015-05-07 14:46:15 -0700267 // Same data as above, but stored so it's not a literal in the next test. -2 stands for
268 // indexOf(I) instead of indexOf(II).
269 start--;
270 int[][] searchData = {
271 { 'a', -2, -1 },
272 { 'a', -2, 0 },
273 { 'b', -2, 1 },
274 { 'c', -2, 2 },
275 { 'j', -2, 9 },
276 { 'a', -2, 0 },
277 { 'b', -2, 38 },
278 { 'c', -2, 39 },
279 { 'a', 20, -1 },
280 { 'a', 0, -1 },
281 { 'a', -1, -1 },
282 { '/', ++start, -1 },
283 { 'a', negIndex[0], -1 },
284 { 'a', 0, 0 },
285 { 'a', 1, -1 },
286 { 'a', 1234, -1 },
287 { 'b', 0, 1 },
288 { 'b', 1, 1 },
289 { 'c', 2, 2 },
290 { 'j', 5, 9 },
291 { 'j', 9, 9 },
292 { 'a', 10, 10 },
293 { 'b', 40, -1 },
294 };
295 testStringIndexOfChars(searchData);
296
Andreas Gampe678e6952015-05-07 16:44:58 -0700297 testSurrogateIndexOf();
298 }
299
Andreas Gampe21030dd2015-05-07 14:46:15 -0700300 private static void testStringIndexOfChars(int[][] searchData) {
301 // Use a try-catch to avoid inlining.
302 try {
303 testStringIndexOfCharsImpl(searchData);
304 } catch (Exception e) {
305 System.out.println("Unexpected exception");
306 }
307 }
308
309 private static void testStringIndexOfCharsImpl(int[][] searchData) {
310 String str0 = "";
311 String str1 = "/";
312 String str3 = "abc";
313 String str10 = "abcdefghij";
314 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
315
316 Assert.assertEquals(str0.indexOf(searchData[0][0]), searchData[0][2]);
317 Assert.assertEquals(str3.indexOf(searchData[1][0]), searchData[1][2]);
318 Assert.assertEquals(str3.indexOf(searchData[2][0]), searchData[2][2]);
319 Assert.assertEquals(str3.indexOf(searchData[3][0]), searchData[3][2]);
320 Assert.assertEquals(str10.indexOf(searchData[4][0]), searchData[4][2]);
321 Assert.assertEquals(str40.indexOf(searchData[5][0]), searchData[5][2]);
322 Assert.assertEquals(str40.indexOf(searchData[6][0]), searchData[6][2]);
323 Assert.assertEquals(str40.indexOf(searchData[7][0]), searchData[7][2]);
324 Assert.assertEquals(str0.indexOf(searchData[8][0], searchData[8][1]), searchData[8][2]);
325 Assert.assertEquals(str0.indexOf(searchData[9][0], searchData[9][1]), searchData[9][2]);
326 Assert.assertEquals(str0.indexOf(searchData[10][0], searchData[10][1]), searchData[10][2]);
327 Assert.assertEquals(str1.indexOf(searchData[11][0], searchData[11][1]), searchData[11][2]);
328 Assert.assertEquals(str1.indexOf(searchData[12][0], searchData[12][1]), searchData[12][2]);
329 Assert.assertEquals(str3.indexOf(searchData[13][0], searchData[13][1]), searchData[13][2]);
330 Assert.assertEquals(str3.indexOf(searchData[14][0], searchData[14][1]), searchData[14][2]);
331 Assert.assertEquals(str3.indexOf(searchData[15][0], searchData[15][1]), searchData[15][2]);
332 Assert.assertEquals(str3.indexOf(searchData[16][0], searchData[16][1]), searchData[16][2]);
333 Assert.assertEquals(str3.indexOf(searchData[17][0], searchData[17][1]), searchData[17][2]);
334 Assert.assertEquals(str3.indexOf(searchData[18][0], searchData[18][1]), searchData[18][2]);
335 Assert.assertEquals(str10.indexOf(searchData[19][0], searchData[19][1]), searchData[19][2]);
336 Assert.assertEquals(str10.indexOf(searchData[20][0], searchData[20][1]), searchData[20][2]);
337 Assert.assertEquals(str40.indexOf(searchData[21][0], searchData[21][1]), searchData[21][2]);
338 Assert.assertEquals(str40.indexOf(searchData[22][0], searchData[22][1]), searchData[22][2]);
339 }
340
Andreas Gampe678e6952015-05-07 16:44:58 -0700341 private static void testSurrogateIndexOf() {
342 int supplementaryChar = 0x20b9f;
343 String surrogatePair = "\ud842\udf9f";
344 String stringWithSurrogates = "hello " + surrogatePair + " world";
345
346 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
347 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
348 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
349 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
Andreas Gampeba6fdbc2015-05-07 22:31:55 -0700350
351 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar - 0x10000), -1);
352 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar | 0x80000000), -1);
Andreas Gampe678e6952015-05-07 16:44:58 -0700353 }
354
355 private static void testIndexOfNull() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700356 String strNull = null;
357 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700358 testNullIndex(strNull, 'a');
Elliott Hughes28c384b2012-06-15 16:46:25 -0700359 Assert.fail();
360 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700361 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700362 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700363 testNullIndex(strNull, 'a', 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700364 Assert.fail();
365 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700366 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700367 try {
Andreas Gampe678e6952015-05-07 16:44:58 -0700368 testNullIndex(strNull, 'a', -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700369 Assert.fail();
370 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700371 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700372 }
jeffhao5d1ac922011-09-29 17:41:15 -0700373
Andreas Gampe678e6952015-05-07 16:44:58 -0700374 private static int testNullIndex(String strNull, int c) {
375 return strNull.indexOf(c);
376 }
377
378 private static int testNullIndex(String strNull, int c, int startIndex) {
379 return strNull.indexOf(c, startIndex);
380 }
381
Elliott Hughes28c384b2012-06-15 16:46:25 -0700382 public static void test_String_compareTo() {
383 String test = "0123456789";
384 String test1 = new String("0123456789"); // different object
385 String test2 = new String("0123456780"); // different value
386 String offset = new String("xxx0123456789yyy");
387 String sub = offset.substring(3, 13);
388 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
389 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
390 String lc = "abcdefg";
391 String uc = "ABCDEFG";
392 Object blah = new Object();
393
394 Assert.assertTrue(lc.toUpperCase().equals(uc));
395
396 Assert.assertEquals(str32.compareTo(str33), -1);
397 Assert.assertEquals(str33.compareTo(str32), 1);
398
399 Assert.assertTrue(test.equals(test));
400 Assert.assertTrue(test.equals(test1));
401 Assert.assertFalse(test.equals(test2));
402
403 Assert.assertEquals(test.compareTo(test1), 0);
404 Assert.assertTrue(test1.compareTo(test2) > 0);
405 Assert.assertTrue(test2.compareTo(test1) < 0);
406
407 // Compare string with a nonzero offset, in left/right side.
408 Assert.assertEquals(test.compareTo(sub), 0);
409 Assert.assertEquals(sub.compareTo(test), 0);
410 Assert.assertTrue(test.equals(sub));
411 Assert.assertTrue(sub.equals(test));
412 // Same base, one is a substring.
413 Assert.assertFalse(offset.equals(sub));
414 Assert.assertFalse(sub.equals(offset));
415 // Wrong class.
416 Assert.assertFalse(test.equals(blah));
417
418 // Null lhs - throw.
419 try {
420 test.compareTo(null);
421 Assert.fail("didn't get expected npe");
422 } catch (NullPointerException npe) {
423 }
424 // Null rhs - okay.
425 Assert.assertFalse(test.equals(null));
426
427 test = test.substring(1);
428 Assert.assertTrue(test.equals("123456789"));
429 Assert.assertFalse(test.equals(test1));
430
431 test = test.substring(1);
432 Assert.assertTrue(test.equals("23456789"));
433
434 test = test.substring(1);
435 Assert.assertTrue(test.equals("3456789"));
436
437 test = test.substring(1);
438 Assert.assertTrue(test.equals("456789"));
439
440 test = test.substring(3,5);
441 Assert.assertTrue(test.equals("78"));
442
443 test = "this/is/a/path";
444 String[] strings = test.split("/");
445 Assert.assertEquals(4, strings.length);
446
447 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
448 Assert.assertEquals("this is a path", test.replace("/", " "));
449 }
450
451 public static void test_Math_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800452 Math.abs(-1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700453 Assert.assertEquals(Math.abs(0), 0);
454 Assert.assertEquals(Math.abs(123), 123);
455 Assert.assertEquals(Math.abs(-123), 123);
456 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
457 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
458 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100459 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700460 }
461
462 public static void test_Math_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800463 Math.abs(-1L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700464 Assert.assertEquals(Math.abs(0L), 0L);
465 Assert.assertEquals(Math.abs(123L), 123L);
466 Assert.assertEquals(Math.abs(-123L), 123L);
467 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
468 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
469 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
Andreas Gampe2bcf9bf2015-01-29 09:56:07 -0800470 Assert.assertEquals(Math.abs(2147483648L), 2147483648L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700471 }
472
Serban Constantinescu23abec92014-07-02 16:13:38 +0100473 public static void test_Math_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800474 Math.min(1, 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700475 Assert.assertEquals(Math.min(0, 0), 0);
476 Assert.assertEquals(Math.min(1, 0), 0);
477 Assert.assertEquals(Math.min(0, 1), 0);
478 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
479 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
480 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
481 }
482
Serban Constantinescu23abec92014-07-02 16:13:38 +0100483 public static void test_Math_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800484 Math.max(1, 0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700485 Assert.assertEquals(Math.max(0, 0), 0);
486 Assert.assertEquals(Math.max(1, 0), 1);
487 Assert.assertEquals(Math.max(0, 1), 1);
488 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
489 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
490 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
491 }
492
Serban Constantinescu23abec92014-07-02 16:13:38 +0100493 public static void test_Math_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800494 Math.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100495 Assert.assertEquals(Math.min(0L, 0L), 0L);
496 Assert.assertEquals(Math.min(1L, 0L), 0L);
497 Assert.assertEquals(Math.min(0L, 1L), 0L);
498 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
499 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
500 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
501 }
502
503 public static void test_Math_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800504 Math.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100505 Assert.assertEquals(Math.max(0L, 0L), 0L);
506 Assert.assertEquals(Math.max(1L, 0L), 1L);
507 Assert.assertEquals(Math.max(0L, 1L), 1L);
508 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
509 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
510 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
511 }
512
513 public static void test_Math_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800514 Math.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700515 Assert.assertTrue(Float.isNaN(Math.min(1.0f, Float.NaN)));
516 Assert.assertTrue(Float.isNaN(Math.min(Float.NaN, 1.0f)));
517 Assert.assertEquals(Math.min(-0.0f, 0.0f), -0.0f);
518 Assert.assertEquals(Math.min(0.0f, -0.0f), -0.0f);
519 Assert.assertEquals(Math.min(-0.0f, -0.0f), -0.0f);
520 Assert.assertEquals(Math.min(0.0f, 0.0f), 0.0f);
521 Assert.assertEquals(Math.min(1.0f, 0.0f), 0.0f);
522 Assert.assertEquals(Math.min(0.0f, 1.0f), 0.0f);
523 Assert.assertEquals(Math.min(0.0f, Float.MAX_VALUE), 0.0f);
524 Assert.assertEquals(Math.min(Float.MIN_VALUE, 0.0f), 0.0f);
525 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100526 }
527
528 public static void test_Math_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800529 Math.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700530 Assert.assertTrue(Float.isNaN(Math.max(1.0f, Float.NaN)));
531 Assert.assertTrue(Float.isNaN(Math.max(Float.NaN, 1.0f)));
532 Assert.assertEquals(Math.max(-0.0f, 0.0f), 0.0f);
533 Assert.assertEquals(Math.max(0.0f, -0.0f), 0.0f);
534 Assert.assertEquals(Math.max(-0.0f, -0.0f), -0.0f);
535 Assert.assertEquals(Math.max(0.0f, 0.0f), 0.0f);
536 Assert.assertEquals(Math.max(1.0f, 0.0f), 1.0f);
537 Assert.assertEquals(Math.max(0.0f, 1.0f), 1.0f);
538 Assert.assertEquals(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
539 Assert.assertEquals(Math.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
540 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100541 }
542
543 public static void test_Math_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800544 Math.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700545 Assert.assertTrue(Double.isNaN(Math.min(1.0d, Double.NaN)));
546 Assert.assertTrue(Double.isNaN(Math.min(Double.NaN, 1.0d)));
547 Assert.assertEquals(Math.min(-0.0d, 0.0d), -0.0d);
548 Assert.assertEquals(Math.min(0.0d, -0.0d), -0.0d);
549 Assert.assertEquals(Math.min(-0.0d, -0.0d), -0.0d);
550 Assert.assertEquals(Math.min(0.0d, 0.0d), 0.0d);
551 Assert.assertEquals(Math.min(1.0d, 0.0d), 0.0d);
552 Assert.assertEquals(Math.min(0.0d, 1.0d), 0.0d);
553 Assert.assertEquals(Math.min(0.0d, Double.MAX_VALUE), 0.0d);
554 Assert.assertEquals(Math.min(Double.MIN_VALUE, 0.0d), 0.0d);
555 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100556 }
557
558 public static void test_Math_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800559 Math.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700560 Assert.assertTrue(Double.isNaN(Math.max(1.0d, Double.NaN)));
561 Assert.assertTrue(Double.isNaN(Math.max(Double.NaN, 1.0d)));
562 Assert.assertEquals(Math.max(-0.0d, 0.0d), 0.0d);
563 Assert.assertEquals(Math.max(0.0d, -0.0d), 0.0d);
564 Assert.assertEquals(Math.max(-0.0d, -0.0d), -0.0d);
565 Assert.assertEquals(Math.max(0.0d, 0.0d), 0.0d);
566 Assert.assertEquals(Math.max(1.0d, 0.0d), 1.0d);
567 Assert.assertEquals(Math.max(0.0d, 1.0d), 1.0d);
568 Assert.assertEquals(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
569 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
570 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100571 }
572
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800573 public static void test_Math_sqrt() {
574 Math.sqrt(+4.0);
575 Assert.assertEquals(Math.sqrt(+4.0), +2.0d, 0.0);
576 Assert.assertEquals(Math.sqrt(+49.0), +7.0d, 0.0);
577 Assert.assertEquals(Math.sqrt(+1.44), +1.2d, 0.0);
578 }
579
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100580 public static void test_Math_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800581 Math.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100582 Assert.assertEquals(Math.ceil(+0.0), +0.0d, 0.0);
583 Assert.assertEquals(Math.ceil(-0.0), -0.0d, 0.0);
584 Assert.assertEquals(Math.ceil(-0.9), -0.0d, 0.0);
585 Assert.assertEquals(Math.ceil(-0.5), -0.0d, 0.0);
586 Assert.assertEquals(Math.ceil(0.0), -0.0d, 0.0);
587 Assert.assertEquals(Math.ceil(+2.0), +2.0d, 0.0);
588 Assert.assertEquals(Math.ceil(+2.1), +3.0d, 0.0);
589 Assert.assertEquals(Math.ceil(+2.5), +3.0d, 0.0);
590 Assert.assertEquals(Math.ceil(+2.9), +3.0d, 0.0);
591 Assert.assertEquals(Math.ceil(+3.0), +3.0d, 0.0);
592 Assert.assertEquals(Math.ceil(-2.0), -2.0d, 0.0);
593 Assert.assertEquals(Math.ceil(-2.1), -2.0d, 0.0);
594 Assert.assertEquals(Math.ceil(-2.5), -2.0d, 0.0);
595 Assert.assertEquals(Math.ceil(-2.9), -2.0d, 0.0);
596 Assert.assertEquals(Math.ceil(-3.0), -3.0d, 0.0);
597 Assert.assertEquals(Math.ceil(Double.NaN), Double.NaN, 0.0);
598 Assert.assertEquals(Math.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
599 Assert.assertEquals(Math.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
600 }
601
602 public static void test_Math_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800603 Math.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100604 Assert.assertEquals(Math.floor(+0.0), +0.0d, 0.0);
605 Assert.assertEquals(Math.floor(-0.0), -0.0d, 0.0);
606 Assert.assertEquals(Math.floor(+2.0), +2.0d, 0.0);
607 Assert.assertEquals(Math.floor(+2.1), +2.0d, 0.0);
608 Assert.assertEquals(Math.floor(+2.5), +2.0d, 0.0);
609 Assert.assertEquals(Math.floor(+2.9), +2.0d, 0.0);
610 Assert.assertEquals(Math.floor(+3.0), +3.0d, 0.0);
611 Assert.assertEquals(Math.floor(-2.0), -2.0d, 0.0);
612 Assert.assertEquals(Math.floor(-2.1), -3.0d, 0.0);
613 Assert.assertEquals(Math.floor(-2.5), -3.0d, 0.0);
614 Assert.assertEquals(Math.floor(-2.9), -3.0d, 0.0);
615 Assert.assertEquals(Math.floor(-3.0), -3.0d, 0.0);
616 Assert.assertEquals(Math.floor(Double.NaN), Double.NaN, 0.0);
617 Assert.assertEquals(Math.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
618 Assert.assertEquals(Math.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
619 }
620
621 public static void test_Math_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800622 Math.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100623 Assert.assertEquals(Math.rint(+0.0), +0.0d, 0.0);
624 Assert.assertEquals(Math.rint(-0.0), -0.0d, 0.0);
625 Assert.assertEquals(Math.rint(+2.0), +2.0d, 0.0);
626 Assert.assertEquals(Math.rint(+2.1), +2.0d, 0.0);
627 Assert.assertEquals(Math.rint(+2.5), +2.0d, 0.0);
628 Assert.assertEquals(Math.rint(+2.9), +3.0d, 0.0);
629 Assert.assertEquals(Math.rint(+3.0), +3.0d, 0.0);
630 Assert.assertEquals(Math.rint(-2.0), -2.0d, 0.0);
631 Assert.assertEquals(Math.rint(-2.1), -2.0d, 0.0);
632 Assert.assertEquals(Math.rint(-2.5), -2.0d, 0.0);
633 Assert.assertEquals(Math.rint(-2.9), -3.0d, 0.0);
634 Assert.assertEquals(Math.rint(-3.0), -3.0d, 0.0);
635 Assert.assertEquals(Math.rint(Double.NaN), Double.NaN, 0.0);
636 Assert.assertEquals(Math.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
637 Assert.assertEquals(Math.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
638 }
639
640 public static void test_Math_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800641 Math.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100642 Assert.assertEquals(Math.round(+0.0d), (long)+0.0);
643 Assert.assertEquals(Math.round(-0.0d), (long)+0.0);
644 Assert.assertEquals(Math.round(2.0d), 2l);
645 Assert.assertEquals(Math.round(2.1d), 2l);
646 Assert.assertEquals(Math.round(2.5d), 3l);
647 Assert.assertEquals(Math.round(2.9d), 3l);
648 Assert.assertEquals(Math.round(3.0d), 3l);
649 Assert.assertEquals(Math.round(-2.0d), -2l);
650 Assert.assertEquals(Math.round(-2.1d), -2l);
651 Assert.assertEquals(Math.round(-2.5d), -2l);
652 Assert.assertEquals(Math.round(-2.9d), -3l);
653 Assert.assertEquals(Math.round(-3.0d), -3l);
654 Assert.assertEquals(Math.round(0.49999999999999994d), 1l);
655 Assert.assertEquals(Math.round(Double.NaN), (long)+0.0d);
656 Assert.assertEquals(Math.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
657 Assert.assertEquals(Math.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
658 Assert.assertEquals(Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
659 Assert.assertEquals(Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
660 }
661
662 public static void test_Math_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800663 Math.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100664 Assert.assertEquals(Math.round(+0.0f), (int)+0.0);
665 Assert.assertEquals(Math.round(-0.0f), (int)+0.0);
666 Assert.assertEquals(Math.round(2.0f), 2);
667 Assert.assertEquals(Math.round(2.1f), 2);
668 Assert.assertEquals(Math.round(2.5f), 3);
669 Assert.assertEquals(Math.round(2.9f), 3);
670 Assert.assertEquals(Math.round(3.0f), 3);
671 Assert.assertEquals(Math.round(-2.0f), -2);
672 Assert.assertEquals(Math.round(-2.1f), -2);
673 Assert.assertEquals(Math.round(-2.5f), -2);
674 Assert.assertEquals(Math.round(-2.9f), -3);
675 Assert.assertEquals(Math.round(-3.0f), -3);
676 Assert.assertEquals(Math.round(Float.NaN), (int)+0.0f);
677 Assert.assertEquals(Math.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
678 Assert.assertEquals(Math.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
679 Assert.assertEquals(Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
680 Assert.assertEquals(Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
681 }
682
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100683 public static void test_StrictMath_abs_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800684 StrictMath.abs(-1);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100685 Assert.assertEquals(StrictMath.abs(0), 0);
686 Assert.assertEquals(StrictMath.abs(123), 123);
687 Assert.assertEquals(StrictMath.abs(-123), 123);
688 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
689 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
690 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
691 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
692 }
693
694 public static void test_StrictMath_abs_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800695 StrictMath.abs(-1L);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100696 Assert.assertEquals(StrictMath.abs(0L), 0L);
697 Assert.assertEquals(StrictMath.abs(123L), 123L);
698 Assert.assertEquals(StrictMath.abs(-123L), 123L);
699 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
700 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
701 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
702 }
703
Serban Constantinescu23abec92014-07-02 16:13:38 +0100704 public static void test_StrictMath_min_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800705 StrictMath.min(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100706 Assert.assertEquals(StrictMath.min(0, 0), 0);
707 Assert.assertEquals(StrictMath.min(1, 0), 0);
708 Assert.assertEquals(StrictMath.min(0, 1), 0);
709 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
710 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
711 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
712 }
713
Serban Constantinescu23abec92014-07-02 16:13:38 +0100714 public static void test_StrictMath_max_I() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800715 StrictMath.max(1, 0);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100716 Assert.assertEquals(StrictMath.max(0, 0), 0);
717 Assert.assertEquals(StrictMath.max(1, 0), 1);
718 Assert.assertEquals(StrictMath.max(0, 1), 1);
719 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
720 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
721 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
722 }
723
Serban Constantinescu23abec92014-07-02 16:13:38 +0100724 public static void test_StrictMath_min_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800725 StrictMath.min(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100726 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
727 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
728 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
729 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
730 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
731 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
732 }
733
734 public static void test_StrictMath_max_J() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800735 StrictMath.max(1L, 0L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100736 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
737 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
738 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
739 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
740 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
741 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
742 }
743
744 public static void test_StrictMath_min_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800745 StrictMath.min(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700746 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN)));
747 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f)));
748 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f);
749 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f);
750 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f);
751 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f);
752 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f);
753 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f);
754 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f);
755 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f);
756 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100757 }
758
759 public static void test_StrictMath_max_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800760 StrictMath.max(1.0f, Float.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700761 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN)));
762 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f)));
763 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f);
764 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f);
765 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f);
766 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f);
767 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f);
768 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f);
769 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
770 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
771 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100772 }
773
774 public static void test_StrictMath_min_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800775 StrictMath.min(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700776 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN)));
777 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d)));
778 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d);
779 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d);
780 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d);
781 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d);
782 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d);
783 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d);
784 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d);
785 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d);
786 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100787 }
788
789 public static void test_StrictMath_max_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800790 StrictMath.max(1.0d, Double.NaN);
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700791 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN)));
792 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d)));
793 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d);
794 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d);
795 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d);
796 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d);
797 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d);
798 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d);
799 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
800 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
801 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100802 }
803
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800804 public static void test_StrictMath_sqrt() {
805 StrictMath.sqrt(+4.0);
806 Assert.assertEquals(StrictMath.sqrt(+4.0), +2.0d, 0.0);
807 Assert.assertEquals(StrictMath.sqrt(+49.0), +7.0d, 0.0);
808 Assert.assertEquals(StrictMath.sqrt(+1.44), +1.2d, 0.0);
809 }
810
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100811 public static void test_StrictMath_ceil() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800812 StrictMath.ceil(-0.9);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100813 Assert.assertEquals(StrictMath.ceil(+0.0), +0.0d, 0.0);
814 Assert.assertEquals(StrictMath.ceil(-0.0), -0.0d, 0.0);
815 Assert.assertEquals(StrictMath.ceil(-0.9), -0.0d, 0.0);
816 Assert.assertEquals(StrictMath.ceil(-0.5), -0.0d, 0.0);
817 Assert.assertEquals(StrictMath.ceil(0.0), -0.0d, 0.0);
818 Assert.assertEquals(StrictMath.ceil(+2.0), +2.0d, 0.0);
819 Assert.assertEquals(StrictMath.ceil(+2.1), +3.0d, 0.0);
820 Assert.assertEquals(StrictMath.ceil(+2.5), +3.0d, 0.0);
821 Assert.assertEquals(StrictMath.ceil(+2.9), +3.0d, 0.0);
822 Assert.assertEquals(StrictMath.ceil(+3.0), +3.0d, 0.0);
823 Assert.assertEquals(StrictMath.ceil(-2.0), -2.0d, 0.0);
824 Assert.assertEquals(StrictMath.ceil(-2.1), -2.0d, 0.0);
825 Assert.assertEquals(StrictMath.ceil(-2.5), -2.0d, 0.0);
826 Assert.assertEquals(StrictMath.ceil(-2.9), -2.0d, 0.0);
827 Assert.assertEquals(StrictMath.ceil(-3.0), -3.0d, 0.0);
828 Assert.assertEquals(StrictMath.ceil(Double.NaN), Double.NaN, 0.0);
829 Assert.assertEquals(StrictMath.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
830 Assert.assertEquals(StrictMath.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
831 }
832
833 public static void test_StrictMath_floor() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800834 StrictMath.floor(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100835 Assert.assertEquals(StrictMath.floor(+0.0), +0.0d, 0.0);
836 Assert.assertEquals(StrictMath.floor(-0.0), -0.0d, 0.0);
837 Assert.assertEquals(StrictMath.floor(+2.0), +2.0d, 0.0);
838 Assert.assertEquals(StrictMath.floor(+2.1), +2.0d, 0.0);
839 Assert.assertEquals(StrictMath.floor(+2.5), +2.0d, 0.0);
840 Assert.assertEquals(StrictMath.floor(+2.9), +2.0d, 0.0);
841 Assert.assertEquals(StrictMath.floor(+3.0), +3.0d, 0.0);
842 Assert.assertEquals(StrictMath.floor(-2.0), -2.0d, 0.0);
843 Assert.assertEquals(StrictMath.floor(-2.1), -3.0d, 0.0);
844 Assert.assertEquals(StrictMath.floor(-2.5), -3.0d, 0.0);
845 Assert.assertEquals(StrictMath.floor(-2.9), -3.0d, 0.0);
846 Assert.assertEquals(StrictMath.floor(-3.0), -3.0d, 0.0);
847 Assert.assertEquals(StrictMath.floor(Double.NaN), Double.NaN, 0.0);
848 Assert.assertEquals(StrictMath.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
849 Assert.assertEquals(StrictMath.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
850 }
851
852 public static void test_StrictMath_rint() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800853 StrictMath.rint(+2.1);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100854 Assert.assertEquals(StrictMath.rint(+0.0), +0.0d, 0.0);
855 Assert.assertEquals(StrictMath.rint(-0.0), -0.0d, 0.0);
856 Assert.assertEquals(StrictMath.rint(+2.0), +2.0d, 0.0);
857 Assert.assertEquals(StrictMath.rint(+2.1), +2.0d, 0.0);
858 Assert.assertEquals(StrictMath.rint(+2.5), +2.0d, 0.0);
859 Assert.assertEquals(StrictMath.rint(+2.9), +3.0d, 0.0);
860 Assert.assertEquals(StrictMath.rint(+3.0), +3.0d, 0.0);
861 Assert.assertEquals(StrictMath.rint(-2.0), -2.0d, 0.0);
862 Assert.assertEquals(StrictMath.rint(-2.1), -2.0d, 0.0);
863 Assert.assertEquals(StrictMath.rint(-2.5), -2.0d, 0.0);
864 Assert.assertEquals(StrictMath.rint(-2.9), -3.0d, 0.0);
865 Assert.assertEquals(StrictMath.rint(-3.0), -3.0d, 0.0);
866 Assert.assertEquals(StrictMath.rint(Double.NaN), Double.NaN, 0.0);
867 Assert.assertEquals(StrictMath.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
868 Assert.assertEquals(StrictMath.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
869 }
870
871 public static void test_StrictMath_round_D() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800872 StrictMath.round(2.1d);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100873 Assert.assertEquals(StrictMath.round(+0.0d), (long)+0.0);
874 Assert.assertEquals(StrictMath.round(-0.0d), (long)+0.0);
875 Assert.assertEquals(StrictMath.round(2.0d), 2l);
876 Assert.assertEquals(StrictMath.round(2.1d), 2l);
877 Assert.assertEquals(StrictMath.round(2.5d), 3l);
878 Assert.assertEquals(StrictMath.round(2.9d), 3l);
879 Assert.assertEquals(StrictMath.round(3.0d), 3l);
880 Assert.assertEquals(StrictMath.round(-2.0d), -2l);
881 Assert.assertEquals(StrictMath.round(-2.1d), -2l);
882 Assert.assertEquals(StrictMath.round(-2.5d), -2l);
883 Assert.assertEquals(StrictMath.round(-2.9d), -3l);
884 Assert.assertEquals(StrictMath.round(-3.0d), -3l);
885 Assert.assertEquals(StrictMath.round(0.49999999999999994d), 1l);
886 Assert.assertEquals(StrictMath.round(Double.NaN), (long)+0.0d);
887 Assert.assertEquals(StrictMath.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
888 Assert.assertEquals(StrictMath.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
889 Assert.assertEquals(StrictMath.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
890 Assert.assertEquals(StrictMath.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
891 }
892
893 public static void test_StrictMath_round_F() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800894 StrictMath.round(2.1f);
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100895 Assert.assertEquals(StrictMath.round(+0.0f), (int)+0.0);
896 Assert.assertEquals(StrictMath.round(-0.0f), (int)+0.0);
897 Assert.assertEquals(StrictMath.round(2.0f), 2);
898 Assert.assertEquals(StrictMath.round(2.1f), 2);
899 Assert.assertEquals(StrictMath.round(2.5f), 3);
900 Assert.assertEquals(StrictMath.round(2.9f), 3);
901 Assert.assertEquals(StrictMath.round(3.0f), 3);
902 Assert.assertEquals(StrictMath.round(-2.0f), -2);
903 Assert.assertEquals(StrictMath.round(-2.1f), -2);
904 Assert.assertEquals(StrictMath.round(-2.5f), -2);
905 Assert.assertEquals(StrictMath.round(-2.9f), -3);
906 Assert.assertEquals(StrictMath.round(-3.0f), -3);
907 Assert.assertEquals(StrictMath.round(Float.NaN), (int)+0.0f);
908 Assert.assertEquals(StrictMath.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
909 Assert.assertEquals(StrictMath.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
910 Assert.assertEquals(StrictMath.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
911 Assert.assertEquals(StrictMath.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
912 }
913
Elliott Hughes28c384b2012-06-15 16:46:25 -0700914 public static void test_Float_floatToRawIntBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800915 Float.floatToRawIntBits(-1.0f);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700916 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
917 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
918 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
919 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
920 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
921 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
922 }
923
924 public static void test_Float_intBitsToFloat() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800925 Float.intBitsToFloat(0xbf800000);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700926 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
927 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
928 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
929 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
930 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
931 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
932 }
933
934 public static void test_Double_doubleToRawLongBits() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800935 Double.doubleToRawLongBits(-1.0);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700936 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
937 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
938 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
939 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
940 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
941 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
942 }
943
944 public static void test_Double_longBitsToDouble() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800945 Double.longBitsToDouble(0xbff0000000000000L);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700946 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
947 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
948 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
949 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
950 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
951 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
952 }
Serban Constantinescu23abec92014-07-02 16:13:38 +0100953
Zheng Xua3fe7422014-07-09 14:03:15 +0800954 public static void test_Short_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800955 Short.reverseBytes((short)0x1357);
Zheng Xua3fe7422014-07-09 14:03:15 +0800956 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
957 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
958 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
959 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
960 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
961 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
962 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
963 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
964 }
965
966 public static void test_Integer_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800967 Integer.reverseBytes(0x13579bdf);
Zheng Xua3fe7422014-07-09 14:03:15 +0800968 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
969 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
970 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
971 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
972 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
973 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
974 }
975
976 public static void test_Long_reverseBytes() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800977 Long.reverseBytes(0x13579bdf2468ace0L);
Zheng Xua3fe7422014-07-09 14:03:15 +0800978 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
979 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
980 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
981 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
982 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
983 }
984
Serban Constantinescu23abec92014-07-02 16:13:38 +0100985 public static void test_Integer_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800986 Integer.reverse(0x12345678);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100987 Assert.assertEquals(Integer.reverse(1), 0x80000000);
988 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
989 Assert.assertEquals(Integer.reverse(0), 0);
990 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
991 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
992 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
993 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
994 }
995
996 public static void test_Long_reverse() {
Chao-ying Fuff87d7b2015-01-19 15:51:57 -0800997 Long.reverse(0x1234567812345678L);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100998 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
999 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
1000 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +08001001 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
1002 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
1003 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +01001004 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
Andreas Gampe575422f2015-07-07 13:25:58 -07001005
1006 Assert.assertEquals(test_Long_reverse_b22324327(0xaaaaaaaaaaaaaaaaL, 0x5555555555555555L),
1007 157472205507277347L);
1008 }
1009
1010 // A bit more complicated than the above. Use local variables to stress register allocation.
1011 private static long test_Long_reverse_b22324327(long l1, long l2) {
1012 // A couple of local integers. Use them in a loop, so they get promoted.
1013 int i1 = 0, i2 = 1, i3 = 2, i4 = 3, i5 = 4, i6 = 5, i7 = 6, i8 = 7;
1014 for (int k = 0; k < 10; k++) {
1015 i1 += 1;
1016 i2 += 2;
1017 i3 += 3;
1018 i4 += 4;
1019 i5 += 5;
1020 i6 += 6;
1021 i7 += 7;
1022 i8 += 8;
1023 }
1024
1025 // Do the Long.reverse() calls, save the results.
1026 long r1 = Long.reverse(l1);
1027 long r2 = Long.reverse(l2);
1028
1029 // Some more looping with the ints.
1030 for (int k = 0; k < 10; k++) {
1031 i1 += 1;
1032 i2 += 2;
1033 i3 += 3;
1034 i4 += 4;
1035 i5 += 5;
1036 i6 += 6;
1037 i7 += 7;
1038 i8 += 8;
1039 }
1040
1041 // Include everything in the result, so things are kept live. Try to be a little bit clever to
1042 // avoid things being folded somewhere.
1043 return (r1 / i1) + (r2 / i2) + i3 + i4 + i5 + i6 + i7 + i8;
Serban Constantinescu23abec92014-07-02 16:13:38 +01001044 }
1045
Scott Wakeling611d3392015-07-10 11:42:06 +01001046 public static void test_Integer_numberOfLeadingZeros() {
1047 Assert.assertEquals(Integer.numberOfLeadingZeros(0), Integer.SIZE);
1048 for (int i = 0; i < Integer.SIZE; i++) {
1049 Assert.assertEquals(Integer.numberOfLeadingZeros(1 << i), Integer.SIZE - 1 - i);
1050 Assert.assertEquals(Integer.numberOfLeadingZeros((1 << i) | 1), Integer.SIZE - 1 - i);
1051 Assert.assertEquals(Integer.numberOfLeadingZeros(0xFFFFFFFF >>> i), i);
1052 }
1053 }
1054
1055 public static void test_Long_numberOfLeadingZeros() {
1056 Assert.assertEquals(Long.numberOfLeadingZeros(0L), Long.SIZE);
1057 for (int i = 0; i < Long.SIZE; i++) {
1058 Assert.assertEquals(Long.numberOfLeadingZeros(1L << i), Long.SIZE - 1 - i);
1059 Assert.assertEquals(Long.numberOfLeadingZeros((1L << i) | 1L), Long.SIZE - 1 - i);
1060 Assert.assertEquals(Long.numberOfLeadingZeros(0xFFFFFFFFFFFFFFFFL >>> i), i);
1061 }
1062 }
1063
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001064 static Object runtime;
1065 static Method address_of;
Zuo Wangf37a88b2014-07-10 04:26:41 -07001066 static Method new_non_movable_array;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001067 static Method peek_byte;
1068 static Method peek_short;
1069 static Method peek_int;
1070 static Method peek_long;
1071 static Method poke_byte;
1072 static Method poke_short;
1073 static Method poke_int;
1074 static Method poke_long;
1075
1076 public static void initSupportMethodsForPeekPoke() throws Exception {
1077 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
1078 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
1079 runtime = get_runtime.invoke(null);
1080 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
Zuo Wangf37a88b2014-07-10 04:26:41 -07001081 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001082
1083 Class<?> io_memory = Class.forName("libcore.io.Memory");
1084 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
1085 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
1086 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
1087 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
1088 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
1089 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
1090 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
1091 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
1092 }
1093
1094 public static void test_Memory_peekByte() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001095 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001096 b[0] = 0x12;
1097 b[1] = 0x11;
1098 long address = (long)address_of.invoke(runtime, b);
1099 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
1100 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
1101 }
1102
1103 public static void test_Memory_peekShort() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001104 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001105 b[0] = 0x13;
1106 b[1] = 0x12;
1107 b[2] = 0x11;
1108 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001109 peek_short.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001110 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
1111 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
1112 }
1113
1114 public static void test_Memory_peekInt() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001115 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001116 b[0] = 0x15;
1117 b[1] = 0x14;
1118 b[2] = 0x13;
1119 b[3] = 0x12;
1120 b[4] = 0x11;
1121 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001122 peek_int.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001123 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
1124 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
1125 }
1126
1127 public static void test_Memory_peekLong() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -07001128 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001129 b[0] = 0x19;
1130 b[1] = 0x18;
1131 b[2] = 0x17;
1132 b[3] = 0x16;
1133 b[4] = 0x15;
1134 b[5] = 0x14;
1135 b[6] = 0x13;
1136 b[7] = 0x12;
1137 b[8] = 0x11;
1138 long address = (long)address_of.invoke(runtime, b);
Chao-ying Fuff87d7b2015-01-19 15:51:57 -08001139 peek_long.invoke(null, address, false);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001140 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
1141 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
1142 }
1143
1144 public static void test_Memory_pokeByte() throws Exception {
1145 byte[] r = {0x11, 0x12};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001146 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001147 long address = (long)address_of.invoke(runtime, b);
1148 poke_byte.invoke(null, address, (byte)0x11);
1149 poke_byte.invoke(null, address + 1, (byte)0x12);
1150 Assert.assertTrue(Arrays.equals(r, b));
1151 }
1152
1153 public static void test_Memory_pokeShort() throws Exception {
1154 byte[] ra = {0x12, 0x11, 0x13};
1155 byte[] ru = {0x12, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001156 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001157 long address = (long)address_of.invoke(runtime, b);
1158
1159 // Aligned write
1160 b[2] = 0x13;
1161 poke_short.invoke(null, address, (short)0x1112, false);
1162 Assert.assertTrue(Arrays.equals(ra, b));
1163
1164 // Unaligned write
1165 poke_short.invoke(null, address + 1, (short)0x2122, false);
1166 Assert.assertTrue(Arrays.equals(ru, b));
1167 }
1168
1169 public static void test_Memory_pokeInt() throws Exception {
1170 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
1171 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001172 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001173 long address = (long)address_of.invoke(runtime, b);
1174
1175 b[4] = 0x15;
1176 poke_int.invoke(null, address, (int)0x11121314, false);
1177 Assert.assertTrue(Arrays.equals(ra, b));
1178
1179 poke_int.invoke(null, address + 1, (int)0x21222324, false);
1180 Assert.assertTrue(Arrays.equals(ru, b));
1181 }
1182
1183 public static void test_Memory_pokeLong() throws Exception {
1184 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
1185 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -07001186 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +07001187 long address = (long)address_of.invoke(runtime, b);
1188
1189 b[8] = 0x19;
1190 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
1191 Assert.assertTrue(Arrays.equals(ra, b));
1192
1193 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
1194 Assert.assertTrue(Arrays.equals(ru, b));
1195 }
jeffhao5d1ac922011-09-29 17:41:15 -07001196}