diff options
author | Neil Schemenauer <nas-github@arctrix.com> | 2022-10-26 05:00:50 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-10-26 05:00:50 (GMT) |
commit | de6981680bcf6496e5996a853b2eaa700ed59b2c (patch) | |
tree | a83c157e062851f9ea36eb8a11977ecd7e667d38 /Lib/test/test_int.py | |
parent | 5d30544485dc56ab999ad7656ef6559306fd013f (diff) | |
download | cpython-de6981680bcf6496e5996a853b2eaa700ed59b2c.zip cpython-de6981680bcf6496e5996a853b2eaa700ed59b2c.tar.gz cpython-de6981680bcf6496e5996a853b2eaa700ed59b2c.tar.bz2 |
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
Diffstat (limited to 'Lib/test/test_int.py')
-rw-r--r-- | Lib/test/test_int.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/Lib/test/test_int.py b/Lib/test/test_int.py index 625c388..f484c59 100644 --- a/Lib/test/test_int.py +++ b/Lib/test/test_int.py @@ -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() |