annotate Lib/test/test_types.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents 40db45bfc0f2
children 51102abdec50 c5e0d9beebf9
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
46600
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
3 from test.test_support import run_unittest, have_unicode, run_with_locale
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
4 import unittest
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
5 import sys
46600
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
6 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
7
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
8 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
9
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
10 def test_truth_values(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
11 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
12 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
13 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
14 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
15 if '': self.fail('\'\' is true instead of false')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
16 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
17 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
18 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
19 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
20 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
21 def f(): pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
22 class C: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
23 import sys
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
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
116 def test_normal_integers(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
117 # Ensure the first 256 integers are shared
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
118 a = 256
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
119 b = 128*2
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
120 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
121 if 12 + 24 != 36: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
122 if 12 + (-24) != -12: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
123 if (-12) + 24 != 12: self.fail('int op')
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 not 12 < 24: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
126 if not -24 < -12: self.fail('int op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
127 # Test for a particular bug in integer multiply
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
128 xsize, ysize, zsize = 238, 356, 4
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
129 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
130 self.fail('int mul commutativity')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
131 # And another.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
132 m = -sys.maxint - 1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
133 for divisor in 1, 2, 4, 8, 16, 32:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
134 j = m // divisor
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
135 prod = divisor * j
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
136 if prod != m:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
137 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
138 if type(prod) is not int:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
139 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
140 type(prod))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
141 # Check for expected * overflow to long.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
142 for divisor in 1, 2, 4, 8, 16, 32:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
143 j = m // divisor - 1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
144 prod = divisor * j
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
145 if type(prod) is not long:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
146 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
147 (prod, type(prod)))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
148 # Check for expected * overflow to long.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
149 m = sys.maxint
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
150 for divisor in 1, 2, 4, 8, 16, 32:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
151 j = m // divisor + 1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
152 prod = divisor * j
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
153 if type(prod) is not long:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
154 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
155 (prod, type(prod)))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
156
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
157 def test_long_integers(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
158 if 12L + 24L != 36L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
159 if 12L + (-24L) != -12L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
160 if (-12L) + 24L != 12L: self.fail('long op')
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 not 12L < 24L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
163 if not -24L < -12L: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
164 x = sys.maxint
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
165 if int(long(x)) != x: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
166 try: y = int(long(x)+1L)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
167 except OverflowError: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
168 if not isinstance(y, long): self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
169 x = -x
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
170 if int(long(x)) != x: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
171 x = x-1
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
172 if int(long(x)) != x: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
173 try: y = int(long(x)-1L)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
174 except OverflowError: self.fail('long op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
175 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
176
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
177 try: 5 << -5
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
178 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
179 else: self.fail('int negative shift <<')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
180
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
181 try: 5L << -5L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
182 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
183 else: self.fail('long negative shift <<')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
184
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
185 try: 5 >> -5
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
186 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
187 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
188
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
189 try: 5L >> -5L
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
190 except ValueError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
191 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
192
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
193 def test_floats(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
194 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
195 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
196 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
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 not 12.0 < 24.0: self.fail('float op')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
199 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
200
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
201 def test_strings(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
202 if len('') != 0: self.fail('len(\'\')')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
203 if len('a') != 1: self.fail('len(\'a\')')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
204 if len('abcdef') != 6: self.fail('len(\'abcdef\')')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
205 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
206 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
207 if 0*'abcde' != '': self.fail('string repetition 0*')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
208 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
209 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
210 else: self.fail('in/not in string')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
211 x = 'x'*103
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
212 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
213
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
214 #extended slices for strings
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
215 a = '0123456789'
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
216 self.assertEqual(a[::], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
217 self.assertEqual(a[::2], '02468')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
218 self.assertEqual(a[1::2], '13579')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
219 self.assertEqual(a[::-1],'9876543210')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
220 self.assertEqual(a[::-2], '97531')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
221 self.assertEqual(a[3::-2], '31')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
222 self.assertEqual(a[-100:100:], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
223 self.assertEqual(a[100:-100:-1], a[::-1])
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
224 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
225
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
226 if have_unicode:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
227 a = unicode('0123456789', 'ascii')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
228 self.assertEqual(a[::], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
229 self.assertEqual(a[::2], unicode('02468', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
230 self.assertEqual(a[1::2], unicode('13579', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
231 self.assertEqual(a[::-1], unicode('9876543210', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
232 self.assertEqual(a[::-2], unicode('97531', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
233 self.assertEqual(a[3::-2], unicode('31', 'ascii'))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
234 self.assertEqual(a[-100:100:], a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
235 self.assertEqual(a[100:-100:-1], a[::-1])
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
236 self.assertEqual(a[-100L:100L:2L], unicode('02468', 'ascii'))
24263
e6033254df21 Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 23954
diff changeset
237
23778
245224d1b8c9 This is my nearly two year old patch
Michael W. Hudson <mwh@python.net>
parents: 23335
diff changeset
238
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
239 def test_type_function(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
240 self.assertRaises(TypeError, type, 1, 2)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
241 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
242
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
243 def test_buffers(self):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
244 self.assertRaises(ValueError, buffer, 'asdf', -1)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
245 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
246
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
247 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
248
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
249 a = buffer('asdf')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
250 hash(a)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
251 b = a * 5
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
252 if a == b:
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
253 self.fail('buffers should not be equal')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
254 if str(b) != ('asdf' * 5):
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
255 self.fail('repeated buffer has wrong content')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
256 if str(a * 0) != '':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
257 self.fail('repeated buffer zero times has wrong content')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
258 if str(a + buffer('def')) != 'asdfdef':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
259 self.fail('concatenation of buffers yields wrong content')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
260 if str(buffer(a)) != 'asdf':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
261 self.fail('composing buffers failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
262 if str(buffer(a, 2)) != 'df':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
263 self.fail('specifying buffer offset failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
264 if str(buffer(a, 0, 2)) != 'as':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
265 self.fail('specifying buffer size failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
266 if str(buffer(a, 1, 2)) != 'sd':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
267 self.fail('specifying buffer offset and size failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
268 self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
269 if str(buffer(buffer('asdf', 0, 2), 0)) != 'as':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
270 self.fail('composing length-specified buffer failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
271 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
272 self.fail('composing length-specified buffer failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
273 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
274 self.fail('composing length-specified buffer failed')
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
275 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
276 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
277
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
278 try: a[1] = 'g'
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
279 except TypeError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
280 else: self.fail("buffer assignment should raise TypeError")
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
281
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
282 try: a[0:1] = 'g'
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
283 except TypeError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
284 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
285
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
286 # 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
287 # something which int() uses for conversion.
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
288 import array
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
289 try: int(buffer(array.array('c')))
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
290 except TypeError: pass
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
291 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
292
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
293 def test_int__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
294 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
295 # 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
296 assert type(i) == int
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
297 assert type(format_spec) == str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
298 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
299 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
300
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
301 test(123456789, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
302 test(123456789, 'd', '123456789')
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(1, 'c', '\01')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
305
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
306 # sign and aligning are interdependent
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
307 test(1, "-", '1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
308 test(-1, "-", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
309 test(1, "-3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
310 test(-1, "-3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
311 test(1, "+3", ' +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, " ", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
316 test(-1, " ", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
317
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
318 # hex
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
319 test(3, "x", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
320 test(3, "X", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
321 test(1234, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
322 test(-1234, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
323 test(1234, "8x", " 4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
324 test(-1234, "8x", " -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, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
327 test(-3, "x", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
328 test(-3, "X", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
329 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
330 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
331 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
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
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
334 # octal
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
335 test(3, "o", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
336 test(-3, "o", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
337 test(65, "o", "101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
338 test(-65, "o", "-101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
339 test(1234, "o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
340 test(-1234, "o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
341 test(1234, "-o", "2322")
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
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
348 # binary
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
349 test(3, "b", "11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
350 test(-3, "b", "-11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
351 test(1234, "b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
352 test(-1234, "b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
353 test(1234, "-b", "10011010010")
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
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
360 # 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
361 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
362 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
363 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
364 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
365 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
366 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
367 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
368 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
369 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
370
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
371 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
372 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
373 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
374 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
375 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
376 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
377 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
378 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
379 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
380
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
381 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
382 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
383 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
384 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
385 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
386 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
387 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
388 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
389 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
390 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
391 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
392
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
393 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
394 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
395 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
396 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
397 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
398 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
399 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
400 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
401 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
402 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
403 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
404
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
405 # make sure these are errors
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
406
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
407 # precision disallowed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
408 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
409 # sign not allowed with 'c'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
410 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
411 # format spec must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
412 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
413 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
414
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
415 # 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
416 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
417 [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
418 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
419 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
420 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
421 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
422
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
423 # 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
424 # the int to a float
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
425 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
426 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
427 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
428 float(value).__format__(format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
429
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
430 def test_long__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
431 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
432 # 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
433 assert type(i) == long
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
434 assert type(format_spec) == str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
435 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
436 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
437
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
438 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
439 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
440
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
441 test(123456789L, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
442 test(123456789L, 'd', '123456789')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
443
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
444 # sign and aligning are interdependent
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
445 test(1L, "-", '1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
446 test(-1L, "-", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
447 test(1L, "-3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
448 test(-1L, "-3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
449 test(1L, "+3", ' +1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
450 test(-1L, "+3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
451 test(1L, " 3", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
452 test(-1L, " 3", ' -1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
453 test(1L, " ", ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
454 test(-1L, " ", '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
455
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
456 test(1L, 'c', '\01')
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 # hex
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
459 test(3L, "x", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
460 test(3L, "X", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
461 test(1234L, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
462 test(-1234L, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
463 test(1234L, "8x", " 4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
464 test(-1234L, "8x", " -4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
465 test(1234L, "x", "4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
466 test(-1234L, "x", "-4d2")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
467 test(-3L, "x", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
468 test(-3L, "X", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
469 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
470 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
471 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
472 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
473
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
474 # octal
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
475 test(3L, "o", "3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
476 test(-3L, "o", "-3")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
477 test(65L, "o", "101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
478 test(-65L, "o", "-101")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
479 test(1234L, "o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
480 test(-1234L, "o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
481 test(1234L, "-o", "2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
482 test(-1234L, "-o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
483 test(1234L, " o", " 2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
484 test(-1234L, " o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
485 test(1234L, "+o", "+2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
486 test(-1234L, "+o", "-2322")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
487
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
488 # binary
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
489 test(3L, "b", "11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
490 test(-3L, "b", "-11")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
491 test(1234L, "b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
492 test(-1234L, "b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
493 test(1234L, "-b", "10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
494 test(-1234L, "-b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
495 test(1234L, " b", " 10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
496 test(-1234L, " b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
497 test(1234L, "+b", "+10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
498 test(-1234L, "+b", "-10011010010")
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
499
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
500 # make sure these are errors
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
501
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
502 # precision disallowed
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
503 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
504 # sign not allowed with 'c'
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
505 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
506 # format spec must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
507 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
508 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
509 # 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
510 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
511 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
512
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
513 # 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
514 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
515 [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
516 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
517 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
518 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
519 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
520
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
521 # 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
522 # the long to a float
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
523 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
524 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
525 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
526 float(value).__format__(format_spec))
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
527
46600
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
528 @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
529 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
530 # 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
531
37d645b01523 Issue 2526, float.__format__ 'n' specifier does not support thousands grouping.
Eric Smith <eric@trueblade.com>
parents: 45785
diff changeset
532 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
533 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
534 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
535 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
536
46898
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
537 @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
538 def test_int__format__locale(self):
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
539 # 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
540
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
541 x = 123456789012345678901234567890
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
542 for i in range(0, 30):
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
543 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
544
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
545 # move to the next integer to test
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
546 x = x // 10
206700d749f0 Addresses issue 2802: 'n' formatting for integers.
Eric Smith <eric@trueblade.com>
parents: 46600
diff changeset
547
47819
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
548 rfmt = ">20n"
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
549 lfmt = "<20n"
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
550 cfmt = "^20n"
12be44c6a702 Fixed formatting with thousands separator and padding. Resolves issue 3140.
Eric Smith <eric@trueblade.com>
parents: 46898
diff changeset
551 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
552 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
553 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
554 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
555
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
556 def test_float__format__(self):
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
557 # 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
558 # x.__format__(spec)
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
559
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
560 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
561 assert type(f) == float
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
562 assert type(format_spec) == str
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
563 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
564 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
565
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
566 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
567
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
568 # 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
569 test(0.0, '', '0.0')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
570 test(0.01, '', '0.01')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
571 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
572
48021
40db45bfc0f2 Fix issue 3411: default float format spec fails on negative numbers.
Eric Smith <eric@trueblade.com>
parents: 48003
diff changeset
573 # 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
574 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
575 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
576 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
577 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
578
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
579 test( 1.0, ' g', ' 1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
580 test(-1.0, ' g', '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
581 test( 1.0, '+g', '+1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
582 test(-1.0, '+g', '-1')
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
583 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
584 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
585
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
586
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
587 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
588
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
589 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
590
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
591 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
592 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
593 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
594 test(-1.0, '+f', '-1.000000')
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
595 test(1.1234e90, 'f', '1.1234e+90')
48003
5c33009994b4 Backed out r65069, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48001
diff changeset
596 test(1.1234e90, 'F', '1.1234e+90')
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
597 test(1.1234e200, 'f', '1.1234e+200')
48003
5c33009994b4 Backed out r65069, pending fixing it in Windows.
Eric Smith <eric@trueblade.com>
parents: 48001
diff changeset
598 test(1.1234e200, 'F', '1.1234e+200')
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
599
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
600 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
601 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
602 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
603 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
604 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
605 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
606
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
607 # 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
608 # 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
609 # 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
610 # 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
611 test(1e200, '+g', '+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
612 test(1e200, '+', '+1.0e+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
613 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
614 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
615
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
616 # % formatting
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
617 test(-1.0, '%', '-100.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 # format spec must be string
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
620 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
621 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
622
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
623 # 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
624 # in particular int specifiers
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
625 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
626 [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
627 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
628 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
629 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
630 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
631 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
632 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
633 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
634 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
635
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
636 # 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
637 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
638 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
639
45381
b7231bf6bfb7 Backport of PEP 3101, Advanced String Formatting, from py3k.
Eric Smith <eric@trueblade.com>
parents: 40262
diff changeset
640
40262
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
641 def test_main():
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
642 run_unittest(TypesTests)
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
643
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
644 if __name__ == '__main__':
414ec262d608 Convert test_types to unittest.
Georg Brandl <georg@python.org>
parents: 39396
diff changeset
645 test_main()