Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
28dfbc2b
Commit
28dfbc2b
authored
May 01, 2014
by
Paul Sokolovsky
Browse files
Merge pull request #544 from lurch/fix-minmax
Fix the builtin min() and max() functions (and add tests).
parents
7917b731
37067666
Changes
3
Hide whitespace changes
Inline
Side-by-side
py/builtin.c
View file @
28dfbc2b
...
...
@@ -245,7 +245,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
mp_obj_t
max_obj
=
NULL
;
mp_obj_t
item
;
while
((
item
=
mp_iternext
(
iterable
))
!=
MP_OBJ_STOP_ITERATION
)
{
if
(
max_obj
==
NULL
||
mp_binary_op
(
MP_BINARY_OP_LESS
,
max_obj
,
item
))
{
if
(
max_obj
==
NULL
||
(
mp_binary_op
(
MP_BINARY_OP_LESS
,
max_obj
,
item
)
==
mp_const_true
)
)
{
max_obj
=
item
;
}
}
...
...
@@ -257,7 +257,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
// given many args
mp_obj_t
max_obj
=
args
[
0
];
for
(
int
i
=
1
;
i
<
n_args
;
i
++
)
{
if
(
mp_binary_op
(
MP_BINARY_OP_LESS
,
max_obj
,
args
[
i
]))
{
if
(
mp_binary_op
(
MP_BINARY_OP_LESS
,
max_obj
,
args
[
i
])
==
mp_const_true
)
{
max_obj
=
args
[
i
];
}
}
...
...
@@ -274,7 +274,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
mp_obj_t
min_obj
=
NULL
;
mp_obj_t
item
;
while
((
item
=
mp_iternext
(
iterable
))
!=
MP_OBJ_STOP_ITERATION
)
{
if
(
min_obj
==
NULL
||
mp_binary_op
(
MP_BINARY_OP_LESS
,
item
,
min_obj
))
{
if
(
min_obj
==
NULL
||
(
mp_binary_op
(
MP_BINARY_OP_LESS
,
item
,
min_obj
)
==
mp_const_true
)
)
{
min_obj
=
item
;
}
}
...
...
@@ -286,7 +286,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
// given many args
mp_obj_t
min_obj
=
args
[
0
];
for
(
int
i
=
1
;
i
<
n_args
;
i
++
)
{
if
(
mp_binary_op
(
MP_BINARY_OP_LESS
,
args
[
i
],
min_obj
))
{
if
(
mp_binary_op
(
MP_BINARY_OP_LESS
,
args
[
i
],
min_obj
)
==
mp_const_true
)
{
min_obj
=
args
[
i
];
}
}
...
...
tests/basics/builtin-minmax.py
0 → 100644
View file @
28dfbc2b
# test builtin min and max functions
print
(
min
(
0
,
1
))
print
(
min
(
1
,
0
))
print
(
min
(
0
,
-
1
))
print
(
min
(
-
1
,
0
))
print
(
max
(
0
,
1
))
print
(
max
(
1
,
0
))
print
(
max
(
0
,
-
1
))
print
(
max
(
-
1
,
0
))
print
(
min
([
1
,
2
,
4
,
0
,
-
1
,
2
]))
print
(
max
([
1
,
2
,
4
,
0
,
-
1
,
2
]))
tests/float/builtin-float-minmax.py
0 → 100644
View file @
28dfbc2b
# test builtin min and max functions with float args
print
(
min
(
0
,
1.0
))
print
(
min
(
1.0
,
0
))
print
(
min
(
0
,
-
1.0
))
print
(
min
(
-
1.0
,
0
))
print
(
max
(
0
,
1.0
))
print
(
max
(
1.0
,
0
))
print
(
max
(
0
,
-
1.0
))
print
(
max
(
-
1.0
,
0
))
print
(
min
(
1.5
,
-
1.5
))
print
(
min
(
-
1.5
,
1.5
))
print
(
max
(
1.5
,
-
1.5
))
print
(
max
(
-
1.5
,
1.5
))
print
(
min
([
1
,
2.9
,
4
,
0
,
-
1
,
2
]))
print
(
max
([
1
,
2.9
,
4
,
0
,
-
1
,
2
]))
print
(
min
([
1
,
2.9
,
4
,
6.5
,
-
1
,
2
]))
print
(
max
([
1
,
2.9
,
4
,
6.5
,
-
1
,
2
]))
print
(
min
([
1
,
2.9
,
4
,
-
6.5
,
-
1
,
2
]))
print
(
max
([
1
,
2.9
,
4
,
-
6.5
,
-
1
,
2
]))
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment