forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltin_min.py
More file actions
60 lines (44 loc) · 1.04 KB
/
builtin_min.py
File metadata and controls
60 lines (44 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from testutils import assert_raises
# simple values
assert min(0, 0) == 0
assert min(1, 0) == 0
assert min(1.0, 0.0) == 0.0
assert min(-1, 0) == -1
assert min(1, 2, 3) == 1
# iterables
assert min([1, 2, 3]) == 1
assert min((1, 2, 3)) == 1
assert (
min(
{
"a": 0,
"b": 1,
}
)
== "a"
)
assert min([1, 2], default=0) == 1
assert min([], default=0) == 0
assert_raises(ValueError, min, [])
# key parameter
assert min(1, 2, -3, key=abs) == 1
assert min([1, 2, -3], key=abs) == 1
# no argument
assert_raises(TypeError, min)
# one non-iterable argument
assert_raises(TypeError, min, 1)
# custom class
class MyComparable:
nb = 0
def __init__(self):
self.my_nb = MyComparable.nb
MyComparable.nb += 1
def __gt__(self, other):
return self.my_nb > other.my_nb
first = MyComparable()
second = MyComparable()
assert min(first, second) == first
assert min([first, second]) == first
class MyNotComparable:
pass
assert_raises(TypeError, min, MyNotComparable(), MyNotComparable())