generic interface for map-like data structures
[nbds] / test / ht_test.c
index a334db2ce485b599393a774a5eb6d8d08b4ab4ed..f90caeda9120a38112909390818107719dce9c38 100644 (file)
@@ -7,7 +7,7 @@
 #include "runtime.h"
 #include "CuTest.h"
 #include "common.h"
-#include "struct.h"
+#include "map.h"
 #include "mem.h"
 #include "lwt.h"
 
 typedef struct worker_data {
     int id;
     CuTest *tc;
-    hashtable_t *ht;
+    map_t *ht;
     int *wait;
 } worker_data_t;
 
-int64_t ht_set (hashtable_t *ht, const char *key, uint32_t key_len, int64_t val) {
-    return ht_compare_and_set(ht, key, key_len, EXPECT_WHATEVER, val);
-}
-
-int64_t ht_add (hashtable_t *ht, const char *key, uint32_t key_len, int64_t val) {
-    return ht_compare_and_set(ht, key, key_len, EXPECT_DOES_NOT_EXIST, val);
-}
-
-int64_t ht_replace (hashtable_t *ht, const char *key, uint32_t key_len, int64_t val) {
-    return ht_compare_and_set(ht, key, key_len, EXPECT_EXISTS, val);
-}
-
 // Test some basic stuff; add a few keys, remove a few keys
 void basic_test (CuTest* tc) {
 
-    hashtable_t *ht = ht_alloc();
-
-    ASSERT_EQUAL( 0,              ht_count(ht)          );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"a",2,10)     );
-    ASSERT_EQUAL( 1,              ht_count(ht)          );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"b",2,20)     );
-    ASSERT_EQUAL( 2,              ht_count(ht)          );
-    ASSERT_EQUAL( 20,             ht_get(ht,"b",2)        );
-    ASSERT_EQUAL( 10,             ht_set(ht,"a",2,11)     );
-    ASSERT_EQUAL( 20,             ht_set(ht,"b",2,21)     );
-    ASSERT_EQUAL( 2,              ht_count(ht)          );
-    ASSERT_EQUAL( 21,             ht_add(ht,"b",2,22)     );
-    ASSERT_EQUAL( 11,             ht_remove(ht,"a",2)     );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"a",2)        );
-    ASSERT_EQUAL( 1,              ht_count(ht)          );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"a",2)     );
-    ASSERT_EQUAL( 21,             ht_remove(ht,"b",2)     );
-    ASSERT_EQUAL( 0,              ht_count(ht)          );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"b",2)     );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_remove(ht,"c",2)     );
-    ASSERT_EQUAL( 0,              ht_count(ht)          );
+    map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
+
+    ASSERT_EQUAL( 0,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"a",2,10)         );
+    ASSERT_EQUAL( 1,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"b",2,20)         );
+    ASSERT_EQUAL( 2,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( 20,             map_get(ht,"b",2)            );
+    ASSERT_EQUAL( 10,             map_set(ht,"a",2,11)         );
+    ASSERT_EQUAL( 20,             map_set(ht,"b",2,21)         );
+    ASSERT_EQUAL( 2,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( 21,             map_add(ht,"b",2,22)         );
+    ASSERT_EQUAL( 11,             map_remove(ht,"a",2)         );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"a",2)            );
+    ASSERT_EQUAL( 1,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"a",2)         );
+    ASSERT_EQUAL( 21,             map_remove(ht,"b",2)         );
+    ASSERT_EQUAL( 0,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"b",2)         );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_remove(ht,"c",2)         );
+    ASSERT_EQUAL( 0,              map_stat(ht, MAP_STAT_COUNT) );
     
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_add(ht,"d",2,40)     );
-    ASSERT_EQUAL( 40,             ht_get(ht,"d",2)        );
-    ASSERT_EQUAL( 1,              ht_count(ht)          );
-    ASSERT_EQUAL( 40,             ht_remove(ht,"d",2)     );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2)        );
-    ASSERT_EQUAL( 0,              ht_count(ht)          );
-
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_replace(ht,"d",2,10) );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2)        );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_set(ht,"d",2,40)     );
-    ASSERT_EQUAL( 40,             ht_replace(ht,"d",2,41) );
-    ASSERT_EQUAL( 41,             ht_get(ht,"d",2)        );
-    ASSERT_EQUAL( 41,             ht_remove(ht,"d",2)     );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"d",2)        );
-    ASSERT_EQUAL( 0,              ht_count(ht)          );
-
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_replace(ht,"b",2,20) );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"b",2)        );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_add(ht,"d",2,40)         );
+    ASSERT_EQUAL( 40,             map_get(ht,"d",2)            );
+    ASSERT_EQUAL( 1,              map_stat(ht, MAP_STAT_COUNT) );
+    ASSERT_EQUAL( 40,             map_remove(ht,"d",2)         );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2)            );
+    ASSERT_EQUAL( 0,              map_stat(ht, MAP_STAT_COUNT) );
+
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"d",2,10)     );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2)            );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"d",2,40)         );
+    ASSERT_EQUAL( 40,             map_replace(ht,"d",2,41)     );
+    ASSERT_EQUAL( 41,             map_get(ht,"d",2)            );
+    ASSERT_EQUAL( 41,             map_remove(ht,"d",2)         );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"d",2)            );
+    ASSERT_EQUAL( 0,              map_stat(ht, MAP_STAT_COUNT) );
+
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_replace(ht,"b",2,20)     );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2)            );
     // In the end, all members should be removed
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_set(ht,"b",2,20)     );
-    ASSERT_EQUAL( 20,             ht_replace(ht,"b",2,21) );
-    ASSERT_EQUAL( 21,             ht_get(ht,"b",2)        );
-    ASSERT_EQUAL( 21,             ht_remove(ht,"b",2)     );
-    ASSERT_EQUAL( DOES_NOT_EXIST, ht_get(ht,"b",2)        );
-    ASSERT_EQUAL( 0,              ht_count(ht)          );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_set(ht,"b",2,20)         );
+    ASSERT_EQUAL( 20,             map_replace(ht,"b",2,21)     );
+    ASSERT_EQUAL( 21,             map_get(ht,"b",2)            );
+    ASSERT_EQUAL( 21,             map_remove(ht,"b",2)         );
+    ASSERT_EQUAL( DOES_NOT_EXIST, map_get(ht,"b",2)            );
+    ASSERT_EQUAL( 0,              map_stat(ht, MAP_STAT_COUNT) );
 
