blob: f61bc858cce536910d151816c71f39edd440fac0 [file] [log] [blame]
Eric Smithdf9d4d62009-04-29 12:34:19 +00001# 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.
4
5import unittest
6from test.support import check_warnings, run_unittest, cpython_only
7
8class FormatDeprecationTests(unittest.TestCase):
9
10 @cpython_only
11 def testFormatDeprecation(self):
12 # delay importing ctypes until we know we're in CPython
13 from ctypes import (pythonapi, create_string_buffer, sizeof, byref,
14 c_double)
15 PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
16 buf = create_string_buffer(100)
17
18 with check_warnings() as w:
19 PyOS_ascii_formatd(byref(buf), sizeof(buf), b'%+.10f',
20 c_double(10.0))
21 self.assertEqual(buf.value, b'+10.0000000000')
22
Eric Smith1bfa7992009-10-31 17:08:48 +000023 self.assertEqual(w.category, DeprecationWarning)
Eric Smithdf9d4d62009-04-29 12:34:19 +000024
25class FormatTests(unittest.TestCase):
26 # ensure that, for the restricted set of format codes,
27 # %-formatting returns the same values os PyOS_ascii_formatd
28 @cpython_only
29 def testFormat(self):
30 # delay importing ctypes until we know we're in CPython
31 from ctypes import (pythonapi, create_string_buffer, sizeof, byref,
32 c_double)
33 PyOS_ascii_formatd = pythonapi.PyOS_ascii_formatd
34 buf = create_string_buffer(100)
35
36 tests = [
37 ('%f', 100.0),
38 ('%g', 100.0),
39 ('%#g', 100.0),
40 ('%#.2g', 100.0),
41 ('%#.2g', 123.4567),
42 ('%#.2g', 1.234567e200),
43 ('%e', 1.234567e200),
44 ('%e', 1.234),
45 ('%+e', 1.234),
46 ('%-e', 1.234),
47 ]
48
49 with check_warnings():
50 for format, val in tests:
51 PyOS_ascii_formatd(byref(buf), sizeof(buf),
52 bytes(format, 'ascii'),
53 c_double(val))
54 self.assertEqual(buf.value, bytes(format % val, 'ascii'))
55
56
57def test_main():
58 run_unittest(FormatDeprecationTests, FormatTests)
59
60if __name__ == '__main__':
61 test_main()