-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-111495: Add tests for PyList C API #111562
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 15 commits
d43d9b2
119cd0e
ccf1bff
8d17ca6
bd9415d
17d60d1
322eab9
5c58022
6b12f1f
70e385f
3166ba4
2a50fb8
1c30f3b
392b3c6
eac8c67
0f40f6d
40e4022
d31fa57
433351d
34e915b
0e340a1
8de9cf8
44dcf85
1000126
2c6a41c
e486451
633951b
9afa50c
361b045
ca07ac9
dc039f1
5ecfa40
42a2a54
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import unittest | ||
import sys | ||
from test.support import import_helper | ||
from collections import UserList | ||
|
||
_testcapi = import_helper.import_module('_testcapi') | ||
|
||
NULL = None | ||
|
||
class ListSubclass(list): | ||
... | ||
|
||
|
||
class CAPITest(unittest.TestCase): | ||
def test_check(self): | ||
# Test PyList_Check() | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
check = _testcapi.list_check | ||
self.assertTrue(check([1, 2])) | ||
self.assertTrue(check([])) | ||
self.assertTrue(check(ListSubclass([1, 2]))) | ||
self.assertFalse(check({1: 2})) | ||
self.assertFalse(check((1, 2))) | ||
self.assertFalse(check(42)) | ||
self.assertFalse(check(object())) | ||
serhiy-storchaka marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# CRASHES check(NULL) | ||
|
||
|
||
def test_list_check_exact(self): | ||
# Test PyList_CheckExact() | ||
check = _testcapi.list_check_exact | ||
self.assertTrue(check([1])) | ||
self.assertTrue(check([])) | ||
self.assertFalse(check(ListSubclass([1]))) | ||
self.assertFalse(check(UserList([1, 2]))) | ||
self.assertFalse(check({1: 2})) | ||
self.assertFalse(check(object())) | ||
|
||
# CRASHES check(NULL) | ||
|
||
def test_list_new(self): | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# Test PyList_New() | ||
list_new = _testcapi.list_new | ||
lst = list_new(0) | ||
self.assertEqual(lst, []) | ||
self.assertIs(type(lst), list) | ||
lst2 = list_new(0) | ||
self.assertIsNot(lst2, lst) | ||
self.assertRaises(SystemError, list_new, NULL) | ||
self.assertRaises(SystemError, list_new, -1) | ||
|
||
def test_list_size(self): | ||
# Test PyList_Size() | ||
size = _testcapi.list_size | ||
self.assertEqual(size([1, 2]), 2) | ||
self.assertEqual(size(ListSubclass([1, 2])), 2) | ||
self.assertRaises(SystemError, size, UserList()) | ||
self.assertRaises(SystemError, size, {}) | ||
self.assertRaises(SystemError, size, 23) | ||
self.assertRaises(SystemError, size, object()) | ||
# CRASHES size(NULL) | ||
|
||
|
||
def test_list_getitem(self): | ||
# Test PyList_GetItem() | ||
getitem = _testcapi.list_getitem | ||
lst = [1, 2, NULL] | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertEqual(getitem(lst, 0), 1) | ||
self.assertRaises(IndexError, getitem, lst, -1) | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertRaises(IndexError, getitem, lst, 10) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add tests for index equal to the size of the list, All functions that have Py_ssize_t parameter should be tested with the following values: 0, size-1, -1, size, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For methods like |
||
self.assertRaises(SystemError, getitem, 42, 1) | ||
|
||
# CRASHES getitem(NULL, 1) | ||
|
||
def test_list_get_item(self): | ||
# Test PyList_GET_ITEM() | ||
get_item = _testcapi.list_get_item | ||
lst = [1, 2, NULL] | ||
self.assertEqual(get_item(lst, 0), 1) | ||
self.assertNotEqual(get_item(lst, 1), 12) | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# CRASHES for out of index: get_item(lst, 3) | ||
# CRASHES get_item(21, 2) | ||
# CRASHES get_item(Null,1) | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
|
||
def test_list_setitem(self): | ||
# Test PyList_SET_ITEM() | ||
setitem = _testcapi.list_setitem | ||
lst = [1, 2, 3] | ||
setitem(lst, 1, 10) | ||
self.assertEqual(lst[1], 10) | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertRaises(IndexError, setitem, [1], -1, 5) | ||
rawwar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self.assertRaises(TypeError, setitem, lst, 1.5, 10) | ||
self.assertRaises(TypeError, setitem, 23, 'a', 5) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It only tests the wrapper, not the C API function. |
||
self.assertRaises(SystemError, setitem, {}, 0, 5) | ||
|
||
# CRASHES setitem(NULL, 'a', 5) | ||
|
||
def test_list_insert(self): | ||
# Test PyList_Insert() | ||
insert = _testcapi.list_insert | ||
lst = [1, 2, 3] | ||
insert(lst, 1, 23) | ||
self.assertEqual(lst[1], 23) | ||
insert(lst, -1, 22) | ||
self.assertEqual(lst[-2], 22) | ||
self.assertRaises(TypeError, insert, lst, 1.5, 10) | ||
self.assertRaises(TypeError, insert, 23, 'a', 5) | ||
self.assertRaises(SystemError, insert, {}, 0, 5) | ||
# CRASHES insert(NULL, 'a', 5) | ||
|
||
def test_list_append(self): | ||
# Test PyList_Append() | ||
append = _testcapi.list_append | ||
lst = [1, 2, 3] | ||
append(lst, 1) | ||
self.assertEqual(lst[-1], 1) | ||
append(lst, [4,5,6]) | ||
self.assertEqual(lst[-1], [4,5,6]) | ||
self.assertRaises(SystemError, append, lst, NULL) | ||
# CRASHES append(NULL, 0) | ||
|
||
def test_list_getslice(self): | ||
# Test PyList_GetSlice() | ||
getslice = _testcapi.list_getslice | ||
lst = [1,2,3,4,5,6,7,8,9,10] | ||
self.assertEqual(getslice(lst, 0, 3), [1, 2, 3]) | ||
self.assertEqual(getslice(lst, 4, 6), [5,6]) | ||
self.assertEqual(getslice(lst, 6, 10), [7,8,9,10]) | ||
self.assertEqual(getslice(lst, 6, 100), [7,8,9,10]) | ||
self.assertNotEqual(getslice(lst, -2, -1), [9]) | ||
self.assertRaises(TypeError, lst, 'a', '2') | ||
|
||
# CRASHES getslice(NULL, 1, 2) |
Uh oh!
There was an error while loading. Please reload this page.