Updated for custom hash functions/setting functions.
[awesomized/libmemcached] / tests / hashkit_functions.c
index 1302011e76f48f14ad49f362bcf51c42f85bad09..0e4a894da8f55d78f7ea1cfd0ab6f7866bc116e2 100644 (file)
@@ -35,15 +35,12 @@ static test_return_t init_test(void *not_used __attribute__((unused)))
   hashkit_st *hashk_ptr;
 
   hashk_ptr= hashkit_create(&hashk);
-  test_truth(hashk_ptr);
-  test_truth(hashk_ptr == &hashk);
-  test_truth(hashkit_is_initialized(&hashk) == true);
-  test_truth(hashkit_is_allocated(hashk_ptr) == false);
+  test_true(hashk_ptr);
+  test_true(hashk_ptr == &hashk);
+  test_true(hashkit_is_allocated(hashk_ptr) == false);
 
   hashkit_free(hashk_ptr);
 
-  test_truth(hashkit_is_initialized(&hashk) == false);
-
   return TEST_SUCCESS;
 }
 
@@ -52,9 +49,8 @@ static test_return_t allocation_test(void *not_used __attribute__((unused)))
   hashkit_st *hashk_ptr;
 
   hashk_ptr= hashkit_create(NULL);
-  test_truth(hashk_ptr);
-  test_truth(hashkit_is_allocated(hashk_ptr) == true);
-  test_truth(hashkit_is_initialized(hashk_ptr) == true);
+  test_true(hashk_ptr);
+  test_true(hashkit_is_allocated(hashk_ptr) == true);
   hashkit_free(hashk_ptr);
 
   return TEST_SUCCESS;
@@ -66,15 +62,13 @@ static test_return_t clone_test(hashkit_st *hashk)
   assert(&global_hashk == hashk);
 
   // Second we test if hashk is even valid
-  test_truth(hashkit_is_initialized(hashk) == true);
 
   /* All null? */
   {
     hashkit_st *hashk_ptr;
     hashk_ptr= hashkit_clone(NULL, NULL);
-    test_truth(hashk_ptr);
-    test_truth(hashkit_is_allocated(hashk_ptr) == true);
-    test_truth(hashkit_is_initialized(hashk_ptr) == true);
+    test_true(hashk_ptr);
+    test_true(hashkit_is_allocated(hashk_ptr));
     hashkit_free(hashk_ptr);
   }
 
@@ -84,23 +78,8 @@ static test_return_t clone_test(hashkit_st *hashk)
 
     hashk_ptr= hashkit_clone(NULL, hashk);
 
-    test_truth(hashk_ptr);
-    test_truth(hashkit_is_allocated(hashk_ptr) == true);
-    test_truth(hashkit_is_initialized(hashk_ptr) == true);
-
-    test_truth(hashk_ptr->distribution == hashk->distribution);
-    test_truth(hashk_ptr->continuum_count == hashk->continuum_count);
-    test_truth(hashk_ptr->continuum_points_count == hashk->continuum_points_count);
-    test_truth(hashk_ptr->list_size == hashk->list_size);
-    test_truth(hashk_ptr->context_size == hashk->context_size);
-    test_truth(hashk_ptr->continuum == NULL);
-    test_truth(hashk_ptr->hash_fn == hashk->hash_fn);
-    test_truth(hashk_ptr->active_fn == hashk->active_fn);
-    test_truth(hashk_ptr->continuum_hash_fn == hashk->continuum_hash_fn);
-    test_truth(hashk_ptr->continuum_key_fn == hashk->continuum_key_fn);
-    test_truth(hashk_ptr->sort_fn == hashk->sort_fn);
-    test_truth(hashk_ptr->weight_fn == hashk->weight_fn);
-    test_truth(hashk_ptr->list == hashk->list);
+    test_true(hashk_ptr);
+    test_true(hashkit_is_allocated(hashk_ptr));
 
     hashkit_free(hashk_ptr);
   }
