Test PyUnicode_Fill()
(self)
| 54 | @support.cpython_only |
| 55 | @unittest.skipIf(_testcapi is None, 'need _testcapi module') |
| 56 | def test_fill(self): |
| 57 | """Test PyUnicode_Fill()""" |
| 58 | from _testcapi import unicode_fill as fill |
| 59 | |
| 60 | strings = [ |
| 61 | # all strings have exactly 5 characters |
| 62 | 'abcde', '\xa1\xa2\xa3\xa4\xa5', |
| 63 | '\u4f60\u597d\u4e16\u754c\uff01', |
| 64 | '\U0001f600\U0001f601\U0001f602\U0001f603\U0001f604' |
| 65 | ] |
| 66 | chars = [0x78, 0xa9, 0x20ac, 0x1f638] |
| 67 | |
| 68 | for idx, fill_char in enumerate(chars): |
| 69 | # wide -> narrow: exceed maxchar limitation |
| 70 | for to in strings[:idx]: |
| 71 | self.assertRaises(ValueError, fill, to, 0, 0, fill_char) |
| 72 | for to in strings[idx:]: |
| 73 | for start in [*range(7), PY_SSIZE_T_MAX]: |
| 74 | for length in [*range(-1, 7 - start), PY_SSIZE_T_MIN, PY_SSIZE_T_MAX]: |
| 75 | filled = max(min(length, 5 - start), 0) |
| 76 | if filled == 5 and to != strings[idx]: |
| 77 | # narrow -> wide |
| 78 | # Tests omitted since this creates invalid strings. |
| 79 | continue |
| 80 | expected = to[:start] + chr(fill_char) * filled + to[start + filled:] |
| 81 | self.assertEqual(fill(to, start, length, fill_char), |
| 82 | (expected, filled)) |
| 83 | |
| 84 | s = strings[0] |
| 85 | self.assertRaises(IndexError, fill, s, -1, 0, 0x78) |
| 86 | self.assertRaises(IndexError, fill, s, PY_SSIZE_T_MIN, 0, 0x78) |
| 87 | self.assertRaises(ValueError, fill, s, 0, 0, 0x110000) |
| 88 | self.assertRaises(SystemError, fill, b'abc', 0, 0, 0x78) |
| 89 | self.assertRaises(SystemError, fill, [], 0, 0, 0x78) |
| 90 | # CRASHES fill(s, 0, NULL, 0, 0) |
| 91 | # CRASHES fill(NULL, 0, 0, 0x78) |
| 92 | # TODO: Test PyUnicode_Fill() with non-modifiable unicode. |
| 93 | |
| 94 | @support.cpython_only |
| 95 | @unittest.skipIf(_testlimitedcapi is None, 'need _testlimitedcapi module') |
nothing calls this directly
no test coverage detected