~ubuntu-branches/ubuntu/karmic/python3.0/karmic

« back to all changes in this revision

Viewing changes to Lib/test/test_long.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-16 17:18:23 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090216171823-1d5cm5qnnjvmnzzm
Tags: 3.0.1-0ubuntu1
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
283
283
 
284
284
        self.assertRaises(ValueError, int, '123\0')
285
285
        self.assertRaises(ValueError, int, '53', 40)
 
286
        # trailing L should no longer be accepted...
 
287
        self.assertRaises(ValueError, int, '123L')
 
288
        self.assertRaises(ValueError, int, '123l')
 
289
        self.assertRaises(ValueError, int, '0L')
 
290
        self.assertRaises(ValueError, int, '-37L')
 
291
        self.assertRaises(ValueError, int, '0x32L', 16)
 
292
        self.assertRaises(ValueError, int, '1L', 21)
 
293
        # ... but it's just a normal digit if base >= 22
 
294
        self.assertEqual(int('1L', 22), 43)
 
295
 
286
296
        self.assertRaises(TypeError, int, 1, 12)
287
297
 
288
298
        # SF patch #1638879: embedded NULs were not detected with
366
376
 
367
377
 
368
378
    def test_conversion(self):
369
 
        # Test __long__()
 
379
        # Test __int__()
370
380
        class ClassicMissingMethods:
371
381
            pass
372
382
        self.assertRaises(TypeError, int, ClassicMissingMethods())
409
419
        class Classic:
410
420
            pass
411
421
        for base in (object, Classic):
412
 
            class LongOverridesTrunc(base):
413
 
                def __long__(self):
 
422
            class IntOverridesTrunc(base):
 
423
                def __int__(self):
414
424
                    return 42
415
425
                def __trunc__(self):
416
426
                    return -12
417
 
            self.assertEqual(int(LongOverridesTrunc()), 42)
 
427
            self.assertEqual(int(IntOverridesTrunc()), 42)
418
428
 
419
429
            class JustTrunc(base):
420
430
                def __trunc__(self):
421
431
                    return 42
422
432
            self.assertEqual(int(JustTrunc()), 42)
423
433
 
 
434
            class JustLong(base):
 
435
                # test that __long__ no longer used in 3.x
 
436
                def __long__(self):
 
437
                    return 42
 
438
            self.assertRaises(TypeError, int, JustLong())
 
439
 
 
440
            class LongTrunc(base):
 
441
                # __long__ should be ignored in 3.x
 
442
                def __long__(self):
 
443
                    return 42
 
444
                def __trunc__(self):
 
445
                    return 1729
 
446
            self.assertEqual(int(LongTrunc()), 1729)
 
447
 
424
448
            for trunc_result_base in (object, Classic):
425
449
                class Integral(trunc_result_base):
426
450
                    def __int__(self):
672
696
            def _cmp__(self, other):
673
697
                if not isinstance(other, Rat):
674
698
                    other = Rat(other)
675
 
                return cmp(self.n * other.d, self.d * other.n)
 
699
                x, y = self.n * other.d, self.d * other.n
 
700
                return (x > y) - (x < y)
676
701
            def __eq__(self, other):
677
702
                return self._cmp__(other) == 0
678
703
            def __ne__(self, other):
702
727
            Rx = Rat(x)
703
728
            for y in cases:
704
729
                Ry = Rat(y)
705
 
                Rcmp = cmp(Rx, Ry)
706
 
                xycmp = cmp(x, y)
 
730
                Rcmp = (Rx > Ry) - (Rx < Ry)
 
731
                xycmp = (x > y) - (x < y)
707
732
                eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp))
708
733
                eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp))
709
734
                eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp))
836
861
        self.assertTrue(i - i is 0)
837
862
        self.assertTrue(0 * i is 0)
838
863
 
 
864
    def test_round(self):
 
865
        # check round-half-even algorithm. For round to nearest ten;
 
866
        # rounding map is invariant under adding multiples of 20
 
