blob: 1c3c89ea3d58e09c8230141255abdc30691792da [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();
Zheng Xua3fe7422014-07-09 14:03:15 +080037 test_Short_reverseBytes();
38 test_Integer_reverseBytes();
39 test_Long_reverseBytes();
Serban Constantinescu23abec92014-07-02 16:13:38 +010040 test_Integer_reverse();
41 test_Long_reverse();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010042 test_StrictMath_abs_I();
43 test_StrictMath_abs_J();
Serban Constantinescu23abec92014-07-02 16:13:38 +010044 test_StrictMath_min_I();
45 test_StrictMath_max_I();
46 test_StrictMath_min_J();
47 test_StrictMath_max_J();
48 test_StrictMath_min_F();
49 test_StrictMath_max_F();
50 test_StrictMath_min_D();
51 test_StrictMath_max_D();
Elliott Hughes28c384b2012-06-15 16:46:25 -070052 test_String_charAt();
53 test_String_compareTo();
54 test_String_indexOf();
55 test_String_isEmpty();
56 test_String_length();
Andreas Gampe7a949612014-07-08 11:03:59 -070057 test_Thread_currentThread();
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +070058 initSupportMethodsForPeekPoke();
59 test_Memory_peekByte();
60 test_Memory_peekShort();
61 test_Memory_peekInt();
62 test_Memory_peekLong();
63 test_Memory_pokeByte();
64 test_Memory_pokeShort();
65 test_Memory_pokeInt();
66 test_Memory_pokeLong();
Elliott Hughes28c384b2012-06-15 16:46:25 -070067 }
68
Serban Constantinescu23abec92014-07-02 16:13:38 +010069 /*
70 * Determine if two floating point numbers are approximately equal.
71 *
72 * (Assumes that floating point is generally working, so we can't use
73 * this for the first set of tests.)
74 */
75 static boolean approxEqual(float a, float b, float maxDelta) {
76 if (a > b)
77 return (a - b) < maxDelta;
78 else
79 return (b - a) < maxDelta;
80 }
81 static boolean approxEqual(double a, double b, double maxDelta) {
82 if (a > b)
83 return (a - b) < maxDelta;
84 else
85 return (b - a) < maxDelta;
86 }
87
Andreas Gampe7a949612014-07-08 11:03:59 -070088 /**
89 * Will test inlining Thread.currentThread().
90 */
91 public static void test_Thread_currentThread() {
92 // 1. Do not use result.
93 Thread.currentThread();
94
95 // 2. Result should not be null.
96 Assert.assertNotNull(Thread.currentThread());
97 }
98
Elliott Hughes28c384b2012-06-15 16:46:25 -070099 public static void test_String_length() {
100 String str0 = "";
101 String str1 = "x";
102 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
103
104 Assert.assertEquals(str0.length(), 0);
105 Assert.assertEquals(str1.length(), 1);
106 Assert.assertEquals(str80.length(), 80);
107
108 String strNull = null;
109 try {
110 strNull.length();
111 Assert.fail();
112 } catch (NullPointerException expected) {
113 }
114 }
115
116 public static void test_String_isEmpty() {
117 String str0 = "";
118 String str1 = "x";
119
120 Assert.assertTrue(str0.isEmpty());
121 Assert.assertFalse(str1.isEmpty());
122
123 String strNull = null;
124 try {
125 strNull.isEmpty();
126 Assert.fail();
127 } catch (NullPointerException expected) {
128 }
129 }
130
131 public static void test_String_charAt() {
132 String testStr = "Now is the time";
133
134 Assert.assertEquals('N', testStr.charAt(0));
135 Assert.assertEquals('o', testStr.charAt(1));
136 Assert.assertEquals(' ', testStr.charAt(10));
137 Assert.assertEquals('e', testStr.charAt(testStr.length()-1));
138
139 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
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700158 static int start;
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700159 private static int[] negIndex = { -100000 };
Elliott Hughes28c384b2012-06-15 16:46:25 -0700160 public static void test_String_indexOf() {
161 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700162 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700163 String str3 = "abc";
164 String str10 = "abcdefghij";
165 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700166
Elliott Hughes28c384b2012-06-15 16:46:25 -0700167 int supplementaryChar = 0x20b9f;
168 String surrogatePair = "\ud842\udf9f";
169 String stringWithSurrogates = "hello " + surrogatePair + " world";
jeffhao5d1ac922011-09-29 17:41:15 -0700170
Elliott Hughes28c384b2012-06-15 16:46:25 -0700171 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
172 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
173 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
174 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
jeffhao5d1ac922011-09-29 17:41:15 -0700175
Elliott Hughes28c384b2012-06-15 16:46:25 -0700176 Assert.assertEquals(str0.indexOf('a'), -1);
177 Assert.assertEquals(str3.indexOf('a'), 0);
178 Assert.assertEquals(str3.indexOf('b'), 1);
179 Assert.assertEquals(str3.indexOf('c'), 2);
180 Assert.assertEquals(str10.indexOf('j'), 9);
181 Assert.assertEquals(str40.indexOf('a'), 0);
182 Assert.assertEquals(str40.indexOf('b'), 38);
183 Assert.assertEquals(str40.indexOf('c'), 39);
184 Assert.assertEquals(str0.indexOf('a',20), -1);
185 Assert.assertEquals(str0.indexOf('a',0), -1);
186 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700187 Assert.assertEquals(str1.indexOf('/',++start), -1);
Alexei Zavjalova1758d82014-04-17 01:55:43 +0700188 Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700189 Assert.assertEquals(str3.indexOf('a',0), 0);
190 Assert.assertEquals(str3.indexOf('a',1), -1);
191 Assert.assertEquals(str3.indexOf('a',1234), -1);
192 Assert.assertEquals(str3.indexOf('b',0), 1);
193 Assert.assertEquals(str3.indexOf('b',1), 1);
194 Assert.assertEquals(str3.indexOf('c',2), 2);
195 Assert.assertEquals(str10.indexOf('j',5), 9);
196 Assert.assertEquals(str10.indexOf('j',9), 9);
197 Assert.assertEquals(str40.indexOf('a',10), 10);
198 Assert.assertEquals(str40.indexOf('b',40), -1);
199
200 String strNull = null;
201 try {
202 strNull.indexOf('a');
203 Assert.fail();
204 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700205 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700206 try {
207 strNull.indexOf('a', 0);
208 Assert.fail();
209 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700210 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700211 try {
212 strNull.indexOf('a', -1);
213 Assert.fail();
214 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700215 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700216 }
jeffhao5d1ac922011-09-29 17:41:15 -0700217
Elliott Hughes28c384b2012-06-15 16:46:25 -0700218 public static void test_String_compareTo() {
219 String test = "0123456789";
220 String test1 = new String("0123456789"); // different object
221 String test2 = new String("0123456780"); // different value
222 String offset = new String("xxx0123456789yyy");
223 String sub = offset.substring(3, 13);
224 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
225 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
226 String lc = "abcdefg";
227 String uc = "ABCDEFG";
228 Object blah = new Object();
229
230 Assert.assertTrue(lc.toUpperCase().equals(uc));
231
232 Assert.assertEquals(str32.compareTo(str33), -1);
233 Assert.assertEquals(str33.compareTo(str32), 1);
234
235 Assert.assertTrue(test.equals(test));
236 Assert.assertTrue(test.equals(test1));
237 Assert.assertFalse(test.equals(test2));
238
239 Assert.assertEquals(test.compareTo(test1), 0);
240 Assert.assertTrue(test1.compareTo(test2) > 0);
241 Assert.assertTrue(test2.compareTo(test1) < 0);
242
243 // Compare string with a nonzero offset, in left/right side.
244 Assert.assertEquals(test.compareTo(sub), 0);
245 Assert.assertEquals(sub.compareTo(test), 0);
246 Assert.assertTrue(test.equals(sub));
247 Assert.assertTrue(sub.equals(test));
248 // Same base, one is a substring.
249 Assert.assertFalse(offset.equals(sub));
250 Assert.assertFalse(sub.equals(offset));
251 // Wrong class.
252 Assert.assertFalse(test.equals(blah));
253
254 // Null lhs - throw.
255 try {
256 test.compareTo(null);
257 Assert.fail("didn't get expected npe");
258 } catch (NullPointerException npe) {
259 }
260 // Null rhs - okay.
261 Assert.assertFalse(test.equals(null));
262
263 test = test.substring(1);
264 Assert.assertTrue(test.equals("123456789"));
265 Assert.assertFalse(test.equals(test1));
266
267 test = test.substring(1);
268 Assert.assertTrue(test.equals("23456789"));
269
270 test = test.substring(1);
271 Assert.assertTrue(test.equals("3456789"));
272
273 test = test.substring(1);
274 Assert.assertTrue(test.equals("456789"));
275
276 test = test.substring(3,5);
277 Assert.assertTrue(test.equals("78"));
278
279 test = "this/is/a/path";
280 String[] strings = test.split("/");
281 Assert.assertEquals(4, strings.length);
282
283 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
284 Assert.assertEquals("this is a path", test.replace("/", " "));
285 }
286
287 public static void test_Math_abs_I() {
288 Assert.assertEquals(Math.abs(0), 0);
289 Assert.assertEquals(Math.abs(123), 123);
290 Assert.assertEquals(Math.abs(-123), 123);
291 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
292 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
293 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100294 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700295 }
296
297 public static void test_Math_abs_J() {
298 Assert.assertEquals(Math.abs(0L), 0L);
299 Assert.assertEquals(Math.abs(123L), 123L);
300 Assert.assertEquals(Math.abs(-123L), 123L);
301 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
302 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
303 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
304 }
305
Serban Constantinescu23abec92014-07-02 16:13:38 +0100306 public static void test_Math_min_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700307 Assert.assertEquals(Math.min(0, 0), 0);
308 Assert.assertEquals(Math.min(1, 0), 0);
309 Assert.assertEquals(Math.min(0, 1), 0);
310 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
311 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
312 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
313 }
314
Serban Constantinescu23abec92014-07-02 16:13:38 +0100315 public static void test_Math_max_I() {
Elliott Hughes28c384b2012-06-15 16:46:25 -0700316 Assert.assertEquals(Math.max(0, 0), 0);
317 Assert.assertEquals(Math.max(1, 0), 1);
318 Assert.assertEquals(Math.max(0, 1), 1);
319 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
320 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
321 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
322 }
323
Serban Constantinescu23abec92014-07-02 16:13:38 +0100324 public static void test_Math_min_J() {
325 Assert.assertEquals(Math.min(0L, 0L), 0L);
326 Assert.assertEquals(Math.min(1L, 0L), 0L);
327 Assert.assertEquals(Math.min(0L, 1L), 0L);
328 Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L);
329 Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
330 Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
331 }
332
333 public static void test_Math_max_J() {
334 Assert.assertEquals(Math.max(0L, 0L), 0L);
335 Assert.assertEquals(Math.max(1L, 0L), 1L);
336 Assert.assertEquals(Math.max(0L, 1L), 1L);
337 Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
338 Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L);
339 Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
340 }
341
342 public static void test_Math_min_F() {
343 Assert.assertTrue(approxEqual(Math.min(0.0f, 0.0f), 0.0f, 0.001f));
344 Assert.assertTrue(approxEqual(Math.min(1.0f, 0.0f), 0.0f, 0.001f));
345 Assert.assertTrue(approxEqual(Math.min(0.0f, 1.0f), 0.0f, 0.001f));
346 Assert.assertTrue(approxEqual(Math.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f));
347 Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f));
348 Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f));
349 }
350
351 public static void test_Math_max_F() {
352 Assert.assertTrue(approxEqual(Math.max(0.0f, 0.0f), 0.0f, 0.001f));
353 Assert.assertTrue(approxEqual(Math.max(1.0f, 0.0f), 1.0f, 0.001f));
354 Assert.assertTrue(approxEqual(Math.max(0.0f, 1.0f), 1.0f, 0.001f));
355 Assert.assertTrue(approxEqual(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
356 Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f));
357 Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
358 }
359
360 public static void test_Math_min_D() {
361 Assert.assertTrue(approxEqual(Math.min(0.0d, 0.0d), 0.0d, 0.001d));
362 Assert.assertTrue(approxEqual(Math.min(1.0d, 0.0d), 0.0d, 0.001d));
363 Assert.assertTrue(approxEqual(Math.min(0.0d, 1.0d), 0.0d, 0.001d));
364 Assert.assertTrue(approxEqual(Math.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d));
365 Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d));
366 Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d));
367 }
368
369 public static void test_Math_max_D() {
370 Assert.assertTrue(approxEqual(Math.max(0.0d, 0.0d), 0.0d, 0.001d));
371 Assert.assertTrue(approxEqual(Math.max(1.0d, 0.0d), 1.0d, 0.001d));
372 Assert.assertTrue(approxEqual(Math.max(0.0d, 1.0d), 1.0d, 0.001d));
373 Assert.assertTrue(approxEqual(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
374 Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d));
375 Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
376 }
377
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100378 public static void test_StrictMath_abs_I() {
379 Assert.assertEquals(StrictMath.abs(0), 0);
380 Assert.assertEquals(StrictMath.abs(123), 123);
381 Assert.assertEquals(StrictMath.abs(-123), 123);
382 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
383 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
384 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
385 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
386 }
387
388 public static void test_StrictMath_abs_J() {
389 Assert.assertEquals(StrictMath.abs(0L), 0L);
390 Assert.assertEquals(StrictMath.abs(123L), 123L);
391 Assert.assertEquals(StrictMath.abs(-123L), 123L);
392 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
393 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
394 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
395 }
396
Serban Constantinescu23abec92014-07-02 16:13:38 +0100397 public static void test_StrictMath_min_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100398 Assert.assertEquals(StrictMath.min(0, 0), 0);
399 Assert.assertEquals(StrictMath.min(1, 0), 0);
400 Assert.assertEquals(StrictMath.min(0, 1), 0);
401 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
402 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
403 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
404 }
405
Serban Constantinescu23abec92014-07-02 16:13:38 +0100406 public static void test_StrictMath_max_I() {
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100407 Assert.assertEquals(StrictMath.max(0, 0), 0);
408 Assert.assertEquals(StrictMath.max(1, 0), 1);
409 Assert.assertEquals(StrictMath.max(0, 1), 1);
410 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
411 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
412 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
413 }
414
Serban Constantinescu23abec92014-07-02 16:13:38 +0100415 public static void test_StrictMath_min_J() {
416 Assert.assertEquals(StrictMath.min(0L, 0L), 0L);
417 Assert.assertEquals(StrictMath.min(1L, 0L), 0L);
418 Assert.assertEquals(StrictMath.min(0L, 1L), 0L);
419 Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L);
420 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE);
421 Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE);
422 }
423
424 public static void test_StrictMath_max_J() {
425 Assert.assertEquals(StrictMath.max(0L, 0L), 0L);
426 Assert.assertEquals(StrictMath.max(1L, 0L), 1L);
427 Assert.assertEquals(StrictMath.max(0L, 1L), 1L);
428 Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE);
429 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L);
430 Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE);
431 }
432
433 public static void test_StrictMath_min_F() {
434 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 0.0f), 0.0f, 0.001f));
435 Assert.assertTrue(approxEqual(StrictMath.min(1.0f, 0.0f), 0.0f, 0.001f));
436 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 1.0f), 0.0f, 0.001f));
437 Assert.assertTrue(approxEqual(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f));
438 Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f));
439 Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f));
440 }
441
442 public static void test_StrictMath_max_F() {
443 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 0.0f), 0.0f, 0.001f));
444 Assert.assertTrue(approxEqual(StrictMath.max(1.0f, 0.0f), 1.0f, 0.001f));
445 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 1.0f), 1.0f, 0.001f));
446 Assert.assertTrue(approxEqual(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
447 Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f));
448 Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f));
449 }
450
451 public static void test_StrictMath_min_D() {
452 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 0.0d), 0.0d, 0.001d));
453 Assert.assertTrue(approxEqual(StrictMath.min(1.0d, 0.0d), 0.0d, 0.001d));
454 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 1.0d), 0.0d, 0.001d));
455 Assert.assertTrue(approxEqual(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d));
456 Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d));
457 Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d));
458 }
459
460 public static void test_StrictMath_max_D() {
461 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 0.0d), 0.0d, 0.001d));
462 Assert.assertTrue(approxEqual(StrictMath.max(1.0d, 0.0d), 1.0d, 0.001d));
463 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 1.0d), 1.0d, 0.001d));
464 Assert.assertTrue(approxEqual(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
465 Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d));
466 Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d));
467 }
468
Elliott Hughes28c384b2012-06-15 16:46:25 -0700469 public static void test_Float_floatToRawIntBits() {
470 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
471 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
472 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
473 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
474 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
475 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
476 }
477
478 public static void test_Float_intBitsToFloat() {
479 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
480 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
481 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
482 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
483 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
484 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
485 }
486
487 public static void test_Double_doubleToRawLongBits() {
488 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
489 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
490 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
491 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
492 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
493 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
494 }
495
496 public static void test_Double_longBitsToDouble() {
497 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
498 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
499 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
500 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
501 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
502 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
503 }
Serban Constantinescu23abec92014-07-02 16:13:38 +0100504
Zheng Xua3fe7422014-07-09 14:03:15 +0800505 public static void test_Short_reverseBytes() {
506 Assert.assertEquals(Short.reverseBytes((short)0x0000), (short)0x0000);
507 Assert.assertEquals(Short.reverseBytes((short)0xffff), (short)0xffff);
508 Assert.assertEquals(Short.reverseBytes((short)0x8000), (short)0x0080);
509 Assert.assertEquals(Short.reverseBytes((short)0x0080), (short)0x8000);
510 Assert.assertEquals(Short.reverseBytes((short)0x0123), (short)0x2301);
511 Assert.assertEquals(Short.reverseBytes((short)0x4567), (short)0x6745);
512 Assert.assertEquals(Short.reverseBytes((short)0x89ab), (short)0xab89);
513 Assert.assertEquals(Short.reverseBytes((short)0xcdef), (short)0xefcd);
514 }
515
516 public static void test_Integer_reverseBytes() {
517 Assert.assertEquals(Integer.reverseBytes(0x00000000), 0x00000000);
518 Assert.assertEquals(Integer.reverseBytes(0xffffffff), 0xffffffff);
519 Assert.assertEquals(Integer.reverseBytes(0x80000000), 0x00000080);
520 Assert.assertEquals(Integer.reverseBytes(0x00000080), 0x80000000);
521 Assert.assertEquals(Integer.reverseBytes(0x01234567), 0x67452301);
522 Assert.assertEquals(Integer.reverseBytes(0x89abcdef), 0xefcdab89);
523 }
524
525 public static void test_Long_reverseBytes() {
526 Assert.assertEquals(Long.reverseBytes(0x0000000000000000L), 0x0000000000000000L);
527 Assert.assertEquals(Long.reverseBytes(0xffffffffffffffffL), 0xffffffffffffffffL);
528 Assert.assertEquals(Long.reverseBytes(0x8000000000000000L), 0x0000000000000080L);
529 Assert.assertEquals(Long.reverseBytes(0x0000000000000080L), 0x8000000000000000L);
530 Assert.assertEquals(Long.reverseBytes(0x0123456789abcdefL), 0xefcdab8967452301L);
531 }
532
Serban Constantinescu23abec92014-07-02 16:13:38 +0100533 public static void test_Integer_reverse() {
534 Assert.assertEquals(Integer.reverse(1), 0x80000000);
535 Assert.assertEquals(Integer.reverse(-1), 0xffffffff);
536 Assert.assertEquals(Integer.reverse(0), 0);
537 Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48);
538 Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1);
539 Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe);
540 Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1);
541 }
542
543 public static void test_Long_reverse() {
544 Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L);
545 Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL);
546 Assert.assertEquals(Long.reverse(0L), 0L);
Zheng Xua3fe7422014-07-09 14:03:15 +0800547 Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L);
548 Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L);
549 Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL);
Serban Constantinescu23abec92014-07-02 16:13:38 +0100550 Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L);
551 }
552
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700553 static Object runtime;
554 static Method address_of;
Zuo Wangf37a88b2014-07-10 04:26:41 -0700555 static Method new_non_movable_array;
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700556 static Method peek_byte;
557 static Method peek_short;
558 static Method peek_int;
559 static Method peek_long;
560 static Method poke_byte;
561 static Method poke_short;
562 static Method poke_int;
563 static Method poke_long;
564
565 public static void initSupportMethodsForPeekPoke() throws Exception {
566 Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime");
567 Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime");
568 runtime = get_runtime.invoke(null);
569 address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class);
Zuo Wangf37a88b2014-07-10 04:26:41 -0700570 new_non_movable_array = vm_runtime.getDeclaredMethod("newNonMovableArray", Class.class, Integer.TYPE);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700571
572 Class<?> io_memory = Class.forName("libcore.io.Memory");
573 peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE);
574 peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE);
575 peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE);
576 peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE);
577 poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE);
578 poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE);
579 poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE);
580 poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE);
581 }
582
583 public static void test_Memory_peekByte() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700584 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700585 b[0] = 0x12;
586 b[1] = 0x11;
587 long address = (long)address_of.invoke(runtime, b);
588 Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12);
589 Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11);
590 }
591
592 public static void test_Memory_peekShort() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700593 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700594 b[0] = 0x13;
595 b[1] = 0x12;
596 b[2] = 0x11;
597 long address = (long)address_of.invoke(runtime, b);
598 Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read
599 Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read
600 }
601
602 public static void test_Memory_peekInt() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700603 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700604 b[0] = 0x15;
605 b[1] = 0x14;
606 b[2] = 0x13;
607 b[3] = 0x12;
608 b[4] = 0x11;
609 long address = (long)address_of.invoke(runtime, b);
610 Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415);
611 Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314);
612 }
613
614 public static void test_Memory_peekLong() throws Exception {
Zuo Wangf37a88b2014-07-10 04:26:41 -0700615 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700616 b[0] = 0x19;
617 b[1] = 0x18;
618 b[2] = 0x17;
619 b[3] = 0x16;
620 b[4] = 0x15;
621 b[5] = 0x14;
622 b[6] = 0x13;
623 b[7] = 0x12;
624 b[8] = 0x11;
625 long address = (long)address_of.invoke(runtime, b);
626 Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L);
627 Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L);
628 }
629
630 public static void test_Memory_pokeByte() throws Exception {
631 byte[] r = {0x11, 0x12};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700632 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 2);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700633 long address = (long)address_of.invoke(runtime, b);
634 poke_byte.invoke(null, address, (byte)0x11);
635 poke_byte.invoke(null, address + 1, (byte)0x12);
636 Assert.assertTrue(Arrays.equals(r, b));
637 }
638
639 public static void test_Memory_pokeShort() throws Exception {
640 byte[] ra = {0x12, 0x11, 0x13};
641 byte[] ru = {0x12, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700642 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 3);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700643 long address = (long)address_of.invoke(runtime, b);
644
645 // Aligned write
646 b[2] = 0x13;
647 poke_short.invoke(null, address, (short)0x1112, false);
648 Assert.assertTrue(Arrays.equals(ra, b));
649
650 // Unaligned write
651 poke_short.invoke(null, address + 1, (short)0x2122, false);
652 Assert.assertTrue(Arrays.equals(ru, b));
653 }
654
655 public static void test_Memory_pokeInt() throws Exception {
656 byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15};
657 byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700658 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 5);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700659 long address = (long)address_of.invoke(runtime, b);
660
661 b[4] = 0x15;
662 poke_int.invoke(null, address, (int)0x11121314, false);
663 Assert.assertTrue(Arrays.equals(ra, b));
664
665 poke_int.invoke(null, address + 1, (int)0x21222324, false);
666 Assert.assertTrue(Arrays.equals(ru, b));
667 }
668
669 public static void test_Memory_pokeLong() throws Exception {
670 byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19};
671 byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21};
Zuo Wangf37a88b2014-07-10 04:26:41 -0700672 byte[] b = (byte[])new_non_movable_array.invoke(runtime, Byte.TYPE, 9);
Alexei Zavjaloveb24bae2014-07-08 16:27:17 +0700673 long address = (long)address_of.invoke(runtime, b);
674
675 b[8] = 0x19;
676 poke_long.invoke(null, address, (long)0x1112131415161718L, false);
677 Assert.assertTrue(Arrays.equals(ra, b));
678
679 poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false);
680 Assert.assertTrue(Arrays.equals(ru, b));
681 }
jeffhao5d1ac922011-09-29 17:41:15 -0700682}