From 36848494f57ff8cdc95876c95c36052eca40ccdb Mon Sep 17 00:00:00 2001 From: Hisham Muhammad Date: Sun, 12 Nov 2006 21:52:14 +0000 Subject: [PATCH] Add debugging sanity checks. --- Hashtable.c | 13 +++++++++++++ Hashtable.h | 11 ++++++++++- Vector.c | 13 ++++++++++++- Vector.h | 2 ++ 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/Hashtable.c b/Hashtable.c index c1e63c05..4cd63109 100644 --- a/Hashtable.c +++ b/Hashtable.c @@ -46,6 +46,19 @@ bool Hashtable_isConsistent(Hashtable* this) { return items == this->items; } +int Hashtable_count(Hashtable* this) { + int items = 0; + for (int i = 0; i < this->size; i++) { + HashtableItem* bucket = this->buckets[i]; + while (bucket) { + items++; + bucket = bucket->next; + } + } + assert(items == this->items); + return items; +} + #endif HashtableItem* HashtableItem_new(int key, void* value) { diff --git a/Hashtable.h b/Hashtable.h index df481f2d..7c1a6789 100644 --- a/Hashtable.h +++ b/Hashtable.h @@ -12,6 +12,7 @@ in the source distribution for its full text. #include #include +#include #include "debug.h" @@ -32,6 +33,14 @@ struct Hashtable_ { bool owner; }; +#ifdef DEBUG + +bool Hashtable_isConsistent(Hashtable* this); + +int Hashtable_count(Hashtable* this); + +#endif + HashtableItem* HashtableItem_new(int key, void* value); Hashtable* Hashtable_new(int size, bool owner); @@ -43,7 +52,7 @@ inline int Hashtable_size(Hashtable* this); void Hashtable_put(Hashtable* this, int key, void* value); void* Hashtable_remove(Hashtable* this, int key); -//#include + inline void* Hashtable_get(Hashtable* this, int key); void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData); diff --git a/Vector.c b/Vector.c index 0ee290a8..4c2a4f9b 100644 --- a/Vector.c +++ b/Vector.c @@ -74,6 +74,16 @@ static inline bool Vector_isConsistent(Vector* this) { } } +int Vector_count(Vector* this) { + int items = 0; + for (int i = 0; i < this->items; i++) { + if (this->array[i]) + items++; + } + assert(items == this->items); + return items; +} + #endif void Vector_prune(Vector* this) { @@ -223,8 +233,9 @@ void Vector_add(Vector* this, void* data_) { assert(data_ && ((Object*)data_)->class == this->vectorType); Object* data = data_; assert(Vector_isConsistent(this)); - + int i = this->items; Vector_set(this, this->items, data); + assert(this->items == i+1); (void)(i); assert(Vector_isConsistent(this)); } diff --git a/Vector.h b/Vector.h index b332ae80..3ecfd270 100644 --- a/Vector.h +++ b/Vector.h @@ -41,6 +41,8 @@ void Vector_delete(Vector* this); #ifdef DEBUG +int Vector_count(Vector* this); + #endif void Vector_prune(Vector* this);