867
        test_dict = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0,
 
868
                     6:10, 7:10, 8:10, 9:10, 10:10, 11:10, 12:10, 13:10, 14:10,
 
869
                     15:20, 16:20, 17:20, 18:20, 19:20}
 
870
        for offset in range(-520, 520, 20):
 
871
            for k, v in test_dict.items():
 
872
                got = round(k+offset, -1)
 
873
                expected = v+offset
 
874
                self.assertEqual(got, expected)
 
875
                self.assert_(type(got) is int)
 
876
 
 
877
        # larger second argument
 
878
        self.assertEqual(round(-150, -2), -200)
 
879
        self.assertEqual(round(-149, -2), -100)
 
880
        self.assertEqual(round(-51, -2), -100)
 
881
        self.assertEqual(round(-50, -2), 0)
 
882
        self.assertEqual(round(-49, -2), 0)
 
883
        self.assertEqual(round(-1, -2), 0)
 
884
        self.assertEqual(round(0, -2), 0)
 
885
        self.assertEqual(round(1, -2), 0)
 
886
        self.assertEqual(round(49, -2), 0)
 
887
        self.assertEqual(round(50, -2), 0)
 
888
        self.assertEqual(round(51, -2), 100)
 
889
        self.assertEqual(round(149, -2), 100)
 
890
        self.assertEqual(round(150, -2), 200)
 
891
        self.assertEqual(round(250, -2), 200)
 
892
        self.assertEqual(round(251, -2), 300)
 
893
        self.assertEqual(round(172500, -3), 172000)
 
894
        self.assertEqual(round(173500, -3), 174000)
 
895
        self.assertEqual(round(31415926535, -1), 31415926540)
 
896
        self.assertEqual(round(31415926535, -2), 31415926500)
 
897
        self.assertEqual(round(31415926535, -3), 31415927000)
 
898
        self.assertEqual(round(31415926535, -4), 31415930000)
 
899
        self.assertEqual(round(31415926535, -5), 31415900000)
 
900
        self.assertEqual(round(31415926535, -6), 31416000000)
 
901
        self.assertEqual(round(31415926535, -7), 31420000000)
 
902
        self.assertEqual(round(31415926535, -8), 31400000000)
 
903
        self.assertEqual(round(31415926535, -9), 31000000000)
 
904
        self.assertEqual(round(31415926535, -10), 30000000000)
 
905
        self.assertEqual(round(31415926535, -11), 0)
 
906
        self.assertEqual(round(31415926535, -12), 0)
 
907
        self.assertEqual(round(31415926535, -999), 0)
 
908
 
 
909
        # should get correct results even for huge inputs
 
910
        for k in range(10, 100):
 
911
            got = round(10**k + 324678, -3)
 
912
            expect = 10**k + 325000
 
913
            self.assertEqual(got, expect)
 
914
            self.assert_(type(got) is int)
 
915
 
 
916
        # nonnegative second argument: round(x, n) should just return x
 
917
        for n in range(5):
 
918
            for i in range(100):
 
919
                x = random.randrange(-10000, 10000)
 
920
                got = round(x, n)
 
921
                self.assertEqual(got, x)
 
922
                self.assert_(type(got) is int)
 
923
        for huge_n in 2**31-1, 2**31, 2**63-1, 2**63, 2**100, 10**100:
 
924
            self.assertEqual(round(8979323, huge_n), 8979323)
 
925
 
 
926
        # omitted second argument
 
927
        for i in range(100):
 
928
            x = random.randrange(-10000, 10000)
 
929
            got = round(x)
 
930
            self.assertEqual(got, x)
 
931
            self.assert_(type(got) is int)
 
932
 
 
933
        # bad second argument
 
934
        bad_exponents = ('brian', 2.0, 0j, None)
 
935
        for e in bad_exponents:
 
936
            self.assertRaises(TypeError, round, 3, e)
 
937
 
 
938
 
 
939
 
839
940
def test_main():
840
941
    support.run_unittest(LongTest)
841
942