@@ -111,7 +90,9 @@ static test_return_t clone_test(hashkit_st *hashk)
     hashkit_st *hash_clone;
 
     hash_clone= hashkit_clone(&declared_clone, NULL);
-    test_truth(hash_clone);
+    test_true(hash_clone);
+    test_true(hash_clone == &declared_clone);
+    test_false(hashkit_is_allocated(hash_clone));
 
     hashkit_free(hash_clone);
   }
@@ -120,15 +101,33 @@ static test_return_t clone_test(hashkit_st *hashk)
   {
     hashkit_st declared_clone;
     hashkit_st *hash_clone;
-    memset(&declared_clone, 0 , sizeof(hashkit_st));
+
     hash_clone= hashkit_clone(&declared_clone, hashk);
-    test_truth(hash_clone);
+    test_true(hash_clone);
+    test_true(hash_clone == &declared_clone);
+    test_false(hashkit_is_allocated(hash_clone));
+
     hashkit_free(hash_clone);
   }
 
   return TEST_SUCCESS;
 }
 
+static test_return_t one_at_a_time_run (hashkit_st *hashk __attribute__((unused)))
+{
+  uint32_t x;
+  const char **ptr;
+
+  for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+  {
+    uint32_t hash_val;
+
+    hash_val= hashkit_one_at_a_time(*ptr, strlen(*ptr));
+    test_true(one_at_a_time_values[x] == hash_val);
+  }
+
+  return TEST_SUCCESS;
+}
 
 static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
 {
@@ -140,7 +139,7 @@ static test_return_t md5_run (hashkit_st *hashk __attribute__((unused)))
     uint32_t hash_val;
 
     hash_val= hashkit_md5(*ptr, strlen(*ptr));
-    test_truth(md5_values[x] == hash_val);
+    test_true(md5_values[x] == hash_val);
   }
 
   return TEST_SUCCESS;
@@ -249,6 +248,9 @@ static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
 
 static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
 {
+#ifdef __sparc
+  return TEST_SKIPPED;
+#else
   uint32_t x;
   const char **ptr;
 
@@ -261,6 +263,7 @@ static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
   }
 
   return TEST_SUCCESS;
+#endif
 }
 
 static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
@@ -294,7 +297,128 @@ test_st allocation[]= {
   {0, 0, 0}
 };
 
