Commit 7244a144 authored by John R. Lenton's avatar John R. Lenton
Browse files

oops, nasty off-by-one in set_copy

parent be790f94
...@@ -27,6 +27,10 @@ static mp_obj_t set_it_iternext(mp_obj_t self_in); ...@@ -27,6 +27,10 @@ static mp_obj_t set_it_iternext(mp_obj_t self_in);
void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) { void set_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in) {
mp_obj_set_t *self = self_in; mp_obj_set_t *self = self_in;
if (self->set.used == 0) {
print(env, "set()");
return;
}
bool first = true; bool first = true;
print(env, "{"); print(env, "{");
for (int i = 0; i < self->set.alloc; i++) { for (int i = 0; i < self->set.alloc; i++) {
...@@ -122,7 +126,7 @@ static mp_obj_t set_copy(mp_obj_t self_in) { ...@@ -122,7 +126,7 @@ static mp_obj_t set_copy(mp_obj_t self_in) {
mp_obj_set_t *other = m_new_obj(mp_obj_set_t); mp_obj_set_t *other = m_new_obj(mp_obj_set_t);
other->base.type = &set_type; other->base.type = &set_type;
mp_set_init(&other->set, self->set.alloc); mp_set_init(&other->set, self->set.alloc - 1);
other->set.used = self->set.used; other->set.used = self->set.used;
memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t)); memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));
......
def r(s): def r(s):
l = list(s) l = list(s)
l.sort() l.sort()
print(l) return l
s = {1, 2} sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
t = {2, 3} for s in sets:
r(s | t) for t in sets:
r(s ^ t) print(s, '|', t, '=', r(s | t))
r(s & t) print(s, '^', t, '=', r(s ^ t))
r(s - t) print(s, '&', t, '=', r(s & t))
u = s.copy() print(s, '-', t, '=', r(s - t))
u |= t u = s.copy()
r(u) u |= t
u = s.copy() print(s, "|=", t, '-->', r(u))
u ^= t u = s.copy()
r(u) u ^= t
u = s.copy() print(s, "^=", t, '-->', r(u))
u &= t u = s.copy()
r(u) u &= t
u = s.copy() print(s, "&=", t, "-->", r(u))
u -= t u = s.copy()
r(u) u -= t
print(s, "-=", t, "-->", r(u))
print(s == t) print(s, '==', t, '=', s == t)
print(s != t) print(s, '!=', t, '=', s != t)
print(s > t) print(s, '>', t, '=', s > t)
print(s >= t) print(s, '>=', t, '=', s >= t)
print(s < t) print(s, '<', t, '=', s < t)
print(s <= t) print(s, '<=', t, '=', s <= t)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment