annotate Lib/test/test_types.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents b36cb4602e21
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- part 6, built-in types
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48021
diff changeset
3 from test.test_support import run_unittest, have_unicode, run_with_locale, \
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59043
diff changeset
4 check_py3k_warnings
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
5 import unittest
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
6 import sys
46600
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
7 import locale
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
9 class TypesTests(unittest.TestCase):
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
11 def test_truth_values(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
12 if None: self.fail('None is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
13 if 0: self.fail('0 is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
14 if 0L: self.fail('0L is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
15 if 0.0: self.fail('0.0 is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
16 if '': self.fail('\'\' is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
17 if not 1: self.fail('1 is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
18 if not 1L: self.fail('1L is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
19 if not 1.0: self.fail('1.0 is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
20 if not 'x': self.fail('\'x\' is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
21 if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
22 def f(): pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
23 class C: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
24 x = C()
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
25 if not f: self.fail('f is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
26 if not C: self.fail('C is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
27 if not sys: self.fail('sys is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
28 if not x: self.fail('x is false instead of true')
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
29
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
30 def test_boolean_ops(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
31 if 0 or 0: self.fail('0 or 0 is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
32 if 1 and 1: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
33 else: self.fail('1 and 1 is false instead of true')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
34 if not 1: self.fail('not 1 is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
35
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
36 def test_comparisons(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
37 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
38 else: self.fail('int comparisons failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
39 if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
40 else: self.fail('long int comparisons failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
41 if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
42 else: self.fail('float comparisons failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
43 if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
44 else: self.fail('string comparisons failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
45 if None is None: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
46 else: self.fail('identity test failed')
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
47
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
48 def test_float_constructor(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
49 self.assertRaises(ValueError, float, '')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
50 self.assertRaises(ValueError, float, '5\0')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
51
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
52 def test_zero_division(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
53 try: 5.0 / 0.0
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
54 except ZeroDivisionError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
55 else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError")
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
57 try: 5.0 // 0.0
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
58 except ZeroDivisionError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
59 else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError")
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
60
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
61 try: 5.0 % 0.0
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
62 except ZeroDivisionError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
63 else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError")
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
64
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
65 try: 5 / 0L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
66 except ZeroDivisionError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
67 else: self.fail("5 / 0L didn't raise ZeroDivisionError")
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
68
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
69 try: 5 // 0L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
70 except ZeroDivisionError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
71 else: self.fail("5 // 0L didn't raise ZeroDivisionError")
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
72
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
73 try: 5 % 0L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
74 except ZeroDivisionError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
75 else: self.fail("5 % 0L didn't raise ZeroDivisionError")
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
76
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
77 def test_numeric_types(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
78 if 0 != 0L or 0 != 0.0 or 0L != 0.0: self.fail('mixed comparisons')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
79 if 1 != 1L or 1 != 1.0 or 1L != 1.0: self.fail('mixed comparisons')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
80 if -1 != -1L or -1 != -1.0 or -1L != -1.0:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
81 self.fail('int/long/float value not equal')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
82 # calling built-in types without argument must return 0
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
83 if int() != 0: self.fail('int() does not return 0')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
84 if long() != 0L: self.fail('long() does not return 0L')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
85 if float() != 0.0: self.fail('float() does not return 0.0')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
86 if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
87 else: self.fail('int() does not round properly')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
88 if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
89 else: self.fail('long() does not round properly')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
90 if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
91 else: self.fail('float() does not work properly')
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
92
45403
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
93 def test_float_to_string(self):
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
94 def test(f, result):
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
95 self.assertEqual(f.__format__('e'), result)
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
96 self.assertEqual('%e' % f, result)
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
97
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
98 # test all 2 digit exponents, both with __format__ and with
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
99 # '%' formatting
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
100 for i in range(-99, 100):
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
101 test(float('1.5e'+str(i)), '1.500000e{0:+03d}'.format(i))
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
102
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
103 # test some 3 digit exponents
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
104 self.assertEqual(1.5e100.__format__('e'), '1.500000e+100')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
105 self.assertEqual('%e' % 1.5e100, '1.500000e+100')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
106
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
107 self.assertEqual(1.5e101.__format__('e'), '1.500000e+101')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
108 self.assertEqual('%e' % 1.5e101, '1.500000e+101')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
109
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
110 self.assertEqual(1.5e-100.__format__('e'), '1.500000e-100')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
111 self.assertEqual('%e' % 1.5e-100, '1.500000e-100')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
112
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
113 self.assertEqual(1.5e-101.__format__('e'), '1.500000e-101')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
114 self.assertEqual('%e' % 1.5e-101, '1.500000e-101')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
115
53312
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
116 self.assertEqual('%g' % 1.0, '1')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
117 self.assertEqual('%#g' % 1.0, '1.00000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
118
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
119 def test_normal_integers(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
120 # Ensure the first 256 integers are shared
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
121 a = 256
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
122 b = 128*2
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
123 if a is not b: self.fail('256 is not shared')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
124 if 12 + 24 != 36: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
125 if 12 + (-24) != -12: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
126 if (-12) + 24 != 12: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
127 if (-12) + (-24) != -36: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
128 if not 12 < 24: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
129 if not -24 < -12: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
130 # Test for a particular bug in integer multiply
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
131 xsize, ysize, zsize = 238, 356, 4
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
132 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
133 self.fail('int mul commutativity')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
134 # And another.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
135 m = -sys.maxint - 1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
136 for divisor in 1, 2, 4, 8, 16, 32:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
137 j = m // divisor
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
138 prod = divisor * j
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
139 if prod != m:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
140 self.fail("%r * %r == %r != %r" % (divisor, j, prod, m))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
141 if type(prod) is not int:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
142 self.fail("expected type(prod) to be int, not %r" %
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
143 type(prod))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
144 # Check for expected * overflow to long.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
145 for divisor in 1, 2, 4, 8, 16, 32:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
146 j = m // divisor - 1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
147 prod = divisor * j
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
148 if type(prod) is not long:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
149 self.fail("expected type(%r) to be long, not %r" %
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
150 (prod, type(prod)))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
151 # Check for expected * overflow to long.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
152 m = sys.maxint
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
153 for divisor in 1, 2, 4, 8, 16, 32:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
154 j = m // divisor + 1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
155 prod = divisor * j
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
156 if type(prod) is not long:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
157 self.fail("expected type(%r) to be long, not %r" %
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
158 (prod, type(prod)))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
159
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
160 def test_long_integers(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
161 if 12L + 24L != 36L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
162 if 12L + (-24L) != -12L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
163 if (-12L) + 24L != 12L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
164 if (-12L) + (-24L) != -36L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
165 if not 12L < 24L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
166 if not -24L < -12L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
167 x = sys.maxint
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
168 if int(long(x)) != x: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
169 try: y = int(long(x)+1L)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
170 except OverflowError: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
171 if not isinstance(y, long): self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
172 x = -x
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
173 if int(long(x)) != x: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
174 x = x-1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
175 if int(long(x)) != x: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
176 try: y = int(long(x)-1L)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
177 except OverflowError: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
178 if not isinstance(y, long): self.fail('long op')
21206
81928c9d7011 SF bug #488480: integer multiply to return -max_int-1.
Tim Peters <tim.peters@gmail.com>
parents: 18284
diff changeset
179
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
180 try: 5 << -5
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
181 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
182 else: self.fail('int negative shift <<')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
183
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
184 try: 5L << -5L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
185 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
186 else: self.fail('long negative shift <<')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
187
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
188 try: 5 >> -5
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
189 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
190 else: self.fail('int negative shift >>')
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
191
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
192 try: 5L >> -5L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
193 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
194 else: self.fail('long negative shift >>')
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
195
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
196 def test_floats(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
197 if 12.0 + 24.0 != 36.0: self.fail('float op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
198 if 12.0 + (-24.0) != -12.0: self.fail('float op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
199 if (-12.0) + 24.0 != 12.0: self.fail('float op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
200 if (-12.0) + (-24.0) != -36.0: self.fail('float op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
201 if not 12.0 < 24.0: self.fail('float op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
202 if not -24.0 < -12.0: self.fail('float op')
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
203
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
204 def test_strings(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
205 if len('') != 0: self.fail('len(\'\')')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
206 if len('a') != 1: self.fail('len(\'a\')')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
207 if len('abcdef') != 6: self.fail('len(\'abcdef\')')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
208 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
209 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
210 if 0*'abcde' != '': self.fail('string repetition 0*')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
211 if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
212 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
213 else: self.fail('in/not in string')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
214 x = 'x'*103
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
215 if '%s!'%x != x+'!': self.fail('nasty string formatting bug')
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
217 #extended slices for strings
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
218 a = '0123456789'
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
219 self.assertEqual(a[::], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
220 self.assertEqual(a[::2], '02468')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
221 self.assertEqual(a[1::2], '13579')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
222 self.assertEqual(a[::-1],'9876543210')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
223 self.assertEqual(a[::-2], '97531')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
224 self.assertEqual(a[3::-2], '31')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
225 self.assertEqual(a[-100:100:], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
226 self.assertEqual(a[100:-100:-1], a[::-1])
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
227 self.assertEqual(a[-100L:100L:2L], '02468')
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23335
diff changeset
228
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
229 if have_unicode:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
230 a = unicode('0123456789', 'ascii')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
231 self.assertEqual(a[::], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
232 self.assertEqual(a[::2], unicode('02468', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
233 self.assertEqual(a[1::2], unicode('13579', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
234 self.assertEqual(a[::-1], unicode('9876543210', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
235 self.assertEqual(a[::-2], unicode('97531', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
236 self.assertEqual(a[3::-2], unicode('31', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
237 self.assertEqual(a[-100:100:], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
238 self.assertEqual(a[100:-100:-1], a[::-1])
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
239 self.assertEqual(a[-100L:100L:2L], unicode('02468', 'ascii'))
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23954
diff changeset
240
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23335
diff changeset
241
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
242 def test_type_function(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
243 self.assertRaises(TypeError, type, 1, 2)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
244 self.assertRaises(TypeError, type, 1, 2, 3, 4)
21341
0b1faf116f63 Additional coverage tests by Neil Norwitz.
Guido van Rossum <guido@python.org>
parents: 21207
diff changeset
245
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
246 def test_buffers(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
247 self.assertRaises(ValueError, buffer, 'asdf', -1)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
248 cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
249
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
250 self.assertRaises(TypeError, buffer, None)
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
251
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
252 a = buffer('asdf')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
253 hash(a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
254 b = a * 5
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
255 if a == b:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
256 self.fail('buffers should not be equal')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
257 if str(b) != ('asdf' * 5):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
258 self.fail('repeated buffer has wrong content')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
259 if str(a * 0) != '':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
260 self.fail('repeated buffer zero times has wrong content')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
261 if str(a + buffer('def')) != 'asdfdef':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
262 self.fail('concatenation of buffers yields wrong content')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
263 if str(buffer(a)) != 'asdf':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
264 self.fail('composing buffers failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
265 if str(buffer(a, 2)) != 'df':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
266 self.fail('specifying buffer offset failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
267 if str(buffer(a, 0, 2)) != 'as':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
268 self.fail('specifying buffer size failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
269 if str(buffer(a, 1, 2)) != 'sd':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
270 self.fail('specifying buffer offset and size failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
271 self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
272 if str(buffer(buffer('asdf', 0, 2), 0)) != 'as':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
273 self.fail('composing length-specified buffer failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
274 if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
275 self.fail('composing length-specified buffer failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
276 if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
277 self.fail('composing length-specified buffer failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
278 if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
279 self.fail('composing length-specified buffer failed')
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
280
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
281 try: a[1] = 'g'
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
282 except TypeError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
283 else: self.fail("buffer assignment should raise TypeError")
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
284
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
285 try: a[0:1] = 'g'
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
286 except TypeError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
287 else: self.fail("buffer slice assignment should raise TypeError")
22489
6039254aaadc Add more tests for built-in types. Add tests for buffer objects.
Neil Schemenauer <nascheme@enme.ucalgary.ca>
parents: 21497
diff changeset
288
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
289 # array.array() returns an object that does not implement a char buffer,
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
290 # something which int() uses for conversion.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
291 import array
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
292 try: int(buffer(array.array('c')))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
293 except TypeError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
294 else: self.fail("char buffer (at C level) not working")
38676
5bc7272b81e5 Buffer objects would return the read or write buffer for a wrapped object when
Brett Cannon <bcannon@gmail.com>
parents: 36645
diff changeset
295
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
296 def test_int__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
297 def test(i, format_spec, result):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
298 # just make sure I'm not accidentally checking longs
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
299 assert type(i) == int
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
300 assert type(format_spec) == str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
301 self.assertEqual(i.__format__(format_spec), result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
302 self.assertEqual(i.__format__(unicode(format_spec)), result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
303
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
304 test(123456789, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
305 test(123456789, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
306
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
307 test(1, 'c', '\01')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
308
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
309 # sign and aligning are interdependent
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
310 test(1, "-", '1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
311 test(-1, "-", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
312 test(1, "-3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
313 test(-1, "-3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
314 test(1, "+3", ' +1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
315 test(-1, "+3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
316 test(1, " 3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
317 test(-1, " 3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
318 test(1, " ", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
319 test(-1, " ", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
320
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
321 # hex
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
322 test(3, "x", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
323 test(3, "X", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
324 test(1234, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
325 test(-1234, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
326 test(1234, "8x", " 4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
327 test(-1234, "8x", " -4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
328 test(1234, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
329 test(-1234, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
330 test(-3, "x", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
331 test(-3, "X", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
332 test(int('be', 16), "x", "be")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
333 test(int('be', 16), "X", "BE")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
334 test(-int('be', 16), "x", "-be")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
335 test(-int('be', 16), "X", "-BE")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
336
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
337 # octal
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
338 test(3, "o", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
339 test(-3, "o", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
340 test(65, "o", "101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
341 test(-65, "o", "-101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
342 test(1234, "o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
343 test(-1234, "o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
344 test(1234, "-o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
345 test(-1234, "-o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
346 test(1234, " o", " 2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
347 test(-1234, " o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
348 test(1234, "+o", "+2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
349 test(-1234, "+o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
350
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
351 # binary
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
352 test(3, "b", "11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
353 test(-3, "b", "-11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
354 test(1234, "b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
355 test(-1234, "b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
356 test(1234, "-b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
357 test(-1234, "-b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
358 test(1234, " b", " 10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
359 test(-1234, " b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
360 test(1234, "+b", "+10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
361 test(-1234, "+b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
362
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
363 # alternate (#) formatting
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
364 test(0, "#b", '0b0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
365 test(0, "-#b", '0b0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
366 test(1, "-#b", '0b1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
367 test(-1, "-#b", '-0b1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
368 test(-1, "-#5b", ' -0b1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
369 test(1, "+#5b", ' +0b1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
370 test(100, "+#b", '+0b1100100')
47973
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
371 test(100, "#012b", '0b0001100100')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
372 test(-100, "#012b", '-0b001100100')
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
373
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
374 test(0, "#o", '0o0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
375 test(0, "-#o", '0o0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
376 test(1, "-#o", '0o1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
377 test(-1, "-#o", '-0o1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
378 test(-1, "-#5o", ' -0o1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
379 test(1, "+#5o", ' +0o1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
380 test(100, "+#o", '+0o144')
47973
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
381 test(100, "#012o", '0o0000000144')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
382 test(-100, "#012o", '-0o000000144')
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
383
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
384 test(0, "#x", '0x0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
385 test(0, "-#x", '0x0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
386 test(1, "-#x", '0x1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
387 test(-1, "-#x", '-0x1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
388 test(-1, "-#5x", ' -0x1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
389 test(1, "+#5x", ' +0x1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
390 test(100, "+#x", '+0x64')
47973
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
391 test(100, "#012x", '0x0000000064')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
392 test(-100, "#012x", '-0x000000064')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
393 test(123456, "#012x", '0x000001e240')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
394 test(-123456, "#012x", '-0x00001e240')
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
395
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
396 test(0, "#X", '0X0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
397 test(0, "-#X", '0X0')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
398 test(1, "-#X", '0X1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
399 test(-1, "-#X", '-0X1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
400 test(-1, "-#5X", ' -0X1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
401 test(1, "+#5X", ' +0X1')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
402 test(100, "+#X", '+0X64')
47973
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
403 test(100, "#012X", '0X0000000064')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
404 test(-100, "#012X", '-0X000000064')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
405 test(123456, "#012X", '0X000001E240')
3fa44e5e4e2b Complete issue 3083: add alternate (#) formatting to bin, oct, hex in str.format().
Eric Smith <eric@trueblade.com>
parents: 47959
diff changeset
406 test(-123456, "#012X", '-0X00001E240')
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
407
53318
44a8a525eceb Fixed issue 5782: formatting with commas didn't work if no specifier type code was given.
Eric Smith <eric@trueblade.com>
parents: 53312
diff changeset
408 # issue 5782, commas with no specifier type
44a8a525eceb Fixed issue 5782: formatting with commas didn't work if no specifier type code was given.
Eric Smith <eric@trueblade.com>
parents: 53312
diff changeset
409 test(1234, '010,', '00,001,234')
44a8a525eceb Fixed issue 5782: formatting with commas didn't work if no specifier type code was given.
Eric Smith <eric@trueblade.com>
parents: 53312
diff changeset
410
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
411 # make sure these are errors
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
412
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
413 # precision disallowed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
414 self.assertRaises(ValueError, 3 .__format__, "1.3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
415 # sign not allowed with 'c'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
416 self.assertRaises(ValueError, 3 .__format__, "+c")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
417 # format spec must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
418 self.assertRaises(TypeError, 3 .__format__, None)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
419 self.assertRaises(TypeError, 3 .__format__, 0)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
420
53312
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
421 # can't have ',' with 'c'
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
422 self.assertRaises(ValueError, 3 .__format__, ",c")
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
423
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
424 # ensure that only int and float type specifiers work
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
425 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
426 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
427 if not format_spec in 'bcdoxXeEfFgGn%':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
428 self.assertRaises(ValueError, 0 .__format__, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
429 self.assertRaises(ValueError, 1 .__format__, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
430 self.assertRaises(ValueError, (-1) .__format__, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
431
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
432 # ensure that float type specifiers work; format converts
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
433 # the int to a float
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
434 for format_spec in 'eEfFgG%':
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
435 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
436 self.assertEqual(value.__format__(format_spec),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
437 float(value).__format__(format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
438
59043
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
439 # Issue 6902
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
440 test(123456, "0<20", '12345600000000000000')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
441 test(123456, "1<20", '12345611111111111111')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
442 test(123456, "*<20", '123456**************')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
443 test(123456, "0>20", '00000000000000123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
444 test(123456, "1>20", '11111111111111123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
445 test(123456, "*>20", '**************123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
446 test(123456, "0=20", '00000000000000123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
447 test(123456, "1=20", '11111111111111123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
448 test(123456, "*=20", '**************123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
449
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
450 def test_long__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
451 def test(i, format_spec, result):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
452 # make sure we're not accidentally checking ints
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
453 assert type(i) == long
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
454 assert type(format_spec) == str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
455 self.assertEqual(i.__format__(format_spec), result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
456 self.assertEqual(i.__format__(unicode(format_spec)), result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
457
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
458 test(10**100, 'd', '1' + '0' * 100)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
459 test(10**100+100, 'd', '1' + '0' * 97 + '100')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
460
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
461 test(123456789L, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
462 test(123456789L, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
463
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
464 # sign and aligning are interdependent
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
465 test(1L, "-", '1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
466 test(-1L, "-", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
467 test(1L, "-3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
468 test(-1L, "-3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
469 test(1L, "+3", ' +1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
470 test(-1L, "+3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
471 test(1L, " 3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
472 test(-1L, " 3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
473 test(1L, " ", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
474 test(-1L, " ", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
475
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
476 test(1L, 'c', '\01')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
477
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
478 # hex
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
479 test(3L, "x", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
480 test(3L, "X", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
481 test(1234L, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
482 test(-1234L, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
483 test(1234L, "8x", " 4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
484 test(-1234L, "8x", " -4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
485 test(1234L, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
486 test(-1234L, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
487 test(-3L, "x", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
488 test(-3L, "X", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
489 test(long('be', 16), "x", "be")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
490 test(long('be', 16), "X", "BE")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
491 test(-long('be', 16), "x", "-be")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
492 test(-long('be', 16), "X", "-BE")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
493
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
494 # octal
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
495 test(3L, "o", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
496 test(-3L, "o", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
497 test(65L, "o", "101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
498 test(-65L, "o", "-101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
499 test(1234L, "o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
500 test(-1234L, "o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
501 test(1234L, "-o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
502 test(-1234L, "-o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
503 test(1234L, " o", " 2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
504 test(-1234L, " o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
505 test(1234L, "+o", "+2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
506 test(-1234L, "+o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
507
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
508 # binary
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
509 test(3L, "b", "11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
510 test(-3L, "b", "-11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
511 test(1234L, "b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
512 test(-1234L, "b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
513 test(1234L, "-b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
514 test(-1234L, "-b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
515 test(1234L, " b", " 10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
516 test(-1234L, " b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
517 test(1234L, "+b", "+10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
518 test(-1234L, "+b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
519
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
520 # make sure these are errors
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
521
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
522 # precision disallowed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
523 self.assertRaises(ValueError, 3L .__format__, "1.3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
524 # sign not allowed with 'c'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
525 self.assertRaises(ValueError, 3L .__format__, "+c")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
526 # format spec must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
527 self.assertRaises(TypeError, 3L .__format__, None)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
528 self.assertRaises(TypeError, 3L .__format__, 0)
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
529 # alternate specifier in wrong place
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
530 self.assertRaises(ValueError, 1L .__format__, "#+5x")
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
531 self.assertRaises(ValueError, 1L .__format__, "+5#x")
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
532
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
533 # ensure that only int and float type specifiers work
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
534 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
535 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
536 if not format_spec in 'bcdoxXeEfFgGn%':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
537 self.assertRaises(ValueError, 0L .__format__, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
538 self.assertRaises(ValueError, 1L .__format__, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
539 self.assertRaises(ValueError, (-1L) .__format__, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
540
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
541 # ensure that float type specifiers work; format converts
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
542 # the long to a float
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
543 for format_spec in 'eEfFgG%':
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
544 for value in [0L, 1L, -1L, 100L, -100L, 1234567890L, -1234567890L]:
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
545 self.assertEqual(value.__format__(format_spec),
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
546 float(value).__format__(format_spec))
59043
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
547 # Issue 6902
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
548 test(123456L, "0<20", '12345600000000000000')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
549 test(123456L, "1<20", '12345611111111111111')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
550 test(123456L, "*<20", '123456**************')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
551 test(123456L, "0>20", '00000000000000123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
552 test(123456L, "1>20", '11111111111111123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
553 test(123456L, "*>20", '**************123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
554 test(123456L, "0=20", '00000000000000123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
555 test(123456L, "1=20", '11111111111111123456')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
556 test(123456L, "*=20", '**************123456')
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
557
46600
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
558 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
559 def test_float__format__locale(self):
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
560 # test locale support for __format__ code 'n'
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
561
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
562 for i in range(-10, 10):
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
563 x = 1234567890.0 * (10.0 ** i)
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
564 self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n'))
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
565 self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n'))
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
566
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
567 @run_with_locale('LC_NUMERIC', 'en_US.UTF8')
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
568 def test_int__format__locale(self):
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
569 # test locale support for __format__ code 'n' for integers
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
570
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
571 x = 123456789012345678901234567890
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
572 for i in range(0, 30):
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
573 self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n'))
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
574
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
575 # move to the next integer to test
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
576 x = x // 10
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
577
47819
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
578 rfmt = ">20n"
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
579 lfmt = "<20n"
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
580 cfmt = "^20n"
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
581 for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900):
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
582 self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt)))
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
583 self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt)))
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
584 self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt)))
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
585
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
586 def test_float__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
587 # these should be rewritten to use both format(x, spec) and
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
588 # x.__format__(spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
589
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
590 def test(f, format_spec, result):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
591 assert type(f) == float
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
592 assert type(format_spec) == str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
593 self.assertEqual(f.__format__(format_spec), result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
594 self.assertEqual(f.__format__(unicode(format_spec)), result)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
595
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
596 test(0.0, 'f', '0.000000')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
597
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
598 # the default is 'g', except for empty format spec
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
599 test(0.0, '', '0.0')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
600 test(0.01, '', '0.01')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
601 test(0.01, 'g', '0.01')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
602
48021
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
603 # test for issue 3411
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
604 test(1.23, '1', '1.23')
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
605 test(-1.23, '1', '-1.23')
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
606 test(1.23, '1g', '1.23')
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
607 test(-1.23, '1g', '-1.23')
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
608
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
609 test( 1.0, ' g', ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
610 test(-1.0, ' g', '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
611 test( 1.0, '+g', '+1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
612 test(-1.0, '+g', '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
613 test(1.1234e200, 'g', '1.1234e+200')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
614 test(1.1234e200, 'G', '1.1234E+200')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
615
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
616
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
617 test(1.0, 'f', '1.000000')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
618
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
619 test(-1.0, 'f', '-1.000000')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
620
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
621 test( 1.0, ' f', ' 1.000000')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
622 test(-1.0, ' f', '-1.000000')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
623 test( 1.0, '+f', '+1.000000')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
624 test(-1.0, '+f', '-1.000000')
57419
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
625
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
626 # Python versions <= 2.6 switched from 'f' to 'g' formatting for
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
627 # values larger than 1e50. No longer.
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
628 f = 1.1234e90
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
629 for fmt in 'f', 'F':
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
630 # don't do a direct equality check, since on some
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
631 # platforms only the first few digits of dtoa
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
632 # will be reliable
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
633 result = f.__format__(fmt)
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
634 self.assertEqual(len(result), 98)
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
635 self.assertEqual(result[-7], '.')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
636 self.assertIn(result[:12], ('112340000000', '112339999999'))
57419
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
637 f = 1.1234e200
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
638 for fmt in 'f', 'F':
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
639 result = f.__format__(fmt)
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
640 self.assertEqual(len(result), 208)
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
641 self.assertEqual(result[-7], '.')
58460
17891566a478 use assert[Not]In where appropriate
Ezio Melotti <ezio.melotti@gmail.com>
parents: 58198
diff changeset
642 self.assertIn(result[:12], ('112340000000', '112339999999'))
57419
a7eb1ae4e919 Issue #7117, continued: Remove substitution of %g-style formatting for
Mark Dickinson <dickinsm@gmail.com>
parents: 53318
diff changeset
643
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
644
45403
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
645 test( 1.0, 'e', '1.000000e+00')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
646 test(-1.0, 'e', '-1.000000e+00')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
647 test( 1.0, 'E', '1.000000E+00')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
648 test(-1.0, 'E', '-1.000000E+00')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
649 test(1.1234e20, 'e', '1.123400e+20')
0ea236a71d77 Trim leading zeros from a floating point exponent, per C99. See issue 1600. As far as I know, this only affects Windows. Add float type 'n' to PyOS_ascii_formatd (see PEP 3101 for 'n' description).
Eric Smith <eric@trueblade.com>
parents: 45388
diff changeset
650 test(1.1234e20, 'E', '1.123400E+20')
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
651
45785
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
652 # No format code means use g, but must have a decimal
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
653 # and a number after the decimal. This is tricky, because
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
654 # a totaly empty format specifier means something else.
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
655 # So, just use a sign flag
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
656 test(1e200, '+g', '+1e+200')
53312
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
657 test(1e200, '+', '+1e+200')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
658 test(1.1e200, '+g', '+1.1e+200')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
659 test(1.1e200, '+', '+1.1e+200')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
660
45785
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
661 test(1.1e200, '+g', '+1.1e+200')
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
662 test(1.1e200, '+', '+1.1e+200')
4d29127f3fa0 Issue 2264: empty float presentation type needs to have at least one digit past the decimal point.
Eric Smith <eric@trueblade.com>
parents: 45403
diff changeset
663
53312
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
664 # 0 padding
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
665 test(1234., '010f', '1234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
666 test(1234., '011f', '1234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
667 test(1234., '012f', '01234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
668 test(-1234., '011f', '-1234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
669 test(-1234., '012f', '-1234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
670 test(-1234., '013f', '-01234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
671 test(-1234.12341234, '013f', '-01234.123412')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
672 test(-123456.12341234, '011.2f', '-0123456.12')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
673
53318
44a8a525eceb Fixed issue 5782: formatting with commas didn't work if no specifier type code was given.
Eric Smith <eric@trueblade.com>
parents: 53312
diff changeset
674 # issue 5782, commas with no specifier type
44a8a525eceb Fixed issue 5782: formatting with commas didn't work if no specifier type code was given.
Eric Smith <eric@trueblade.com>
parents: 53312
diff changeset
675 test(1.2, '010,.2', '0,000,001.2')
44a8a525eceb Fixed issue 5782: formatting with commas didn't work if no specifier type code was given.
Eric Smith <eric@trueblade.com>
parents: 53312
diff changeset
676
53312
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
677 # 0 padding with commas
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
678 test(1234., '011,f', '1,234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
679 test(1234., '012,f', '1,234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
680 test(1234., '013,f', '01,234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
681 test(-1234., '012,f', '-1,234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
682 test(-1234., '013,f', '-1,234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
683 test(-1234., '014,f', '-01,234.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
684 test(-12345., '015,f', '-012,345.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
685 test(-123456., '016,f', '-0,123,456.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
686 test(-123456., '017,f', '-0,123,456.000000')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
687 test(-123456.12341234, '017,f', '-0,123,456.123412')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
688 test(-123456.12341234, '013,.2f', '-0,123,456.12')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
689
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
690 # % formatting
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
691 test(-1.0, '%', '-100.000000%')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
692
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
693 # format spec must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
694 self.assertRaises(TypeError, 3.0.__format__, None)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
695 self.assertRaises(TypeError, 3.0.__format__, 0)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
696
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
697 # other format specifiers shouldn't work on floats,
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
698 # in particular int specifiers
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
699 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
700 [chr(x) for x in range(ord('A'), ord('Z')+1)]):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
701 if not format_spec in 'eEfFgGn%':
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
702 self.assertRaises(ValueError, format, 0.0, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
703 self.assertRaises(ValueError, format, 1.0, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
704 self.assertRaises(ValueError, format, -1.0, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
705 self.assertRaises(ValueError, format, 1e100, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
706 self.assertRaises(ValueError, format, -1e100, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
707 self.assertRaises(ValueError, format, 1e-100, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
708 self.assertRaises(ValueError, format, -1e-100, format_spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
709
47959
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
710 # Alternate formatting is not supported
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
711 self.assertRaises(ValueError, format, 0.0, '#')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
712 self.assertRaises(ValueError, format, 0.0, '#20f')
c2b114d0e1cf Added '#' formatting to integers. This adds the 0b, 0o, or 0x prefix for bin, oct, hex. There's still one failing case, and I need to finish the docs. I hope to finish those today.
Eric Smith <eric@trueblade.com>
parents: 47819
diff changeset
713
59043
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
714 # Issue 6902
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
715 test(12345.6, "0<20", '12345.60000000000000')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
716 test(12345.6, "1<20", '12345.61111111111111')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
717 test(12345.6, "*<20", '12345.6*************')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
718 test(12345.6, "0>20", '000000000000012345.6')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
719 test(12345.6, "1>20", '111111111111112345.6')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
720 test(12345.6, "*>20", '*************12345.6')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
721 test(12345.6, "0=20", '000000000000012345.6')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
722 test(12345.6, "1=20", '111111111111112345.6')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
723 test(12345.6, "*=20", '*************12345.6')
15286d9f1a8e Issue #6902: Fix problem with built-in types format incorrectly with 0 padding.
Eric Smith <eric@trueblade.com>
parents: 58817
diff changeset
724
53312
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
725 def test_format_spec_errors(self):
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
726 # int, float, and string all share the same format spec
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
727 # mini-language parser.
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
728
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
729 # Check that we can't ask for too many digits. This is
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
730 # probably a CPython specific test. It tries to put the width
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
731 # into a C long.
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
732 self.assertRaises(ValueError, format, 0, '1'*10000 + 'd')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
733
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
734 # Similar with the precision.
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
735 self.assertRaises(ValueError, format, 0, '.' + '1'*10000 + 'd')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
736
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
737 # And may as well test both.
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
738 self.assertRaises(ValueError, format, 0, '1'*1000 + '.' + '1'*10000 + 'd')
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
739
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
740 # Make sure commas aren't allowed with various type codes
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
741 for code in 'xXobns':
c5e0d9beebf9 Backport of some of the work in r71665 to trunk. This reworks much of
Eric Smith <eric@trueblade.com>
parents: 48021
diff changeset
742 self.assertRaises(ValueError, format, 0, ',' + code)
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
743
64284
ebfb94f2cbc3 Merged revisions 84320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
744 def test_internal_sizes(self):
ebfb94f2cbc3 Merged revisions 84320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
745 self.assertGreater(object.__basicsize__, 0)
ebfb94f2cbc3 Merged revisions 84320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
746 self.assertGreater(tuple.__itemsize__, 0)
ebfb94f2cbc3 Merged revisions 84320 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 59783
diff changeset
747
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
748
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
749 def test_main():
59783
c8c840162804 #7092 - Silence more py3k deprecation warnings, using test_support.check_py3k_warnings() helper.
Florent Xicluna <florent.xicluna@gmail.com>
parents: 59043
diff changeset
750 with check_py3k_warnings(
63572
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48021
diff changeset
751 ("buffer.. not supported", DeprecationWarning),
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48021
diff changeset
752 ("classic long division", DeprecationWarning)):
51102abdec50 Merged revisions 79165 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 48021
diff changeset
753 run_unittest(TypesTests)
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
754
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
755 if __name__ == '__main__':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
756 test_main()