Skip to content
Snippets Groups Projects
Commit 83eba5de authored by Paul Sokolovsky's avatar Paul Sokolovsky
Browse files

sequence: Fix glaring bug in sequence comparison.

parent 7de5377c
No related branches found
No related tags found
No related merge requests found
#include <assert.h>
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
...@@ -109,18 +110,24 @@ bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t * ...@@ -109,18 +110,24 @@ bool mp_seq_cmp_objs(int op, const mp_obj_t *items1, uint len1, const mp_obj_t *
int len = len1 < len2 ? len1 : len2; int len = len1 < len2 ? len1 : len2;
bool eq_status = true; // empty lists are equal bool eq_status = true; // empty lists are equal
bool rel_status;
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
eq_status = mp_obj_equal(items1[i], items2[i]); eq_status = mp_obj_equal(items1[i], items2[i]);
if (op == MP_BINARY_OP_EQUAL && !eq_status) { // If current elements equal, can't decide anything - go on
return false; if (eq_status) {
continue;
} }
rel_status = (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
if (!eq_status && !rel_status) { // Othewise, if they are not equal, we can have final decision based on them
if (op == MP_BINARY_OP_EQUAL) {
// In particular, if we are checking for equality, here're the answer
return false; return false;
} }
// Otherwise, application of relation op gives the answer
return (mp_binary_op(op, items1[i], items2[i]) == mp_const_true);
} }
assert(eq_status);
// If we had tie in the last element... // If we had tie in the last element...
if (eq_status) { if (eq_status) {
// ... and we have lists of different lengths... // ... and we have lists of different lengths...
......
...@@ -48,3 +48,8 @@ print((1,) <= (1, 0,)) ...@@ -48,3 +48,8 @@ print((1,) <= (1, 0,))
print((1,) <= (1, -1,)) print((1,) <= (1, -1,))
print((1, 0,) <= (1,)) print((1, 0,) <= (1,))
print((1, -1,) <= (1,)) print((1, -1,) <= (1,))
print((10, 0) > (1, 1))
print((10, 0) < (1, 1))
print((0, 0, 10, 0) > (0, 0, 1, 1))
print((0, 0, 10, 0) < (0, 0, 1, 1))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment