jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 17 | import junit.framework.Assert; |
Alexei Zavjalov | eb24bae | 2014-07-08 16:27:17 +0700 | [diff] [blame^] | 18 | import java.util.Arrays; |
| 19 | import java.lang.reflect.Method; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 20 | |
| 21 | public class Main { |
Alexei Zavjalov | eb24bae | 2014-07-08 16:27:17 +0700 | [diff] [blame^] | 22 | public static void main(String args[]) throws Exception { |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 23 | 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 Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 29 | 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(); |
| 37 | test_Integer_reverse(); |
| 38 | test_Long_reverse(); |
Sebastien Hertz | bf1442d | 2013-03-05 15:12:40 +0100 | [diff] [blame] | 39 | test_StrictMath_abs_I(); |
| 40 | test_StrictMath_abs_J(); |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 41 | test_StrictMath_min_I(); |
| 42 | test_StrictMath_max_I(); |
| 43 | test_StrictMath_min_J(); |
| 44 | test_StrictMath_max_J(); |
| 45 | test_StrictMath_min_F(); |
| 46 | test_StrictMath_max_F(); |
| 47 | test_StrictMath_min_D(); |
| 48 | test_StrictMath_max_D(); |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 49 | test_String_charAt(); |
| 50 | test_String_compareTo(); |
| 51 | test_String_indexOf(); |
| 52 | test_String_isEmpty(); |
| 53 | test_String_length(); |
Andreas Gampe | 7a94961 | 2014-07-08 11:03:59 -0700 | [diff] [blame] | 54 | test_Thread_currentThread(); |
Alexei Zavjalov | eb24bae | 2014-07-08 16:27:17 +0700 | [diff] [blame^] | 55 | initSupportMethodsForPeekPoke(); |
| 56 | test_Memory_peekByte(); |
| 57 | test_Memory_peekShort(); |
| 58 | test_Memory_peekInt(); |
| 59 | test_Memory_peekLong(); |
| 60 | test_Memory_pokeByte(); |
| 61 | test_Memory_pokeShort(); |
| 62 | test_Memory_pokeInt(); |
| 63 | test_Memory_pokeLong(); |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 66 | /* |
| 67 | * Determine if two floating point numbers are approximately equal. |
| 68 | * |
| 69 | * (Assumes that floating point is generally working, so we can't use |
| 70 | * this for the first set of tests.) |
| 71 | */ |
| 72 | static boolean approxEqual(float a, float b, float maxDelta) { |
| 73 | if (a > b) |
| 74 | return (a - b) < maxDelta; |
| 75 | else |
| 76 | return (b - a) < maxDelta; |
| 77 | } |
| 78 | static boolean approxEqual(double a, double b, double maxDelta) { |
| 79 | if (a > b) |
| 80 | return (a - b) < maxDelta; |
| 81 | else |
| 82 | return (b - a) < maxDelta; |
| 83 | } |
| 84 | |
Andreas Gampe | 7a94961 | 2014-07-08 11:03:59 -0700 | [diff] [blame] | 85 | /** |
| 86 | * Will test inlining Thread.currentThread(). |
| 87 | */ |
| 88 | public static void test_Thread_currentThread() { |
| 89 | // 1. Do not use result. |
| 90 | Thread.currentThread(); |
| 91 | |
| 92 | // 2. Result should not be null. |
| 93 | Assert.assertNotNull(Thread.currentThread()); |
| 94 | } |
| 95 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 96 | public static void test_String_length() { |
| 97 | String str0 = ""; |
| 98 | String str1 = "x"; |
| 99 | String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; |
| 100 | |
| 101 | Assert.assertEquals(str0.length(), 0); |
| 102 | Assert.assertEquals(str1.length(), 1); |
| 103 | Assert.assertEquals(str80.length(), 80); |
| 104 | |
| 105 | String strNull = null; |
| 106 | try { |
| 107 | strNull.length(); |
| 108 | Assert.fail(); |
| 109 | } catch (NullPointerException expected) { |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | public static void test_String_isEmpty() { |
| 114 | String str0 = ""; |
| 115 | String str1 = "x"; |
| 116 | |
| 117 | Assert.assertTrue(str0.isEmpty()); |
| 118 | Assert.assertFalse(str1.isEmpty()); |
| 119 | |
| 120 | String strNull = null; |
| 121 | try { |
| 122 | strNull.isEmpty(); |
| 123 | Assert.fail(); |
| 124 | } catch (NullPointerException expected) { |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | public static void test_String_charAt() { |
| 129 | String testStr = "Now is the time"; |
| 130 | |
| 131 | Assert.assertEquals('N', testStr.charAt(0)); |
| 132 | Assert.assertEquals('o', testStr.charAt(1)); |
| 133 | Assert.assertEquals(' ', testStr.charAt(10)); |
| 134 | Assert.assertEquals('e', testStr.charAt(testStr.length()-1)); |
| 135 | |
| 136 | try { |
| 137 | testStr.charAt(-1); |
| 138 | Assert.fail(); |
| 139 | } catch (StringIndexOutOfBoundsException expected) { |
| 140 | } |
| 141 | try { |
| 142 | testStr.charAt(80); |
| 143 | Assert.fail(); |
| 144 | } catch (StringIndexOutOfBoundsException expected) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 147 | String strNull = null; |
| 148 | try { |
| 149 | strNull.charAt(0); |
| 150 | Assert.fail(); |
| 151 | } catch (NullPointerException expected) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 152 | } |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 153 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 154 | |
Yevgeny Rouban | 34fa0d9 | 2014-03-13 12:15:58 +0700 | [diff] [blame] | 155 | static int start; |
Alexei Zavjalov | a1758d8 | 2014-04-17 01:55:43 +0700 | [diff] [blame] | 156 | private static int[] negIndex = { -100000 }; |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 157 | public static void test_String_indexOf() { |
| 158 | String str0 = ""; |
Yevgeny Rouban | 34fa0d9 | 2014-03-13 12:15:58 +0700 | [diff] [blame] | 159 | String str1 = "/"; |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 160 | String str3 = "abc"; |
| 161 | String str10 = "abcdefghij"; |
| 162 | String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc"; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 163 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 164 | int supplementaryChar = 0x20b9f; |
| 165 | String surrogatePair = "\ud842\udf9f"; |
| 166 | String stringWithSurrogates = "hello " + surrogatePair + " world"; |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 168 | Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length()); |
| 169 | Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length()); |
| 170 | Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6); |
| 171 | Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1); |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 172 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 173 | Assert.assertEquals(str0.indexOf('a'), -1); |
| 174 | Assert.assertEquals(str3.indexOf('a'), 0); |
| 175 | Assert.assertEquals(str3.indexOf('b'), 1); |
| 176 | Assert.assertEquals(str3.indexOf('c'), 2); |
| 177 | Assert.assertEquals(str10.indexOf('j'), 9); |
| 178 | Assert.assertEquals(str40.indexOf('a'), 0); |
| 179 | Assert.assertEquals(str40.indexOf('b'), 38); |
| 180 | Assert.assertEquals(str40.indexOf('c'), 39); |
| 181 | Assert.assertEquals(str0.indexOf('a',20), -1); |
| 182 | Assert.assertEquals(str0.indexOf('a',0), -1); |
| 183 | Assert.assertEquals(str0.indexOf('a',-1), -1); |
Yevgeny Rouban | 34fa0d9 | 2014-03-13 12:15:58 +0700 | [diff] [blame] | 184 | Assert.assertEquals(str1.indexOf('/',++start), -1); |
Alexei Zavjalov | a1758d8 | 2014-04-17 01:55:43 +0700 | [diff] [blame] | 185 | Assert.assertEquals(str1.indexOf('a',negIndex[0]), -1); |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 186 | Assert.assertEquals(str3.indexOf('a',0), 0); |
| 187 | Assert.assertEquals(str3.indexOf('a',1), -1); |
| 188 | Assert.assertEquals(str3.indexOf('a',1234), -1); |
| 189 | Assert.assertEquals(str3.indexOf('b',0), 1); |
| 190 | Assert.assertEquals(str3.indexOf('b',1), 1); |
| 191 | Assert.assertEquals(str3.indexOf('c',2), 2); |
| 192 | Assert.assertEquals(str10.indexOf('j',5), 9); |
| 193 | Assert.assertEquals(str10.indexOf('j',9), 9); |
| 194 | Assert.assertEquals(str40.indexOf('a',10), 10); |
| 195 | Assert.assertEquals(str40.indexOf('b',40), -1); |
| 196 | |
| 197 | String strNull = null; |
| 198 | try { |
| 199 | strNull.indexOf('a'); |
| 200 | Assert.fail(); |
| 201 | } catch (NullPointerException expected) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 202 | } |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 203 | try { |
| 204 | strNull.indexOf('a', 0); |
| 205 | Assert.fail(); |
| 206 | } catch (NullPointerException expected) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 207 | } |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 208 | try { |
| 209 | strNull.indexOf('a', -1); |
| 210 | Assert.fail(); |
| 211 | } catch (NullPointerException expected) { |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 212 | } |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 213 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 214 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 215 | public static void test_String_compareTo() { |
| 216 | String test = "0123456789"; |
| 217 | String test1 = new String("0123456789"); // different object |
| 218 | String test2 = new String("0123456780"); // different value |
| 219 | String offset = new String("xxx0123456789yyy"); |
| 220 | String sub = offset.substring(3, 13); |
| 221 | String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; |
| 222 | String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy"; |
| 223 | String lc = "abcdefg"; |
| 224 | String uc = "ABCDEFG"; |
| 225 | Object blah = new Object(); |
| 226 | |
| 227 | Assert.assertTrue(lc.toUpperCase().equals(uc)); |
| 228 | |
| 229 | Assert.assertEquals(str32.compareTo(str33), -1); |
| 230 | Assert.assertEquals(str33.compareTo(str32), 1); |
| 231 | |
| 232 | Assert.assertTrue(test.equals(test)); |
| 233 | Assert.assertTrue(test.equals(test1)); |
| 234 | Assert.assertFalse(test.equals(test2)); |
| 235 | |
| 236 | Assert.assertEquals(test.compareTo(test1), 0); |
| 237 | Assert.assertTrue(test1.compareTo(test2) > 0); |
| 238 | Assert.assertTrue(test2.compareTo(test1) < 0); |
| 239 | |
| 240 | // Compare string with a nonzero offset, in left/right side. |
| 241 | Assert.assertEquals(test.compareTo(sub), 0); |
| 242 | Assert.assertEquals(sub.compareTo(test), 0); |
| 243 | Assert.assertTrue(test.equals(sub)); |
| 244 | Assert.assertTrue(sub.equals(test)); |
| 245 | // Same base, one is a substring. |
| 246 | Assert.assertFalse(offset.equals(sub)); |
| 247 | Assert.assertFalse(sub.equals(offset)); |
| 248 | // Wrong class. |
| 249 | Assert.assertFalse(test.equals(blah)); |
| 250 | |
| 251 | // Null lhs - throw. |
| 252 | try { |
| 253 | test.compareTo(null); |
| 254 | Assert.fail("didn't get expected npe"); |
| 255 | } catch (NullPointerException npe) { |
| 256 | } |
| 257 | // Null rhs - okay. |
| 258 | Assert.assertFalse(test.equals(null)); |
| 259 | |
| 260 | test = test.substring(1); |
| 261 | Assert.assertTrue(test.equals("123456789")); |
| 262 | Assert.assertFalse(test.equals(test1)); |
| 263 | |
| 264 | test = test.substring(1); |
| 265 | Assert.assertTrue(test.equals("23456789")); |
| 266 | |
| 267 | test = test.substring(1); |
| 268 | Assert.assertTrue(test.equals("3456789")); |
| 269 | |
| 270 | test = test.substring(1); |
| 271 | Assert.assertTrue(test.equals("456789")); |
| 272 | |
| 273 | test = test.substring(3,5); |
| 274 | Assert.assertTrue(test.equals("78")); |
| 275 | |
| 276 | test = "this/is/a/path"; |
| 277 | String[] strings = test.split("/"); |
| 278 | Assert.assertEquals(4, strings.length); |
| 279 | |
| 280 | Assert.assertEquals("this is a path", test.replaceAll("/", " ")); |
| 281 | Assert.assertEquals("this is a path", test.replace("/", " ")); |
| 282 | } |
| 283 | |
| 284 | public static void test_Math_abs_I() { |
| 285 | Assert.assertEquals(Math.abs(0), 0); |
| 286 | Assert.assertEquals(Math.abs(123), 123); |
| 287 | Assert.assertEquals(Math.abs(-123), 123); |
| 288 | Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); |
| 289 | Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); |
| 290 | Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); |
Sebastien Hertz | bf1442d | 2013-03-05 15:12:40 +0100 | [diff] [blame] | 291 | Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | public static void test_Math_abs_J() { |
| 295 | Assert.assertEquals(Math.abs(0L), 0L); |
| 296 | Assert.assertEquals(Math.abs(123L), 123L); |
| 297 | Assert.assertEquals(Math.abs(-123L), 123L); |
| 298 | Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE); |
| 299 | Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE); |
| 300 | Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); |
| 301 | } |
| 302 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 303 | public static void test_Math_min_I() { |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 304 | Assert.assertEquals(Math.min(0, 0), 0); |
| 305 | Assert.assertEquals(Math.min(1, 0), 0); |
| 306 | Assert.assertEquals(Math.min(0, 1), 0); |
| 307 | Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0); |
| 308 | Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); |
| 309 | Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); |
| 310 | } |
| 311 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 312 | public static void test_Math_max_I() { |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 313 | Assert.assertEquals(Math.max(0, 0), 0); |
| 314 | Assert.assertEquals(Math.max(1, 0), 1); |
| 315 | Assert.assertEquals(Math.max(0, 1), 1); |
| 316 | Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); |
| 317 | Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0); |
| 318 | Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); |
| 319 | } |
| 320 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 321 | public static void test_Math_min_J() { |
| 322 | Assert.assertEquals(Math.min(0L, 0L), 0L); |
| 323 | Assert.assertEquals(Math.min(1L, 0L), 0L); |
| 324 | Assert.assertEquals(Math.min(0L, 1L), 0L); |
| 325 | Assert.assertEquals(Math.min(0L, Long.MAX_VALUE), 0L); |
| 326 | Assert.assertEquals(Math.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE); |
| 327 | Assert.assertEquals(Math.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE); |
| 328 | } |
| 329 | |
| 330 | public static void test_Math_max_J() { |
| 331 | Assert.assertEquals(Math.max(0L, 0L), 0L); |
| 332 | Assert.assertEquals(Math.max(1L, 0L), 1L); |
| 333 | Assert.assertEquals(Math.max(0L, 1L), 1L); |
| 334 | Assert.assertEquals(Math.max(0L, Long.MAX_VALUE), Long.MAX_VALUE); |
| 335 | Assert.assertEquals(Math.max(Long.MIN_VALUE, 0L), 0L); |
| 336 | Assert.assertEquals(Math.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE); |
| 337 | } |
| 338 | |
| 339 | public static void test_Math_min_F() { |
| 340 | Assert.assertTrue(approxEqual(Math.min(0.0f, 0.0f), 0.0f, 0.001f)); |
| 341 | Assert.assertTrue(approxEqual(Math.min(1.0f, 0.0f), 0.0f, 0.001f)); |
| 342 | Assert.assertTrue(approxEqual(Math.min(0.0f, 1.0f), 0.0f, 0.001f)); |
| 343 | Assert.assertTrue(approxEqual(Math.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f)); |
| 344 | Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f)); |
| 345 | Assert.assertTrue(approxEqual(Math.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f)); |
| 346 | } |
| 347 | |
| 348 | public static void test_Math_max_F() { |
| 349 | Assert.assertTrue(approxEqual(Math.max(0.0f, 0.0f), 0.0f, 0.001f)); |
| 350 | Assert.assertTrue(approxEqual(Math.max(1.0f, 0.0f), 1.0f, 0.001f)); |
| 351 | Assert.assertTrue(approxEqual(Math.max(0.0f, 1.0f), 1.0f, 0.001f)); |
| 352 | Assert.assertTrue(approxEqual(Math.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f)); |
| 353 | Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f)); |
| 354 | Assert.assertTrue(approxEqual(Math.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f)); |
| 355 | } |
| 356 | |
| 357 | public static void test_Math_min_D() { |
| 358 | Assert.assertTrue(approxEqual(Math.min(0.0d, 0.0d), 0.0d, 0.001d)); |
| 359 | Assert.assertTrue(approxEqual(Math.min(1.0d, 0.0d), 0.0d, 0.001d)); |
| 360 | Assert.assertTrue(approxEqual(Math.min(0.0d, 1.0d), 0.0d, 0.001d)); |
| 361 | Assert.assertTrue(approxEqual(Math.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d)); |
| 362 | Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d)); |
| 363 | Assert.assertTrue(approxEqual(Math.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d)); |
| 364 | } |
| 365 | |
| 366 | public static void test_Math_max_D() { |
| 367 | Assert.assertTrue(approxEqual(Math.max(0.0d, 0.0d), 0.0d, 0.001d)); |
| 368 | Assert.assertTrue(approxEqual(Math.max(1.0d, 0.0d), 1.0d, 0.001d)); |
| 369 | Assert.assertTrue(approxEqual(Math.max(0.0d, 1.0d), 1.0d, 0.001d)); |
| 370 | Assert.assertTrue(approxEqual(Math.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d)); |
| 371 | Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d)); |
| 372 | Assert.assertTrue(approxEqual(Math.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d)); |
| 373 | } |
| 374 | |
Sebastien Hertz | bf1442d | 2013-03-05 15:12:40 +0100 | [diff] [blame] | 375 | public static void test_StrictMath_abs_I() { |
| 376 | Assert.assertEquals(StrictMath.abs(0), 0); |
| 377 | Assert.assertEquals(StrictMath.abs(123), 123); |
| 378 | Assert.assertEquals(StrictMath.abs(-123), 123); |
| 379 | Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE); |
| 380 | Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE); |
| 381 | Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE); |
| 382 | Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE); |
| 383 | } |
| 384 | |
| 385 | public static void test_StrictMath_abs_J() { |
| 386 | Assert.assertEquals(StrictMath.abs(0L), 0L); |
| 387 | Assert.assertEquals(StrictMath.abs(123L), 123L); |
| 388 | Assert.assertEquals(StrictMath.abs(-123L), 123L); |
| 389 | Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE); |
| 390 | Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE); |
| 391 | Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE); |
| 392 | } |
| 393 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 394 | public static void test_StrictMath_min_I() { |
Sebastien Hertz | bf1442d | 2013-03-05 15:12:40 +0100 | [diff] [blame] | 395 | Assert.assertEquals(StrictMath.min(0, 0), 0); |
| 396 | Assert.assertEquals(StrictMath.min(1, 0), 0); |
| 397 | Assert.assertEquals(StrictMath.min(0, 1), 0); |
| 398 | Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0); |
| 399 | Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE); |
| 400 | Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE); |
| 401 | } |
| 402 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 403 | public static void test_StrictMath_max_I() { |
Sebastien Hertz | bf1442d | 2013-03-05 15:12:40 +0100 | [diff] [blame] | 404 | Assert.assertEquals(StrictMath.max(0, 0), 0); |
| 405 | Assert.assertEquals(StrictMath.max(1, 0), 1); |
| 406 | Assert.assertEquals(StrictMath.max(0, 1), 1); |
| 407 | Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE); |
| 408 | Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0); |
| 409 | Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE); |
| 410 | } |
| 411 | |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 412 | public static void test_StrictMath_min_J() { |
| 413 | Assert.assertEquals(StrictMath.min(0L, 0L), 0L); |
| 414 | Assert.assertEquals(StrictMath.min(1L, 0L), 0L); |
| 415 | Assert.assertEquals(StrictMath.min(0L, 1L), 0L); |
| 416 | Assert.assertEquals(StrictMath.min(0L, Long.MAX_VALUE), 0L); |
| 417 | Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, 0L), Long.MIN_VALUE); |
| 418 | Assert.assertEquals(StrictMath.min(Long.MIN_VALUE, Long.MAX_VALUE), Long.MIN_VALUE); |
| 419 | } |
| 420 | |
| 421 | public static void test_StrictMath_max_J() { |
| 422 | Assert.assertEquals(StrictMath.max(0L, 0L), 0L); |
| 423 | Assert.assertEquals(StrictMath.max(1L, 0L), 1L); |
| 424 | Assert.assertEquals(StrictMath.max(0L, 1L), 1L); |
| 425 | Assert.assertEquals(StrictMath.max(0L, Long.MAX_VALUE), Long.MAX_VALUE); |
| 426 | Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, 0L), 0L); |
| 427 | Assert.assertEquals(StrictMath.max(Long.MIN_VALUE, Long.MAX_VALUE), Long.MAX_VALUE); |
| 428 | } |
| 429 | |
| 430 | public static void test_StrictMath_min_F() { |
| 431 | Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 0.0f), 0.0f, 0.001f)); |
| 432 | Assert.assertTrue(approxEqual(StrictMath.min(1.0f, 0.0f), 0.0f, 0.001f)); |
| 433 | Assert.assertTrue(approxEqual(StrictMath.min(0.0f, 1.0f), 0.0f, 0.001f)); |
| 434 | Assert.assertTrue(approxEqual(StrictMath.min(0.0f, Float.MAX_VALUE), 0.0f, 0.001f)); |
| 435 | Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, 0.0f), Float.MIN_VALUE, 0.001f)); |
| 436 | Assert.assertTrue(approxEqual(StrictMath.min(Float.MIN_VALUE, Float.MAX_VALUE), Float.MIN_VALUE, 0.001f)); |
| 437 | } |
| 438 | |
| 439 | public static void test_StrictMath_max_F() { |
| 440 | Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 0.0f), 0.0f, 0.001f)); |
| 441 | Assert.assertTrue(approxEqual(StrictMath.max(1.0f, 0.0f), 1.0f, 0.001f)); |
| 442 | Assert.assertTrue(approxEqual(StrictMath.max(0.0f, 1.0f), 1.0f, 0.001f)); |
| 443 | Assert.assertTrue(approxEqual(StrictMath.max(0.0f, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f)); |
| 444 | Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, 0.0f), 0.0f, 0.001f)); |
| 445 | Assert.assertTrue(approxEqual(StrictMath.max(Float.MIN_VALUE, Float.MAX_VALUE), Float.MAX_VALUE, 0.001f)); |
| 446 | } |
| 447 | |
| 448 | public static void test_StrictMath_min_D() { |
| 449 | Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 0.0d), 0.0d, 0.001d)); |
| 450 | Assert.assertTrue(approxEqual(StrictMath.min(1.0d, 0.0d), 0.0d, 0.001d)); |
| 451 | Assert.assertTrue(approxEqual(StrictMath.min(0.0d, 1.0d), 0.0d, 0.001d)); |
| 452 | Assert.assertTrue(approxEqual(StrictMath.min(0.0d, Double.MAX_VALUE), 0.0d, 0.001d)); |
| 453 | Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, 0.0d), Double.MIN_VALUE, 0.001d)); |
| 454 | Assert.assertTrue(approxEqual(StrictMath.min(Double.MIN_VALUE, Double.MAX_VALUE), Double.MIN_VALUE, 0.001d)); |
| 455 | } |
| 456 | |
| 457 | public static void test_StrictMath_max_D() { |
| 458 | Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 0.0d), 0.0d, 0.001d)); |
| 459 | Assert.assertTrue(approxEqual(StrictMath.max(1.0d, 0.0d), 1.0d, 0.001d)); |
| 460 | Assert.assertTrue(approxEqual(StrictMath.max(0.0d, 1.0d), 1.0d, 0.001d)); |
| 461 | Assert.assertTrue(approxEqual(StrictMath.max(0.0d, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d)); |
| 462 | Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, 0.0d), 0.0d, 0.001d)); |
| 463 | Assert.assertTrue(approxEqual(StrictMath.max(Double.MIN_VALUE, Double.MAX_VALUE), Double.MAX_VALUE, 0.001d)); |
| 464 | } |
| 465 | |
Elliott Hughes | 28c384b | 2012-06-15 16:46:25 -0700 | [diff] [blame] | 466 | public static void test_Float_floatToRawIntBits() { |
| 467 | Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000); |
| 468 | Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0); |
| 469 | Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000); |
| 470 | Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000); |
| 471 | Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000); |
| 472 | Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000); |
| 473 | } |
| 474 | |
| 475 | public static void test_Float_intBitsToFloat() { |
| 476 | Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f); |
| 477 | Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f); |
| 478 | Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f); |
| 479 | Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN); |
| 480 | Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY); |
| 481 | Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY); |
| 482 | } |
| 483 | |
| 484 | public static void test_Double_doubleToRawLongBits() { |
| 485 | Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L); |
| 486 | Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L); |
| 487 | Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L); |
| 488 | Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L); |
| 489 | Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L); |
| 490 | Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L); |
| 491 | } |
| 492 | |
| 493 | public static void test_Double_longBitsToDouble() { |
| 494 | Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0); |
| 495 | Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0); |
| 496 | Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0); |
| 497 | Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN); |
| 498 | Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY); |
| 499 | Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY); |
| 500 | } |
Serban Constantinescu | 23abec9 | 2014-07-02 16:13:38 +0100 | [diff] [blame] | 501 | |
| 502 | public static void test_Integer_reverse() { |
| 503 | Assert.assertEquals(Integer.reverse(1), 0x80000000); |
| 504 | Assert.assertEquals(Integer.reverse(-1), 0xffffffff); |
| 505 | Assert.assertEquals(Integer.reverse(0), 0); |
| 506 | Assert.assertEquals(Integer.reverse(0x12345678), 0x1e6a2c48); |
| 507 | Assert.assertEquals(Integer.reverse(0x87654321), 0x84c2a6e1); |
| 508 | Assert.assertEquals(Integer.reverse(Integer.MAX_VALUE), 0xfffffffe); |
| 509 | Assert.assertEquals(Integer.reverse(Integer.MIN_VALUE), 1); |
| 510 | } |
| 511 | |
| 512 | public static void test_Long_reverse() { |
| 513 | Assert.assertEquals(Long.reverse(1L), 0x8000000000000000L); |
| 514 | Assert.assertEquals(Long.reverse(-1L), 0xffffffffffffffffL); |
| 515 | Assert.assertEquals(Long.reverse(0L), 0L); |
| 516 | // FIXME: This asserts fail with or without this patch. I have collected |
| 517 | // the expected results on my host machine. |
| 518 | // Assert.assertEquals(Long.reverse(0x1234567812345678L), 0x1e6a2c481e6a2c48L); |
| 519 | // Assert.assertEquals(Long.reverse(0x8765432187654321L), 0x84c2a6e184c2a6e1L); |
| 520 | // Assert.assertEquals(Long.reverse(Long.MAX_VALUE), 0xfffffffffffffffeL); |
| 521 | Assert.assertEquals(Long.reverse(Long.MIN_VALUE), 1L); |
| 522 | } |
| 523 | |
Alexei Zavjalov | eb24bae | 2014-07-08 16:27:17 +0700 | [diff] [blame^] | 524 | static Object runtime; |
| 525 | static Method address_of; |
| 526 | static Method peek_byte; |
| 527 | static Method peek_short; |
| 528 | static Method peek_int; |
| 529 | static Method peek_long; |
| 530 | static Method poke_byte; |
| 531 | static Method poke_short; |
| 532 | static Method poke_int; |
| 533 | static Method poke_long; |
| 534 | |
| 535 | public static void initSupportMethodsForPeekPoke() throws Exception { |
| 536 | Class<?> vm_runtime = Class.forName("dalvik.system.VMRuntime"); |
| 537 | Method get_runtime = vm_runtime.getDeclaredMethod("getRuntime"); |
| 538 | runtime = get_runtime.invoke(null); |
| 539 | address_of = vm_runtime.getDeclaredMethod("addressOf", Object.class); |
| 540 | |
| 541 | Class<?> io_memory = Class.forName("libcore.io.Memory"); |
| 542 | peek_byte = io_memory.getDeclaredMethod("peekByte", Long.TYPE); |
| 543 | peek_int = io_memory.getDeclaredMethod("peekInt", Long.TYPE, Boolean.TYPE); |
| 544 | peek_short = io_memory.getDeclaredMethod("peekShort", Long.TYPE, Boolean.TYPE); |
| 545 | peek_long = io_memory.getDeclaredMethod("peekLong", Long.TYPE, Boolean.TYPE); |
| 546 | poke_byte = io_memory.getDeclaredMethod("pokeByte", Long.TYPE, Byte.TYPE); |
| 547 | poke_short = io_memory.getDeclaredMethod("pokeShort", Long.TYPE, Short.TYPE, Boolean.TYPE); |
| 548 | poke_int = io_memory.getDeclaredMethod("pokeInt", Long.TYPE, Integer.TYPE, Boolean.TYPE); |
| 549 | poke_long = io_memory.getDeclaredMethod("pokeLong", Long.TYPE, Long.TYPE, Boolean.TYPE); |
| 550 | } |
| 551 | |
| 552 | public static void test_Memory_peekByte() throws Exception { |
| 553 | byte[] b = new byte [2]; |
| 554 | b[0] = 0x12; |
| 555 | b[1] = 0x11; |
| 556 | long address = (long)address_of.invoke(runtime, b); |
| 557 | Assert.assertEquals((byte)peek_byte.invoke(null, address), 0x12); |
| 558 | Assert.assertEquals((byte)peek_byte.invoke(null, address + 1), 0x11); |
| 559 | } |
| 560 | |
| 561 | public static void test_Memory_peekShort() throws Exception { |
| 562 | byte[] b = new byte [3]; |
| 563 | b[0] = 0x13; |
| 564 | b[1] = 0x12; |
| 565 | b[2] = 0x11; |
| 566 | long address = (long)address_of.invoke(runtime, b); |
| 567 | Assert.assertEquals((short)peek_short.invoke(null, address, false), 0x1213); // Aligned read |
| 568 | Assert.assertEquals((short)peek_short.invoke(null, address + 1, false), 0x1112); // Unaligned read |
| 569 | } |
| 570 | |
| 571 | public static void test_Memory_peekInt() throws Exception { |
| 572 | byte[] b = new byte [5]; |
| 573 | b[0] = 0x15; |
| 574 | b[1] = 0x14; |
| 575 | b[2] = 0x13; |
| 576 | b[3] = 0x12; |
| 577 | b[4] = 0x11; |
| 578 | long address = (long)address_of.invoke(runtime, b); |
| 579 | Assert.assertEquals((int)peek_int.invoke(null, address, false), 0x12131415); |
| 580 | Assert.assertEquals((int)peek_int.invoke(null, address + 1, false), 0x11121314); |
| 581 | } |
| 582 | |
| 583 | public static void test_Memory_peekLong() throws Exception { |
| 584 | byte[] b = new byte [9]; |
| 585 | b[0] = 0x19; |
| 586 | b[1] = 0x18; |
| 587 | b[2] = 0x17; |
| 588 | b[3] = 0x16; |
| 589 | b[4] = 0x15; |
| 590 | b[5] = 0x14; |
| 591 | b[6] = 0x13; |
| 592 | b[7] = 0x12; |
| 593 | b[8] = 0x11; |
| 594 | long address = (long)address_of.invoke(runtime, b); |
| 595 | Assert.assertEquals((long)peek_long.invoke(null, address, false), 0x1213141516171819L); |
| 596 | Assert.assertEquals((long)peek_long.invoke(null, address + 1, false), 0x1112131415161718L); |
| 597 | } |
| 598 | |
| 599 | public static void test_Memory_pokeByte() throws Exception { |
| 600 | byte[] r = {0x11, 0x12}; |
| 601 | byte[] b = new byte [2]; |
| 602 | long address = (long)address_of.invoke(runtime, b); |
| 603 | poke_byte.invoke(null, address, (byte)0x11); |
| 604 | poke_byte.invoke(null, address + 1, (byte)0x12); |
| 605 | Assert.assertTrue(Arrays.equals(r, b)); |
| 606 | } |
| 607 | |
| 608 | public static void test_Memory_pokeShort() throws Exception { |
| 609 | byte[] ra = {0x12, 0x11, 0x13}; |
| 610 | byte[] ru = {0x12, 0x22, 0x21}; |
| 611 | byte[] b = new byte [3]; |
| 612 | long address = (long)address_of.invoke(runtime, b); |
| 613 | |
| 614 | // Aligned write |
| 615 | b[2] = 0x13; |
| 616 | poke_short.invoke(null, address, (short)0x1112, false); |
| 617 | Assert.assertTrue(Arrays.equals(ra, b)); |
| 618 | |
| 619 | // Unaligned write |
| 620 | poke_short.invoke(null, address + 1, (short)0x2122, false); |
| 621 | Assert.assertTrue(Arrays.equals(ru, b)); |
| 622 | } |
| 623 | |
| 624 | public static void test_Memory_pokeInt() throws Exception { |
| 625 | byte[] ra = {0x14, 0x13, 0x12, 0x11, 0x15}; |
| 626 | byte[] ru = {0x14, 0x24, 0x23, 0x22, 0x21}; |
| 627 | byte[] b = new byte [5]; |
| 628 | long address = (long)address_of.invoke(runtime, b); |
| 629 | |
| 630 | b[4] = 0x15; |
| 631 | poke_int.invoke(null, address, (int)0x11121314, false); |
| 632 | Assert.assertTrue(Arrays.equals(ra, b)); |
| 633 | |
| 634 | poke_int.invoke(null, address + 1, (int)0x21222324, false); |
| 635 | Assert.assertTrue(Arrays.equals(ru, b)); |
| 636 | } |
| 637 | |
| 638 | public static void test_Memory_pokeLong() throws Exception { |
| 639 | byte[] ra = {0x18, 0x17, 0x16, 0x15, 0x14, 0x13, 0x12, 0x11, 0x19}; |
| 640 | byte[] ru = {0x18, 0x28, 0x27, 0x26, 0x25, 0x24, 0x23, 0x22, 0x21}; |
| 641 | byte[] b = new byte [9]; |
| 642 | long address = (long)address_of.invoke(runtime, b); |
| 643 | |
| 644 | b[8] = 0x19; |
| 645 | poke_long.invoke(null, address, (long)0x1112131415161718L, false); |
| 646 | Assert.assertTrue(Arrays.equals(ra, b)); |
| 647 | |
| 648 | poke_long.invoke(null, address + 1, (long)0x2122232425262728L, false); |
| 649 | Assert.assertTrue(Arrays.equals(ru, b)); |
| 650 | } |
jeffhao | 5d1ac92 | 2011-09-29 17:41:15 -0700 | [diff] [blame] | 651 | } |