|
12 | 12 | INF=float("inf") |
13 | 13 | NAN=float("nan") |
14 | 14 |
|
| 15 | +have_getformat=hasattr(float,"__getformat__") |
| 16 | +requires_getformat=unittest.skipUnless(have_getformat, |
| 17 | +"requires __getformat__") |
| 18 | +requires_setformat=unittest.skipUnless(hasattr(float,"__setformat__"), |
| 19 | +"requires __setformat__") |
15 | 20 | # decorator for skipping tests on non-IEEE 754 platforms |
16 | | -requires_IEEE_754=unittest.skipUnless( |
| 21 | +requires_IEEE_754=unittest.skipUnless(have_getformatand |
17 | 22 | float.__getformat__("double").startswith("IEEE"), |
18 | 23 | "test requires IEEE 754 doubles") |
19 | 24 |
|
@@ -357,6 +362,7 @@ def test_float_pow(self): |
357 | 362 | #self.assertTrue(0.0 > pow_op(-2.0, -1047) > -1e-315) |
358 | 363 |
|
359 | 364 |
|
| 365 | +@requires_setformat |
360 | 366 | classFormatFunctionsTestCase(unittest.TestCase): |
361 | 367 |
|
362 | 368 | defsetUp(self): |
@@ -407,6 +413,7 @@ def test_setformat(self): |
407 | 413 | # on non-IEEE platforms, attempting to unpack a bit pattern |
408 | 414 | # representing an infinity or a NaN should raise an exception. |
409 | 415 |
|
| 416 | +@requires_setformat |
410 | 417 | classUnknownFormatTestCase(unittest.TestCase): |
411 | 418 | defsetUp(self): |
412 | 419 | self.save_formats= {'double':float.__getformat__('double'), |
@@ -439,41 +446,42 @@ def test_float_specials_dont_unpack(self): |
439 | 446 | # let's also try to guarantee that -0.0 and 0.0 don't get confused. |
440 | 447 |
|
441 | 448 | classIEEEFormatTestCase(unittest.TestCase): |
442 | | -iffloat.__getformat__("double").startswith("IEEE"): |
443 | | -deftest_double_specials_do_unpack(self): |
444 | | -forfmt,datain [('>d',BE_DOUBLE_INF), |
445 | | - ('>d',BE_DOUBLE_NAN), |
446 | | - ('<d',LE_DOUBLE_INF), |
447 | | - ('<d',LE_DOUBLE_NAN)]: |
448 | | -struct.unpack(fmt,data) |
449 | | - |
450 | | -iffloat.__getformat__("float").startswith("IEEE"): |
451 | | -deftest_float_specials_do_unpack(self): |
452 | | -forfmt,datain [('>f',BE_FLOAT_INF), |
453 | | - ('>f',BE_FLOAT_NAN), |
454 | | - ('<f',LE_FLOAT_INF), |
455 | | - ('<f',LE_FLOAT_NAN)]: |
456 | | -struct.unpack(fmt,data) |
457 | | - |
458 | | -iffloat.__getformat__("double").startswith("IEEE"): |
459 | | -deftest_negative_zero(self): |
460 | | -defpos_pos(): |
461 | | -return0.0,math.atan2(0.0,-1) |
462 | | -defpos_neg(): |
463 | | -return0.0,math.atan2(-0.0,-1) |
464 | | -defneg_pos(): |
465 | | -return-0.0,math.atan2(0.0,-1) |
466 | | -defneg_neg(): |
467 | | -return-0.0,math.atan2(-0.0,-1) |
468 | | -self.assertEquals(pos_pos(),neg_pos()) |
469 | | -self.assertEquals(pos_neg(),neg_neg()) |
470 | | - |
471 | | -iffloat.__getformat__("double").startswith("IEEE"): |
472 | | -deftest_underflow_sign(self): |
473 | | -# check that -1e-1000 gives -0.0, not 0.0 |
474 | | -self.assertEquals(math.atan2(-1e-1000,-1),math.atan2(-0.0,-1)) |
475 | | -self.assertEquals(math.atan2(float('-1e-1000'),-1), |
476 | | -math.atan2(-0.0,-1)) |
| 449 | + |
| 450 | +@requires_IEEE_754 |
| 451 | +deftest_double_specials_do_unpack(self): |
| 452 | +forfmt,datain [('>d',BE_DOUBLE_INF), |
| 453 | + ('>d',BE_DOUBLE_NAN), |
| 454 | + ('<d',LE_DOUBLE_INF), |
| 455 | + ('<d',LE_DOUBLE_NAN)]: |
| 456 | +struct.unpack(fmt,data) |
| 457 | + |
| 458 | +@requires_IEEE_754 |
| 459 | +deftest_float_specials_do_unpack(self): |
| 460 | +forfmt,datain [('>f',BE_FLOAT_INF), |
| 461 | + ('>f',BE_FLOAT_NAN), |
| 462 | + ('<f',LE_FLOAT_INF), |
| 463 | + ('<f',LE_FLOAT_NAN)]: |
| 464 | +struct.unpack(fmt,data) |
| 465 | + |
| 466 | +@requires_IEEE_754 |
| 467 | +deftest_negative_zero(self): |
| 468 | +defpos_pos(): |
| 469 | +return0.0,math.atan2(0.0,-1) |
| 470 | +defpos_neg(): |
| 471 | +return0.0,math.atan2(-0.0,-1) |
| 472 | +defneg_pos(): |
| 473 | +return-0.0,math.atan2(0.0,-1) |
| 474 | +defneg_neg(): |
| 475 | +return-0.0,math.atan2(-0.0,-1) |
| 476 | +self.assertEquals(pos_pos(),neg_pos()) |
| 477 | +self.assertEquals(pos_neg(),neg_neg()) |
| 478 | + |
| 479 | +@requires_IEEE_754 |
| 480 | +deftest_underflow_sign(self): |
| 481 | +# check that -1e-1000 gives -0.0, not 0.0 |
| 482 | +self.assertEquals(math.atan2(-1e-1000,-1),math.atan2(-0.0,-1)) |
| 483 | +self.assertEquals(math.atan2(float('-1e-1000'),-1), |
| 484 | +math.atan2(-0.0,-1)) |
477 | 485 |
|
478 | 486 | deftest_format(self): |
479 | 487 | # these should be rewritten to use both format(x, spec) and |
@@ -530,8 +538,7 @@ def test_format(self): |
530 | 538 | self.assertEqual('{0:f}'.format(NAN),'nan') |
531 | 539 | self.assertEqual('{0:F}'.format(NAN),'NAN') |
532 | 540 |
|
533 | | -@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
534 | | -"test requires IEEE 754 doubles") |
| 541 | +@requires_IEEE_754 |
535 | 542 | deftest_format_testfile(self): |
536 | 543 | forlineinopen(format_testfile): |
537 | 544 | ifline.startswith('--'): |
@@ -613,8 +620,8 @@ def test_short_repr(self): |
613 | 620 | self.assertEqual(s,repr(float(s))) |
614 | 621 | self.assertEqual(negs,repr(float(negs))) |
615 | 622 |
|
616 | | -@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
617 | | -"test requires IEEE 754 doubles") |
| 623 | + |
| 624 | +@requires_IEEE_754 |
618 | 625 | classRoundTestCase(unittest.TestCase): |
619 | 626 | deftest_second_argument_type(self): |
620 | 627 | # any type with an __index__ method should be permitted as |
@@ -752,8 +759,8 @@ def test_halfway_cases(self): |
752 | 759 | self.assertAlmostEqual(round(0.5e22,-22),1e22) |
753 | 760 | self.assertAlmostEqual(round(1.5e22,-22),2e22) |
754 | 761 |
|
755 | | -@unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), |
756 | | -"test requires IEEE 754 doubles") |
| 762 | + |
| 763 | +@requires_IEEE_754 |
757 | 764 | deftest_format_specials(self): |
758 | 765 | # Test formatting of nans and infs. |
759 | 766 |
|
|