Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.4k
gh-141360: Refactor test decimal#141361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
efimov-mikhail wants to merge18 commits intopython:mainChoose a base branch fromefimov-mikhail:refactor_test_decimal
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+6,155 −6,044
Draft
Changes from1 commit
Commits
Show all changes
18 commits Select commitHold shift + click to select a range
c5ebc42 Remove redundant line
efimov-mikhail9aee907 Create test_decimal package, move decimaltestdata
efimov-mikhailc6c2eb1 Some fix to previous commit
efimov-mikhail5b7c58f Big file divided
efimov-mikhailb9328c9 New files: test_arithmetic.py and test_doc.py
efimov-mikhailf1f1c7a Introduce load_tests_for_base_classes
efimov-mikhail939cb5b Add test_format
efimov-mikhaile47c83d Add test_operators
efimov-mikhail876d9a7 Add test_threading
efimov-mikhailb674b77 Add test_usability
efimov-mikhailf81966d Add test_api
efimov-mikhail7244822 Add test_contexts
efimov-mikhail67e6a08 Add test_coverage
efimov-mikhail5585777 Add test_py_specific
efimov-mikhail70716f0 Add test_c_specific
efimov-mikhail223faa1 Little improvements
efimov-mikhail067b9de NEWS entry
efimov-mikhail27d2014 Update Misc/NEWS.d/next/Tests/2025-11-10-18-31-48.gh-issue-141360.VQL…
efimov-mikhailFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Add test_py_specific
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit558577778634a2471acb00ebef599b1e45356957
There are no files selected for viewing
180 changes: 0 additions & 180 deletionsLib/test/test_decimal/test_other.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletionsLib/test/test_decimal/test_py_specific.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| import unittest | ||
| from . import P, OrderedSignals | ||
| class PyFunctionality(unittest.TestCase): | ||
| """Extra functionality in decimal.py""" | ||
| def test_py_alternate_formatting(self): | ||
| # triples giving a format, a Decimal, and the expected result | ||
| Decimal = P.Decimal | ||
| localcontext = P.localcontext | ||
| test_values = [ | ||
| # Issue 7094: Alternate formatting (specified by #) | ||
| ('.0e', '1.0', '1e+0'), | ||
| ('#.0e', '1.0', '1.e+0'), | ||
| ('.0f', '1.0', '1'), | ||
| ('#.0f', '1.0', '1.'), | ||
| ('g', '1.1', '1.1'), | ||
| ('#g', '1.1', '1.1'), | ||
| ('.0g', '1', '1'), | ||
| ('#.0g', '1', '1.'), | ||
| ('.0%', '1.0', '100%'), | ||
| ('#.0%', '1.0', '100.%'), | ||
| ] | ||
| for fmt, d, result in test_values: | ||
| self.assertEqual(format(Decimal(d), fmt), result) | ||
| class PyWhitebox(unittest.TestCase): | ||
| """White box testing for decimal.py""" | ||
| def test_py_exact_power(self): | ||
| # Rarely exercised lines in _power_exact. | ||
| Decimal = P.Decimal | ||
| localcontext = P.localcontext | ||
| with localcontext() as c: | ||
| c.prec = 8 | ||
| x = Decimal(2**16) ** Decimal("-0.5") | ||
| self.assertEqual(x, Decimal('0.00390625')) | ||
| x = Decimal(2**16) ** Decimal("-0.6") | ||
| self.assertEqual(x, Decimal('0.0012885819')) | ||
| x = Decimal("256e7") ** Decimal("-0.5") | ||
| x = Decimal(152587890625) ** Decimal('-0.0625') | ||
| self.assertEqual(x, Decimal("0.2")) | ||
| x = Decimal("152587890625e7") ** Decimal('-0.0625') | ||
| x = Decimal(5**2659) ** Decimal('-0.0625') | ||
| c.prec = 1 | ||
| x = Decimal("152587890625") ** Decimal('-0.5') | ||
| self.assertEqual(x, Decimal('3e-6')) | ||
| c.prec = 2 | ||
| x = Decimal("152587890625") ** Decimal('-0.5') | ||
| self.assertEqual(x, Decimal('2.6e-6')) | ||
| c.prec = 3 | ||
| x = Decimal("152587890625") ** Decimal('-0.5') | ||
| self.assertEqual(x, Decimal('2.56e-6')) | ||
| c.prec = 28 | ||
| x = Decimal("152587890625") ** Decimal('-0.5') | ||
| self.assertEqual(x, Decimal('2.56e-6')) | ||
| c.prec = 201 | ||
| x = Decimal(2**578) ** Decimal("-0.5") | ||
| # See https://github.com/python/cpython/issues/118027 | ||
| # Testing for an exact power could appear to hang, in the Python | ||
| # version, as it attempted to compute 10**(MAX_EMAX + 1). | ||
| # Fixed via https://github.com/python/cpython/pull/118503. | ||
| c.prec = P.MAX_PREC | ||
| c.Emax = P.MAX_EMAX | ||
| c.Emin = P.MIN_EMIN | ||
| c.traps[P.Inexact] = 1 | ||
| D2 = Decimal(2) | ||
| # If the bug is still present, the next statement won't complete. | ||
| res = D2 ** 117 | ||
| self.assertEqual(res, 1 << 117) | ||
| def test_py_immutability_operations(self): | ||
| # Do operations and check that it didn't change internal objects. | ||
| Decimal = P.Decimal | ||
| DefaultContext = P.DefaultContext | ||
| setcontext = P.setcontext | ||
| c = DefaultContext.copy() | ||
| c.traps = dict((s, 0) for s in OrderedSignals[P]) | ||
| setcontext(c) | ||
| d1 = Decimal('-25e55') | ||
| b1 = Decimal('-25e55') | ||
| d2 = Decimal('33e+33') | ||
| b2 = Decimal('33e+33') | ||
| def checkSameDec(operation, useOther=False): | ||
| if useOther: | ||
| eval("d1." + operation + "(d2)") | ||
| self.assertEqual(d1._sign, b1._sign) | ||
| self.assertEqual(d1._int, b1._int) | ||
| self.assertEqual(d1._exp, b1._exp) | ||
| self.assertEqual(d2._sign, b2._sign) | ||
| self.assertEqual(d2._int, b2._int) | ||
| self.assertEqual(d2._exp, b2._exp) | ||
| else: | ||
| eval("d1." + operation + "()") | ||
| self.assertEqual(d1._sign, b1._sign) | ||
| self.assertEqual(d1._int, b1._int) | ||
| self.assertEqual(d1._exp, b1._exp) | ||
| Decimal(d1) | ||
| self.assertEqual(d1._sign, b1._sign) | ||
| self.assertEqual(d1._int, b1._int) | ||
| self.assertEqual(d1._exp, b1._exp) | ||
| checkSameDec("__abs__") | ||
| checkSameDec("__add__", True) | ||
| checkSameDec("__divmod__", True) | ||
| checkSameDec("__eq__", True) | ||
| checkSameDec("__ne__", True) | ||
| checkSameDec("__le__", True) | ||
| checkSameDec("__lt__", True) | ||
| checkSameDec("__ge__", True) | ||
| checkSameDec("__gt__", True) | ||
| checkSameDec("__float__") | ||
| checkSameDec("__floordiv__", True) | ||
| checkSameDec("__hash__") | ||
| checkSameDec("__int__") | ||
| checkSameDec("__trunc__") | ||
| checkSameDec("__mod__", True) | ||
| checkSameDec("__mul__", True) | ||
| checkSameDec("__neg__") | ||
| checkSameDec("__bool__") | ||
| checkSameDec("__pos__") | ||
| checkSameDec("__pow__", True) | ||
| checkSameDec("__radd__", True) | ||
| checkSameDec("__rdivmod__", True) | ||
| checkSameDec("__repr__") | ||
| checkSameDec("__rfloordiv__", True) | ||
| checkSameDec("__rmod__", True) | ||
| checkSameDec("__rmul__", True) | ||
| checkSameDec("__rpow__", True) | ||
| checkSameDec("__rsub__", True) | ||
| checkSameDec("__str__") | ||
| checkSameDec("__sub__", True) | ||
| checkSameDec("__truediv__", True) | ||
| checkSameDec("adjusted") | ||
| checkSameDec("as_tuple") | ||
| checkSameDec("compare", True) | ||
| checkSameDec("max", True) | ||
| checkSameDec("min", True) | ||
| checkSameDec("normalize") | ||
| checkSameDec("quantize", True) | ||
| checkSameDec("remainder_near", True) | ||
| checkSameDec("same_quantum", True) | ||
| checkSameDec("sqrt") | ||
| checkSameDec("to_eng_string") | ||
| checkSameDec("to_integral") | ||
| def test_py_decimal_id(self): | ||
| Decimal = P.Decimal | ||
| d = Decimal(45) | ||
| e = Decimal(d) | ||
| self.assertEqual(str(e), '45') | ||
| self.assertNotEqual(id(d), id(e)) | ||
| def test_py_rescale(self): | ||
| # Coverage | ||
| Decimal = P.Decimal | ||
| localcontext = P.localcontext | ||
| with localcontext() as c: | ||
| x = Decimal("NaN")._rescale(3, P.ROUND_UP) | ||
| self.assertTrue(x.is_nan()) | ||
| def test_py__round(self): | ||
| # Coverage | ||
| Decimal = P.Decimal | ||
| self.assertRaises(ValueError, Decimal("3.1234")._round, 0, P.ROUND_UP) | ||
| if __name__ == '__main__': | ||
| unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.