Skip to content

Commit f411862

Browse files
committed
Add tests for list.sort
1 parent cf32df7 commit f411862

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

py/tests/list.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 The go-python Authors. All rights reserved.
1+
# Copyright 2019 The go-python Authors. All rights reserved.
22
# Use of this source code is governed by a BSD-style
33
# license that can be found in the LICENSE file.
44

@@ -39,4 +39,37 @@
3939
assert a * 0 == []
4040
assert a * -1 == []
4141

42+
doc="sort"
43+
a = [3, 1.1, 1, 2]
44+
s1 = list(a)
45+
s1.sort()
46+
assert s1 == [1, 1.1, 2, 3]
47+
s1.sort() # sort a sorted list
48+
assert s1 == [1, 1.1, 2, 3]
49+
s2 = list(a)
50+
s2.sort(reverse=True)
51+
assert s2 == [3, 2, 1.1, 1]
52+
s2.sort() # sort a reversed list
53+
assert s2 == [1, 1.1, 2, 3]
54+
s3 = list(a)
55+
s3.sort(key=lambda l: l+1) # test lambda key
56+
assert s3 == [1, 1.1, 2, 3]
57+
s4 = [2.0, 2, 1, 1.0]
58+
s4.sort(key=lambda l: 0) # test stability
59+
assert s4 == [2.0, 2, 1, 1.0]
60+
assert [type(t) for t in s4] == [float, int, int, float]
61+
s4 = [2.0, 2, 1, 1.0]
62+
s4.sort() # test stability
63+
assert s4 == [1, 1.0, 2.0, 2]
64+
assert [type(t) for t in s4] == [int, float, float, int]
65+
s5 = [2.0, "abc"]
66+
assertRaises(TypeError, lambda: s5.sort())
67+
s5 = []
68+
s5.sort()
69+
assert s5 == []
70+
s5 = [0]
71+
s5.sort()
72+
assert s5 == [0]
73+
assertRaises(TypeError, lambda: s5.sort(key=1))
74+
4275
doc="finished"

0 commit comments

Comments
 (0)