Test PyUnicode_CopyCharacters()
(self)
| 1669 | @support.cpython_only |
| 1670 | @unittest.skipIf(_testcapi is None, 'need _testcapi module') |
| 1671 | def test_copycharacters(self): |
| 1672 | """Test PyUnicode_CopyCharacters()""" |
| 1673 | from _testcapi import unicode_copycharacters |
| 1674 | |
| 1675 | strings = [ |
| 1676 | # all strings have exactly 5 characters |
| 1677 | 'abcde', '\xa1\xa2\xa3\xa4\xa5', |
| 1678 | '\u4f60\u597d\u4e16\u754c\uff01', |
| 1679 | '\U0001f600\U0001f601\U0001f602\U0001f603\U0001f604' |
| 1680 | ] |
| 1681 | |
| 1682 | for idx, from_ in enumerate(strings): |
| 1683 | # wide -> narrow: exceed maxchar limitation |
| 1684 | for to in strings[:idx]: |
| 1685 | self.assertRaises( |
| 1686 | SystemError, |
| 1687 | unicode_copycharacters, to, 0, from_, 0, 5 |
| 1688 | ) |
| 1689 | # same kind |
| 1690 | for from_start in range(5): |
| 1691 | self.assertEqual( |
| 1692 | unicode_copycharacters(from_, 0, from_, from_start, 5), |
| 1693 | (from_[from_start:from_start+5].ljust(5, '\0'), |
| 1694 | 5-from_start) |
| 1695 | ) |
| 1696 | for to_start in range(5): |
| 1697 | self.assertEqual( |
| 1698 | unicode_copycharacters(from_, to_start, from_, to_start, 5), |
| 1699 | (from_[to_start:to_start+5].rjust(5, '\0'), |
| 1700 | 5-to_start) |
| 1701 | ) |
| 1702 | # narrow -> wide |
| 1703 | # Tests omitted since this creates invalid strings. |
| 1704 | |
| 1705 | s = strings[0] |
| 1706 | self.assertRaises(IndexError, unicode_copycharacters, s, 6, s, 0, 5) |
| 1707 | self.assertRaises(IndexError, unicode_copycharacters, s, PY_SSIZE_T_MAX, s, 0, 5) |
| 1708 | self.assertRaises(IndexError, unicode_copycharacters, s, -1, s, 0, 5) |
| 1709 | self.assertRaises(IndexError, unicode_copycharacters, s, PY_SSIZE_T_MIN, s, 0, 5) |
| 1710 | self.assertRaises(IndexError, unicode_copycharacters, s, 0, s, 6, 5) |
| 1711 | self.assertRaises(IndexError, unicode_copycharacters, s, 0, s, PY_SSIZE_T_MAX, 5) |
| 1712 | self.assertRaises(IndexError, unicode_copycharacters, s, 0, s, -1, 5) |
| 1713 | self.assertRaises(IndexError, unicode_copycharacters, s, 0, s, PY_SSIZE_T_MIN, 5) |
| 1714 | self.assertRaises(SystemError, unicode_copycharacters, s, 1, s, 0, 5) |
| 1715 | self.assertRaises(SystemError, unicode_copycharacters, s, 1, s, 0, PY_SSIZE_T_MAX) |
| 1716 | self.assertRaises(SystemError, unicode_copycharacters, s, 0, s, 0, -1) |
| 1717 | self.assertRaises(SystemError, unicode_copycharacters, s, 0, s, 0, PY_SSIZE_T_MIN) |
| 1718 | self.assertRaises(SystemError, unicode_copycharacters, s, 0, b'', 0, 0) |
| 1719 | self.assertRaises(SystemError, unicode_copycharacters, s, 0, [], 0, 0) |
| 1720 | # CRASHES unicode_copycharacters(s, 0, NULL, 0, 0) |
| 1721 | # TODO: Test PyUnicode_CopyCharacters() with non-unicode and |
| 1722 | # non-modifiable unicode as "to". |
| 1723 | |
| 1724 | @support.cpython_only |
| 1725 | @unittest.skipIf(_testcapi is None, 'need _testcapi module') |
nothing calls this directly
no test coverage detected