mirror of
https://github.com/python/cpython.git
synced 2025-08-23 10:16:01 +00:00
gh-90716: add _pylong.py module (#96673)
Add Python implementations of certain longobject.c functions. These use asymptotically faster algorithms that can be used for operations on integers with many digits. In those cases, the performance overhead of the Python implementation is not significant since the asymptotic behavior is what dominates runtime. Functions provided by this module should be considered private and not part of any public API. Co-author: Tim Peters <tim.peters@gmail.com> Co-author: Mark Dickinson <dickinsm@gmail.com> Co-author: Bjorn Martinsson
This commit is contained in:
parent
5d30544485
commit
de6981680b
6 changed files with 572 additions and 0 deletions
|
@ -795,5 +795,52 @@ class IntSubclassStrDigitLimitsTests(IntStrDigitLimitsTests):
|
|||
int_class = IntSubclass
|
||||
|
||||
|
||||
class PyLongModuleTests(unittest.TestCase):
|
||||
# Tests of the functions in _pylong.py. Those get used when the
|
||||
# number of digits in the input values are large enough.
|
||||
|
||||
def setUp(self):
|
||||
super().setUp()
|
||||
self._previous_limit = sys.get_int_max_str_digits()
|
||||
sys.set_int_max_str_digits(0)
|
||||
|
||||
def tearDown(self):
|
||||
sys.set_int_max_str_digits(self._previous_limit)
|
||||
super().tearDown()
|
||||
|
||||
def test_pylong_int_to_decimal(self):
|
||||
n = (1 << 100_000) - 1
|
||||
suffix = '9883109375'
|
||||
s = str(n)
|
||||
assert s[-10:] == suffix
|
||||
s = str(-n)
|
||||
assert s[-10:] == suffix
|
||||
s = '%d' % n
|
||||
assert s[-10:] == suffix
|
||||
s = b'%d' % n
|
||||
assert s[-10:] == suffix.encode('ascii')
|
||||
|
||||
def test_pylong_int_divmod(self):
|
||||
n = (1 << 100_000)
|
||||
a, b = divmod(n*3 + 1, n)
|
||||
assert a == 3 and b == 1
|
||||
|
||||
def test_pylong_str_to_int(self):
|
||||
v1 = 1 << 100_000
|
||||
s = str(v1)
|
||||
v2 = int(s)
|
||||
assert v1 == v2
|
||||
v3 = int(' -' + s)
|
||||
assert -v1 == v3
|
||||
v4 = int(' +' + s + ' ')
|
||||
assert v1 == v4
|
||||
with self.assertRaises(ValueError) as err:
|
||||
int(s + 'z')
|
||||
with self.assertRaises(ValueError) as err:
|
||||
int(s + '_')
|
||||
with self.assertRaises(ValueError) as err:
|
||||
int('_' + s)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue