-
-
Notifications
You must be signed in to change notification settings - Fork 32k
gh-111495: Add tests for PyFloat C API #111624
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2f2e77f
gh-111495: Add tests for PyFloat C API
skirpichev 1d12623
Add missing coverage tests
skirpichev 3e9c05b
remove redundant tests for PyFloat_FromString()
skirpichev 04718df
Update Lib/test/test_capi/test_float.py
skirpichev 7b9b886
Merge branch 'main' into capi-float-tests
skirpichev 71e9cd0
Address review (partially)
skirpichev 4a00f1d
Use PyArg_Parse() in float_asdouble()
skirpichev 918b56e
Add float_asdouble()
skirpichev 44ff4b2
s/3.14/4.25/g
skirpichev 14c932e
memoryview tests
skirpichev 26f843c
Group logical blocks
skirpichev 54ec7b3
make test module executable
skirpichev 463ec44
Apply suggestions from code review
serhiy-storchaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,117 @@ | ||
import math | ||
import sys | ||
import unittest | ||
import warnings | ||
|
||
from test.test_capi.test_getargs import (Float, FloatSubclass, FloatSubclass2, | ||
BadIndex2, BadFloat2, Index, BadIndex, | ||
BadFloat) | ||
from test.support import import_helper | ||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
NULL = None | ||
|
||
|
||
class CAPIFloatTest(unittest.TestCase): | ||
def test_check(self): | ||
# Test PyFloat_Check() | ||
check = _testcapi.float_check | ||
|
||
self.assertTrue(check(4.25)) | ||
self.assertTrue(check(FloatSubclass(4.25))) | ||
self.assertFalse(check(Float())) | ||
self.assertFalse(check(3)) | ||
self.assertFalse(check(object())) | ||
|
||
# CRASHES check(NULL) | ||
|
||
def test_checkexact(self): | ||
# Test PyFloat_CheckExact() | ||
checkexact = _testcapi.float_checkexact | ||
|
||
self.assertTrue(checkexact(4.25)) | ||
self.assertFalse(checkexact(FloatSubclass(4.25))) | ||
self.assertFalse(checkexact(Float())) | ||
self.assertFalse(checkexact(3)) | ||
self.assertFalse(checkexact(object())) | ||
|
||
# CRASHES checkexact(NULL) | ||
|
||
def test_fromstring(self): | ||
# Test PyFloat_FromString() | ||
fromstring = _testcapi.float_fromstring | ||
|
||
self.assertEqual(fromstring("4.25"), 4.25) | ||
self.assertEqual(fromstring(b"4.25"), 4.25) | ||
self.assertRaises(ValueError, fromstring, "4.25\0") | ||
self.assertRaises(ValueError, fromstring, b"4.25\0") | ||
|
||
self.assertEqual(fromstring(bytearray(b"4.25")), 4.25) | ||
|
||
self.assertEqual(fromstring(memoryview(b"4.25")), 4.25) | ||
self.assertEqual(fromstring(memoryview(b"4.255")[:-1]), 4.25) | ||
self.assertRaises(TypeError, fromstring, memoryview(b"4.25")[::2]) | ||
|
||
self.assertRaises(TypeError, fromstring, 4.25) | ||
|
||
# CRASHES fromstring(NULL) | ||
|
||
def test_fromdouble(self): | ||
# Test PyFloat_FromDouble() | ||
fromdouble = _testcapi.float_fromdouble | ||
|
||
self.assertEqual(fromdouble(4.25), 4.25) | ||
|
||
def test_asdouble(self): | ||
# Test PyFloat_AsDouble() | ||
asdouble = _testcapi.float_asdouble | ||
|
||
class BadFloat3: | ||
def __float__(self): | ||
raise RuntimeError | ||
|
||
self.assertEqual(asdouble(4.25), 4.25) | ||
self.assertEqual(asdouble(-1.0), -1.0) | ||
self.assertEqual(asdouble(42), 42.0) | ||
self.assertEqual(asdouble(-1), -1.0) | ||
skirpichev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(asdouble(2**1000), float(2**1000)) | ||
|
||
self.assertEqual(asdouble(FloatSubclass(4.25)), 4.25) | ||
self.assertEqual(asdouble(FloatSubclass2(4.25)), 4.25) | ||
self.assertEqual(asdouble(Index()), 99.) | ||
|
||
self.assertRaises(TypeError, asdouble, BadIndex()) | ||
self.assertRaises(TypeError, asdouble, BadFloat()) | ||
self.assertRaises(RuntimeError, asdouble, BadFloat3()) | ||
with self.assertWarns(DeprecationWarning): | ||
self.assertEqual(asdouble(BadIndex2()), 1.) | ||
with self.assertWarns(DeprecationWarning): | ||
self.assertEqual(asdouble(BadFloat2()), 4.25) | ||
with warnings.catch_warnings(): | ||
warnings.simplefilter("error", DeprecationWarning) | ||
self.assertRaises(DeprecationWarning, asdouble, BadFloat2()) | ||
self.assertRaises(TypeError, asdouble, object()) | ||
self.assertRaises(TypeError, asdouble, NULL) | ||
|
||
def test_getinfo(self): | ||
# Test PyFloat_GetInfo() | ||
getinfo = _testcapi.float_getinfo | ||
|
||
self.assertEqual(getinfo(), sys.float_info) | ||
|
||
def test_getmax(self): | ||
# Test PyFloat_GetMax() | ||
getmax = _testcapi.float_getmax | ||
|
||
self.assertEqual(getmax(), sys.float_info.max) | ||
|
||
def test_getmin(self): | ||
# Test PyFloat_GetMax() | ||
getmin = _testcapi.float_getmin | ||
|
||
self.assertEqual(getmin(), sys.float_info.min) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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
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.
Uh oh!
There was an error while loading. Please reload this page.