blob: 862fe066ce5bce2130c0aff446ab6dfdae035844 [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();
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010037 test_Math_ceil();
38 test_Math_floor();
39 test_Math_rint();
40 test_Math_round_D();
41 test_Math_round_F();
Zheng Xua3fe7422014-07-09 14:03:15 +080042 test_Short_reverseBytes();
43 test_Integer_reverseBytes();
44 test_Long_reverseBytes();
Serban Constantinescu23abec92014-07-02 16:13:38 +010045 test_Integer_reverse();
46 test_Long_reverse();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010047 test_StrictMath_abs_I();
48 test_StrictMath_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010049 test_StrictMath_min_I();
50 test_StrictMath_max_I();
51 test_StrictMath_min_J();
52 test_StrictMath_max_J();
53 test_StrictMath_min_F();
54 test_StrictMath_max_F();
55 test_StrictMath_min_D();
56 test_StrictMath_max_D();
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +010057 test_StrictMath_ceil();
58 test_StrictMath_floor();
59 test_StrictMath_rint();
60 test_StrictMath_round_D();
61 test_StrictMath_round_F();
Elliott Hughes28c384b2012-06-15 16:46:25 -070062 test_String_charAt();
63 test_String_compareTo();
64 test_String_indexOf();
65 test_String_isEmpty();
66 test_String_length();
Andreas Gampe7a949612014-07-08 11:03:59 -070067 test_Thread_currentThread();
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070068 initSupportMethodsForPeekPoke();
69 test_Memory_peekByte();
70 test_Memory_peekShort();
71 test_Memory_peekInt();
72 test_Memory_peekLong();
73 test_Memory_pokeByte();
74 test_Memory_pokeShort();
75 test_Memory_pokeInt();
76 test_Memory_pokeLong();
Elliott Hughes28c384b2012-06-15 16:46:25 -070077 }
78
Andreas Gampe7a949612014-07-08 11:03:59 -070079 /**
80 * Will test inlining Thread.currentThread().
81 */
82 public static void test_Thread_currentThread() {
83 // 1. Do not use result.
84 Thread.currentThread();
85
86 // 2. Result should not be null.
87 Assert.assertNotNull(Thread.currentThread());
88 }
89
Elliott Hughes28c384b2012-06-15 16:46:25 -070090 public static void test_String_length() {
91 String str0 = "";
92 String str1 = "x";
93 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
94
95 Assert.assertEquals(str0.length(), 0);
96 Assert.assertEquals(str1.length(), 1);
97 Assert.assertEquals(str80.length(), 80);
98
99 String strNull = null;
100 try {
101 strNull.length();
102 Assert.fail();
103 } catch (NullPointerException expected) {
104 }
105 }
106
107 public static void test_String_isEmpty() {
108 String str0 = "";
109 String str1 = "x";
110
111 Assert.assertTrue(str0.isEmpty());
112 Assert.assertFalse(str1.isEmpty());
113
114 String strNull = null;
115 try {
116 strNull.isEmpty();
117 Assert.fail();
118 } catch (NullPointerException expected) {
119 }
120 }
121
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800122 // Break up the charAt tests. The optimizing compiler doesn't optimize methods with try-catch yet,
123 // so we need to separate out the tests that are expected to throw exception
124
Elliott Hughes28c384b2012-06-15 16:46:25 -0700125 public static void test_String_charAt() {
126 String testStr = "Now is the time";
127
128 Assert.assertEquals('N', testStr.charAt(0));
129 Assert.assertEquals('o', testStr.charAt(1));
130 Assert.assertEquals(' ', testStr.charAt(10));
131 Assert.assertEquals('e', testStr.charAt(testStr.length()-1));
132
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800133 test_String_charAtExc();
134 test_String_charAtExc2();
135 }
136
137 private static void test_String_charAtExc() {
138 String testStr = "Now is the time";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700139 try {
140 testStr.charAt(-1);
141 Assert.fail();
142 } catch (StringIndexOutOfBoundsException expected) {
143 }
144 try {
145 testStr.charAt(80);
146 Assert.fail();
147 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700148 }
149
Elliott Hughes28c384b2012-06-15 16:46:25 -0700150 String strNull = null;
151 try {
152 strNull.charAt(0);
153 Assert.fail();
154 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700155 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700156 }
jeffhao5d1ac922011-09-29 17:41:15 -0700157
Andreas Gampe71fb52f2014-12-29 17:43:08 -0800158 private static void test_String_charAtExc2() {
159 try {
160 test_String_charAtExc3();
161 Assert.fail();
162 } catch (StringIndexOutOfBoundsException expected) {
163 }
164 }
165
166 private static void test_String_charAtExc3() {
167 String testStr = "Now is the time";
168 Assert.assertEquals('N', testStr.charAt(-1));
169 }
170
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700171 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700172 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700173 public static void test_String_indexOf() {
174 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700175 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700176 String str3 = "abc";
177 String str10 = "abcdefghij";
178 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700179
Elliott Hughes28c384b2012-06-15 16:46:25 -0700180 int supplementaryChar = 0x20b9f;
181 String surrogatePair = "\ud842\udf9f";
182 String stringWithSurrogates = "hello " + surrogatePair + " world";
jeffhao5d1ac922011-09-29 17:41:15 -0700183
Elliott Hughes28c384b2012-06-15 16:46:25 -0700184 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
185 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
186 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
187 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
jeffhao5d1ac922011-09-29 17:41:15 -0700188
Elliott Hughes28c384b2012-06-15 16:46:25 -0700189 Assert.assertEquals(str0.indexOf('a'), -1);
190 Assert.assertEquals(str3.indexOf('a'), 0);
191 Assert.assertEquals(str3.indexOf('b'), 1);
192 Assert.assertEquals(str3.indexOf('c'), 2);
193 Assert.assertEquals(str10.indexOf('j'), 9);
194 Assert.assertEquals(str40.indexOf('a'), 0);
195 Assert.assertEquals(str40.indexOf('b'), 38);
196 Assert.assertEquals(str40.indexOf('c'), 39);
197 Assert.assertEquals(str0.indexOf('a',20), -1);
198 Assert.assertEquals(str0.indexOf('a',0), -1);
199 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700200 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700201 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700202 Assert.assertEquals(str3.indexOf('a',0), 0);
203 Assert.assertEquals(str3.indexOf('a',1), -1);
204 Assert.assertEquals(str3.indexOf('a',1234), -1);
205 Assert.assertEquals(str3.indexOf('b',0), 1);
206 Assert.assertEquals(str3.indexOf('b',1), 1);
207 Assert.assertEquals(str3.indexOf('c',2), 2);
208 Assert.assertEquals(str10.indexOf('j',5), 9);
209 Assert.assertEquals(str10.indexOf('j',9), 9);
210 Assert.assertEquals(str40.indexOf('a',10), 10);
211 Assert.assertEquals(str40.indexOf('b',40), -1);
212
213 String strNull = null;
214 try {
215 strNull.indexOf('a');
216 Assert.fail();
217 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700218 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700219 try {
220 strNull.indexOf('a', 0);
221 Assert.fail();
222 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700223 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700224 try {
225 strNull.indexOf('a', -1);
226 Assert.fail();
227 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700228 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700229 }
jeffhao5d1ac922011-09-29 17:41:15 -0700230
Elliott Hughes28c384b2012-06-15 16:46:25 -0700231 public static void test_String_compareTo() {
232 String test = "0123456789";
233 String test1 = new String("0123456789"); // different object
234 String test2 = new String("0123456780"); // different value
235 String offset = new String("xxx0123456789yyy");
236 String sub = offset.substring(3, 13);
237 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
238 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
239 String lc = "abcdefg";
240 String uc = "ABCDEFG";
241 Object blah = new Object();
242
243 Assert.assertTrue(lc.toUpperCase().equals(uc));
244
245 Assert.assertEquals(str32.compareTo(str33), -1);
246 Assert.assertEquals(str33.compareTo(str32), 1);
247
248 Assert.assertTrue(test.equals(test));
249 Assert.assertTrue(test.equals(test1));
250 Assert.assertFalse(test.equals(test2));
251
252 Assert.assertEquals(test.compareTo(test1), 0);
253 Assert.assertTrue(test1.compareTo(test2) > 0);
254 Assert.assertTrue(test2.compareTo(test1) < 0);
255
256 // Compare string with a nonzero offset, in left/right side.
257 Assert.assertEquals(test.compareTo(sub), 0);
258 Assert.assertEquals(sub.compareTo(test), 0);
259 Assert.assertTrue(test.equals(sub));
260 Assert.assertTrue(sub.equals(test));
261 // Same base, one is a substring.
262 Assert.assertFalse(offset.equals(sub));
263 Assert.assertFalse(sub.equals(offset));
264 // Wrong class.
265 Assert.assertFalse(test.equals(blah));
266
267 // Null lhs - throw.
268 try {
269 test.compareTo(null);
270 Assert.fail("didn't get expected npe");
271 } catch (NullPointerException npe) {
272 }
273 // Null rhs - okay.
274 Assert.assertFalse(test.equals(null));
275
276 test = test.substring(1);
277 Assert.assertTrue(test.equals("123456789"));
278 Assert.assertFalse(test.equals(test1));
279
280 test = test.substring(1);
281 Assert.assertTrue(test.equals("23456789"));
282
283 test = test.substring(1);
284 Assert.assertTrue(test.equals("3456789"));
285
286 test = test.substring(1);
287 Assert.assertTrue(test.equals("456789"));
288
289 test = test.substring(3,5);
290 Assert.assertTrue(test.equals("78"));
291
292 test = "this/is/a/path";
293 String[] strings = test.split("/");
294 Assert.assertEquals(4, strings.length);
295
296 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
297 Assert.assertEquals("this is a path", test.replace("/", " "));
298 }
299
300 public static void test_Math_abs_I() {
301 Assert.assertEquals(Math.abs(0), 0);
302 Assert.assertEquals(Math.abs(123), 123);
303 Assert.assertEquals(Math.abs(-123), 123);
304 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
305 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
306 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100307 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700308 }
309
310 public static void test_Math_abs_J() {
311 Assert.assertEquals(Math.abs(0L), 0L);
312 Assert.assertEquals(Math.abs(123L), 123L);
313 Assert.assertEquals(Math.abs(-123L), 123L);
314 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
315 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
316 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
317 }
318
Serban Constantinescu23abec92014-07-02 16:13:38 +0100319 public static void test_Math_min_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700320 Assert.assertEquals(Math.min(0, 0), 0);
321 Assert.assertEquals(Math.min(1, 0), 0);
322 Assert.assertEquals(Math.min(0, 1), 0);
323 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
324 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
325 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
326 }
327
Serban Constantinescu23abec92014-07-02 16:13:38 +0100328 public static void test_Math_max_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700329 Assert.assertEquals(Math.max(0, 0), 0);
330 Assert.assertEquals(Math.max(1, 0), 1);
331 Assert.assertEquals(Math.max(0, 1), 1);
332 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
333 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
334 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
335 }
336
Serban Constantinescu23abec92014-07-02 16:13:38 +0100337 public static void test_Math_min_J() {
338 Assert.assertEquals(Math.min(0L, 0L), 0L);
339 Assert.assertEquals(Math.min(1L, 0L), 0L);
340 Assert.assertEquals(Math.min(0L, 1L), 0L);
341 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
342 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
343 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
344 }
345
346 public static void test_Math_max_J() {
347 Assert.assertEquals(Math.max(0L, 0L), 0L);
348 Assert.assertEquals(Math.max(1L, 0L), 1L);
349 Assert.assertEquals(Math.max(0L, 1L), 1L);
350 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
351 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
352 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
353 }
354
355 public static void test_Math_min_F() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700356 Assert.assertTrue(Float.isNaN(Math.min(1.0f, Float.NaN)));
357 Assert.assertTrue(Float.isNaN(Math.min(Float.NaN, 1.0f)));
358 Assert.assertEquals(Math.min(-0.0f, 0.0f), -0.0f);
359 Assert.assertEquals(Math.min(0.0f, -0.0f), -0.0f);
360 Assert.assertEquals(Math.min(-0.0f, -0.0f), -0.0f);
361 Assert.assertEquals(Math.min(0.0f, 0.0f), 0.0f);
362 Assert.assertEquals(Math.min(1.0f, 0.0f), 0.0f);
363 Assert.assertEquals(Math.min(0.0f, 1.0f), 0.0f);
364 Assert.assertEquals(Math.min(0.0f, Float.MAX_VALUE), 0.0f);
365 Assert.assertEquals(Math.min(Float.MIN_VALUE, 0.0f), 0.0f);
366 Assert.assertEquals(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100367 }
368
369 public static void test_Math_max_F() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700370 Assert.assertTrue(Float.isNaN(Math.max(1.0f, Float.NaN)));
371 Assert.assertTrue(Float.isNaN(Math.max(Float.NaN, 1.0f)));
372 Assert.assertEquals(Math.max(-0.0f, 0.0f), 0.0f);
373 Assert.assertEquals(Math.max(0.0f, -0.0f), 0.0f);
374 Assert.assertEquals(Math.max(-0.0f, -0.0f), -0.0f);
375 Assert.assertEquals(Math.max(0.0f, 0.0f), 0.0f);
376 Assert.assertEquals(Math.max(1.0f, 0.0f), 1.0f);
377 Assert.assertEquals(Math.max(0.0f, 1.0f), 1.0f);
378 Assert.assertEquals(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
379 Assert.assertEquals(Math.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
380 Assert.assertEquals(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100381 }
382
383 public static void test_Math_min_D() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700384 Assert.assertTrue(Double.isNaN(Math.min(1.0d, Double.NaN)));
385 Assert.assertTrue(Double.isNaN(Math.min(Double.NaN, 1.0d)));
386 Assert.assertEquals(Math.min(-0.0d, 0.0d), -0.0d);
387 Assert.assertEquals(Math.min(0.0d, -0.0d), -0.0d);
388 Assert.assertEquals(Math.min(-0.0d, -0.0d), -0.0d);
389 Assert.assertEquals(Math.min(0.0d, 0.0d), 0.0d);
390 Assert.assertEquals(Math.min(1.0d, 0.0d), 0.0d);
391 Assert.assertEquals(Math.min(0.0d, 1.0d), 0.0d);
392 Assert.assertEquals(Math.min(0.0d, Double.MAX_VALUE), 0.0d);
393 Assert.assertEquals(Math.min(Double.MIN_VALUE, 0.0d), 0.0d);
394 Assert.assertEquals(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100395 }
396
397 public static void test_Math_max_D() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700398 Assert.assertTrue(Double.isNaN(Math.max(1.0d, Double.NaN)));
399 Assert.assertTrue(Double.isNaN(Math.max(Double.NaN, 1.0d)));
400 Assert.assertEquals(Math.max(-0.0d, 0.0d), 0.0d);
401 Assert.assertEquals(Math.max(0.0d, -0.0d), 0.0d);
402 Assert.assertEquals(Math.max(-0.0d, -0.0d), -0.0d);
403 Assert.assertEquals(Math.max(0.0d, 0.0d), 0.0d);
404 Assert.assertEquals(Math.max(1.0d, 0.0d), 1.0d);
405 Assert.assertEquals(Math.max(0.0d, 1.0d), 1.0d);
406 Assert.assertEquals(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
407 Assert.assertEquals(Math.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
408 Assert.assertEquals(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100409 }
410
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100411 public static void test_Math_ceil() {
412 Assert.assertEquals(Math.ceil(+0.0), +0.0d, 0.0);
413 Assert.assertEquals(Math.ceil(-0.0), -0.0d, 0.0);
414 Assert.assertEquals(Math.ceil(-0.9), -0.0d, 0.0);
415 Assert.assertEquals(Math.ceil(-0.5), -0.0d, 0.0);
416 Assert.assertEquals(Math.ceil(0.0), -0.0d, 0.0);
417 Assert.assertEquals(Math.ceil(+2.0), +2.0d, 0.0);
418 Assert.assertEquals(Math.ceil(+2.1), +3.0d, 0.0);
419 Assert.assertEquals(Math.ceil(+2.5), +3.0d, 0.0);
420 Assert.assertEquals(Math.ceil(+2.9), +3.0d, 0.0);
421 Assert.assertEquals(Math.ceil(+3.0), +3.0d, 0.0);
422 Assert.assertEquals(Math.ceil(-2.0), -2.0d, 0.0);
423 Assert.assertEquals(Math.ceil(-2.1), -2.0d, 0.0);
424 Assert.assertEquals(Math.ceil(-2.5), -2.0d, 0.0);
425 Assert.assertEquals(Math.ceil(-2.9), -2.0d, 0.0);
426 Assert.assertEquals(Math.ceil(-3.0), -3.0d, 0.0);
427 Assert.assertEquals(Math.ceil(Double.NaN), Double.NaN, 0.0);
428 Assert.assertEquals(Math.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
429 Assert.assertEquals(Math.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
430 }
431
432 public static void test_Math_floor() {
433 Assert.assertEquals(Math.floor(+0.0), +0.0d, 0.0);
434 Assert.assertEquals(Math.floor(-0.0), -0.0d, 0.0);
435 Assert.assertEquals(Math.floor(+2.0), +2.0d, 0.0);
436 Assert.assertEquals(Math.floor(+2.1), +2.0d, 0.0);
437 Assert.assertEquals(Math.floor(+2.5), +2.0d, 0.0);
438 Assert.assertEquals(Math.floor(+2.9), +2.0d, 0.0);
439 Assert.assertEquals(Math.floor(+3.0), +3.0d, 0.0);
440 Assert.assertEquals(Math.floor(-2.0), -2.0d, 0.0);
441 Assert.assertEquals(Math.floor(-2.1), -3.0d, 0.0);
442 Assert.assertEquals(Math.floor(-2.5), -3.0d, 0.0);
443 Assert.assertEquals(Math.floor(-2.9), -3.0d, 0.0);
444 Assert.assertEquals(Math.floor(-3.0), -3.0d, 0.0);
445 Assert.assertEquals(Math.floor(Double.NaN), Double.NaN, 0.0);
446 Assert.assertEquals(Math.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
447 Assert.assertEquals(Math.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
448 }
449
450 public static void test_Math_rint() {
451 Assert.assertEquals(Math.rint(+0.0), +0.0d, 0.0);
452 Assert.assertEquals(Math.rint(-0.0), -0.0d, 0.0);
453 Assert.assertEquals(Math.rint(+2.0), +2.0d, 0.0);
454 Assert.assertEquals(Math.rint(+2.1), +2.0d, 0.0);
455 Assert.assertEquals(Math.rint(+2.5), +2.0d, 0.0);
456 Assert.assertEquals(Math.rint(+2.9), +3.0d, 0.0);
457 Assert.assertEquals(Math.rint(+3.0), +3.0d, 0.0);
458 Assert.assertEquals(Math.rint(-2.0), -2.0d, 0.0);
459 Assert.assertEquals(Math.rint(-2.1), -2.0d, 0.0);
460 Assert.assertEquals(Math.rint(-2.5), -2.0d, 0.0);
461 Assert.assertEquals(Math.rint(-2.9), -3.0d, 0.0);
462 Assert.assertEquals(Math.rint(-3.0), -3.0d, 0.0);
463 Assert.assertEquals(Math.rint(Double.NaN), Double.NaN, 0.0);
464 Assert.assertEquals(Math.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
465 Assert.assertEquals(Math.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
466 }
467
468 public static void test_Math_round_D() {
469 Assert.assertEquals(Math.round(+0.0d), (long)+0.0);
470 Assert.assertEquals(Math.round(-0.0d), (long)+0.0);
471 Assert.assertEquals(Math.round(2.0d), 2l);
472 Assert.assertEquals(Math.round(2.1d), 2l);
473 Assert.assertEquals(Math.round(2.5d), 3l);
474 Assert.assertEquals(Math.round(2.9d), 3l);
475 Assert.assertEquals(Math.round(3.0d), 3l);
476 Assert.assertEquals(Math.round(-2.0d), -2l);
477 Assert.assertEquals(Math.round(-2.1d), -2l);
478 Assert.assertEquals(Math.round(-2.5d), -2l);
479 Assert.assertEquals(Math.round(-2.9d), -3l);
480 Assert.assertEquals(Math.round(-3.0d), -3l);
481 Assert.assertEquals(Math.round(0.49999999999999994d), 1l);
482 Assert.assertEquals(Math.round(Double.NaN), (long)+0.0d);
483 Assert.assertEquals(Math.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
484 Assert.assertEquals(Math.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
485 Assert.assertEquals(Math.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
486 Assert.assertEquals(Math.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
487 }
488
489 public static void test_Math_round_F() {
490 Assert.assertEquals(Math.round(+0.0f), (int)+0.0);
491 Assert.assertEquals(Math.round(-0.0f), (int)+0.0);
492 Assert.assertEquals(Math.round(2.0f), 2);
493 Assert.assertEquals(Math.round(2.1f), 2);
494 Assert.assertEquals(Math.round(2.5f), 3);
495 Assert.assertEquals(Math.round(2.9f), 3);
496 Assert.assertEquals(Math.round(3.0f), 3);
497 Assert.assertEquals(Math.round(-2.0f), -2);
498 Assert.assertEquals(Math.round(-2.1f), -2);
499 Assert.assertEquals(Math.round(-2.5f), -2);
500 Assert.assertEquals(Math.round(-2.9f), -3);
501 Assert.assertEquals(Math.round(-3.0f), -3);
502 Assert.assertEquals(Math.round(Float.NaN), (int)+0.0f);
503 Assert.assertEquals(Math.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
504 Assert.assertEquals(Math.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
505 Assert.assertEquals(Math.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
506 Assert.assertEquals(Math.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
507 }
508
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100509 public static void test_StrictMath_abs_I() {
510 Assert.assertEquals(StrictMath.abs(0), 0);
511 Assert.assertEquals(StrictMath.abs(123), 123);
512 Assert.assertEquals(StrictMath.abs(-123), 123);
513 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
514 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
515 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
516 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
517 }
518
519 public static void test_StrictMath_abs_J() {
520 Assert.assertEquals(StrictMath.abs(0L), 0L);
521 Assert.assertEquals(StrictMath.abs(123L), 123L);
522 Assert.assertEquals(StrictMath.abs(-123L), 123L);
523 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
524 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
525 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
526 }
527
Serban Constantinescu23abec92014-07-02 16:13:38 +0100528 public static void test_StrictMath_min_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100529 Assert.assertEquals(StrictMath.min(0, 0), 0);
530 Assert.assertEquals(StrictMath.min(1, 0), 0);
531 Assert.assertEquals(StrictMath.min(0, 1), 0);
532 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
533 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
534 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
535 }
536
Serban Constantinescu23abec92014-07-02 16:13:38 +0100537 public static void test_StrictMath_max_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100538 Assert.assertEquals(StrictMath.max(0, 0), 0);
539 Assert.assertEquals(StrictMath.max(1, 0), 1);
540 Assert.assertEquals(StrictMath.max(0, 1), 1);
541 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
542 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
543 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
544 }
545
Serban Constantinescu23abec92014-07-02 16:13:38 +0100546 public static void test_StrictMath_min_J() {
547 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
548 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
549 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
550 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
551 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
552 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
553 }
554
555 public static void test_StrictMath_max_J() {
556 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
557 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
558 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
559 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
560 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
561 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
562 }
563
564 public static void test_StrictMath_min_F() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700565 Assert.assertTrue(Float.isNaN(StrictMath.min(1.0f, Float.NaN)));
566 Assert.assertTrue(Float.isNaN(StrictMath.min(Float.NaN, 1.0f)));
567 Assert.assertEquals(StrictMath.min(-0.0f, 0.0f), -0.0f);
568 Assert.assertEquals(StrictMath.min(0.0f, -0.0f), -0.0f);
569 Assert.assertEquals(StrictMath.min(-0.0f, -0.0f), -0.0f);
570 Assert.assertEquals(StrictMath.min(0.0f, 0.0f), 0.0f);
571 Assert.assertEquals(StrictMath.min(1.0f, 0.0f), 0.0f);
572 Assert.assertEquals(StrictMath.min(0.0f, 1.0f), 0.0f);
573 Assert.assertEquals(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f);
574 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, 0.0f), 0.0f);
575 Assert.assertEquals(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100576 }
577
578 public static void test_StrictMath_max_F() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700579 Assert.assertTrue(Float.isNaN(StrictMath.max(1.0f, Float.NaN)));
580 Assert.assertTrue(Float.isNaN(StrictMath.max(Float.NaN, 1.0f)));
581 Assert.assertEquals(StrictMath.max(-0.0f, 0.0f), 0.0f);
582 Assert.assertEquals(StrictMath.max(0.0f, -0.0f), 0.0f);
583 Assert.assertEquals(StrictMath.max(-0.0f, -0.0f), -0.0f);
584 Assert.assertEquals(StrictMath.max(0.0f, 0.0f), 0.0f);
585 Assert.assertEquals(StrictMath.max(1.0f, 0.0f), 1.0f);
586 Assert.assertEquals(StrictMath.max(0.0f, 1.0f), 1.0f);
587 Assert.assertEquals(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE);
588 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE);
589 Assert.assertEquals(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100590 }
591
592 public static void test_StrictMath_min_D() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700593 Assert.assertTrue(Double.isNaN(StrictMath.min(1.0d, Double.NaN)));
594 Assert.assertTrue(Double.isNaN(StrictMath.min(Double.NaN, 1.0d)));
595 Assert.assertEquals(StrictMath.min(-0.0d, 0.0d), -0.0d);
596 Assert.assertEquals(StrictMath.min(0.0d, -0.0d), -0.0d);
597 Assert.assertEquals(StrictMath.min(-0.0d, -0.0d), -0.0d);
598 Assert.assertEquals(StrictMath.min(0.0d, 0.0d), 0.0d);
599 Assert.assertEquals(StrictMath.min(1.0d, 0.0d), 0.0d);
600 Assert.assertEquals(StrictMath.min(0.0d, 1.0d), 0.0d);
601 Assert.assertEquals(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d);
602 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, 0.0d), 0.0d);
603 Assert.assertEquals(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100604 }
605
606 public static void test_StrictMath_max_D() {
Alexei Zavjalov1222c962014-07-16 00:54:13 +0700607 Assert.assertTrue(Double.isNaN(StrictMath.max(1.0d, Double.NaN)));
608 Assert.assertTrue(Double.isNaN(StrictMath.max(Double.NaN, 1.0d)));
609 Assert.assertEquals(StrictMath.max(-0.0d, 0.0d), 0.0d);
610 Assert.assertEquals(StrictMath.max(0.0d, -0.0d), 0.0d);
611 Assert.assertEquals(StrictMath.max(-0.0d, -0.0d), -0.0d);
612 Assert.assertEquals(StrictMath.max(0.0d, 0.0d), 0.0d);
613 Assert.assertEquals(StrictMath.max(1.0d, 0.0d), 1.0d);
614 Assert.assertEquals(StrictMath.max(0.0d, 1.0d), 1.0d);
615 Assert.assertEquals(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE);
616 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE);
617 Assert.assertEquals(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100618 }
619
Serban Constantinescu2eba1fa2014-07-31 19:07:17 +0100620 public static void test_StrictMath_ceil() {
621 Assert.assertEquals(StrictMath.ceil(+0.0), +0.0d, 0.0);
622 Assert.assertEquals(StrictMath.ceil(-0.0), -0.0d, 0.0);
623 Assert.assertEquals(StrictMath.ceil(-0.9), -0.0d, 0.0);
624 Assert.assertEquals(StrictMath.ceil(-0.5), -0.0d, 0.0);
625 Assert.assertEquals(StrictMath.ceil(0.0), -0.0d, 0.0);
626 Assert.assertEquals(StrictMath.ceil(+2.0), +2.0d, 0.0);
627 Assert.assertEquals(StrictMath.ceil(+2.1), +3.0d, 0.0);
628 Assert.assertEquals(StrictMath.ceil(+2.5), +3.0d, 0.0);
629 Assert.assertEquals(StrictMath.ceil(+2.9), +3.0d, 0.0);
630 Assert.assertEquals(StrictMath.ceil(+3.0), +3.0d, 0.0);
631 Assert.assertEquals(StrictMath.ceil(-2.0), -2.0d, 0.0);
632 Assert.assertEquals(StrictMath.ceil(-2.1), -2.0d, 0.0);
633 Assert.assertEquals(StrictMath.ceil(-2.5), -2.0d, 0.0);
634 Assert.assertEquals(StrictMath.ceil(-2.9), -2.0d, 0.0);
635 Assert.assertEquals(StrictMath.ceil(-3.0), -3.0d, 0.0);
636 Assert.assertEquals(StrictMath.ceil(Double.NaN), Double.NaN, 0.0);
637 Assert.assertEquals(StrictMath.ceil(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
638 Assert.assertEquals(StrictMath.ceil(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
639 }
640
641 public static void test_StrictMath_floor() {
642 Assert.assertEquals(StrictMath.floor(+0.0), +0.0d, 0.0);
643 Assert.assertEquals(StrictMath.floor(-0.0), -0.0d, 0.0);
644 Assert.assertEquals(StrictMath.floor(+2.0), +2.0d, 0.0);
645 Assert.assertEquals(StrictMath.floor(+2.1), +2.0d, 0.0);
646 Assert.assertEquals(StrictMath.floor(+2.5), +2.0d, 0.0);
647 Assert.assertEquals(StrictMath.floor(+2.9), +2.0d, 0.0);
648 Assert.assertEquals(StrictMath.floor(+3.0), +3.0d, 0.0);
649 Assert.assertEquals(StrictMath.floor(-2.0), -2.0d, 0.0);
650 Assert.assertEquals(StrictMath.floor(-2.1), -3.0d, 0.0);
651 Assert.assertEquals(StrictMath.floor(-2.5), -3.0d, 0.0);
652 Assert.assertEquals(StrictMath.floor(-2.9), -3.0d, 0.0);
653 Assert.assertEquals(StrictMath.floor(-3.0), -3.0d, 0.0);
654 Assert.assertEquals(StrictMath.floor(Double.NaN), Double.NaN, 0.0);
655 Assert.assertEquals(StrictMath.floor(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
656 Assert.assertEquals(StrictMath.floor(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
657 }
658
659 public static void test_StrictMath_rint() {
660 Assert.assertEquals(StrictMath.rint(+0.0), +0.0d, 0.0);
661 Assert.assertEquals(StrictMath.rint(-0.0), -0.0d, 0.0);
662 Assert.assertEquals(StrictMath.rint(+2.0), +2.0d, 0.0);
663 Assert.assertEquals(StrictMath.rint(+2.1), +2.0d, 0.0);
664 Assert.assertEquals(StrictMath.rint(+2.5), +2.0d, 0.0);
665 Assert.assertEquals(StrictMath.rint(+2.9), +3.0d, 0.0);
666 Assert.assertEquals(StrictMath.rint(+3.0), +3.0d, 0.0);
667 Assert.assertEquals(StrictMath.rint(-2.0), -2.0d, 0.0);
668 Assert.assertEquals(StrictMath.rint(-2.1), -2.0d, 0.0);
669 Assert.assertEquals(StrictMath.rint(-2.5), -2.0d, 0.0);
670 Assert.assertEquals(StrictMath.rint(-2.9), -3.0d, 0.0);
671 Assert.assertEquals(StrictMath.rint(-3.0), -3.0d, 0.0);
672 Assert.assertEquals(StrictMath.rint(Double.NaN), Double.NaN, 0.0);
673 Assert.assertEquals(StrictMath.rint(Double.POSITIVE_INFINITY), Double.POSITIVE_INFINITY, 0.0);
674 Assert.assertEquals(StrictMath.rint(Double.NEGATIVE_INFINITY), Double.NEGATIVE_INFINITY, 0.0);
675 }
676
677 public static void test_StrictMath_round_D() {
678 Assert.assertEquals(StrictMath.round(+0.0d), (long)+0.0);
679 Assert.assertEquals(StrictMath.round(-0.0d), (long)+0.0);
680 Assert.assertEquals(StrictMath.round(2.0d), 2l);
681 Assert.assertEquals(StrictMath.round(2.1d), 2l);
682 Assert.assertEquals(StrictMath.round(2.5d), 3l);
683 Assert.assertEquals(StrictMath.round(2.9d), 3l);
684 Assert.assertEquals(StrictMath.round(3.0d), 3l);
685 Assert.assertEquals(StrictMath.round(-2.0d), -2l);
686 Assert.assertEquals(StrictMath.round(-2.1d), -2l);
687 Assert.assertEquals(StrictMath.round(-2.5d), -2l);
688 Assert.assertEquals(StrictMath.round(-2.9d), -3l);
689 Assert.assertEquals(StrictMath.round(-3.0d), -3l);
690 Assert.assertEquals(StrictMath.round(0.49999999999999994d), 1l);
691 Assert.assertEquals(StrictMath.round(Double.NaN), (long)+0.0d);
692 Assert.assertEquals(StrictMath.round(Long.MAX_VALUE + 1.0d), Long.MAX_VALUE);
693 Assert.assertEquals(StrictMath.round(Long.MIN_VALUE - 1.0d), Long.MIN_VALUE);
694 Assert.assertEquals(StrictMath.round(Double.POSITIVE_INFINITY), Long.MAX_VALUE);
695 Assert.assertEquals(StrictMath.round(Double.NEGATIVE_INFINITY), Long.MIN_VALUE);
696 }
697
698 public static void test_StrictMath_round_F() {
699 Assert.assertEquals(StrictMath.round(+0.0f), (int)+0.0);
700 Assert.assertEquals(StrictMath.round(-0.0f), (int)+0.0);
701 Assert.assertEquals(StrictMath.round(2.0f), 2);
702 Assert.assertEquals(StrictMath.round(2.1f), 2);
703 Assert.assertEquals(StrictMath.round(2.5f), 3);
704 Assert.assertEquals(StrictMath.round(2.9f), 3);
705 Assert.assertEquals(StrictMath.round(3.0f), 3);
706 Assert.assertEquals(StrictMath.round(-2.0f), -2);
707 Assert.assertEquals(StrictMath.round(-2.1f), -2);
708 Assert.assertEquals(StrictMath.round(-2.5f), -2);
709 Assert.assertEquals(StrictMath.round(-2.9f), -3);
710 Assert.assertEquals(StrictMath.round(-3.0f), -3);
711 Assert.assertEquals(StrictMath.round(Float.NaN), (int)+0.0f);
712 Assert.assertEquals(StrictMath.round(Integer.MAX_VALUE + 1.0f), Integer.MAX_VALUE);
713 Assert.assertEquals(StrictMath.round(Integer.MIN_VALUE - 1.0f), Integer.MIN_VALUE);
714 Assert.assertEquals(StrictMath.round(Float.POSITIVE_INFINITY), Integer.MAX_VALUE);
715 Assert.assertEquals(StrictMath.round(Float.NEGATIVE_INFINITY), Integer.MIN_VALUE);
716 }
717
Elliott Hughes28c384b2012-06-15 16:46:25 -0700718 public static void test_Float_floatToRawIntBits() {
719 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
720 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
721 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
722 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
723 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
724 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
725 }
726
727 public static void test_Float_intBitsToFloat() {
728 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
729 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
730 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
731 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
732 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
733 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
734 }
735
736 public static void test_Double_doubleToRawLongBits() {
737 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
738 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
739 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
740 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
741 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
742 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
743 }
744
745 public static void test_Double_longBitsToDouble() {
746 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
747 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
748 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
749 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
750 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
751 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
752 }
Serban Constantinescu23abec92014-07-02 16:13:38 +0100753
Zheng Xua3fe7422014-07-09 14:03:15 +0800754 public static void test_Short_reverseBytes() {
755 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
756 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
757 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
758 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
759 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
760 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
761 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
762 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
763 }
764
765 public static void test_Integer_reverseBytes() {
766 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
767 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
768 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
769 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
770 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
771 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
772 }
773
774 public static void test_Long_reverseBytes() {
775 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
776 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
777 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
778 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
779 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
780 }
781
Serban Constantinescu23abec92014-07-02 16:13:38 +0100782 public static void test_Integer_reverse() {
783 Assert.assertEquals(Integer.reverse(1), 0x80000000);
784 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
785 Assert.assertEquals(Integer.reverse(0), 0);
786 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
787 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
788 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
789 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
790 }
791
792 public static void test_Long_reverse() {
793 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
794 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
795 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +0800796 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
797 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
798 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100799 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
800 }
801
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700802 static Object runtime;
803 static Method address_of;
Zuo Wangf37a88b2014-07-10 04:26:41 -0700804 static Method new_non_movable_array;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700805 static Method peek_byte;
806 static Method peek_short;
807 static Method peek_int;
808 static Method peek_long;
809 static Method poke_byte;
810 static Method poke_short;
811 static Method poke_int;
812 static Method poke_long;
813
814 public static void initSupportMethodsForPeekPoke() throws Exception {
815 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
816 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
817 runtime = get_runtime.invoke(null);
818 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
Zuo Wangf37a88b2014-07-10 04:26:41 -0700819 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700820
821 Class<?> io_memory = Class.forName("libcore.io.Memory");
822 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
823 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
824 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
825 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
826 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
827 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
828 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
829 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
830 }
831
832 public static void test_Memory_peekByte() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700833 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700834 b[0] = 0x12;
835 b[1] = 0x11;
836 long address = (long)address_of.invoke(runtime, b);
837 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
838 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
839 }
840
841 public static void test_Memory_peekShort() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700842 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700843 b[0] = 0x13;
844 b[1] = 0x12;
845 b[2] = 0x11;
846 long address = (long)address_of.invoke(runtime, b);
847 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
848 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
849 }
850
851 public static void test_Memory_peekInt() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700852 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700853 b[0] = 0x15;
854 b[1] = 0x14;
855 b[2] = 0x13;
856 b[3] = 0x12;
857 b[4] = 0x11;
858 long address = (long)address_of.invoke(runtime, b);
859 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
860 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
861 }
862
863 public static void test_Memory_peekLong() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700864 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700865 b[0] = 0x19;
866 b[1] = 0x18;
867 b[2] = 0x17;
868 b[3] = 0x16;
869 b[4] = 0x15;
870 b[5] = 0x14;
871 b[6] = 0x13;
872 b[7] = 0x12;
873 b[8] = 0x11;
874 long address = (long)address_of.invoke(runtime, b);
875 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
876 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
877 }
878
879 public static void test_Memory_pokeByte() throws Exception {
880 byte[] r = {0x11, 0x12};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700881 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700882 long address = (long)address_of.invoke(runtime, b);
883 poke_byte.invoke(null, address, (byte)0x11);
884 poke_byte.invoke(null, address + 1, (byte)0x12);
885 Assert.assertTrue(Arrays.equals(r, b));
886 }
887
888 public static void test_Memory_pokeShort() throws Exception {
889 byte[] ra = {0x12, 0x11, 0x13};
890 byte[] ru = {0x12, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700891 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700892 long address = (long)address_of.invoke(runtime, b);
893
894 // Aligned write
895 b[2] = 0x13;
896 poke_short.invoke(null, address, (short)0x1112, false);
897 Assert.assertTrue(Arrays.equals(ra, b));
898
899 // Unaligned write
900 poke_short.invoke(null, address + 1, (short)0x2122, false);
901 Assert.assertTrue(Arrays.equals(ru, b));
902 }
903
904 public static void test_Memory_pokeInt() throws Exception {
905 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
906 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700907 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700908 long address = (long)address_of.invoke(runtime, b);
909
910 b[4] = 0x15;
911 poke_int.invoke(null, address, (int)0x11121314, false);
912 Assert.assertTrue(Arrays.equals(ra, b));
913
914 poke_int.invoke(null, address + 1, (int)0x21222324, false);
915 Assert.assertTrue(Arrays.equals(ru, b));
916 }
917
918 public static void test_Memory_pokeLong() throws Exception {
919 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
920 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700921 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700922 long address = (long)address_of.invoke(runtime, b);
923
924 b[8] = 0x19;
925 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
926 Assert.assertTrue(Arrays.equals(ra, b));
927
928 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
929 Assert.assertTrue(Arrays.equals(ru, b));
930 }
jeffhao5d1ac922011-09-29 17:41:15 -0700931}