Datasets:
Download Python/val/3.13/numeric-underscores/modern_02.py from SwashBuckler001/idiombench: direct link, hf CLI and curl.
- Browser
- Download file 4.23 kB
-
https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/val/3.13/numeric-underscores/modern_02.py
- Command line
-
hf download hf://datasets/SwashBuckler001/idiombench/Python/val/3.13/numeric-underscores/modern_02.py
-
curl -L -o modern_02.py https://huggingface.co/datasets/SwashBuckler001/idiombench/resolve/main/Python/val/3.13/numeric-underscores/modern_02.py
4.23 kB
| import sys | |
| import time | |
| import unittest | |
| from unittest import mock | |
| from test import support | |
| from test.test_grammar import (VALID_UNDERSCORE_LITERALS, | |
| INVALID_UNDERSCORE_LITERALS) | |
| 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, suffix): | |
| s = str(n) | |
| self.assertEqual(s[-10:], suffix) | |
| s2 = str(-n) | |
| self.assertEqual(s2, '-' + s) | |
| s3 = '%d' % n | |
| self.assertEqual(s3, s) | |
| s4 = b'%d' % n | |
| self.assertEqual(s4, s.encode('ascii')) | |
| def test_pylong_int_to_decimal(self): | |
| self._test_pylong_int_to_decimal((1 << 100_000), '9883109376') | |
| self._test_pylong_int_to_decimal((1 << 100_000) - 1, '9883109375') | |
| self._test_pylong_int_to_decimal(10**30_000, '0000000000') | |
| self._test_pylong_int_to_decimal(10**30_000 - 1, '9999999999') | |
| self._test_pylong_int_to_decimal(3**60_000, '9313200001') | |
| def test_pylong_int_to_decimal_2(self): | |
| self._test_pylong_int_to_decimal(2**1_000_000, '2747109376') | |
| self._test_pylong_int_to_decimal(10**300_000, '0000000000') | |
| self._test_pylong_int_to_decimal(3**600_000, '3132000001') | |
| 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) | |
| # tests implementation details of CPython. | |
| def test_pylong_misbehavior_error_path_to_str( | |
| self, mock_int_to_str): | |
| with support.adjust_int_max_str_digits(20_000): | |
| big_value = int('7'*19_999) | |
| mock_int_to_str.return_value = None # not a str | |
| with self.assertRaises(TypeError) as ctx: | |
| str(big_value) | |
| self.assertIn('_pylong.int_to_decimal_string did not', | |
| str(ctx.exception)) | |
| mock_int_to_str.side_effect = RuntimeError("testABC") | |
| with self.assertRaises(RuntimeError): | |
| str(big_value) | |
| # tests implementation details of CPython. | |
| def test_pylong_misbehavior_error_path_from_str( | |
| self, mock_int_from_str): | |
| big_value = '7'*19_999 | |
| with support.adjust_int_max_str_digits(20_000): | |
| mock_int_from_str.return_value = b'not an int' | |
| with self.assertRaises(TypeError) as ctx: | |
| int(big_value) | |
| self.assertIn('_pylong.int_from_string did not', | |
| str(ctx.exception)) | |
| mock_int_from_str.side_effect = RuntimeError("test123") | |
| with self.assertRaises(RuntimeError): | |
| int(big_value) | |
| def test_pylong_roundtrip(self): | |
| from random import randrange, getrandbits | |
| bits = 5000 | |
| while bits <= 1_000_000: | |
| bits += randrange(-100, 101) # break bitlength patterns | |
| hibit = 1 << (bits - 1) | |
| n = hibit | getrandbits(bits - 1) | |
| assert n.bit_length() == bits | |
| sn = str(n) | |
| self.assertFalse(sn.startswith('0')) | |
| self.assertEqual(n, int(sn)) | |
| bits <<= 1 | |