mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
f34035100c
commit
e4c8c47c2e
@ -237,7 +237,6 @@ static bool _py_list__count(int argc, py_Ref argv) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
static bool _py_list__clear(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
py_list__clear(py_arg(0));
|
||||
@ -255,8 +254,13 @@ static bool _py_list__copy(int argc, py_Ref argv) {
|
||||
}
|
||||
|
||||
static bool _py_list__index(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
for(int i = 0; i < py_list__len(py_arg(0)); i++) {
|
||||
if(argc > 3) return TypeError("index() takes at most 3 arguments");
|
||||
int start = 0;
|
||||
if(argc == 3) {
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
start = py_toint(py_arg(2));
|
||||
}
|
||||
for(int i = start; i < py_list__len(py_arg(0)); i++) {
|
||||
int res = py_eq(py_list__getitem(py_arg(0), i), py_arg(1));
|
||||
if(res == -1) return false;
|
||||
if(res) {
|
||||
@ -301,6 +305,7 @@ static bool _py_list__pop(int argc, py_Ref argv) {
|
||||
c11_vector__pop(self);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool _py_list__insert(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(3);
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
|
@ -20,6 +20,7 @@ l1 = [1];l2 = l1;l1.append(2);l3 = [1,1,2]
|
||||
assert l2[1] == 2
|
||||
assert l1 == l2
|
||||
assert l1*3 == [1,2,1,2,1,2]
|
||||
assert 2*l1 == [1,2,1,2]
|
||||
assert l3.count(1) == 2
|
||||
|
||||
member = ['Tom', 'Sunny', 'Honer', 'Lily']
|
||||
@ -55,27 +56,7 @@ l.insert(1, 'e')
|
||||
assert l == ['h', 'e', 'l', 'l', 'o']
|
||||
assert l[-2] == 'l'
|
||||
|
||||
# test sort
|
||||
a = [8, 2, 4, 2, 9]
|
||||
assert sorted(a) == [2, 2, 4, 8, 9]
|
||||
assert sorted(a, reverse=True) == [9, 8, 4, 2, 2]
|
||||
|
||||
assert sorted(a, key=lambda x:-x, reverse=True) == [2, 2, 4, 8, 9]
|
||||
assert a == [8, 2, 4, 2, 9]
|
||||
|
||||
b = [(1, 2), (3, 3), (5, 1)]
|
||||
b.sort(key=lambda x:x[1])
|
||||
assert b == [(5, 1), (1, 2), (3,3)]
|
||||
|
||||
# unpacking builder
|
||||
a = [1, 2, 3]
|
||||
b = [*a, 4, 5]
|
||||
assert b == [1, 2, 3, 4, 5]
|
||||
|
||||
a = []
|
||||
b = [*a, 1, 2, 3, *a, *a]
|
||||
assert b == [1, 2, 3]
|
||||
|
||||
b = [1, 2, 3]
|
||||
assert b[
|
||||
1
|
||||
] == 2
|
||||
@ -87,12 +68,6 @@ assert b[0] == 1
|
||||
assert b[
|
||||
0] == 1
|
||||
|
||||
a = []
|
||||
a.append(0)
|
||||
a.append([1, 2, a])
|
||||
|
||||
assert repr(a) == "[0, [1, 2, [...]]]"
|
||||
|
||||
a = [1, 2, 3]
|
||||
assert a.index(2) == 1
|
||||
assert a.index(1) == 0
|
||||
@ -110,6 +85,24 @@ assert list(range(1, 5, 2)) == [1, 3]
|
||||
assert list(range(5, 1, -1)) == [5, 4, 3, 2]
|
||||
assert list(range(5, 1, -2)) == [5, 3]
|
||||
|
||||
# test sort
|
||||
a = [8, 2, 4, 2, 9]
|
||||
assert sorted(a) == [2, 2, 4, 8, 9]
|
||||
assert sorted(a, reverse=True) == [9, 8, 4, 2, 2]
|
||||
|
||||
assert sorted(a, key=lambda x:-x, reverse=True) == [2, 2, 4, 8, 9]
|
||||
assert a == [8, 2, 4, 2, 9]
|
||||
|
||||
b = [(1, 2), (3, 3), (5, 1)]
|
||||
b.sort(key=lambda x:x[1])
|
||||
assert b == [(5, 1), (1, 2), (3,3)]
|
||||
|
||||
# test cyclic reference
|
||||
a = []
|
||||
a.append(0)
|
||||
a.append([1, 2, a])
|
||||
|
||||
assert repr(a) == "[0, [1, 2, [...]]]"
|
||||
|
||||
# try:
|
||||
# a.index(1, 1)
|
||||
@ -130,3 +123,12 @@ assert list(range(5, 1, -2)) == [5, 3]
|
||||
# assert A()[::, :2] == (slice(None, None, None), slice(None, 2, None))
|
||||
# assert A()['b':'c':1, :] == (slice('b', 'c', 1), slice(None, None, None))
|
||||
# assert A()[1:2, :A()[3:4, ::-1]] == (slice(1, 2, None), slice(None, (slice(3, 4, None), slice(None, None, -1)), None))
|
||||
|
||||
# unpacking builder (not supported)
|
||||
# a = [1, 2, 3]
|
||||
# b = [*a, 4, 5]
|
||||
# assert b == [1, 2, 3, 4, 5]
|
||||
|
||||
# a = []
|
||||
# b = [*a, 1, 2, 3, *a, *a]
|
||||
# assert b == [1, 2, 3]
|
Loading…
x
Reference in New Issue
Block a user