1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
from __future__ import division
# When true division is the default, get rid of this and add it to
# test_long.py instead. In the meantime, it's too obscure to try to
# trick just part of test_long into using future division.
import unittest
from test.support import run_unittest
class TrueDivisionTests(unittest.TestCase):
def test(self):
huge = 1 << 40000
mhuge = -huge
self.assertEqual(huge / huge, 1.0)
self.assertEqual(mhuge / mhuge, 1.0)
self.assertEqual(huge / mhuge, -1.0)
self.assertEqual(mhuge / huge, -1.0)
self.assertEqual(1 / huge, 0.0)
self.assertEqual(1 / huge, 0.0)
self.assertEqual(1 / mhuge, 0.0)
self.assertEqual(1 / mhuge, 0.0)
self.assertEqual((666 * huge + (huge >> 1)) / huge, 666.5)
self.assertEqual((666 * mhuge + (mhuge >> 1)) / mhuge, 666.5)
self.assertEqual((666 * huge + (huge >> 1)) / mhuge, -666.5)
self.assertEqual((666 * mhuge + (mhuge >> 1)) / huge, -666.5)
self.assertEqual(huge / (huge << 1), 0.5)
self.assertEqual((1000000 * huge) / huge, 1000000)
namespace = {'huge': huge, 'mhuge': mhuge}
for overflow in ["float(huge)", "float(mhuge)",
"huge / 1", "huge / 2", "huge / -1", "huge / -2",
"mhuge / 100", "mhuge / 200"]:
# XXX(cwinter) this test doesn't pass when converted to
# use assertRaises.
try:
eval(overflow, namespace)
self.fail("expected OverflowError from %r" % overflow)
except OverflowError:
pass
for underflow in ["1 / huge", "2 / huge", "-1 / huge", "-2 / huge",
"100 / mhuge", "200 / mhuge"]:
result = eval(underflow, namespace)
self.assertEqual(result, 0.0,
"expected underflow to 0 from %r" % underflow)
for zero in ["huge / 0", "mhuge / 0"]:
self.assertRaises(ZeroDivisionError, eval, zero, namespace)
def test_main():
run_unittest(TrueDivisionTests)
if __name__ == "__main__":
test_main()
|