Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4365cab

Browse files
committed
Add Tim Peters' test for long ints
1 parent 0ba3536 commit 4365cab

2 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lib/test/output/test_long

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
test_long
2+
long / * % divmod
3+
long bit-operation identities
4+
long str/hex/oct
5+

Lib/test/test_long.py

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
from test_support import TestFailed, verbose
2+
from string import join
3+
from random import random, randint
4+
5+
# SHIFT should match the value in longintrepr.h for best testing.
6+
SHIFT = 15
7+
BASE = 2 ** SHIFT
8+
MASK = BASE - 1
9+
10+
# Max number of base BASE digits to use in test cases. Doubling
11+
# this will at least quadruple the runtime.
12+
MAXDIGITS = 10
13+
14+
# ------------------------------------------------------------ utilities
15+
16+
# Use check instead of assert so the test still does something
17+
# under -O.
18+
19+
def check(ok, *args):
20+
if not ok:
21+
raise TestFailed, join(map(str, args), " ")
22+
23+
# Get random long consisting of ndigits random digits (relative to base
24+
# BASE). The sign bit is also random.
25+
26+
def getran(ndigits):
27+
answer = 0L
28+
for i in range(ndigits):
29+
answer = (answer << SHIFT) | randint(0, MASK)
30+
if random() < 0.5:
31+
answer = -answer
32+
return answer
33+
34+
# --------------------------------------------------------------- divmod
35+
36+
def test_division_2(x, y):
37+
q, r = divmod(x, y)
38+
q2, r2 = x/y, x%y
39+
check(q == q2, "divmod returns different quotient than / for", x, y)
40+
check(r == r2, "divmod returns different mod than % for", x, y)
41+
check(x == q*y + r, "x != q*y + r after divmod on", x, y)
42+
if y > 0:
43+
check(0 <= r < y, "bad mod from divmod on", x, y)
44+
else:
45+
check(y < r <= 0, "bad mod from divmod on", x, y)
46+
47+
def test_division(maxdigits=MAXDIGITS):
48+
print "long / * % divmod"
49+
digits = range(1, maxdigits+1)
50+
for lenx in digits:
51+
x = getran(lenx)
52+
for leny in digits:
53+
y = getran(leny) or 1L
54+
test_division_2(x, y)
55+
56+
# -------------------------------------------------------------- ~ & | ^
57+
58+
def test_bitop_identities_1(x):
59+
check(x == ~~x, "x != ~~x for", x)
60+
check(x & x == x, "x & x != x for", x)
61+
check(x | x == x, "x | x != x for", x)
62+
check(x ^ x == 0, "x ^ x != 0 for", x)
63+
check(x & ~x == 0, "x & ~x != 0 for", x)
64+
check(x | ~x == -1, "x | ~x != -1 for", x)
65+
check(x ^ ~x == -1, "x ^ ~x != -1 for", x)
66+
check(-x == 1 + ~x, "-x != 1 + ~x for", x)
67+
for s in range(2*SHIFT):
68+
check( ((x >> s) << s) | (x & ((1L << s) - 1)) == x,
69+
"((x >> s) << s) | (x & ((1L << s) - 1)) != x for",
70+
x, s)
71+
72+
def test_bitop_identities_2(x, y):
73+
check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y)
74+
check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y)
75+
check(x ^ y == (x | y) & ~(x & y),
76+
"x ^ y != (x | y) & ~(x & y) for", x, y)
77+
check(x ^ y == (x & ~y) | (~x & y),
78+
"x ^ y == (x & ~y) | (~x & y) for", x, y)
79+
check(x ^ y == (x | y) & (~x | ~y),
80+
"x ^ y == (x | y) & (~x | ~y) for", x, y)
81+
82+
def test_bitop_identities_3(x, y, z):
83+
check(x & (y | z) == (x & y) | (x & z),
84+
"x & (y | z) != (x & y) | (x & z) for", x, y, z)
85+
check(x | (y & z) == (x | y) & (x | z),
86+
"x | (y & z) != (x | y) & (x | z) for", x, y, z)
87+
88+
def test_bitop_identities(maxdigits=MAXDIGITS):
89+
print "long bit-operation identities"
90+
digits = range(1, maxdigits+1)
91+
for lenx in digits:
92+
x = getran(lenx)
93+
test_bitop_identities_1(x)
94+
for leny in digits:
95+
y = getran(leny)
96+
test_bitop_identities_2(x, y)
97+
test_bitop_identities_3(x, y, getran((lenx + leny)/2))
98+
99+
# ---------------------------------------------------------- hex oct str
100+
101+
def slow_format(x, base):
102+
if (x, base) == (0, 8):
103+
# this is an oddball!
104+
return "0L"
105+
digits = []
106+
sign = 0
107+
if x < 0:
108+
sign, x = 1, -x
109+
marks = "0123456789ABCDEF"
110+
while x:
111+
x, r = divmod(x, base)
112+
digits.append(int(r))
113+
digits.reverse()
114+
digits = digits or [0]
115+
return ['', '-'][sign] + \
116+
{8: '0', 10: '', 16: '0x'}[base] + \
117+
join(map(lambda i, marks=marks: marks[i], digits), '') + \
118+
"L"
119+
120+
def test_format_1(x):
121+
for base, mapper in (8, oct), (10, str), (16, hex):
122+
got = mapper(x)
123+
expected = slow_format(x, base)
124+
check(got == expected, mapper.__name__, "returned",
125+
got, "but expected", expected, "for", x)
126+
127+
def test_format(maxdigits=MAXDIGITS):
128+
print "long str/hex/oct"
129+
for i in range(10):
130+
for lenx in range(1, maxdigits+1):
131+
x = getran(lenx)
132+
test_format_1(x)
133+
134+
# ---------------------------------------------------------------- do it
135+
136+
test_division()
137+
test_bitop_identities()
138+
test_format()
139+

0 commit comments

Comments
 (0)