Changeset 391 for python/trunk/Lib/test/test_decimal.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_decimal.py
r2 r391 25 25 """ 26 26 27 import glob28 27 import math 29 28 import os, sys 29 import operator 30 30 import pickle, copy 31 31 import unittest 32 32 from decimal import * 33 33 import numbers 34 from test.test_support import ( TestSkipped,run_unittest, run_doctest,35 is_resource_enabled )34 from test.test_support import (run_unittest, run_doctest, 35 is_resource_enabled, check_py3k_warnings) 36 36 import random 37 37 try: … … 41 41 42 42 # Useful Test Constant 43 Signals = getcontext().flags.keys() 43 Signals = tuple(getcontext().flags.keys()) 44 45 # Signals ordered with respect to precedence: when an operation 46 # produces multiple signals, signals occurring later in the list 47 # should be handled before those occurring earlier in the list. 48 OrderedSignals = (Clamped, Rounded, Inexact, Subnormal, 49 Underflow, Overflow, DivisionByZero, InvalidOperation) 44 50 45 51 # Tests are built around these assumed context defaults. … … 55 61 setcontext(DefaultTestContext) 56 62 63 # decorator for skipping tests on non-IEEE 754 platforms 64 requires_IEEE_754 = unittest.skipUnless( 65 float.__getformat__("double").startswith("IEEE"), 66 "test requires IEEE 754 doubles") 67 57 68 TESTDATADIR = 'decimaltestdata' 58 69 if __name__ == '__main__': … … 67 78 # list of individual .decTest test ids that correspond to tests that 68 79 # we're skipping for one reason or another. 69 skipped_test_ids = [ 70 'scbx164', # skipping apparently implementation-specific scaleb 71 'scbx165', # tests, pending clarification of scaleb rules. 72 ] 80 skipped_test_ids = set([ 81 # Skip implementation-specific scaleb tests. 82 'scbx164', 83 'scbx165', 84 85 # For some operations (currently exp, ln, log10, power), the decNumber 86 # reference implementation imposes additional restrictions on the context 87 # and operands. These restrictions are not part of the specification; 88 # however, the effect of these restrictions does show up in some of the 89 # testcases. We skip testcases that violate these restrictions, since 90 # Decimal behaves differently from decNumber for these testcases so these 91 # testcases would otherwise fail. 92 'expx901', 93 'expx902', 94 'expx903', 95 'expx905', 96 'lnx901', 97 'lnx902', 98 'lnx903', 99 'lnx905', 100 'logx901', 101 'logx902', 102 'logx903', 103 'logx905', 104 'powx1183', 105 'powx1184', 106 'powx4001', 107 'powx4002', 108 'powx4003', 109 'powx4005', 110 'powx4008', 111 'powx4010', 112 'powx4012', 113 'powx4014', 114 ]) 73 115 74 116 # Make sure it actually raises errors when not expected and caught in flags … … 161 203 ) 162 204 163 # For some operations (currently exp, ln, log10, power), the decNumber164 # reference implementation imposes additional restrictions on the165 # context and operands. These restrictions are not part of the166 # specification; however, the effect of these restrictions does show167 # up in some of the testcases. We skip testcases that violate these168 # restrictions, since Decimal behaves differently from decNumber for169 # these testcases so these testcases would otherwise fail.170 171 decNumberRestricted = ('power', 'ln', 'log10', 'exp')172 DEC_MAX_MATH = 999999173 def outside_decNumber_bounds(v, context):174 if (context.prec > DEC_MAX_MATH or175 context.Emax > DEC_MAX_MATH or176 -context.Emin > DEC_MAX_MATH):177 return True178 if not v._is_special and v and (179 v.adjusted() > DEC_MAX_MATH or180 v.adjusted() < 1-2*DEC_MAX_MATH):181 return True182 return False183 184 205 class DecimalTest(unittest.TestCase): 185 206 """Class which tests the Decimal class against the test cases. … … 202 223 global skip_expected 203 224 if skip_expected: 204 raise TestSkipped225 raise unittest.SkipTest 205 226 return 206 for line in open(file).xreadlines(): 207 line = line.replace('\r\n', '').replace('\n', '') 208 #print line 209 try: 210 t = self.eval_line(line) 211 except DecimalException, exception: 212 #Exception raised where there shoudn't have been one. 213 self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line) 227 with open(file) as f: 228 for line in f: 229 line = line.replace('\r\n', '').replace('\n', '') 230 #print line 231 try: 232 t = self.eval_line(line) 233 except DecimalException as exception: 234 #Exception raised where there shouldn't have been one. 235 self.fail('Exception "'+exception.__class__.__name__ + '" raised on line '+line) 214 236 215 237 return … … 319 341 ans = FixQuotes(ans) 320 342 321 # skip tests that are related to bounds imposed in the decNumber322 # reference implementation323 if fname in decNumberRestricted:324 if fname == 'power':325 if not (vals[1]._isinteger() and326 -1999999997 <= vals[1] <= 999999999):327 if outside_decNumber_bounds(vals[0], self.context) or \328 outside_decNumber_bounds(vals[1], self.context):329 #print "Skipping test %s" % s330 return331 else:332 if outside_decNumber_bounds(vals[0], self.context):333 #print "Skipping test %s" % s334 return335 336 337 343 if EXTENDEDERRORTEST and fname not in ('to_sci_string', 'to_eng_string'): 338 344 for error in theirexceptions: … … 348 354 self.fail("Did not raise %s in %s" % (error, s)) 349 355 self.context.traps[error] = 0 356 357 # as above, but add traps cumulatively, to check precedence 358 ordered_errors = [e for e in OrderedSignals if e in theirexceptions] 359 for error in ordered_errors: 360 self.context.traps[error] = 1 361 try: 362 funct(*vals) 363 except error: 364 pass 365 except Signals, e: 366 self.fail("Raised %s in %s; expected %s" % 367 (type(e), s, error)) 368 else: 369 self.fail("Did not raise %s in %s" % (error, s)) 370 # reset traps 371 for error in ordered_errors: 372 self.context.traps[error] = 0 373 374 350 375 if DEBUG: 351 376 print "--", self.context … … 363 388 self.context.clear_flags() 364 389 365 myexceptions.sort()366 theirexceptions.sort()367 368 390 self.assertEqual(result, ans, 369 391 'Incorrect answer for ' + s + ' -- got ' + result) 370 self.assert Equal(myexceptions, theirexceptions,392 self.assertItemsEqual(myexceptions, theirexceptions, 371 393 'Incorrect flags set in ' + s + ' -- got ' + str(myexceptions)) 372 394 return … … 481 503 self.assertRaises(ValueError, Decimal, (1, (4, 3, 4, 'a', 1), 2) ) 482 504 505 def test_explicit_from_bool(self): 506 self.assertIs(bool(Decimal(0)), False) 507 self.assertIs(bool(Decimal(1)), True) 508 self.assertEqual(Decimal(False), Decimal(0)) 509 self.assertEqual(Decimal(True), Decimal(1)) 510 483 511 def test_explicit_from_Decimal(self): 484 512 … … 507 535 self.assertNotEqual(id(d), id(e)) 508 536 537 @requires_IEEE_754 538 def test_explicit_from_float(self): 539 r = Decimal(0.1) 540 self.assertEqual(type(r), Decimal) 541 self.assertEqual(str(r), 542 '0.1000000000000000055511151231257827021181583404541015625') 543 self.assertTrue(Decimal(float('nan')).is_qnan()) 544 self.assertTrue(Decimal(float('inf')).is_infinite()) 545 self.assertTrue(Decimal(float('-inf')).is_infinite()) 546 self.assertEqual(str(Decimal(float('nan'))), 547 str(Decimal('NaN'))) 548 self.assertEqual(str(Decimal(float('inf'))), 549 str(Decimal('Infinity'))) 550 self.assertEqual(str(Decimal(float('-inf'))), 551 str(Decimal('-Infinity'))) 552 self.assertEqual(str(Decimal(float('-0.0'))), 553 str(Decimal('-0'))) 554 for i in range(200): 555 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) 556 self.assertEqual(x, float(Decimal(x))) # roundtrip 557 509 558 def test_explicit_context_create_decimal(self): 510 559 … … 523 572 # from int 524 573 d = nc.create_decimal(456) 525 self. failUnless(isinstance(d, Decimal))574 self.assertIsInstance(d, Decimal) 526 575 self.assertEqual(nc.create_decimal(45678), 527 576 nc.create_decimal('457E+2')) … … 619 668 ('**', '__pow__', '__rpow__') 620 669 ] 621 if 1/2 == 0: 622 # testing with classic division, so add __div__ 623 oplist.append(('/', '__div__', '__rdiv__')) 624 else: 625 # testing with -Qnew, so add __truediv__ 626 oplist.append(('/', '__truediv__', '__rtruediv__')) 670 with check_py3k_warnings(): 671 if 1 / 2 == 0: 672 # testing with classic division, so add __div__ 673 oplist.append(('/', '__div__', '__rdiv__')) 674 else: 675 # testing with -Qnew, so add __truediv__ 676 oplist.append(('/', '__truediv__', '__rtruediv__')) 627 677 628 678 for sym, lop, rop in oplist: … … 633 683 self.assertEqual(eval('Decimal(10)' + sym + 'E()'), 634 684 '10' + rop + 'str') 685 635 686 636 687 class DecimalFormatTest(unittest.TestCase): … … 723 774 ('', '1.00', '1.00'), 724 775 725 # check alignment 776 # test alignment and padding 777 ('6', '123', ' 123'), 726 778 ('<6', '123', '123 '), 727 779 ('>6', '123', ' 123'), 728 780 ('^6', '123', ' 123 '), 729 781 ('=+6', '123', '+ 123'), 782 ('#<10', 'NaN', 'NaN#######'), 783 ('#<10', '-4.3', '-4.3######'), 784 ('#<+10', '0.0130', '+0.0130###'), 785 ('#< 10', '0.0130', ' 0.0130###'), 786 ('@>10', '-Inf', '@-Infinity'), 787 ('#>5', '-Inf', '-Infinity'), 788 ('?^5', '123', '?123?'), 789 ('%^6', '123', '%123%%'), 790 (' ^6', '-45.6', '-45.6 '), 791 ('/=10', '-45.6', '-/////45.6'), 792 ('/=+10', '45.6', '+/////45.6'), 793 ('/= 10', '45.6', ' /////45.6'), 794 795 # thousands separator 796 (',', '1234567', '1,234,567'), 797 (',', '123456', '123,456'), 798 (',', '12345', '12,345'), 799 (',', '1234', '1,234'), 800 (',', '123', '123'), 801 (',', '12', '12'), 802 (',', '1', '1'), 803 (',', '0', '0'), 804 (',', '-1234567', '-1,234,567'), 805 (',', '-123456', '-123,456'), 806 ('7,', '123456', '123,456'), 807 ('8,', '123456', ' 123,456'), 808 ('08,', '123456', '0,123,456'), # special case: extra 0 needed 809 ('+08,', '123456', '+123,456'), # but not if there's a sign 810 (' 08,', '123456', ' 123,456'), 811 ('08,', '-123456', '-123,456'), 812 ('+09,', '123456', '+0,123,456'), 813 # ... with fractional part... 814 ('07,', '1234.56', '1,234.56'), 815 ('08,', '1234.56', '1,234.56'), 816 ('09,', '1234.56', '01,234.56'), 817 ('010,', '1234.56', '001,234.56'), 818 ('011,', '1234.56', '0,001,234.56'), 819 ('012,', '1234.56', '0,001,234.56'), 820 ('08,.1f', '1234.5', '01,234.5'), 821 # no thousands separators in fraction part 822 (',', '1.23456789', '1.23456789'), 823 (',%', '123.456789', '12,345.6789%'), 824 (',e', '123456', '1.23456e+5'), 825 (',E', '123456', '1.23456E+5'), 730 826 731 827 # issue 6850 … … 734 830 for fmt, d, result in test_values: 735 831 self.assertEqual(format(Decimal(d), fmt), result) 832 833 def test_n_format(self): 834 try: 835 from locale import CHAR_MAX 836 except ImportError: 837 return 838 839 # Set up some localeconv-like dictionaries 840 en_US = { 841 'decimal_point' : '.', 842 'grouping' : [3, 3, 0], 843 'thousands_sep': ',' 844 } 845 846 fr_FR = { 847 'decimal_point' : ',', 848 'grouping' : [CHAR_MAX], 849 'thousands_sep' : '' 850 } 851 852 ru_RU = { 853 'decimal_point' : ',', 854 'grouping' : [3, 3, 0], 855 'thousands_sep' : ' ' 856 } 857 858 crazy = { 859 'decimal_point' : '&', 860 'grouping' : [1, 4, 2, CHAR_MAX], 861 'thousands_sep' : '-' 862 } 863 864 865 def get_fmt(x, locale, fmt='n'): 866 return Decimal.__format__(Decimal(x), fmt, _localeconv=locale) 867 868 self.assertEqual(get_fmt(Decimal('12.7'), en_US), '12.7') 869 self.assertEqual(get_fmt(Decimal('12.7'), fr_FR), '12,7') 870 self.assertEqual(get_fmt(Decimal('12.7'), ru_RU), '12,7') 871 self.assertEqual(get_fmt(Decimal('12.7'), crazy), '1-2&7') 872 873 self.assertEqual(get_fmt(123456789, en_US), '123,456,789') 874 self.assertEqual(get_fmt(123456789, fr_FR), '123456789') 875 self.assertEqual(get_fmt(123456789, ru_RU), '123 456 789') 876 self.assertEqual(get_fmt(1234567890123, crazy), '123456-78-9012-3') 877 878 self.assertEqual(get_fmt(123456789, en_US, '.6n'), '1.23457e+8') 879 self.assertEqual(get_fmt(123456789, fr_FR, '.6n'), '1,23457e+8') 880 self.assertEqual(get_fmt(123456789, ru_RU, '.6n'), '1,23457e+8') 881 self.assertEqual(get_fmt(123456789, crazy, '.6n'), '1&23457e+8') 882 883 # zero padding 884 self.assertEqual(get_fmt(1234, fr_FR, '03n'), '1234') 885 self.assertEqual(get_fmt(1234, fr_FR, '04n'), '1234') 886 self.assertEqual(get_fmt(1234, fr_FR, '05n'), '01234') 887 self.assertEqual(get_fmt(1234, fr_FR, '06n'), '001234') 888 889 self.assertEqual(get_fmt(12345, en_US, '05n'), '12,345') 890 self.assertEqual(get_fmt(12345, en_US, '06n'), '12,345') 891 self.assertEqual(get_fmt(12345, en_US, '07n'), '012,345') 892 self.assertEqual(get_fmt(12345, en_US, '08n'), '0,012,345') 893 self.assertEqual(get_fmt(12345, en_US, '09n'), '0,012,345') 894 self.assertEqual(get_fmt(12345, en_US, '010n'), '00,012,345') 895 896 self.assertEqual(get_fmt(123456, crazy, '06n'), '1-2345-6') 897 self.assertEqual(get_fmt(123456, crazy, '07n'), '1-2345-6') 898 self.assertEqual(get_fmt(123456, crazy, '08n'), '1-2345-6') 899 self.assertEqual(get_fmt(123456, crazy, '09n'), '01-2345-6') 900 self.assertEqual(get_fmt(123456, crazy, '010n'), '0-01-2345-6') 901 self.assertEqual(get_fmt(123456, crazy, '011n'), '0-01-2345-6') 902 self.assertEqual(get_fmt(123456, crazy, '012n'), '00-01-2345-6') 903 self.assertEqual(get_fmt(123456, crazy, '013n'), '000-01-2345-6') 904 736 905 737 906 class DecimalArithmeticOperatorsTest(unittest.TestCase): … … 959 1128 960 1129 def test_nan_comparisons(self): 1130 # comparisons involving signaling nans signal InvalidOperation 1131 1132 # order comparisons (<, <=, >, >=) involving only quiet nans 1133 # also signal InvalidOperation 1134 1135 # equality comparisons (==, !=) involving only quiet nans 1136 # don't signal, but return False or True respectively. 1137 961 1138 n = Decimal('NaN') 962 1139 s = Decimal('sNaN') 963 1140 i = Decimal('Inf') 964 1141 f = Decimal('2') 965 for x, y in [(n, n), (n, i), (i, n), (n, f), (f, n), 966 (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s)]: 967 self.assert_(x != y) 968 self.assert_(not (x == y)) 969 self.assert_(not (x < y)) 970 self.assert_(not (x <= y)) 971 self.assert_(not (x > y)) 972 self.assert_(not (x >= y)) 1142 1143 qnan_pairs = (n, n), (n, i), (i, n), (n, f), (f, n) 1144 snan_pairs = (s, n), (n, s), (s, i), (i, s), (s, f), (f, s), (s, s) 1145 order_ops = operator.lt, operator.le, operator.gt, operator.ge 1146 equality_ops = operator.eq, operator.ne 1147 1148 # results when InvalidOperation is not trapped 1149 for x, y in qnan_pairs + snan_pairs: 1150 for op in order_ops + equality_ops: 1151 got = op(x, y) 1152 expected = True if op is operator.ne else False 1153 self.assertIs(expected, got, 1154 "expected {0!r} for operator.{1}({2!r}, {3!r}); " 1155 "got {4!r}".format( 1156 expected, op.__name__, x, y, got)) 1157 1158 # repeat the above, but this time trap the InvalidOperation 1159 with localcontext() as ctx: 1160 ctx.traps[InvalidOperation] = 1 1161 1162 for x, y in qnan_pairs: 1163 for op in equality_ops: 1164 got = op(x, y) 1165 expected = True if op is operator.ne else False 1166 self.assertIs(expected, got, 1167 "expected {0!r} for " 1168 "operator.{1}({2!r}, {3!r}); " 1169 "got {4!r}".format( 1170 expected, op.__name__, x, y, got)) 1171 1172 for x, y in snan_pairs: 1173 for op in equality_ops: 1174 self.assertRaises(InvalidOperation, operator.eq, x, y) 1175 self.assertRaises(InvalidOperation, operator.ne, x, y) 1176 1177 for x, y in qnan_pairs + snan_pairs: 1178 for op in order_ops: 1179 self.assertRaises(InvalidOperation, op, x, y) 1180 1181 def test_copy_sign(self): 1182 d = Decimal(1).copy_sign(Decimal(-2)) 1183 1184 self.assertEqual(Decimal(1).copy_sign(-2), d) 1185 self.assertRaises(TypeError, Decimal(1).copy_sign, '-2') 973 1186 974 1187 # The following are two functions used to test threading in the next class … … 1043 1256 1044 1257 #two Decimals 1045 self. failUnless(dc >da)1046 self. failUnless(dc >=da)1047 self. failUnless(da <dc)1048 self. failUnless(da <=dc)1049 self. failUnless(da ==db)1050 self. failUnless(da !=dc)1051 self. failUnless(da <=db)1052 self. failUnless(da >=db)1258 self.assertGreater(dc, da) 1259 self.assertGreaterEqual(dc, da) 1260 self.assertLess(da, dc) 1261 self.assertLessEqual(da, dc) 1262 self.assertEqual(da, db) 1263 self.assertNotEqual(da, dc) 1264 self.assertLessEqual(da, db) 1265 self.assertGreaterEqual(da, db) 1053 1266 self.assertEqual(cmp(dc,da), 1) 1054 1267 self.assertEqual(cmp(da,dc), -1) … … 1056 1269 1057 1270 #a Decimal and an int 1058 self. failUnless(dc >23)1059 self. failUnless(23 <dc)1060 self. failUnless(dc ==45)1271 self.assertGreater(dc, 23) 1272 self.assertLess(23, dc) 1273 self.assertEqual(dc, 45) 1061 1274 self.assertEqual(cmp(dc,23), 1) 1062 1275 self.assertEqual(cmp(23,dc), -1) … … 1077 1290 1078 1291 # with None 1079 self.assertFalse(Decimal(1) < None) 1080 self.assertTrue(Decimal(1) > None) 1292 with check_py3k_warnings(): 1293 self.assertFalse(Decimal(1) < None) 1294 self.assertTrue(Decimal(1) > None) 1295 1296 def test_decimal_float_comparison(self): 1297 da = Decimal('0.25') 1298 db = Decimal('3.0') 1299 self.assertLess(da, 3.0) 1300 self.assertLessEqual(da, 3.0) 1301 self.assertGreater(db, 0.25) 1302 self.assertGreaterEqual(db, 0.25) 1303 self.assertNotEqual(da, 1.5) 1304 self.assertEqual(da, 0.25) 1305 self.assertGreater(3.0, da) 1306 self.assertGreaterEqual(3.0, da) 1307 self.assertLess(0.25, db) 1308 self.assertLessEqual(0.25, db) 1309 self.assertNotEqual(0.25, db) 1310 self.assertEqual(3.0, db) 1311 self.assertNotEqual(0.1, Decimal('0.1')) 1081 1312 1082 1313 def test_copy_and_deepcopy_methods(self): … … 1090 1321 #just that it's hashable 1091 1322 hash(Decimal(23)) 1323 hash(Decimal('Infinity')) 1324 hash(Decimal('-Infinity')) 1325 hash(Decimal('nan123')) 1326 hash(Decimal('-NaN')) 1092 1327 1093 1328 test_values = [Decimal(sign*(2**m + n)) … … 1124 1359 #the same hash that to an int 1125 1360 self.assertEqual(hash(Decimal(23)), hash(23)) 1126 self.assertRaises(TypeError, hash, Decimal('NaN')) 1127 self.assert_(hash(Decimal('Inf'))) 1128 self.assert_(hash(Decimal('-Inf'))) 1361 self.assertRaises(TypeError, hash, Decimal('sNaN')) 1362 self.assertTrue(hash(Decimal('Inf'))) 1363 self.assertTrue(hash(Decimal('-Inf'))) 1364 1365 # check that the hashes of a Decimal float match when they 1366 # represent exactly the same values 1367 test_strings = ['inf', '-Inf', '0.0', '-.0e1', 1368 '34.0', '2.5', '112390.625', '-0.515625'] 1369 for s in test_strings: 1370 f = float(s) 1371 d = Decimal(s) 1372 self.assertEqual(hash(f), hash(d)) 1129 1373 1130 1374 # check that the value of the hash doesn't depend on the … … 1153 1397 1154 1398 #between Decimals 1155 self. failUnless(min(d1,d2) isd1)1156 self. failUnless(min(d2,d1) isd1)1157 self. failUnless(max(d1,d2) isd2)1158 self. failUnless(max(d2,d1) isd2)1399 self.assertIs(min(d1,d2), d1) 1400 self.assertIs(min(d2,d1), d1) 1401 self.assertIs(max(d1,d2), d2) 1402 self.assertIs(max(d2,d1), d2) 1159 1403 1160 1404 #between Decimal and long 1161 self. failUnless(min(d1,l2) isd1)1162 self. failUnless(min(l2,d1) isd1)1163 self. failUnless(max(l1,d2) isd2)1164 self. failUnless(max(d2,l1) isd2)1405 self.assertIs(min(d1,l2), d1) 1406 self.assertIs(min(l2,d1), d1) 1407 self.assertIs(max(l1,d2), d2) 1408 self.assertIs(max(d2,l1), d2) 1165 1409 1166 1410 def test_as_nonzero(self): 1167 1411 #as false 1168 self. failIf(Decimal(0))1412 self.assertFalse(Decimal(0)) 1169 1413 #as true 1170 self. failUnless(Decimal('0.372'))1414 self.assertTrue(Decimal('0.372')) 1171 1415 1172 1416 def test_tostring_methods(self): … … 1204 1448 self.assertEqual(float(d1), 66) 1205 1449 self.assertEqual(float(d2), 15.32) 1450 1451 def test_nan_to_float(self): 1452 # Test conversions of decimal NANs to float. 1453 # See http://bugs.python.org/issue15544 1454 for s in ('nan', 'nan1234', '-nan', '-nan2468'): 1455 f = float(Decimal(s)) 1456 self.assertTrue(math.isnan(f)) 1457 1458 def test_snan_to_float(self): 1459 for s in ('snan', '-snan', 'snan1357', '-snan1234'): 1460 d = Decimal(s) 1461 self.assertRaises(ValueError, float, d) 1206 1462 1207 1463 def test_eval_round_trip(self): … … 1348 1604 d2 = MyDecimal(2) 1349 1605 d = d1 + d2 1350 self.assert True(type(d) isDecimal)1606 self.assertIs(type(d), Decimal) 1351 1607 1352 1608 d = d1.max(d2) 1353 self.assert True(type(d) isDecimal)1609 self.assertIs(type(d), Decimal) 1354 1610 1355 1611 def test_implicit_context(self): … … 1410 1666 1411 1667 def test_abc(self): 1412 self.assert _(issubclass(Decimal, numbers.Number))1413 self.assert _(notissubclass(Decimal, numbers.Real))1414 self.assert _(isinstance(Decimal(0), numbers.Number))1415 self.assert _(not isinstance(Decimal(0), numbers.Real))1668 self.assertTrue(issubclass(Decimal, numbers.Number)) 1669 self.assertFalse(issubclass(Decimal, numbers.Real)) 1670 self.assertIsInstance(Decimal(0), numbers.Number) 1671 self.assertNotIsInstance(Decimal(0), numbers.Real) 1416 1672 1417 1673 def test_pickle(self): … … 1451 1707 self.assertEqual(Decimal(math.trunc(d)), r) 1452 1708 1709 def test_from_float(self): 1710 1711 class MyDecimal(Decimal): 1712 pass 1713 1714 r = MyDecimal.from_float(0.1) 1715 self.assertEqual(type(r), MyDecimal) 1716 self.assertEqual(str(r), 1717 '0.1000000000000000055511151231257827021181583404541015625') 1718 bigint = 12345678901234567890123456789 1719 self.assertEqual(MyDecimal.from_float(bigint), MyDecimal(bigint)) 1720 self.assertTrue(MyDecimal.from_float(float('nan')).is_qnan()) 1721 self.assertTrue(MyDecimal.from_float(float('inf')).is_infinite()) 1722 self.assertTrue(MyDecimal.from_float(float('-inf')).is_infinite()) 1723 self.assertEqual(str(MyDecimal.from_float(float('nan'))), 1724 str(Decimal('NaN'))) 1725 self.assertEqual(str(MyDecimal.from_float(float('inf'))), 1726 str(Decimal('Infinity'))) 1727 self.assertEqual(str(MyDecimal.from_float(float('-inf'))), 1728 str(Decimal('-Infinity'))) 1729 self.assertRaises(TypeError, MyDecimal.from_float, 'abc') 1730 for i in range(200): 1731 x = random.expovariate(0.01) * (random.random() * 2.0 - 1.0) 1732 self.assertEqual(x, float(MyDecimal.from_float(x))) # roundtrip 1733 1734 def test_create_decimal_from_float(self): 1735 context = Context(prec=5, rounding=ROUND_DOWN) 1736 self.assertEqual( 1737 context.create_decimal_from_float(math.pi), 1738 Decimal('3.1415') 1739 ) 1740 context = Context(prec=5, rounding=ROUND_UP) 1741 self.assertEqual( 1742 context.create_decimal_from_float(math.pi), 1743 Decimal('3.1416') 1744 ) 1745 context = Context(prec=5, traps=[Inexact]) 1746 self.assertRaises( 1747 Inexact, 1748 context.create_decimal_from_float, 1749 math.pi 1750 ) 1751 self.assertEqual(repr(context.create_decimal_from_float(-0.0)), 1752 "Decimal('-0')") 1753 self.assertEqual(repr(context.create_decimal_from_float(1.0)), 1754 "Decimal('1')") 1755 self.assertEqual(repr(context.create_decimal_from_float(10)), 1756 "Decimal('10')") 1757 1453 1758 class ContextAPItests(unittest.TestCase): 1454 1759 … … 1462 1767 1463 1768 def test_equality_with_other_types(self): 1464 self.assert _(Decimal(10) in['a', 1.0, Decimal(10), (1,2), {}])1465 self.assert _(Decimal(10) not in['a', 1.0, (1,2), {}])1769 self.assertIn(Decimal(10), ['a', 1.0, Decimal(10), (1,2), {}]) 1770 self.assertNotIn(Decimal(10), ['a', 1.0, (1,2), {}]) 1466 1771 1467 1772 def test_copy(self): … … 1472 1777 self.assertNotEqual(id(c.flags), id(d.flags)) 1473 1778 self.assertNotEqual(id(c.traps), id(d.traps)) 1779 1780 def test_abs(self): 1781 c = Context() 1782 d = c.abs(Decimal(-1)) 1783 self.assertEqual(c.abs(-1), d) 1784 self.assertRaises(TypeError, c.abs, '-1') 1785 1786 def test_add(self): 1787 c = Context() 1788 d = c.add(Decimal(1), Decimal(1)) 1789 self.assertEqual(c.add(1, 1), d) 1790 self.assertEqual(c.add(Decimal(1), 1), d) 1791 self.assertEqual(c.add(1, Decimal(1)), d) 1792 self.assertRaises(TypeError, c.add, '1', 1) 1793 self.assertRaises(TypeError, c.add, 1, '1') 1794 1795 def test_compare(self): 1796 c = Context() 1797 d = c.compare(Decimal(1), Decimal(1)) 1798 self.assertEqual(c.compare(1, 1), d) 1799 self.assertEqual(c.compare(Decimal(1), 1), d) 1800 self.assertEqual(c.compare(1, Decimal(1)), d) 1801 self.assertRaises(TypeError, c.compare, '1', 1) 1802 self.assertRaises(TypeError, c.compare, 1, '1') 1803 1804 def test_compare_signal(self): 1805 c = Context() 1806 d = c.compare_signal(Decimal(1), Decimal(1)) 1807 self.assertEqual(c.compare_signal(1, 1), d) 1808 self.assertEqual(c.compare_signal(Decimal(1), 1), d) 1809 self.assertEqual(c.compare_signal(1, Decimal(1)), d) 1810 self.assertRaises(TypeError, c.compare_signal, '1', 1) 1811 self.assertRaises(TypeError, c.compare_signal, 1, '1') 1812 1813 def test_compare_total(self): 1814 c = Context() 1815 d = c.compare_total(Decimal(1), Decimal(1)) 1816 self.assertEqual(c.compare_total(1, 1), d) 1817 self.assertEqual(c.compare_total(Decimal(1), 1), d) 1818 self.assertEqual(c.compare_total(1, Decimal(1)), d) 1819 self.assertRaises(TypeError, c.compare_total, '1', 1) 1820 self.assertRaises(TypeError, c.compare_total, 1, '1') 1821 1822 def test_compare_total_mag(self): 1823 c = Context() 1824 d = c.compare_total_mag(Decimal(1), Decimal(1)) 1825 self.assertEqual(c.compare_total_mag(1, 1), d) 1826 self.assertEqual(c.compare_total_mag(Decimal(1), 1), d) 1827 self.assertEqual(c.compare_total_mag(1, Decimal(1)), d) 1828 self.assertRaises(TypeError, c.compare_total_mag, '1', 1) 1829 self.assertRaises(TypeError, c.compare_total_mag, 1, '1') 1830 1831 def test_copy_abs(self): 1832 c = Context() 1833 d = c.copy_abs(Decimal(-1)) 1834 self.assertEqual(c.copy_abs(-1), d) 1835 self.assertRaises(TypeError, c.copy_abs, '-1') 1836 1837 def test_copy_decimal(self): 1838 c = Context() 1839 d = c.copy_decimal(Decimal(-1)) 1840 self.assertEqual(c.copy_decimal(-1), d) 1841 self.assertRaises(TypeError, c.copy_decimal, '-1') 1842 1843 def test_copy_negate(self): 1844 c = Context() 1845 d = c.copy_negate(Decimal(-1)) 1846 self.assertEqual(c.copy_negate(-1), d) 1847 self.assertRaises(TypeError, c.copy_negate, '-1') 1848 1849 def test_copy_sign(self): 1850 c = Context() 1851 d = c.copy_sign(Decimal(1), Decimal(-2)) 1852 self.assertEqual(c.copy_sign(1, -2), d) 1853 self.assertEqual(c.copy_sign(Decimal(1), -2), d) 1854 self.assertEqual(c.copy_sign(1, Decimal(-2)), d) 1855 self.assertRaises(TypeError, c.copy_sign, '1', -2) 1856 self.assertRaises(TypeError, c.copy_sign, 1, '-2') 1857 1858 def test_divide(self): 1859 c = Context() 1860 d = c.divide(Decimal(1), Decimal(2)) 1861 self.assertEqual(c.divide(1, 2), d) 1862 self.assertEqual(c.divide(Decimal(1), 2), d) 1863 self.assertEqual(c.divide(1, Decimal(2)), d) 1864 self.assertRaises(TypeError, c.divide, '1', 2) 1865 self.assertRaises(TypeError, c.divide, 1, '2') 1866 1867 def test_divide_int(self): 1868 c = Context() 1869 d = c.divide_int(Decimal(1), Decimal(2)) 1870 self.assertEqual(c.divide_int(1, 2), d) 1871 self.assertEqual(c.divide_int(Decimal(1), 2), d) 1872 self.assertEqual(c.divide_int(1, Decimal(2)), d) 1873 self.assertRaises(TypeError, c.divide_int, '1', 2) 1874 self.assertRaises(TypeError, c.divide_int, 1, '2') 1875 1876 def test_divmod(self): 1877 c = Context() 1878 d = c.divmod(Decimal(1), Decimal(2)) 1879 self.assertEqual(c.divmod(1, 2), d) 1880 self.assertEqual(c.divmod(Decimal(1), 2), d) 1881 self.assertEqual(c.divmod(1, Decimal(2)), d) 1882 self.assertRaises(TypeError, c.divmod, '1', 2) 1883 self.assertRaises(TypeError, c.divmod, 1, '2') 1884 1885 def test_exp(self): 1886 c = Context() 1887 d = c.exp(Decimal(10)) 1888 self.assertEqual(c.exp(10), d) 1889 self.assertRaises(TypeError, c.exp, '10') 1890 1891 def test_fma(self): 1892 c = Context() 1893 d = c.fma(Decimal(2), Decimal(3), Decimal(4)) 1894 self.assertEqual(c.fma(2, 3, 4), d) 1895 self.assertEqual(c.fma(Decimal(2), 3, 4), d) 1896 self.assertEqual(c.fma(2, Decimal(3), 4), d) 1897 self.assertEqual(c.fma(2, 3, Decimal(4)), d) 1898 self.assertEqual(c.fma(Decimal(2), Decimal(3), 4), d) 1899 self.assertRaises(TypeError, c.fma, '2', 3, 4) 1900 self.assertRaises(TypeError, c.fma, 2, '3', 4) 1901 self.assertRaises(TypeError, c.fma, 2, 3, '4') 1902 1903 def test_is_finite(self): 1904 c = Context() 1905 d = c.is_finite(Decimal(10)) 1906 self.assertEqual(c.is_finite(10), d) 1907 self.assertRaises(TypeError, c.is_finite, '10') 1908 1909 def test_is_infinite(self): 1910 c = Context() 1911 d = c.is_infinite(Decimal(10)) 1912 self.assertEqual(c.is_infinite(10), d) 1913 self.assertRaises(TypeError, c.is_infinite, '10') 1914 1915 def test_is_nan(self): 1916 c = Context() 1917 d = c.is_nan(Decimal(10)) 1918 self.assertEqual(c.is_nan(10), d) 1919 self.assertRaises(TypeError, c.is_nan, '10') 1920 1921 def test_is_normal(self): 1922 c = Context() 1923 d = c.is_normal(Decimal(10)) 1924 self.assertEqual(c.is_normal(10), d) 1925 self.assertRaises(TypeError, c.is_normal, '10') 1926 1927 def test_is_qnan(self): 1928 c = Context() 1929 d = c.is_qnan(Decimal(10)) 1930 self.assertEqual(c.is_qnan(10), d) 1931 self.assertRaises(TypeError, c.is_qnan, '10') 1932 1933 def test_is_signed(self): 1934 c = Context() 1935 d = c.is_signed(Decimal(10)) 1936 self.assertEqual(c.is_signed(10), d) 1937 self.assertRaises(TypeError, c.is_signed, '10') 1938 1939 def test_is_snan(self): 1940 c = Context() 1941 d = c.is_snan(Decimal(10)) 1942 self.assertEqual(c.is_snan(10), d) 1943 self.assertRaises(TypeError, c.is_snan, '10') 1944 1945 def test_is_subnormal(self): 1946 c = Context() 1947 d = c.is_subnormal(Decimal(10)) 1948 self.assertEqual(c.is_subnormal(10), d) 1949 self.assertRaises(TypeError, c.is_subnormal, '10') 1950 1951 def test_is_zero(self): 1952 c = Context() 1953 d = c.is_zero(Decimal(10)) 1954 self.assertEqual(c.is_zero(10), d) 1955 self.assertRaises(TypeError, c.is_zero, '10') 1956 1957 def test_ln(self): 1958 c = Context() 1959 d = c.ln(Decimal(10)) 1960 self.assertEqual(c.ln(10), d) 1961 self.assertRaises(TypeError, c.ln, '10') 1962 1963 def test_log10(self): 1964 c = Context() 1965 d = c.log10(Decimal(10)) 1966 self.assertEqual(c.log10(10), d) 1967 self.assertRaises(TypeError, c.log10, '10') 1968 1969 def test_logb(self): 1970 c = Context() 1971 d = c.logb(Decimal(10)) 1972 self.assertEqual(c.logb(10), d) 1973 self.assertRaises(TypeError, c.logb, '10') 1974 1975 def test_logical_and(self): 1976 c = Context() 1977 d = c.logical_and(Decimal(1), Decimal(1)) 1978 self.assertEqual(c.logical_and(1, 1), d) 1979 self.assertEqual(c.logical_and(Decimal(1), 1), d) 1980 self.assertEqual(c.logical_and(1, Decimal(1)), d) 1981 self.assertRaises(TypeError, c.logical_and, '1', 1) 1982 self.assertRaises(TypeError, c.logical_and, 1, '1') 1983 1984 def test_logical_invert(self): 1985 c = Context() 1986 d = c.logical_invert(Decimal(1000)) 1987 self.assertEqual(c.logical_invert(1000), d) 1988 self.assertRaises(TypeError, c.logical_invert, '1000') 1989 1990 def test_logical_or(self): 1991 c = Context() 1992 d = c.logical_or(Decimal(1), Decimal(1)) 1993 self.assertEqual(c.logical_or(1, 1), d) 1994 self.assertEqual(c.logical_or(Decimal(1), 1), d) 1995 self.assertEqual(c.logical_or(1, Decimal(1)), d) 1996 self.assertRaises(TypeError, c.logical_or, '1', 1) 1997 self.assertRaises(TypeError, c.logical_or, 1, '1') 1998 1999 def test_logical_xor(self): 2000 c = Context() 2001 d = c.logical_xor(Decimal(1), Decimal(1)) 2002 self.assertEqual(c.logical_xor(1, 1), d) 2003 self.assertEqual(c.logical_xor(Decimal(1), 1), d) 2004 self.assertEqual(c.logical_xor(1, Decimal(1)), d) 2005 self.assertRaises(TypeError, c.logical_xor, '1', 1) 2006 self.assertRaises(TypeError, c.logical_xor, 1, '1') 2007 2008 def test_max(self): 2009 c = Context() 2010 d = c.max(Decimal(1), Decimal(2)) 2011 self.assertEqual(c.max(1, 2), d) 2012 self.assertEqual(c.max(Decimal(1), 2), d) 2013 self.assertEqual(c.max(1, Decimal(2)), d) 2014 self.assertRaises(TypeError, c.max, '1', 2) 2015 self.assertRaises(TypeError, c.max, 1, '2') 2016 2017 def test_max_mag(self): 2018 c = Context() 2019 d = c.max_mag(Decimal(1), Decimal(2)) 2020 self.assertEqual(c.max_mag(1, 2), d) 2021 self.assertEqual(c.max_mag(Decimal(1), 2), d) 2022 self.assertEqual(c.max_mag(1, Decimal(2)), d) 2023 self.assertRaises(TypeError, c.max_mag, '1', 2) 2024 self.assertRaises(TypeError, c.max_mag, 1, '2') 2025 2026 def test_min(self): 2027 c = Context() 2028 d = c.min(Decimal(1), Decimal(2)) 2029 self.assertEqual(c.min(1, 2), d) 2030 self.assertEqual(c.min(Decimal(1), 2), d) 2031 self.assertEqual(c.min(1, Decimal(2)), d) 2032 self.assertRaises(TypeError, c.min, '1', 2) 2033 self.assertRaises(TypeError, c.min, 1, '2') 2034 2035 def test_min_mag(self): 2036 c = Context() 2037 d = c.min_mag(Decimal(1), Decimal(2)) 2038 self.assertEqual(c.min_mag(1, 2), d) 2039 self.assertEqual(c.min_mag(Decimal(1), 2), d) 2040 self.assertEqual(c.min_mag(1, Decimal(2)), d) 2041 self.assertRaises(TypeError, c.min_mag, '1', 2) 2042 self.assertRaises(TypeError, c.min_mag, 1, '2') 2043 2044 def test_minus(self): 2045 c = Context() 2046 d = c.minus(Decimal(10)) 2047 self.assertEqual(c.minus(10), d) 2048 self.assertRaises(TypeError, c.minus, '10') 2049 2050 def test_multiply(self): 2051 c = Context() 2052 d = c.multiply(Decimal(1), Decimal(2)) 2053 self.assertEqual(c.multiply(1, 2), d) 2054 self.assertEqual(c.multiply(Decimal(1), 2), d) 2055 self.assertEqual(c.multiply(1, Decimal(2)), d) 2056 self.assertRaises(TypeError, c.multiply, '1', 2) 2057 self.assertRaises(TypeError, c.multiply, 1, '2') 2058 2059 def test_next_minus(self): 2060 c = Context() 2061 d = c.next_minus(Decimal(10)) 2062 self.assertEqual(c.next_minus(10), d) 2063 self.assertRaises(TypeError, c.next_minus, '10') 2064 2065 def test_next_plus(self): 2066 c = Context() 2067 d = c.next_plus(Decimal(10)) 2068 self.assertEqual(c.next_plus(10), d) 2069 self.assertRaises(TypeError, c.next_plus, '10') 2070 2071 def test_next_toward(self): 2072 c = Context() 2073 d = c.next_toward(Decimal(1), Decimal(2)) 2074 self.assertEqual(c.next_toward(1, 2), d) 2075 self.assertEqual(c.next_toward(Decimal(1), 2), d) 2076 self.assertEqual(c.next_toward(1, Decimal(2)), d) 2077 self.assertRaises(TypeError, c.next_toward, '1', 2) 2078 self.assertRaises(TypeError, c.next_toward, 1, '2') 2079 2080 def test_normalize(self): 2081 c = Context() 2082 d = c.normalize(Decimal(10)) 2083 self.assertEqual(c.normalize(10), d) 2084 self.assertRaises(TypeError, c.normalize, '10') 2085 2086 def test_number_class(self): 2087 c = Context() 2088 self.assertEqual(c.number_class(123), c.number_class(Decimal(123))) 2089 self.assertEqual(c.number_class(0), c.number_class(Decimal(0))) 2090 self.assertEqual(c.number_class(-45), c.number_class(Decimal(-45))) 2091 2092 def test_power(self): 2093 c = Context() 2094 d = c.power(Decimal(1), Decimal(4), Decimal(2)) 2095 self.assertEqual(c.power(1, 4, 2), d) 2096 self.assertEqual(c.power(Decimal(1), 4, 2), d) 2097 self.assertEqual(c.power(1, Decimal(4), 2), d) 2098 self.assertEqual(c.power(1, 4, Decimal(2)), d) 2099 self.assertEqual(c.power(Decimal(1), Decimal(4), 2), d) 2100 self.assertRaises(TypeError, c.power, '1', 4, 2) 2101 self.assertRaises(TypeError, c.power, 1, '4', 2) 2102 self.assertRaises(TypeError, c.power, 1, 4, '2') 2103 2104 def test_plus(self): 2105 c = Context() 2106 d = c.plus(Decimal(10)) 2107 self.assertEqual(c.plus(10), d) 2108 self.assertRaises(TypeError, c.plus, '10') 2109 2110 def test_quantize(self): 2111 c = Context() 2112 d = c.quantize(Decimal(1), Decimal(2)) 2113 self.assertEqual(c.quantize(1, 2), d) 2114 self.assertEqual(c.quantize(Decimal(1), 2), d) 2115 self.assertEqual(c.quantize(1, Decimal(2)), d) 2116 self.assertRaises(TypeError, c.quantize, '1', 2) 2117 self.assertRaises(TypeError, c.quantize, 1, '2') 2118 2119 def test_remainder(self): 2120 c = Context() 2121 d = c.remainder(Decimal(1), Decimal(2)) 2122 self.assertEqual(c.remainder(1, 2), d) 2123 self.assertEqual(c.remainder(Decimal(1), 2), d) 2124 self.assertEqual(c.remainder(1, Decimal(2)), d) 2125 self.assertRaises(TypeError, c.remainder, '1', 2) 2126 self.assertRaises(TypeError, c.remainder, 1, '2') 2127 2128 def test_remainder_near(self): 2129 c = Context() 2130 d = c.remainder_near(Decimal(1), Decimal(2)) 2131 self.assertEqual(c.remainder_near(1, 2), d) 2132 self.assertEqual(c.remainder_near(Decimal(1), 2), d) 2133 self.assertEqual(c.remainder_near(1, Decimal(2)), d) 2134 self.assertRaises(TypeError, c.remainder_near, '1', 2) 2135 self.assertRaises(TypeError, c.remainder_near, 1, '2') 2136 2137 def test_rotate(self): 2138 c = Context() 2139 d = c.rotate(Decimal(1), Decimal(2)) 2140 self.assertEqual(c.rotate(1, 2), d) 2141 self.assertEqual(c.rotate(Decimal(1), 2), d) 2142 self.assertEqual(c.rotate(1, Decimal(2)), d) 2143 self.assertRaises(TypeError, c.rotate, '1', 2) 2144 self.assertRaises(TypeError, c.rotate, 1, '2') 2145 2146 def test_sqrt(self): 2147 c = Context() 2148 d = c.sqrt(Decimal(10)) 2149 self.assertEqual(c.sqrt(10), d) 2150 self.assertRaises(TypeError, c.sqrt, '10') 2151 2152 def test_same_quantum(self): 2153 c = Context() 2154 d = c.same_quantum(Decimal(1), Decimal(2)) 2155 self.assertEqual(c.same_quantum(1, 2), d) 2156 self.assertEqual(c.same_quantum(Decimal(1), 2), d) 2157 self.assertEqual(c.same_quantum(1, Decimal(2)), d) 2158 self.assertRaises(TypeError, c.same_quantum, '1', 2) 2159 self.assertRaises(TypeError, c.same_quantum, 1, '2') 2160 2161 def test_scaleb(self): 2162 c = Context() 2163 d = c.scaleb(Decimal(1), Decimal(2)) 2164 self.assertEqual(c.scaleb(1, 2), d) 2165 self.assertEqual(c.scaleb(Decimal(1), 2), d) 2166 self.assertEqual(c.scaleb(1, Decimal(2)), d) 2167 self.assertRaises(TypeError, c.scaleb, '1', 2) 2168 self.assertRaises(TypeError, c.scaleb, 1, '2') 2169 2170 def test_shift(self): 2171 c = Context() 2172 d = c.shift(Decimal(1), Decimal(2)) 2173 self.assertEqual(c.shift(1, 2), d) 2174 self.assertEqual(c.shift(Decimal(1), 2), d) 2175 self.assertEqual(c.shift(1, Decimal(2)), d) 2176 self.assertRaises(TypeError, c.shift, '1', 2) 2177 self.assertRaises(TypeError, c.shift, 1, '2') 2178 2179 def test_subtract(self): 2180 c = Context() 2181 d = c.subtract(Decimal(1), Decimal(2)) 2182 self.assertEqual(c.subtract(1, 2), d) 2183 self.assertEqual(c.subtract(Decimal(1), 2), d) 2184 self.assertEqual(c.subtract(1, Decimal(2)), d) 2185 self.assertRaises(TypeError, c.subtract, '1', 2) 2186 self.assertRaises(TypeError, c.subtract, 1, '2') 2187 2188 def test_to_eng_string(self): 2189 c = Context() 2190 d = c.to_eng_string(Decimal(10)) 2191 self.assertEqual(c.to_eng_string(10), d) 2192 self.assertRaises(TypeError, c.to_eng_string, '10') 2193 2194 def test_to_sci_string(self): 2195 c = Context() 2196 d = c.to_sci_string(Decimal(10)) 2197 self.assertEqual(c.to_sci_string(10), d) 2198 self.assertRaises(TypeError, c.to_sci_string, '10') 2199 2200 def test_to_integral_exact(self): 2201 c = Context() 2202 d = c.to_integral_exact(Decimal(10)) 2203 self.assertEqual(c.to_integral_exact(10), d) 2204 self.assertRaises(TypeError, c.to_integral_exact, '10') 2205 2206 def test_to_integral_value(self): 2207 c = Context() 2208 d = c.to_integral_value(Decimal(10)) 2209 self.assertEqual(c.to_integral_value(10), d) 2210 self.assertRaises(TypeError, c.to_integral_value, '10') 1474 2211 1475 2212 class WithStatementTest(unittest.TestCase): … … 1483 2220 set_ctx = getcontext() 1484 2221 final_ctx = getcontext() 1485 self.assert _(orig_ctx isfinal_ctx, 'did not restore context correctly')1486 self.assert _(orig_ctx is notset_ctx, 'did not copy the context')1487 self.assert _(set_ctx isenter_ctx, '__enter__ returned wrong context')2222 self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly') 2223 self.assertIsNot(orig_ctx, set_ctx, 'did not copy the context') 2224 self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context') 1488 2225 1489 2226 def test_localcontextarg(self): … … 1494 2231 set_ctx = getcontext() 1495 2232 final_ctx = getcontext() 1496 self.assert _(orig_ctx isfinal_ctx, 'did not restore context correctly')1497 self.assert _(set_ctx.prec ==new_ctx.prec, 'did not set correct context')1498 self.assert _(new_ctx is notset_ctx, 'did not copy the context')1499 self.assert _(set_ctx isenter_ctx, '__enter__ returned wrong context')2233 self.assertIs(orig_ctx, final_ctx, 'did not restore context correctly') 2234 self.assertEqual(set_ctx.prec, new_ctx.prec, 'did not set correct context') 2235 self.assertIsNot(new_ctx, set_ctx, 'did not copy the context') 2236 self.assertIs(set_ctx, enter_ctx, '__enter__ returned wrong context') 1500 2237 1501 2238 class ContextFlags(unittest.TestCase): … … 1538 2275 if flag not in expected_flags: 1539 2276 expected_flags.append(flag) 1540 expected_flags.sort()1541 2277 1542 2278 # flags we actually got 1543 2279 new_flags = [k for k,v in context.flags.items() if v] 1544 new_flags.sort()1545 2280 1546 2281 self.assertEqual(ans, new_ans, 1547 2282 "operation produces different answers depending on flags set: " + 1548 2283 "expected %s, got %s." % (ans, new_ans)) 1549 self.assert Equal(new_flags, expected_flags,2284 self.assertItemsEqual(new_flags, expected_flags, 1550 2285 "operation raises different flags depending on flags set: " + 1551 2286 "expected %s, got %s" % (expected_flags, new_flags))
Note:
See TracChangeset
for help on using the changeset viewer.