mirror of
https://github.com/python/cpython.git
synced 2025-09-26 10:19:53 +00:00
Moved Unicode C API related tests to separate test class.
This commit is contained in:
commit
9bc0507998
1 changed files with 117 additions and 114 deletions
|
@ -2299,6 +2299,123 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
self.assertEqual("%s" % s, '__str__ overridden')
|
self.assertEqual("%s" % s, '__str__ overridden')
|
||||||
self.assertEqual("{}".format(s), '__str__ overridden')
|
self.assertEqual("{}".format(s), '__str__ overridden')
|
||||||
|
|
||||||
|
def test_subclass_add(self):
|
||||||
|
class S(str):
|
||||||
|
def __add__(self, o):
|
||||||
|
return "3"
|
||||||
|
self.assertEqual(S("4") + S("5"), "3")
|
||||||
|
class S(str):
|
||||||
|
def __iadd__(self, o):
|
||||||
|
return "3"
|
||||||
|
s = S("1")
|
||||||
|
s += "4"
|
||||||
|
self.assertEqual(s, "3")
|
||||||
|
|
||||||
|
def test_getnewargs(self):
|
||||||
|
text = 'abc'
|
||||||
|
args = text.__getnewargs__()
|
||||||
|
self.assertIsNot(args[0], text)
|
||||||
|
self.assertEqual(args[0], text)
|
||||||
|
self.assertEqual(len(args), 1)
|
||||||
|
|
||||||
|
def test_resize(self):
|
||||||
|
for length in range(1, 100, 7):
|
||||||
|
# generate a fresh string (refcount=1)
|
||||||
|
text = 'a' * length + 'b'
|
||||||
|
|
||||||
|
with support.check_warnings(('unicode_internal codec has been '
|
||||||
|
'deprecated', DeprecationWarning)):
|
||||||
|
# fill wstr internal field
|
||||||
|
abc = text.encode('unicode_internal')
|
||||||
|
self.assertEqual(abc.decode('unicode_internal'), text)
|
||||||
|
|
||||||
|
# resize text: wstr field must be cleared and then recomputed
|
||||||
|
text += 'c'
|
||||||
|
abcdef = text.encode('unicode_internal')
|
||||||
|
self.assertNotEqual(abc, abcdef)
|
||||||
|
self.assertEqual(abcdef.decode('unicode_internal'), text)
|
||||||
|
|
||||||
|
def test_compare(self):
|
||||||
|
# Issue #17615
|
||||||
|
N = 10
|
||||||
|
ascii = 'a' * N
|
||||||
|
ascii2 = 'z' * N
|
||||||
|
latin = '\x80' * N
|
||||||
|
latin2 = '\xff' * N
|
||||||
|
bmp = '\u0100' * N
|
||||||
|
bmp2 = '\uffff' * N
|
||||||
|
astral = '\U00100000' * N
|
||||||
|
astral2 = '\U0010ffff' * N
|
||||||
|
strings = (
|
||||||
|
ascii, ascii2,
|
||||||
|
latin, latin2,
|
||||||
|
bmp, bmp2,
|
||||||
|
astral, astral2)
|
||||||
|
for text1, text2 in itertools.combinations(strings, 2):
|
||||||
|
equal = (text1 is text2)
|
||||||
|
self.assertEqual(text1 == text2, equal)
|
||||||
|
self.assertEqual(text1 != text2, not equal)
|
||||||
|
|
||||||
|
if equal:
|
||||||
|
self.assertTrue(text1 <= text2)
|
||||||
|
self.assertTrue(text1 >= text2)
|
||||||
|
|
||||||
|
# text1 is text2: duplicate strings to skip the "str1 == str2"
|
||||||
|
# optimization in unicode_compare_eq() and really compare
|
||||||
|
# character per character
|
||||||
|
copy1 = duplicate_string(text1)
|
||||||
|
copy2 = duplicate_string(text2)
|
||||||
|
self.assertIsNot(copy1, copy2)
|
||||||
|
|
||||||
|
self.assertTrue(copy1 == copy2)
|
||||||
|
self.assertFalse(copy1 != copy2)
|
||||||
|
|
||||||
|
self.assertTrue(copy1 <= copy2)
|
||||||
|
self.assertTrue(copy2 >= copy2)
|
||||||
|
|
||||||
|
self.assertTrue(ascii < ascii2)
|
||||||
|
self.assertTrue(ascii < latin)
|
||||||
|
self.assertTrue(ascii < bmp)
|
||||||
|
self.assertTrue(ascii < astral)
|
||||||
|
self.assertFalse(ascii >= ascii2)
|
||||||
|
self.assertFalse(ascii >= latin)
|
||||||
|
self.assertFalse(ascii >= bmp)
|
||||||
|
self.assertFalse(ascii >= astral)
|
||||||
|
|
||||||
|
self.assertFalse(latin < ascii)
|
||||||
|
self.assertTrue(latin < latin2)
|
||||||
|
self.assertTrue(latin < bmp)
|
||||||
|
self.assertTrue(latin < astral)
|
||||||
|
self.assertTrue(latin >= ascii)
|
||||||
|
self.assertFalse(latin >= latin2)
|
||||||
|
self.assertFalse(latin >= bmp)
|
||||||
|
self.assertFalse(latin >= astral)
|
||||||
|
|
||||||
|
self.assertFalse(bmp < ascii)
|
||||||
|
self.assertFalse(bmp < latin)
|
||||||
|
self.assertTrue(bmp < bmp2)
|
||||||
|
self.assertTrue(bmp < astral)
|
||||||
|
self.assertTrue(bmp >= ascii)
|
||||||
|
self.assertTrue(bmp >= latin)
|
||||||
|
self.assertFalse(bmp >= bmp2)
|
||||||
|
self.assertFalse(bmp >= astral)
|
||||||
|
|
||||||
|
self.assertFalse(astral < ascii)
|
||||||
|
self.assertFalse(astral < latin)
|
||||||
|
self.assertFalse(astral < bmp2)
|
||||||
|
self.assertTrue(astral < astral2)
|
||||||
|
self.assertTrue(astral >= ascii)
|
||||||
|
self.assertTrue(astral >= latin)
|
||||||
|
self.assertTrue(astral >= bmp2)
|
||||||
|
self.assertFalse(astral >= astral2)
|
||||||
|
|
||||||
|
def test_free_after_iterating(self):
|
||||||
|
support.check_free_after_iterating(self, iter, str)
|
||||||
|
support.check_free_after_iterating(self, reversed, str)
|
||||||
|
|
||||||
|
|
||||||
|
class CAPITest(unittest.TestCase):
|
||||||
|
|
||||||
# Test PyUnicode_FromFormat()
|
# Test PyUnicode_FromFormat()
|
||||||
def test_from_format(self):
|
def test_from_format(self):
|
||||||
support.import_module('ctypes')
|
support.import_module('ctypes')
|
||||||
|
@ -2594,18 +2711,6 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
self.assertEqual(size, nchar)
|
self.assertEqual(size, nchar)
|
||||||
self.assertEqual(wchar, nonbmp + '\0')
|
self.assertEqual(wchar, nonbmp + '\0')
|
||||||
|
|
||||||
def test_subclass_add(self):
|
|
||||||
class S(str):
|
|
||||||
def __add__(self, o):
|
|
||||||
return "3"
|
|
||||||
self.assertEqual(S("4") + S("5"), "3")
|
|
||||||
class S(str):
|
|
||||||
def __iadd__(self, o):
|
|
||||||
return "3"
|
|
||||||
s = S("1")
|
|
||||||
s += "4"
|
|
||||||
self.assertEqual(s, "3")
|
|
||||||
|
|
||||||
@support.cpython_only
|
@support.cpython_only
|
||||||
def test_encode_decimal(self):
|
def test_encode_decimal(self):
|
||||||
from _testcapi import unicode_encodedecimal
|
from _testcapi import unicode_encodedecimal
|
||||||
|
@ -2634,104 +2739,6 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
self.assertEqual(transform_decimal('123\u20ac'),
|
self.assertEqual(transform_decimal('123\u20ac'),
|
||||||
'123\u20ac')
|
'123\u20ac')
|
||||||
|
|
||||||
def test_getnewargs(self):
|
|
||||||
text = 'abc'
|
|
||||||
args = text.__getnewargs__()
|
|
||||||
self.assertIsNot(args[0], text)
|
|
||||||
self.assertEqual(args[0], text)
|
|
||||||
self.assertEqual(len(args), 1)
|
|
||||||
|
|
||||||
def test_resize(self):
|
|
||||||
for length in range(1, 100, 7):
|
|
||||||
# generate a fresh string (refcount=1)
|
|
||||||
text = 'a' * length + 'b'
|
|
||||||
|
|
||||||
with support.check_warnings(('unicode_internal codec has been '
|
|
||||||
'deprecated', DeprecationWarning)):
|
|
||||||
# fill wstr internal field
|
|
||||||
abc = text.encode('unicode_internal')
|
|
||||||
self.assertEqual(abc.decode('unicode_internal'), text)
|
|
||||||
|
|
||||||
# resize text: wstr field must be cleared and then recomputed
|
|
||||||
text += 'c'
|
|
||||||
abcdef = text.encode('unicode_internal')
|
|
||||||
self.assertNotEqual(abc, abcdef)
|
|
||||||
self.assertEqual(abcdef.decode('unicode_internal'), text)
|
|
||||||
|
|
||||||
def test_compare(self):
|
|
||||||
# Issue #17615
|
|
||||||
N = 10
|
|
||||||
ascii = 'a' * N
|
|
||||||
ascii2 = 'z' * N
|
|
||||||
latin = '\x80' * N
|
|
||||||
latin2 = '\xff' * N
|
|
||||||
bmp = '\u0100' * N
|
|
||||||
bmp2 = '\uffff' * N
|
|
||||||
astral = '\U00100000' * N
|
|
||||||
astral2 = '\U0010ffff' * N
|
|
||||||
strings = (
|
|
||||||
ascii, ascii2,
|
|
||||||
latin, latin2,
|
|
||||||
bmp, bmp2,
|
|
||||||
astral, astral2)
|
|
||||||
for text1, text2 in itertools.combinations(strings, 2):
|
|
||||||
equal = (text1 is text2)
|
|
||||||
self.assertEqual(text1 == text2, equal)
|
|
||||||
self.assertEqual(text1 != text2, not equal)
|
|
||||||
|
|
||||||
if equal:
|
|
||||||
self.assertTrue(text1 <= text2)
|
|
||||||
self.assertTrue(text1 >= text2)
|
|
||||||
|
|
||||||
# text1 is text2: duplicate strings to skip the "str1 == str2"
|
|
||||||
# optimization in unicode_compare_eq() and really compare
|
|
||||||
# character per character
|
|
||||||
copy1 = duplicate_string(text1)
|
|
||||||
copy2 = duplicate_string(text2)
|
|
||||||
self.assertIsNot(copy1, copy2)
|
|
||||||
|
|
||||||
self.assertTrue(copy1 == copy2)
|
|
||||||
self.assertFalse(copy1 != copy2)
|
|
||||||
|
|
||||||
self.assertTrue(copy1 <= copy2)
|
|
||||||
self.assertTrue(copy2 >= copy2)
|
|
||||||
|
|
||||||
self.assertTrue(ascii < ascii2)
|
|
||||||
self.assertTrue(ascii < latin)
|
|
||||||
self.assertTrue(ascii < bmp)
|
|
||||||
self.assertTrue(ascii < astral)
|
|
||||||
self.assertFalse(ascii >= ascii2)
|
|
||||||
self.assertFalse(ascii >= latin)
|
|
||||||
self.assertFalse(ascii >= bmp)
|
|
||||||
self.assertFalse(ascii >= astral)
|
|
||||||
|
|
||||||
self.assertFalse(latin < ascii)
|
|
||||||
self.assertTrue(latin < latin2)
|
|
||||||
self.assertTrue(latin < bmp)
|
|
||||||
self.assertTrue(latin < astral)
|
|
||||||
self.assertTrue(latin >= ascii)
|
|
||||||
self.assertFalse(latin >= latin2)
|
|
||||||
self.assertFalse(latin >= bmp)
|
|
||||||
self.assertFalse(latin >= astral)
|
|
||||||
|
|
||||||
self.assertFalse(bmp < ascii)
|
|
||||||
self.assertFalse(bmp < latin)
|
|
||||||
self.assertTrue(bmp < bmp2)
|
|
||||||
self.assertTrue(bmp < astral)
|
|
||||||
self.assertTrue(bmp >= ascii)
|
|
||||||
self.assertTrue(bmp >= latin)
|
|
||||||
self.assertFalse(bmp >= bmp2)
|
|
||||||
self.assertFalse(bmp >= astral)
|
|
||||||
|
|
||||||
self.assertFalse(astral < ascii)
|
|
||||||
self.assertFalse(astral < latin)
|
|
||||||
self.assertFalse(astral < bmp2)
|
|
||||||
self.assertTrue(astral < astral2)
|
|
||||||
self.assertTrue(astral >= ascii)
|
|
||||||
self.assertTrue(astral >= latin)
|
|
||||||
self.assertTrue(astral >= bmp2)
|
|
||||||
self.assertFalse(astral >= astral2)
|
|
||||||
|
|
||||||
@support.cpython_only
|
@support.cpython_only
|
||||||
def test_pep393_utf8_caching_bug(self):
|
def test_pep393_utf8_caching_bug(self):
|
||||||
# Issue #25709: Problem with string concatenation and utf-8 cache
|
# Issue #25709: Problem with string concatenation and utf-8 cache
|
||||||
|
@ -2749,10 +2756,6 @@ class UnicodeTest(string_tests.CommonTest,
|
||||||
# Check that the second call returns the same result
|
# Check that the second call returns the same result
|
||||||
self.assertEqual(getargs_s_hash(s), chr(k).encode() * (i + 1))
|
self.assertEqual(getargs_s_hash(s), chr(k).encode() * (i + 1))
|
||||||
|
|
||||||
def test_free_after_iterating(self):
|
|
||||||
support.check_free_after_iterating(self, iter, str)
|
|
||||||
support.check_free_after_iterating(self, reversed, str)
|
|
||||||
|
|
||||||
def test_invalid_sequences(self):
|
def test_invalid_sequences(self):
|
||||||
for letter in string.ascii_letters + "89": # 0-7 are octal escapes
|
for letter in string.ascii_letters + "89": # 0-7 are octal escapes
|
||||||
if letter in "abfnrtuvxNU":
|
if letter in "abfnrtuvxNU":
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue