From b75d13838ffbf1beb522787503ef984bcc0854ef Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 23 Mar 2014 13:16:22 -0400 Subject: [PATCH] database: Properly test for double-removal I found that databases were always decrementing _size when remove() was called. This patch fixes the bug by checking if the entry is valid before removal. Signed-off-by: Anna Schumaker --- include/database.hpp | 2 ++ tests/src/database.cpp | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/include/database.hpp b/include/database.hpp index f6c4b4f1..60217352 100644 --- a/include/database.hpp +++ b/include/database.hpp @@ -96,6 +96,8 @@ void Database :: remove(unsigned int id) { if (id >= actual_size()) return; + if (_db[id].valid == false) + return; _keys.erase(_db[id].primary_key()); _db[id].valid = false; _size--; diff --git a/tests/src/database.cpp b/tests/src/database.cpp index e9291984..b99bfb84 100644 --- a/tests/src/database.cpp +++ b/tests/src/database.cpp @@ -54,7 +54,7 @@ void test_results(bool success, unsigned int line) void test_size(Database &db, unsigned int size, unsigned int actual, unsigned int line) { - test_results( (db.size() == size) || (db.actual_size() == actual), line ); + test_results( (db.size() == size) && (db.actual_size() == actual), line ); } int main(int argc, char **argv) @@ -123,7 +123,8 @@ int main(int argc, char **argv) */ for (unsigned int i = 0; i < n + 10; i+=2) { db.remove(i); - size--; + if (i < n) + size--; } test_size(db, size, actual, __LINE__);