From 2c9e2722255c2ddc2d6929e59f491acba45a9d48 Mon Sep 17 00:00:00 2001 From: Chuyue Sun Date: Thu, 18 Jul 2024 23:23:34 +0000 Subject: [PATCH] add new hashtable unit tests --- src/test/hashtable.cpp | 117 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 1 deletion(-) diff --git a/src/test/hashtable.cpp b/src/test/hashtable.cpp index fb8042dc7..37b3884a4 100644 --- a/src/test/hashtable.cpp +++ b/src/test/hashtable.cpp @@ -20,7 +20,7 @@ Revision History: #include #include #include - +#include #include "util/hashtable.h" @@ -119,11 +119,126 @@ static void tst3() { ENSURE(h2.size() == 2); } +// Custom hash and equality functions for testing +struct my_hash { + unsigned operator()(int x) const { return x; } +}; + +struct my_eq { + bool operator()(int x, int y) const { return x == y; } +}; + +void test_hashtable_constructors() { + hashtable ht; + assert(ht.empty()); + assert(ht.size() == 0); + assert(ht.capacity() == DEFAULT_HASHTABLE_INITIAL_CAPACITY); + + // Copy constructor + hashtable ht_copy(ht); + assert(ht_copy.empty()); + assert(ht_copy.size() == 0); + assert(ht_copy.capacity() == ht.capacity()); + + // Move constructor + hashtable ht_move(std::move(ht)); + assert(ht_move.empty()); + assert(ht_move.size() == 0); + assert(ht_move.capacity() == ht_copy.capacity()); +} + +void test_hashtable_insert() { + hashtable ht; + ht.insert(1); + assert(!ht.empty()); + assert(ht.size() == 1); + int value; + assert(ht.find(1, value) && value == 1); +} + +void test_hashtable_remove() { + hashtable ht; + ht.insert(1); + ht.remove(1); + assert(ht.empty()); + assert(ht.size() == 0); +} + +void test_hashtable_find() { + hashtable ht; + ht.insert(1); + int value; + assert(ht.find(1, value) && value == 1); + assert(!ht.find(2, value)); +} + +void test_hashtable_contains() { + hashtable ht; + ht.insert(1); + assert(ht.contains(1)); + assert(!ht.contains(2)); +} + +void test_hashtable_reset() { + hashtable ht; + ht.insert(1); + ht.reset(); + assert(ht.empty()); + assert(ht.size() == 0); +} + +void test_hashtable_finalize() { + hashtable ht; + ht.insert(1); + ht.finalize(); + assert(ht.empty()); + assert(ht.size() == 0); +} + +void test_hashtable_iterators() { + hashtable ht; + ht.insert(1); + ht.insert(2); + ht.insert(3); + + int count = 0; + for(auto it = ht.begin(); it != ht.end(); ++it) { + ++count; + } + assert(count == 3); +} + +void test_hashtable_operators() { + hashtable ht1; + hashtable ht2; + + ht1.insert(1); + ht2.insert(2); + + ht1 |= ht2; + assert(ht1.contains(1)); + assert(ht1.contains(2)); + + ht1 &= ht2; + assert(!ht1.contains(1)); + assert(ht1.contains(2)); +} + void tst_hashtable() { tst3(); for (int i = 0; i < 100; i++) tst2(); tst1(); + test_hashtable_constructors(); + test_hashtable_insert(); + test_hashtable_remove(); + test_hashtable_find(); + test_hashtable_contains(); + test_hashtable_reset(); + test_hashtable_finalize(); + test_hashtable_iterators(); + test_hashtable_operators(); + std::cout << "All tests passed!" << std::endl; } #else void tst_hashtable() {