Skip to content

Commit 10c282d

Browse files
lysnikolaouvstinner
authored andcommitted
Add tests
1 parent 82d0bcb commit 10c282d

File tree

2 files changed

+159
-0
lines changed

2 files changed

+159
-0
lines changed

Lib/test/test_capi/test_unicode.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,6 +1753,55 @@ def test_GET_CACHED_HASH(self):
17531753
# impl detail: ASCII string hashes are equal to bytes ones
17541754
self.assertEqual(unicode_GET_CACHED_HASH(obj), hash(content_bytes))
17551755

1756+
@support.cpython_only
1757+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
1758+
def test_tolower(self):
1759+
import string
1760+
from _testcapi import unicode_tolower
1761+
1762+
for i, c in enumerate(string.ascii_uppercase):
1763+
with self.subTest(c):
1764+
self.assertEqual(unicode_tolower(c), string.ascii_lowercase[i])
1765+
1766+
# Test unicode character
1767+
self.assertEqual(unicode_tolower("Č"), "č")
1768+
1769+
@support.cpython_only
1770+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
1771+
def test_toupper(self):
1772+
import string
1773+
from _testcapi import unicode_toupper
1774+
1775+
for i, c in enumerate(string.ascii_lowercase):
1776+
with self.subTest(c):
1777+
self.assertEqual(unicode_toupper(c), string.ascii_uppercase[i])
1778+
1779+
# Test unicode character
1780+
self.assertEqual(unicode_toupper("č"), "Č")
1781+
1782+
@support.cpython_only
1783+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
1784+
def test_totitle(self):
1785+
from _testcapi import unicode_totitle
1786+
1787+
self.assertEqual(unicode_totitle("t"), "T")
1788+
1789+
# Test unicode character
1790+
self.assertEqual(unicode_totitle("ł"), "Ł")
1791+
1792+
@support.cpython_only
1793+
@unittest.skipIf(_testcapi is None, 'need _testcapi module')
1794+
def test_tofolded(self):
1795+
from _testcapi import unicode_tofolded
1796+
1797+
self.assertEqual(unicode_tofolded("T"), "t")
1798+
1799+
# Test unicode character
1800+
self.assertEqual(unicode_tofolded("Ł"), "ł")
1801+
1802+
# Test case-ignorable character
1803+
self.assertEqual(unicode_tofolded("👍"), "👍")
1804+
17561805

17571806
class PyUnicodeWriterTest(unittest.TestCase):
17581807
def create_writer(self, size):

Modules/_testcapi/unicode.c

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,112 @@ unicode_copycharacters(PyObject *self, PyObject *args)
220220
return Py_BuildValue("(Nn)", to_copy, copied);
221221
}
222222

223+
/* Test PyUnicode_ToLower() */
224+
static PyObject *
225+
unicode_tolower(PyObject *self, PyObject *arg)
226+
{
227+
if (PyUnicode_GET_LENGTH(arg) != 1) {
228+
PyErr_SetString(PyExc_ValueError, "unicode_tolower only accepts 1-character strings");
229+
return NULL;
230+
}
231+
232+
Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);
233+
234+
Py_UCS4 lower[3];
235+
int chars = PyUnicode_ToLower(c, lower, Py_ARRAY_LENGTH(lower));
236+
assert(chars >= 1);
237+
238+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
239+
if (writer == NULL) {
240+
return NULL;
241+
}
242+
if (PyUnicodeWriter_WriteUCS4(writer, lower, chars) < 0) {
243+
PyUnicodeWriter_Discard(writer);
244+
return NULL;
245+
}
246+
return PyUnicodeWriter_Finish(writer);
247+
}
248+
249+
/* Test PyUnicode_ToUpper() */
250+
static PyObject *
251+
unicode_toupper(PyObject *self, PyObject *arg)
252+
{
253+
if (PyUnicode_GET_LENGTH(arg) != 1) {
254+
PyErr_SetString(PyExc_ValueError, "unicode_toupper only accepts 1-character strings");
255+
return NULL;
256+
}
257+
258+
Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);
259+
260+
Py_UCS4 upper[3];
261+
int chars = PyUnicode_ToUpper(c, upper, Py_ARRAY_LENGTH(upper));
262+
assert(chars >= 1);
263+
264+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
265+
if (writer == NULL) {
266+
return NULL;
267+
}
268+
if (PyUnicodeWriter_WriteUCS4(writer, upper, chars) < 0) {
269+
PyUnicodeWriter_Discard(writer);
270+
return NULL;
271+
}
272+
return PyUnicodeWriter_Finish(writer);
273+
}
274+
275+
276+
/* Test PyUnicode_ToLower() */
277+
static PyObject *
278+
unicode_totitle(PyObject *self, PyObject *arg)
279+
{
280+
if (PyUnicode_GET_LENGTH(arg) != 1) {
281+
PyErr_SetString(PyExc_ValueError, "unicode_totitle only accepts 1-character strings");
282+
return NULL;
283+
}
284+
285+
Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);
286+
287+
Py_UCS4 title[3];
288+
int chars = PyUnicode_ToTitle(c, title, Py_ARRAY_LENGTH(title));
289+
assert(chars >= 1);
290+
291+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
292+
if (writer == NULL) {
293+
return NULL;
294+
}
295+
if (PyUnicodeWriter_WriteUCS4(writer, title, chars) < 0) {
296+
PyUnicodeWriter_Discard(writer);
297+
return NULL;
298+
}
299+
return PyUnicodeWriter_Finish(writer);
300+
}
301+
302+
/* Test PyUnicode_ToLower() */
303+
static PyObject *
304+
unicode_tofolded(PyObject *self, PyObject *arg)
305+
{
306+
if (PyUnicode_GET_LENGTH(arg) != 1) {
307+
PyErr_SetString(PyExc_ValueError, "unicode_tofolded only accepts 1-character strings");
308+
return NULL;
309+
}
310+
311+
Py_UCS4 c = PyUnicode_READ_CHAR(arg, 0);
312+
313+
Py_UCS4 folded[3];
314+
int chars = PyUnicode_ToFolded(c, folded, Py_ARRAY_LENGTH(folded));
315+
assert(chars >= 1);
316+
317+
PyUnicodeWriter *writer = PyUnicodeWriter_Create(1);
318+
if (writer == NULL) {
319+
return NULL;
320+
}
321+
if (PyUnicodeWriter_WriteUCS4(writer, folded, chars) < 0) {
322+
PyUnicodeWriter_Discard(writer);
323+
return NULL;
324+
}
325+
return PyUnicodeWriter_Finish(writer);
326+
}
327+
328+
223329
static PyObject*
224330
unicode_GET_CACHED_HASH(PyObject *self, PyObject *arg)
225331
{
@@ -577,6 +683,10 @@ static PyMethodDef TestMethods[] = {
577683
{"unicode_asutf8", unicode_asutf8, METH_VARARGS},
578684
{"unicode_copycharacters", unicode_copycharacters, METH_VARARGS},
579685
{"unicode_GET_CACHED_HASH", unicode_GET_CACHED_HASH, METH_O},
686+
{"unicode_tolower", unicode_tolower, METH_O},
687+
{"unicode_toupper", unicode_toupper, METH_O},
688+
{"unicode_totitle", unicode_totitle, METH_O},
689+
{"unicode_tofolded", unicode_tofolded, METH_O},
580690
{NULL},
581691
};
582692

0 commit comments

Comments
 (0)