-    ht_free(ht);
+    map_free(ht);
 
     // In a quiecent state; it is safe to free.
     rcu_update();
@@ -91,7 +79,7 @@ void basic_test (CuTest* tc) {
 
 void *simple_worker (void *arg) {
     worker_data_t *wd = (worker_data_t *)arg;
-    hashtable_t *ht = wd->ht;
+    map_t *ht = wd->ht;
     CuTest* tc = wd->tc;
     uint64_t d = wd->id;
     int iters = 1000000;
@@ -103,14 +91,14 @@ void *simple_worker (void *arg) {
         for (int i = d; i < iters; i+=2) {
             char key[10];
             sprintf(key, "k%u", i); 
-            TRACE("t0", "test ht_add() iteration (%llu, %llu)", j, i);
-            CuAssertIntEquals_Msg(tc, key, DOES_NOT_EXIST, ht_add(ht, key, strlen(key)+1, d+1) );
+            TRACE("t0", "test map_add() iteration (%llu, %llu)", j, i);
+            CuAssertIntEquals_Msg(tc, key, DOES_NOT_EXIST, map_add(ht, key, strlen(key)+1, d+1) );
         }
         for (int i = d; i < iters; i+=2) {
             char key[10];
             sprintf(key, "k%u", i); 
-            TRACE("t0", "test ht_remove() iteration (%llu, %llu)", j, i);
-            CuAssertIntEquals_Msg(tc, key, d+1, ht_remove(ht, key, strlen(key)+1) );
+            TRACE("t0", "test map_remove() iteration (%llu, %llu)", j, i);
+            CuAssertIntEquals_Msg(tc, key, d+1, map_remove(ht, key, strlen(key)+1) );
         }
         rcu_update();
     }
@@ -123,7 +111,7 @@ void simple_add_remove (CuTest* tc) {
     pthread_t thread[2];
     worker_data_t wd[2];
     int wait = 2;
-    hashtable_t *ht = ht_alloc();
+    map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
 
     // In 2 threads, add & remove even & odd elements concurrently
     int i;
@@ -140,17 +128,17 @@ void simple_add_remove (CuTest* tc) {
     }
 
     // In the end, all members should be removed
-    ASSERT_EQUAL( 0, ht_count(ht) );
+    ASSERT_EQUAL( 0, map_stat(ht, MAP_STAT_COUNT) );
 
     // In a quiecent state; it is safe to free.
-    ht_free(ht);
+    map_free(ht);
 }
 
 
 void *inserter_worker (void *arg) {
     //pthread_t thread[NUM_THREADS];
 
-    //hashtable_t *ht = ht_alloc();
+    //map_t *ht = map_alloc(MAP_TYPE_HASHTABLE);
     return NULL;
 }