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 <anna@ocarinaproject.net>
This commit is contained in:
parent
fc07e28201
commit
b75d13838f
|
@ -96,6 +96,8 @@ void Database<T> :: remove(unsigned int id)
|
||||||
{
|
{
|
||||||
if (id >= actual_size())
|
if (id >= actual_size())
|
||||||
return;
|
return;
|
||||||
|
if (_db[id].valid == false)
|
||||||
|
return;
|
||||||
_keys.erase(_db[id].primary_key());
|
_keys.erase(_db[id].primary_key());
|
||||||
_db[id].valid = false;
|
_db[id].valid = false;
|
||||||
_size--;
|
_size--;
|
||||||
|
|
|
@ -54,7 +54,7 @@ void test_results(bool success, unsigned int line)
|
||||||
void test_size(Database<IntEntry> &db, unsigned int size,
|
void test_size(Database<IntEntry> &db, unsigned int size,
|
||||||
unsigned int actual, unsigned int line)
|
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)
|
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) {
|
for (unsigned int i = 0; i < n + 10; i+=2) {
|
||||||
db.remove(i);
|
db.remove(i);
|
||||||
size--;
|
if (i < n)
|
||||||
|
size--;
|
||||||
}
|
}
|
||||||
test_size(db, size, actual, __LINE__);
|
test_size(db, size, actual, __LINE__);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue