summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHisham Muhammad <hisham@gobolinux.org>2006-11-12 21:52:14 +0000
committerHisham Muhammad <hisham@gobolinux.org>2006-11-12 21:52:14 +0000
commit36848494f57ff8cdc95876c95c36052eca40ccdb (patch)
treed3d50472fa7098e2a27127899e7a9ed20af22fc0
parentc90a4451032d54c3f29cd6b567ba62126fe65208 (diff)
Add debugging sanity checks.
-rw-r--r--Hashtable.c13
-rw-r--r--Hashtable.h11
-rw-r--r--Vector.c13
-rw-r--r--Vector.h2
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 <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);
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);

© 2014-2024 Faster IT GmbH | imprint | privacy policy