Update decimal test data to the most recent set from Mike Cowlishaw.
[python.git] / Lib / test / test_ascii_formatd.py
blob350195535319f2254c6d5668ea0a65567b88429d
1 # PyOS_ascii_formatd is deprecated and not called from anywhere in
2 # Python itself. So this module is the only place it gets tested.
3 # Test that it works, and test that it's deprecated.
5 import unittest
6 from test_support import check_warnings, run_unittest, cpython_only
9 class FormatDeprecationTests(unittest.TestCase):
11 @cpython_only
12 def testFormatDeprecation(self):
13 # delay importing ctypes until we know we're in CPython
14 from ctypes import (pythonapi, create_string_buffer, sizeof, byref,
15 c_double)
16 PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
17 buf = create_string_buffer(' ' * 100)
19 with check_warnings() as w:
20 PyOS_ascii_formatd(byref(buf), sizeof(buf), '%+.10f',
21 c_double(10.0))
22 self.assertEqual(buf.value, '+10.0000000000')
24 self.assertEqual(str(w.message), 'PyOS_ascii_formatd is deprecated, '
25 'use PyOS_double_to_string instead')
27 class FormatTests(unittest.TestCase):
28 # ensure that, for the restricted set of format codes,
29 # %-formatting returns the same values os PyOS_ascii_formatd
30 @cpython_only
31 def testFormat(self):
32 # delay importing ctypes until we know we're in CPython
33 from ctypes import (pythonapi, create_string_buffer, sizeof, byref,
34 c_double)
35 PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
36 buf = create_string_buffer(' ' * 100)
38 tests = [
39 ('%f', 100.0),
40 ('%g', 100.0),
41 ('%#g', 100.0),
42 ('%#.2g', 100.0),
43 ('%#.2g', 123.4567),
44 ('%#.2g', 1.234567e200),
45 ('%e', 1.234567e200),
46 ('%e', 1.234),
47 ('%+e', 1.234),
48 ('%-e', 1.234),
51 with check_warnings():
52 for format, val in tests:
53 PyOS_ascii_formatd(byref(buf), sizeof(buf), format,
54 c_double(val))
55 self.assertEqual(buf.value, format % val)
58 def test_main():
59 run_unittest(FormatDeprecationTests, FormatTests)
61 if __name__ == '__main__':
62 test_main()