From abd49b93a77a317eafe35c5818271a018a6c22e5 Mon Sep 17 00:00:00 2001 From: "K. Lange" Date: Wed, 15 Dec 2021 16:37:19 +0900 Subject: [PATCH] Fix 'del' on table entries not updating count --- src/table.c | 6 ++-- test/testFullOfTombstones.krk | 44 ++++++++++++++++++++++++++++ test/testFullOfTombstones.krk.expect | 3 ++ test/testTablesDelLen.krk | 12 ++++++++ test/testTablesDelLen.krk.expect | 5 ++++ 5 files changed, 68 insertions(+), 2 deletions(-) create mode 100644 test/testFullOfTombstones.krk create mode 100644 test/testFullOfTombstones.krk.expect create mode 100644 test/testTablesDelLen.krk create mode 100644 test/testTablesDelLen.krk.expect diff --git a/src/table.c b/src/table.c index bea2274..f6e7425 100644 --- a/src/table.c +++ b/src/table.c @@ -67,6 +67,7 @@ KrkTableEntry * krk_findEntry(KrkTableEntry * entries, size_t capacity, KrkValue if (IS_NONE(entry->value)) { return tombstone != NULL ? tombstone : entry; } else { + if (tombstone == entry) return tombstone; if (tombstone == NULL) tombstone = entry; } } else if (krk_valuesEqual(entry->key, key)) { @@ -121,7 +122,7 @@ int krk_tableSet(KrkTable * table, KrkValue key, KrkValue value) { KrkTableEntry * entry = krk_findEntry(table->entries, table->capacity, key); if (!entry) return 0; int isNewKey = IS_KWARGS(entry->key); - if (isNewKey && IS_NONE(entry->value)) table->count++; + if (isNewKey) table->count++; entry->key = key; entry->value = value; return isNewKey; @@ -167,8 +168,9 @@ int krk_tableDelete(KrkTable * table, KrkValue key) { if (!entry || IS_KWARGS(entry->key)) { return 0; } + table->count--; entry->key = KWARGS_VAL(0); - entry->value = BOOLEAN_VAL(1); + entry->value = KWARGS_VAL(0); return 1; } diff --git a/test/testFullOfTombstones.krk b/test/testFullOfTombstones.krk new file mode 100644 index 0000000..7001898 --- /dev/null +++ b/test/testFullOfTombstones.krk @@ -0,0 +1,44 @@ +let open = {(0,0)} +open.remove( (0, 0) ) +(1, 0) not in open +open.add( (1, 0) ) +(0, 1) not in open +open.add( (0, 1) ) +open.remove( (0, 1) ) +(1, 1) not in open +open.add( (1, 1) ) +(0, 2) not in open +open.add( (0, 2) ) +open.remove( (1, 0) ) +(2, 0) not in open +open.add( (2, 0) ) +open.remove( (2, 0) ) +(3, 0) not in open +open.add( (3, 0) ) +(2, 1) not in open +open.add( (2, 1) ) +open.remove( (1, 1) ) +(1, 2) not in open +open.add( (1, 2) ) +open.remove( (0, 2) ) +(0, 3) not in open +open.add( (0, 3) ) +open.remove( (2, 1) ) +(3, 1) not in open +open.add( (3, 1) ) +(2, 2) not in open +open.add( (2, 2) ) +open.remove( (3, 0) ) +(4, 0) not in open +open.add( (4, 0) ) +open.remove( (0, 3) ) +(1, 3) not in open +open.add( (1, 3) ) +print((0, 4) not in open) +open.add( (0, 4) ) +open.remove( (1, 2) ) +open.remove( (2, 2) ) +print((3, 2) not in open) + + +print("done") diff --git a/test/testFullOfTombstones.krk.expect b/test/testFullOfTombstones.krk.expect new file mode 100644 index 0000000..ccb61e9 --- /dev/null +++ b/test/testFullOfTombstones.krk.expect @@ -0,0 +1,3 @@ +True +True +done diff --git a/test/testTablesDelLen.krk b/test/testTablesDelLen.krk new file mode 100644 index 0000000..a9b91a4 --- /dev/null +++ b/test/testTablesDelLen.krk @@ -0,0 +1,12 @@ + +let d = {1: 2, 3: 4, 5: 6} +print(len(d)) +del d[3] +print(len(d)) + +let s = {1,2,3} +print(len(s)) +s.remove(2) +print(len(s)) +s.add(2) +print(len(s)) diff --git a/test/testTablesDelLen.krk.expect b/test/testTablesDelLen.krk.expect new file mode 100644 index 0000000..82c5016 --- /dev/null +++ b/test/testTablesDelLen.krk.expect @@ -0,0 +1,5 @@ +3 +2 +3 +2 +3