Add debugging sanity checks.

This commit is contained in:
Hisham Muhammad 2006-11-12 21:52:14 +00:00
parent c90a445103
commit 36848494f5
4 changed files with 37 additions and 2 deletions

View File

@ -46,6 +46,19 @@ bool Hashtable_isConsistent(Hashtable* this) {
return items == this->items; 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 #endif
HashtableItem* HashtableItem_new(int key, void* value) { HashtableItem* HashtableItem_new(int key, void* value) {

View File

@ -12,6 +12,7 @@ in the source distribution for its full text.
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
#include <assert.h>
#include "debug.h" #include "debug.h"
@ -32,6 +33,14 @@ struct Hashtable_ {
bool owner; bool owner;
}; };
#ifdef DEBUG
bool Hashtable_isConsistent(Hashtable* this);
int Hashtable_count(Hashtable* this);
#endif
HashtableItem* HashtableItem_new(int key, void* value); HashtableItem* HashtableItem_new(int key, void* value);
Hashtable* Hashtable_new(int size, bool owner); 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_put(Hashtable* this, int key, void* value);
void* Hashtable_remove(Hashtable* this, int key); void* Hashtable_remove(Hashtable* this, int key);
//#include <stdio.h>
inline void* Hashtable_get(Hashtable* this, int key); inline void* Hashtable_get(Hashtable* this, int key);
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData); void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData);

View File

@ -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 #endif
void Vector_prune(Vector* this) { void Vector_prune(Vector* this) {
@ -223,8 +233,9 @@ void Vector_add(Vector* this, void* data_) {
assert(data_ && ((Object*)data_)->class == this->vectorType); assert(data_ && ((Object*)data_)->class == this->vectorType);
Object* data = data_; Object* data = data_;
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
int i = this->items;
Vector_set(this, this->items, data); Vector_set(this, this->items, data);
assert(this->items == i+1); (void)(i);
assert(Vector_isConsistent(this)); assert(Vector_isConsistent(this));
} }

View File

@ -41,6 +41,8 @@ void Vector_delete(Vector* this);
#ifdef DEBUG #ifdef DEBUG
int Vector_count(Vector* this);
#endif #endif
void Vector_prune(Vector* this); void Vector_prune(Vector* this);