Commit 36848494 authored by Hisham Muhammad's avatar Hisham Muhammad
Browse files

Add debugging sanity checks.

parent c90a4451
......@@ -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) {
......
......@@ -12,6 +12,7 @@ in the source distribution for its full text.
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#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 <stdio.h>
inline void* Hashtable_get(Hashtable* this, int key);
void Hashtable_foreach(Hashtable* this, Hashtable_PairFunction f, void* userData);
......
......@@ -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));
}
......
......@@ -41,6 +41,8 @@ void Vector_delete(Vector* this);
#ifdef DEBUG
int Vector_count(Vector* this);
#endif
void Vector_prune(Vector* this);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment