Skip to content

Commit 28dfbc2

Browse files
committed
Merge pull request adafruit#544 from lurch/fix-minmax
Fix the builtin min() and max() functions (and add tests).
2 parents 7917b73 + 3706766 commit 28dfbc2

3 files changed

Lines changed: 45 additions & 4 deletions

File tree

py/builtin.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
245245
mp_obj_t max_obj = NULL;
246246
mp_obj_t item;
247247
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
248-
if (max_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, max_obj, item)) {
248+
if (max_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, max_obj, item) == mp_const_true)) {
249249
max_obj = item;
250250
}
251251
}
@@ -257,7 +257,7 @@ STATIC mp_obj_t mp_builtin_max(uint n_args, const mp_obj_t *args) {
257257
// given many args
258258
mp_obj_t max_obj = args[0];
259259
for (int i = 1; i < n_args; i++) {
260-
if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i])) {
260+
if (mp_binary_op(MP_BINARY_OP_LESS, max_obj, args[i]) == mp_const_true) {
261261
max_obj = args[i];
262262
}
263263
}
@@ -274,7 +274,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
274274
mp_obj_t min_obj = NULL;
275275
mp_obj_t item;
276276
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
277-
if (min_obj == NULL || mp_binary_op(MP_BINARY_OP_LESS, item, min_obj)) {
277+
if (min_obj == NULL || (mp_binary_op(MP_BINARY_OP_LESS, item, min_obj) == mp_const_true)) {
278278
min_obj = item;
279279
}
280280
}
@@ -286,7 +286,7 @@ STATIC mp_obj_t mp_builtin_min(uint n_args, const mp_obj_t *args) {
286286
// given many args
287287
mp_obj_t min_obj = args[0];
288288
for (int i = 1; i < n_args; i++) {
289-
if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj)) {
289+
if (mp_binary_op(MP_BINARY_OP_LESS, args[i], min_obj) == mp_const_true) {
290290
min_obj = args[i];
291291
}
292292
}

tests/basics/builtin-minmax.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# test builtin min and max functions
2+
3+
print(min(0,1))
4+
print(min(1,0))
5+
print(min(0,-1))
6+
print(min(-1,0))
7+
8+
print(max(0,1))
9+
print(max(1,0))
10+
print(max(0,-1))
11+
print(max(-1,0))
12+
13+
print(min([1,2,4,0,-1,2]))
14+
print(max([1,2,4,0,-1,2]))
15+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# test builtin min and max functions with float args
2+
3+
print(min(0,1.0))
4+
print(min(1.0,0))
5+
print(min(0,-1.0))
6+
print(min(-1.0,0))
7+
8+
print(max(0,1.0))
9+
print(max(1.0,0))
10+
print(max(0,-1.0))
11+
print(max(-1.0,0))
12+
13+
print(min(1.5,-1.5))
14+
print(min(-1.5,1.5))
15+
16+
print(max(1.5,-1.5))
17+
print(max(-1.5,1.5))
18+
19+
print(min([1,2.9,4,0,-1,2]))
20+
print(max([1,2.9,4,0,-1,2]))
21+
22+
print(min([1,2.9,4,6.5,-1,2]))
23+
print(max([1,2.9,4,6.5,-1,2]))
24+
print(min([1,2.9,4,-6.5,-1,2]))
25+
print(max([1,2.9,4,-6.5,-1,2]))
26+

0 commit comments

Comments
 (0)