| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 class MathTest { | 5 class MathTest { |
| 6 static void testConstants() { | 6 static void testConstants() { |
| 7 // Source for mathematical constants is Wolfram Alpha. | 7 // Source for mathematical constants is Wolfram Alpha. |
| 8 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, | 8 Expect.equals(2.7182818284590452353602874713526624977572470936999595749669, |
| 9 Math.E); | 9 Math.E); |
| 10 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, | 10 Expect.equals(2.3025850929940456840179914546843642076011014886287729760333, |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 Expect.equals(-0xabcdef, Math.parseInt("-0x00000abcdef")); | 196 Expect.equals(-0xabcdef, Math.parseInt("-0x00000abcdef")); |
| 197 Expect.equals(-0xABCDEF, Math.parseInt("-0x00000ABCDEF")); | 197 Expect.equals(-0xABCDEF, Math.parseInt("-0x00000ABCDEF")); |
| 198 Expect.equals(0xabcdef, Math.parseInt(" 0x00000abcdef ")); | 198 Expect.equals(0xabcdef, Math.parseInt(" 0x00000abcdef ")); |
| 199 Expect.equals(0xABCDEF, Math.parseInt(" 0x00000ABCDEF ")); | 199 Expect.equals(0xABCDEF, Math.parseInt(" 0x00000ABCDEF ")); |
| 200 Expect.equals(-0xabcdef, Math.parseInt(" -0x00000abcdef ")); | 200 Expect.equals(-0xabcdef, Math.parseInt(" -0x00000abcdef ")); |
| 201 Expect.equals(-0xABCDEF, Math.parseInt(" -0x00000ABCDEF ")); | 201 Expect.equals(-0xABCDEF, Math.parseInt(" -0x00000ABCDEF ")); |
| 202 Expect.equals(10, Math.parseInt("010")); | 202 Expect.equals(10, Math.parseInt("010")); |
| 203 Expect.equals(-10, Math.parseInt("-010")); | 203 Expect.equals(-10, Math.parseInt("-010")); |
| 204 Expect.equals(10, Math.parseInt(" 010 ")); | 204 Expect.equals(10, Math.parseInt(" 010 ")); |
| 205 Expect.equals(-10, Math.parseInt(" -010 ")); | 205 Expect.equals(-10, Math.parseInt(" -010 ")); |
| 206 Expect.equals(9, Math.parseInt("09")); |
| 207 Expect.equals(9, Math.parseInt(" 09 ")); |
| 208 Expect.equals(-9, Math.parseInt("-09")); |
| 206 Expect.equals(true, parseIntThrowsBadNumberFormatException("1b")); | 209 Expect.equals(true, parseIntThrowsBadNumberFormatException("1b")); |
| 207 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1b ")); | 210 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1b ")); |
| 208 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1 b ")); | 211 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1 b ")); |
| 209 Expect.equals(true, parseIntThrowsBadNumberFormatException("1e2")); | 212 Expect.equals(true, parseIntThrowsBadNumberFormatException("1e2")); |
| 210 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1e2 ")); | 213 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 1e2 ")); |
| 211 Expect.equals(true, parseIntThrowsBadNumberFormatException("00x12")); | 214 Expect.equals(true, parseIntThrowsBadNumberFormatException("00x12")); |
| 212 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 00x12 ")); | 215 Expect.equals(true, parseIntThrowsBadNumberFormatException(" 00x12 ")); |
| 213 Expect.equals(true, parseIntThrowsBadNumberFormatException("-1b")); | 216 Expect.equals(true, parseIntThrowsBadNumberFormatException("-1b")); |
| 214 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1b ")); | 217 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1b ")); |
| 215 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1 b ")); | 218 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1 b ")); |
| 216 Expect.equals(true, parseIntThrowsBadNumberFormatException("-1e2")); | 219 Expect.equals(true, parseIntThrowsBadNumberFormatException("-1e2")); |
| 217 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1e2 ")); | 220 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -1e2 ")); |
| 218 Expect.equals(true, parseIntThrowsBadNumberFormatException("-00x12")); | 221 Expect.equals(true, parseIntThrowsBadNumberFormatException("-00x12")); |
| 219 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -00x12 ")); | 222 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -00x12 ")); |
| 220 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -00x12 ")); | 223 Expect.equals(true, parseIntThrowsBadNumberFormatException(" -00x12 ")); |
| 221 Expect.equals(true, parseIntThrowsBadNumberFormatException("0x0x12")); | 224 Expect.equals(true, parseIntThrowsBadNumberFormatException("0x0x12")); |
| 222 Expect.equals(true, parseIntThrowsBadNumberFormatException("0.1")); | 225 Expect.equals(true, parseIntThrowsBadNumberFormatException("0.1")); |
| 223 Expect.equals(true, parseIntThrowsBadNumberFormatException("0x3.1")); | 226 Expect.equals(true, parseIntThrowsBadNumberFormatException("0x3.1")); |
| 224 Expect.equals(true, parseIntThrowsBadNumberFormatException("5.")); | 227 Expect.equals(true, parseIntThrowsBadNumberFormatException("5.")); |
| 225 Expect.equals(true, parseIntThrowsBadNumberFormatException("+-5")); | 228 Expect.equals(true, parseIntThrowsBadNumberFormatException("+-5")); |
| 226 Expect.equals(true, parseIntThrowsBadNumberFormatException("-+5")); | 229 Expect.equals(true, parseIntThrowsBadNumberFormatException("-+5")); |
| 227 Expect.equals(true, parseIntThrowsBadNumberFormatException("--5")); | 230 Expect.equals(true, parseIntThrowsBadNumberFormatException("--5")); |
| 228 Expect.equals(true, parseIntThrowsBadNumberFormatException("++5")); | 231 Expect.equals(true, parseIntThrowsBadNumberFormatException("++5")); |
| 229 Expect.equals(true, parseIntThrowsBadNumberFormatException("+ 5")); | 232 Expect.equals(true, parseIntThrowsBadNumberFormatException("+ 5")); |
| 230 Expect.equals(true, parseIntThrowsBadNumberFormatException("- 5")); | 233 Expect.equals(true, parseIntThrowsBadNumberFormatException("- 5")); |
| 231 Expect.equals(true, parseIntThrowsBadNumberFormatException("")); | 234 Expect.equals(true, parseIntThrowsBadNumberFormatException("")); |
| 232 Expect.equals(true, parseIntThrowsBadNumberFormatException(" ")); | 235 Expect.equals(true, parseIntThrowsBadNumberFormatException(" ")); |
| 233 } | 236 } |
| 234 | 237 |
| 235 static bool parseDoubleThrowsBadNumberFormatException(str) { | |
| 236 try { | |
| 237 Math.parseDouble(str); | |
| 238 return false; | |
| 239 } catch (BadNumberFormatException e) { | |
| 240 return true; | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 static void testParseDouble() { | |
| 245 Expect.equals(499.0, Math.parseDouble("499")); | |
| 246 Expect.equals(499.0, Math.parseDouble("499.0")); | |
| 247 Expect.equals(499.0, Math.parseDouble("499.0")); | |
| 248 Expect.equals(499.0, Math.parseDouble("+499")); | |
| 249 Expect.equals(-499.0, Math.parseDouble("-499")); | |
| 250 Expect.equals(499.0, Math.parseDouble(" 499 ")); | |
| 251 Expect.equals(499.0, Math.parseDouble(" +499 ")); | |
| 252 Expect.equals(-499.0, Math.parseDouble(" -499 ")); | |
| 253 Expect.equals(0.0, Math.parseDouble("0")); | |
| 254 Expect.equals(0.0, Math.parseDouble("+0")); | |
| 255 Expect.equals(-0.0, Math.parseDouble("-0")); | |
| 256 Expect.equals(true, Math.parseDouble("-0").isNegative()); | |
| 257 Expect.equals(0.0, Math.parseDouble(" 0 ")); | |
| 258 Expect.equals(0.0, Math.parseDouble(" +0 ")); | |
| 259 Expect.equals(-0.0, Math.parseDouble(" -0 ")); | |
| 260 Expect.equals(true, Math.parseDouble(" -0 ").isNegative()); | |
| 261 Expect.equals(1.0 * 0x1234567890, Math.parseDouble("0x1234567890")); | |
| 262 Expect.equals(1.0 * 0x1234567890, Math.parseDouble("+0x1234567890")); | |
| 263 Expect.equals(1.0 * -0x1234567890, Math.parseDouble("-0x1234567890")); | |
| 264 Expect.equals(1.0 * 0x1234567890, Math.parseDouble(" 0x1234567890 ")); | |
| 265 Expect.equals(1.0 * 0x1234567890, Math.parseDouble(" +0x1234567890 ")); | |
| 266 Expect.equals(1.0 * -0x1234567890, Math.parseDouble(" -0x1234567890 ")); | |
| 267 Expect.equals(256.0, Math.parseDouble("0x100")); | |
| 268 Expect.equals(256.0, Math.parseDouble("+0x100")); | |
| 269 Expect.equals(-256.0, Math.parseDouble("-0x100")); | |
| 270 Expect.equals(256.0, Math.parseDouble(" 0x100 ")); | |
| 271 Expect.equals(256.0, Math.parseDouble(" +0x100 ")); | |
| 272 Expect.equals(-256.0, Math.parseDouble(" -0x100 ")); | |
| 273 Expect.equals(1.0 * 0xabcdef, Math.parseDouble("0xabcdef")); | |
| 274 Expect.equals(1.0 * 0xABCDEF, Math.parseDouble("0xABCDEF")); | |
| 275 Expect.equals(1.0 * 0xabcdef, Math.parseDouble("0xabCDEf")); | |
| 276 Expect.equals(1.0 * -0xabcdef, Math.parseDouble("-0xabcdef")); | |
| 277 Expect.equals(1.0 * -0xABCDEF, Math.parseDouble("-0xABCDEF")); | |
| 278 Expect.equals(1.0 * 0xabcdef, Math.parseDouble(" 0xabcdef ")); | |
| 279 Expect.equals(1.0 * 0xABCDEF, Math.parseDouble(" 0xABCDEF ")); | |
| 280 Expect.equals(1.0 * -0xabcdef, Math.parseDouble(" -0xabcdef ")); | |
| 281 Expect.equals(1.0 * -0xABCDEF, Math.parseDouble(" -0xABCDEF ")); | |
| 282 Expect.equals(1.0 * 0xabcdef, Math.parseDouble("0x00000abcdef")); | |
| 283 Expect.equals(1.0 * 0xABCDEF, Math.parseDouble("0x00000ABCDEF")); | |
| 284 Expect.equals(1.0 * -0xabcdef, Math.parseDouble("-0x00000abcdef")); | |
| 285 Expect.equals(1.0 * -0xABCDEF, Math.parseDouble("-0x00000ABCDEF")); | |
| 286 Expect.equals(1.0 * 0xabcdef, Math.parseDouble(" 0x00000abcdef ")); | |
| 287 Expect.equals(1.0 * 0xABCDEF, Math.parseDouble(" 0x00000ABCDEF ")); | |
| 288 Expect.equals(1.0 * -0xabcdef, Math.parseDouble(" -0x00000abcdef ")); | |
| 289 Expect.equals(1.0 * -0xABCDEF, Math.parseDouble(" -0x00000ABCDEF ")); | |
| 290 Expect.equals(10.0, Math.parseDouble("010")); | |
| 291 Expect.equals(-10.0, Math.parseDouble("-010")); | |
| 292 Expect.equals(10.0, Math.parseDouble(" 010 ")); | |
| 293 Expect.equals(-10.0, Math.parseDouble(" -010 ")); | |
| 294 Expect.equals(0.1, Math.parseDouble("0.1")); | |
| 295 Expect.equals(0.1, Math.parseDouble(" 0.1 ")); | |
| 296 Expect.equals(0.1, Math.parseDouble(" +0.1 ")); | |
| 297 Expect.equals(-0.1, Math.parseDouble(" -0.1 ")); | |
| 298 Expect.equals(0.1, Math.parseDouble(".1")); | |
| 299 Expect.equals(0.1, Math.parseDouble(" .1 ")); | |
| 300 Expect.equals(0.1, Math.parseDouble(" +.1 ")); | |
| 301 Expect.equals(-0.1, Math.parseDouble(" -.1 ")); | |
| 302 Expect.equals(1234567.89, Math.parseDouble("1234567.89")); | |
| 303 Expect.equals(1234567.89, Math.parseDouble(" 1234567.89 ")); | |
| 304 Expect.equals(1234567.89, Math.parseDouble(" +1234567.89 ")); | |
| 305 Expect.equals(-1234567.89, Math.parseDouble(" -1234567.89 ")); | |
| 306 Expect.equals(1234567e89, Math.parseDouble("1234567e89")); | |
| 307 Expect.equals(1234567e89, Math.parseDouble(" 1234567e89 ")); | |
| 308 Expect.equals(1234567e89, Math.parseDouble(" +1234567e89 ")); | |
| 309 Expect.equals(-1234567e89, Math.parseDouble(" -1234567e89 ")); | |
| 310 Expect.equals(1234567.89e2, Math.parseDouble("1234567.89e2")); | |
| 311 Expect.equals(1234567.89e2, Math.parseDouble(" 1234567.89e2 ")); | |
| 312 Expect.equals(1234567.89e2, Math.parseDouble(" +1234567.89e2 ")); | |
| 313 Expect.equals(-1234567.89e2, Math.parseDouble(" -1234567.89e2 ")); | |
| 314 Expect.equals(1234567.89e2, Math.parseDouble("1234567.89E2")); | |
| 315 Expect.equals(1234567.89e2, Math.parseDouble(" 1234567.89E2 ")); | |
| 316 Expect.equals(1234567.89e2, Math.parseDouble(" +1234567.89E2 ")); | |
| 317 Expect.equals(-1234567.89e2, Math.parseDouble(" -1234567.89E2 ")); | |
| 318 Expect.equals(1234567.89e-2, Math.parseDouble("1234567.89e-2")); | |
| 319 Expect.equals(1234567.89e-2, Math.parseDouble(" 1234567.89e-2 ")); | |
| 320 Expect.equals(1234567.89e-2, Math.parseDouble(" +1234567.89e-2 ")); | |
| 321 Expect.equals(-1234567.89e-2, Math.parseDouble(" -1234567.89e-2 ")); | |
| 322 // TODO(floitsch): add tests for NaN and Infinity. | |
| 323 Expect.equals(false, parseDoubleThrowsBadNumberFormatException("1.5")); | |
| 324 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("1b")); | |
| 325 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 1b ")); | |
| 326 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 1 b ")); | |
| 327 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" e3 ")); | |
| 328 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" .e3 ")); | |
| 329 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("00x12")); | |
| 330 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 00x12 ")); | |
| 331 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("-1b")); | |
| 332 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -1b ")); | |
| 333 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -1 b ")); | |
| 334 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("-00x12")); | |
| 335 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -00x12 ")); | |
| 336 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -00x12 ")); | |
| 337 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("0x0x12")); | |
| 338 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("+ 1.5")); | |
| 339 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("- 1.5")); | |
| 340 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("")); | |
| 341 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" ")); | |
| 342 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("5.")); | |
| 343 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 5. ")); | |
| 344 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" +5. ")); | |
| 345 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -5. ")); | |
| 346 Expect.equals(true, parseDoubleThrowsBadNumberFormatException("1234567.e2"))
; | |
| 347 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" 1234567.e2 "
)); | |
| 348 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" +1234567.e2
")); | |
| 349 Expect.equals(true, parseDoubleThrowsBadNumberFormatException(" -1234567.e2
")); | |
| 350 } | |
| 351 | |
| 352 static testMain() { | 238 static testMain() { |
| 353 testConstants(); | 239 testConstants(); |
| 354 testSin(); | 240 testSin(); |
| 355 testCos(); | 241 testCos(); |
| 356 testTan(); | 242 testTan(); |
| 357 testAsin(); | 243 testAsin(); |
| 358 testAcos(); | 244 testAcos(); |
| 359 testAtan(); | 245 testAtan(); |
| 360 testAtan2(); | 246 testAtan2(); |
| 361 testSqrt(); | 247 testSqrt(); |
| 362 testLog(); | 248 testLog(); |
| 363 testExp(); | 249 testExp(); |
| 364 testPow(); | 250 testPow(); |
| 365 testParseInt(); | 251 testParseInt(); |
| 366 testParseDouble(); | |
| 367 } | 252 } |
| 368 } | 253 } |
| 369 | 254 |
| 370 main() { | 255 main() { |
| 371 MathTest.testMain(); | 256 MathTest.testMain(); |
| 372 } | 257 } |
| OLD | NEW |