diff options
author | Eric Smith <eric@trueblade.com> | 2009-04-29 12:34:19 (GMT) |
---|---|---|
committer | Eric Smith <eric@trueblade.com> | 2009-04-29 12:34:19 (GMT) |
commit | df9d4d6c7fa96e0a7a5d5bc3607fb0ff4977bb4d (patch) | |
tree | a2b004715eb7531e01e7565befbf44b7bc8036ef | |
parent | 8162382c486aac88b4a7c046515bc7c6b1bfc6c8 (diff) | |
download | cpython-df9d4d6c7fa96e0a7a5d5bc3607fb0ff4977bb4d.zip cpython-df9d4d6c7fa96e0a7a5d5bc3607fb0ff4977bb4d.tar.gz cpython-df9d4d6c7fa96e0a7a5d5bc3607fb0ff4977bb4d.tar.bz2 |
Added test that didn't make it in an svnmerge.
-rw-r--r-- | Lib/test/test_ascii_formatd.py | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/Lib/test/test_ascii_formatd.py b/Lib/test/test_ascii_formatd.py new file mode 100644 index 0000000..eaf52c4 --- /dev/null +++ b/Lib/test/test_ascii_formatd.py @@ -0,0 +1,62 @@ +# PyOS_ascii_formatd is deprecated and not called from anywhere in +# Python itself. So this module is the only place it gets tested. +# Test that it works, and test that it's deprecated. + +import unittest +from test.support import check_warnings, run_unittest, cpython_only + +class FormatDeprecationTests(unittest.TestCase): + + @cpython_only + def testFormatDeprecation(self): + # delay importing ctypes until we know we're in CPython + from ctypes import (pythonapi, create_string_buffer, sizeof, byref, + c_double) + PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd + buf = create_string_buffer(100) + + with check_warnings() as w: + PyOS_ascii_formatd(byref(buf), sizeof(buf), b'%+.10f', + c_double(10.0)) + self.assertEqual(buf.value, b'+10.0000000000') + + self.assertEqual(str(w.message), 'PyOS_ascii_formatd is deprecated, ' + 'use PyOS_double_to_string instead') + +class FormatTests(unittest.TestCase): + # ensure that, for the restricted set of format codes, + # %-formatting returns the same values os PyOS_ascii_formatd + @cpython_only + def testFormat(self): + # delay importing ctypes until we know we're in CPython + from ctypes import (pythonapi, create_string_buffer, sizeof, byref, + c_double) + PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd + buf = create_string_buffer(100) + + tests = [ + ('%f', 100.0), + ('%g', 100.0), + ('%#g', 100.0), + ('%#.2g', 100.0), + ('%#.2g', 123.4567), + ('%#.2g', 1.234567e200), + ('%e', 1.234567e200), + ('%e', 1.234), + ('%+e', 1.234), + ('%-e', 1.234), + ] + + with check_warnings(): + for format, val in tests: + PyOS_ascii_formatd(byref(buf), sizeof(buf), + bytes(format, 'ascii'), + c_double(val)) + self.assertEqual(buf.value, bytes(format % val, 'ascii')) + + +def test_main(): + run_unittest(FormatDeprecationTests, FormatTests) + +if __name__ == '__main__': + test_main() |