oops, nasty off-by-one in set_copy

This commit is contained in:
John R. Lenton 2014-01-12 23:37:45 +00:00
parent be790f94d5
commit 7244a14439
2 changed files with 31 additions and 26 deletions

View File

@ -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) {
mp_obj_set_t *self = self_in;
if (self->set.used == 0) {
print(env, "set()");
return;
}
bool first = true;
print(env, "{");
for (int i = 0; i < self->set.alloc; i++) {
@ -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);
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;
memcpy(other->set.table, self->set.table, self->set.alloc * sizeof(mp_obj_t));

View File

@ -1,29 +1,30 @@
def r(s):
l = list(s)
l.sort()
print(l)
s = {1, 2}
t = {2, 3}
r(s | t)
r(s ^ t)
r(s & t)
r(s - t)
u = s.copy()
u |= t
r(u)
u = s.copy()
u ^= t
r(u)
u = s.copy()
u &= t
r(u)
u = s.copy()
u -= t
r(u)
return l
sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
for s in sets:
for t in sets:
print(s, '|', t, '=', r(s | t))
print(s, '^', t, '=', r(s ^ t))
print(s, '&', t, '=', r(s & t))
print(s, '-', t, '=', r(s - t))
u = s.copy()
u |= t
print(s, "|=", t, '-->', r(u))
u = s.copy()
u ^= t
print(s, "^=", t, '-->', r(u))
u = s.copy()
u &= t
print(s, "&=", t, "-->", r(u))
u = s.copy()
u -= t
print(s, "-=", t, "-->", r(u))
print(s == t)
print(s != t)
print(s > t)
print(s >= t)
print(s < t)
print(s <= t)
print(s, '==', t, '=', s == t)
print(s, '!=', t, '=', s != t)
print(s, '>', t, '=', s > t)
print(s, '>=', t, '=', s >= t)
print(s, '<', t, '=', s < t)
print(s, '<=', t, '=', s <= t)