Commits

Amaury Forgeot d'Arc committed 18f3a93

Fix tests: in the py3k branch, we parse unicode strings
and 'L' suffix is not allowed.

  • Participants
  • Parent commits 6be53cd
  • Branches py3k

Comments (0)

Files changed (3)

File rpython/rlib/test/test_rarithmetic.py

 class TestStringToInt:
 
     def test_string_to_int(self):
-        cases = [('0', 0),
-                 ('1', 1),
-                 ('9', 9),
-                 ('10', 10),
-                 ('09', 9),
-                 ('0000101', 101),    # not octal unless base 0 or 8
-                 ('5123', 5123),
-                 (' 0', 0),
-                 ('0  ', 0),
-                 (' \t \n   32313  \f  \v   \r  \n\r    ', 32313),
-                 ('+12', 12),
-                 ('-5', -5),
-                 ('- 5', -5),
-                 ('+ 5', 5),
-                 ('  -123456789 ', -123456789),
+        cases = [(u'0', 0),
+                 (u'1', 1),
+                 (u'9', 9),
+                 (u'10', 10),
+                 (u'09', 9),
+                 (u'0000101', 101),    # not octal unless base 0 or 8
+                 (u'5123', 5123),
+                 (u' 0', 0),
+                 (u'0  ', 0),
+                 (u' \t \n   32313  \f  \v   \r  \n\r    ', 32313),
+                 (u'+12', 12),
+                 (u'-5', -5),
+                 (u'- 5', -5),
+                 (u'+ 5', 5),
+                 (u'  -123456789 ', -123456789),
                  ]
         for s, expected in cases:
             assert string_to_int(s) == expected
             #assert string_to_bigint(s).tolong() == expected
 
     def test_string_to_int_base(self):
-        cases = [('111', 2, 7),
-                 ('010', 2, 2),
-                 ('102', 3, 11),
-                 ('103', 4, 19),
-                 ('107', 8, 71),
-                 ('109', 10, 109),
-                 ('10A', 11, 131),
-                 ('10a', 11, 131),
-                 ('10f', 16, 271),
-                 ('10F', 16, 271),
-                 ('0x10f', 16, 271),
-                 ('0x10F', 16, 271),
-                 ('10z', 36, 1331),
-                 ('10Z', 36, 1331),
-                 ('12',   0, 12),
-                 ('015',  0, 13),
-                 ('0x10', 0, 16),
-                 ('0XE',  0, 14),
-                 ('0',    0, 0),
-                 ('0b11', 2, 3),
-                 ('0B10', 2, 2),
-                 ('0o77', 8, 63),
+        cases = [(u'111', 2, 7),
+                 (u'010', 2, 2),
+                 (u'102', 3, 11),
+                 (u'103', 4, 19),
+                 (u'107', 8, 71),
+                 (u'109', 10, 109),
+                 (u'10A', 11, 131),
+                 (u'10a', 11, 131),
+                 (u'10f', 16, 271),
+                 (u'10F', 16, 271),
+                 (u'0x10f', 16, 271),
+                 (u'0x10F', 16, 271),
+                 (u'10z', 36, 1331),
+                 (u'10Z', 36, 1331),
+                 (u'12',   0, 12),
+                 (u'015',  0, 13),
+                 (u'0x10', 0, 16),
+                 (u'0XE',  0, 14),
+                 (u'0',    0, 0),
+                 (u'0b11', 2, 3),
+                 (u'0B10', 2, 2),
+                 (u'0o77', 8, 63),
                  ]
         for s, base, expected in cases:
             assert string_to_int(s, base) == expected
             assert string_to_int('-'+s+'  ', base) == -expected
 
     def test_string_to_int_error(self):
-        cases = ['0x123',    # must use base 0 or 16
-                 ' 0X12 ',
-                 '0b01',
-                 '0o01',
-                 '',
-                 '++12',
-                 '+-12',
-                 '-+12',
-                 '--12',
-                 '12a6',
-                 '12A6',
-                 'f',
-                 'Z',
-                 '.',
-                 '@',
+        cases = [u'0x123',    # must use base 0 or 16
+                 u' 0X12 ',
+                 u'0b01',
+                 u'0o01',
+                 u'',
+                 u'++12',
+                 u'+-12',
+                 u'-+12',
+                 u'--12',
+                 u'12a6',
+                 u'12A6',
+                 u'f',
+                 u'Z',
+                 u'.',
+                 u'@',
                  ]
         for s in cases:
             py.test.raises(ParseStringError, string_to_int, s)
             py.test.raises(ParseStringError, string_to_int, s+'  ')
             py.test.raises(ParseStringError, string_to_int, '+'+s)
             py.test.raises(ParseStringError, string_to_int, '-'+s)
-        py.test.raises(ParseStringError, string_to_int, '0x', 16)
-        py.test.raises(ParseStringError, string_to_int, '-0x', 16)
+        py.test.raises(ParseStringError, string_to_int, u'0x', 16)
+        py.test.raises(ParseStringError, string_to_int, u'-0x', 16)
 
-        exc = py.test.raises(ParseStringError, string_to_int, '')
+        exc = py.test.raises(ParseStringError, string_to_int, u'')
         assert exc.value.msg == "invalid literal for int() with base 10: ''"
-        exc = py.test.raises(ParseStringError, string_to_int, '', 0)
+        exc = py.test.raises(ParseStringError, string_to_int, u'', 0)
         assert exc.value.msg == "invalid literal for int() with base 0: ''"
 
     def test_string_to_int_overflow(self):
         import sys
         py.test.raises(ParseStringOverflowError, string_to_int,
-               str(sys.maxint*17))
+               unicode(sys.maxint*17))
 
     def test_string_to_int_not_overflow(self):
         import sys
         for x in [-sys.maxint-1, sys.maxint]:
-            y = string_to_int(str(x))
+            y = string_to_int(unicode(x))
             assert y == x
 
     def test_string_to_int_base_error(self):
-        cases = [('1', 1),
-                 ('1', 37),
-                 ('a', 0),
-                 ('9', 9),
-                 ('0x123', 7),
-                 ('145cdf', 15),
-                 ('12', 37),
-                 ('12', 98172),
-                 ('12', -1),
-                 ('12', -908),
-                 ('12.3', 10),
-                 ('12.3', 13),
-                 ('12.3', 16),
+        cases = [(u'1', 1),
+                 (u'1', 37),
+                 (u'a', 0),
+                 (u'9', 9),
+                 (u'0x123', 7),
+                 (u'145cdf', 15),
+                 (u'12', 37),
+                 (u'12', 98172),
+                 (u'12', -1),
+                 (u'12', -908),
+                 (u'12.3', 10),
+                 (u'12.3', 13),
+                 (u'12.3', 16),
                  ]
         for s, base in cases:
             py.test.raises(ParseStringError, string_to_int, s, base)

File rpython/rlib/test/test_rbigint.py

 
     def test_fromstr(self):
         from rpython.rlib.rstring import ParseStringError
-        assert rbigint.fromstr('123L').tolong() == 123
-        assert rbigint.fromstr('123L  ').tolong() == 123
-        py.test.raises(ParseStringError, rbigint.fromstr, 'L')
-        py.test.raises(ParseStringError, rbigint.fromstr, 'L  ')
-        assert rbigint.fromstr('123L', 4).tolong() == 27
-        assert rbigint.fromstr('123L', 30).tolong() == 27000 + 1800 + 90 + 21
-        assert rbigint.fromstr('123L', 22).tolong() == 10648 + 968 + 66 + 21
-        assert rbigint.fromstr('123L', 21).tolong() == 441 + 42 + 3
-        assert rbigint.fromstr('1891234174197319').tolong() == 1891234174197319
+        assert rbigint.fromstr(u'123').tolong() == 123
+        assert rbigint.fromstr(u'123  ').tolong() == 123
+        py.test.raises(ParseStringError, rbigint.fromstr, u'123L')
+        py.test.raises(ParseStringError, rbigint.fromstr, u'123L  ')
+        py.test.raises(ParseStringError, rbigint.fromstr, u'L')
+        py.test.raises(ParseStringError, rbigint.fromstr, u'L  ')
+        assert rbigint.fromstr(u'123', 4).tolong() == 27
+        assert rbigint.fromstr(u'123L', 30).tolong() == 27000 + 1800 + 90 + 21
+        assert rbigint.fromstr(u'123L', 22).tolong() == 10648 + 968 + 66 + 21
+        py.test.raises(ParseStringError, rbigint.fromstr, u'123L', 21)
+        assert rbigint.fromstr(u'1891234174197319').tolong() == 1891234174197319
 
     def test_from_numberstring_parser(self):
         from rpython.rlib.rstring import NumberStringParser
-        parser = NumberStringParser("1231231241", "1231231241", 10, "long")
+        parser = NumberStringParser(u"1231231241", u"1231231241", 10, u"long")
         assert rbigint._from_numberstring_parser(parser).tolong() == 1231231241
 
     def test_add(self):

File rpython/rlib/test/test_rfloat.py

 def test_string_to_float():
     from rpython.rlib.rstring import ParseStringError
     import random
-    assert string_to_float('0') == 0.0
-    assert string_to_float('1') == 1.0
-    assert string_to_float('-1.5') == -1.5
-    assert string_to_float('1.5E2') == 150.0
-    assert string_to_float('2.5E-1') == 0.25
-    assert string_to_float('1e1111111111111') == float('1e1111111111111')
-    assert string_to_float('1e-1111111111111') == float('1e-1111111111111')
-    assert string_to_float('-1e1111111111111') == float('-1e1111111111111')
-    assert string_to_float('-1e-1111111111111') == float('-1e-1111111111111')
-    assert string_to_float('1e111111111111111111111') == float('1e111111111111111111111')
-    assert string_to_float('1e-111111111111111111111') == float('1e-111111111111111111111')
-    assert string_to_float('-1e111111111111111111111') == float('-1e111111111111111111111')
-    assert string_to_float('-1e-111111111111111111111') == float('-1e-111111111111111111111')
+    assert string_to_float(u'0') == 0.0
+    assert string_to_float(u'1') == 1.0
+    assert string_to_float(u'-1.5') == -1.5
+    assert string_to_float(u'1.5E2') == 150.0
+    assert string_to_float(u'2.5E-1') == 0.25
+    assert string_to_float(u'1e1111111111111') == float('1e1111111111111')
+    assert string_to_float(u'1e-1111111111111') == float('1e-1111111111111')
+    assert string_to_float(u'-1e1111111111111') == float('-1e1111111111111')
+    assert string_to_float(u'-1e-1111111111111') == float('-1e-1111111111111')
+    assert string_to_float(u'1e111111111111111111111') == float('1e111111111111111111111')
+    assert string_to_float(u'1e-111111111111111111111') == float('1e-111111111111111111111')
+    assert string_to_float(u'-1e111111111111111111111') == float('-1e111111111111111111111')
+    assert string_to_float(u'-1e-111111111111111111111') == float('-1e-111111111111111111111')
 
     valid_parts = [['', '  ', ' \f\n\r\t\v'],
                    ['', '+', '-'],
             for part2 in valid_parts[2]:
                 for part3 in valid_parts[3]:
                     for part4 in valid_parts[4]:
-                        s = part0+part1+part2+part3+part4
+                        s = unicode(part0+part1+part2+part3+part4)
                         assert (abs(string_to_float(s) - float(s)) <=
                                 1E-13 * abs(float(s)))
 
             for i in range(20):
                 parts = [random.choice(lst) for lst in valid_parts]
                 parts[j] = invalid
-                s = ''.join(parts)
+                s = u''.join(parts)
                 print repr(s)
                 if s.strip(): # empty s raises OperationError directly
                     py.test.raises(ParseStringError, string_to_float, s)
-    py.test.raises(ParseStringError, string_to_float, "")
+    py.test.raises(ParseStringError, string_to_float, u"")