+static test_return_t hashkit_generate_value_test(hashkit_st *hashk)
+{
+  uint32_t value;
+  value= hashkit_generate_value(hashk, "a", sizeof("a"));
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_set_base_function_test(hashkit_st *hashk)
+{
+  for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++) 
+  {
+    hashkit_return_t rc;
+    uint32_t x;
+    const char **ptr;
+    uint32_t *list;
+
+    rc= hashkit_set_base_function(hashk, algo);
+
+    /* Hsieh is disabled most of the time for patent issues */
+    if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
+      continue;
+
+    test_true(rc == HASHKIT_SUCCESS);
+
+    switch (algo)
+    {
+    case HASHKIT_HASH_DEFAULT:
+      list= one_at_a_time_values;
+      break;
+    case HASHKIT_HASH_MD5:
+      list= md5_values;
+      break;
+    case HASHKIT_HASH_CRC:
+      list= crc_values;
+      break;
+    case HASHKIT_HASH_FNV1_64:
+      list= fnv1_64_values;
+      break;
+    case HASHKIT_HASH_FNV1A_64:
+      list= fnv1a_64_values;
+      break;
+    case HASHKIT_HASH_FNV1_32:
+      list= fnv1_32_values;
+      break;
+    case HASHKIT_HASH_FNV1A_32:
+      list= fnv1a_32_values;
+      break;
+    case HASHKIT_HASH_HSIEH:
+      list= hsieh_values;
+      break;
+    case HASHKIT_HASH_MURMUR:
+      list= murmur_values;
+      break;
+    case HASHKIT_HASH_JENKINS:
+      list= jenkins_values;
+      break;
+    case HASHKIT_HASH_MAX:
+    default:
+      list= NULL;
+      test_fail("We ended up on a non-existent hash");
+    }
+
+    // Now we make sure we did set the hash correctly.
+    for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+    {
+      uint32_t hash_val;
+
+      hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr));
+      test_true(list[x] == hash_val);
+    }
+  }
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_set_base_function_custom_test(hashkit_st *hashk)
+{
+  hashkit_return_t rc;
+  uint32_t x;
+  const char **ptr;
+
+
+  rc= hashkit_set_base_function_custom(hashk, hashkit_md5, NULL);
+  test_true(rc == HASHKIT_SUCCESS);
+
+  for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+  {
+    uint32_t hash_val;
+
+    hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr));
+    test_true(md5_values[x] == hash_val);
+  }
+
+  return TEST_SUCCESS;
+}
+
+test_st hashkit_st_functions[] ={
+  {"hashkit_generate_value", 0, (test_callback_fn)hashkit_generate_value_test},
+  {"hashkit_set_base_function", 0, (test_callback_fn)hashkit_set_base_function_test},
+  {"hashkit_set_base_function_custom", 0, (test_callback_fn)hashkit_set_base_function_custom_test},
+  {0, 0, 0}
+};
+
+static test_return_t libhashkit_generate_value_test(hashkit_st *hashk)
+{
+  uint32_t value;
+
+  (void)hashk;
+
+  value= libhashkit_generate_value("a", sizeof("a"), HASHKIT_HASH_DEFAULT);
+
+  return TEST_SUCCESS;
+}
+
+test_st library_functions[] ={
+  {"libhashkit_generate_value", 0, (test_callback_fn)libhashkit_generate_value_test},
+  {0, 0, 0}
+};
+
 test_st hash_tests[] ={
+  {"one_at_a_time", 0, (test_callback_fn)one_at_a_time_run },
   {"md5", 0, (test_callback_fn)md5_run },
   {"crc", 0, (test_callback_fn)crc_run },
   {"fnv1_64", 0, (test_callback_fn)fnv1_64_run },
@@ -319,27 +443,36 @@ test_st regression[]= {
 
 collection_st collection[] ={
   {"allocation", 0, 0, allocation},
-  {"regression", 0, 0, regression},
+  {"hashkit_st_functions", 0, 0, hashkit_st_functions},
+  {"library_functions", 0, 0, library_functions},
   {"hashing", 0, 0, hash_tests},
+  {"regression", 0, 0, regression},
   {0, 0, 0, 0}
 };
 
 /* Prototypes for functions we will pass to test framework */
-void *world_create(void);
+void *world_create(test_return_t *error);
 test_return_t world_destroy(hashkit_st *hashk);
 
-void *world_create(void)
+void *world_create(test_return_t *error)
 {
   hashkit_st *hashk_ptr;
 
   hashk_ptr= hashkit_create(&global_hashk);
 
-  assert(hashk_ptr == &global_hashk);
+  if (hashk_ptr != &global_hashk)
+  {
+    *error= TEST_FAILURE;
+    return NULL;
+  }
+
+  if (hashkit_is_allocated(hashk_ptr) == true)
+  {
+    *error= TEST_FAILURE;
+    return NULL;
+  }
 
-  // First we test if hashk is even valid
-  assert(hashkit_is_initialized(hashk_ptr) == true);
-  assert(hashkit_is_allocated(hashk_ptr) == false);
-  assert(hashk_ptr->continuum == NULL);
+  *error= TEST_SUCCESS;
 
   return hashk_ptr;
 }
@@ -348,7 +481,6 @@ void *world_create(void)
 test_return_t world_destroy(hashkit_st *hashk)
 {
   // Did we get back what we expected?
-  assert(hashkit_is_initialized(hashk) == true);
   assert(hashkit_is_allocated(hashk) == false);
   hashkit_free(&global_hashk);