Fix for using correct memcached binary.
[awesomized/libmemcached] / tests / hashkit_functions.c
index 2990b2331e1739e124414b291920c42ff54e6687..2dad25fa95ed2838b5e9833ed0c800b386d958a1 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= libhashkit_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)))
 {
@@ -139,8 +138,8 @@ 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);
+    hash_val= libhashkit_md5(*ptr, strlen(*ptr));
+    test_true(md5_values[x] == hash_val);
   }
 
   return TEST_SUCCESS;
@@ -155,7 +154,7 @@ static test_return_t crc_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_crc32(*ptr, strlen(*ptr));
+    hash_val= libhashkit_crc32(*ptr, strlen(*ptr));
     assert(crc_values[x] == hash_val);
   }
 
@@ -171,7 +170,7 @@ static test_return_t fnv1_64_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_fnv1_64(*ptr, strlen(*ptr));
+    hash_val= libhashkit_fnv1_64(*ptr, strlen(*ptr));
     assert(fnv1_64_values[x] == hash_val);
   }
 
@@ -187,7 +186,7 @@ static test_return_t fnv1a_64_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_fnv1a_64(*ptr, strlen(*ptr));
+    hash_val= libhashkit_fnv1a_64(*ptr, strlen(*ptr));
     assert(fnv1a_64_values[x] == hash_val);
   }
 
@@ -204,7 +203,7 @@ static test_return_t fnv1_32_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_fnv1_32(*ptr, strlen(*ptr));
+    hash_val= libhashkit_fnv1_32(*ptr, strlen(*ptr));
     assert(fnv1_32_values[x] == hash_val);
   }
 
@@ -220,7 +219,7 @@ static test_return_t fnv1a_32_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_fnv1a_32(*ptr, strlen(*ptr));
+    hash_val= libhashkit_fnv1a_32(*ptr, strlen(*ptr));
     assert(fnv1a_32_values[x] == hash_val);
   }
 
@@ -237,7 +236,7 @@ static test_return_t hsieh_run (hashkit_st *hashk __attribute__((unused)))
     uint32_t hash_val;
 
 #ifdef HAVE_HSIEH_HASH
-    hash_val= hashkit_hsieh(*ptr, strlen(*ptr));
+    hash_val= libhashkit_hsieh(*ptr, strlen(*ptr));
 #else
     hash_val= 1;
 #endif
@@ -259,7 +258,7 @@ static test_return_t murmur_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_murmur(*ptr, strlen(*ptr));
+    hash_val= libhashkit_murmur(*ptr, strlen(*ptr));
     assert(murmur_values[x] == hash_val);
   }
 
@@ -277,7 +276,7 @@ static test_return_t jenkins_run (hashkit_st *hashk __attribute__((unused)))
   {
     uint32_t hash_val;
 
-    hash_val= hashkit_jenkins(*ptr, strlen(*ptr));
+    hash_val= libhashkit_jenkins(*ptr, strlen(*ptr));
     assert(jenkins_values[x] == hash_val);
   }
 
@@ -298,7 +297,210 @@ test_st allocation[]= {
   {0, 0, 0}
 };
 
+static test_return_t hashkit_digest_test(hashkit_st *hashk)
+{
+  uint32_t value;
+  value= hashkit_digest(hashk, "a", sizeof("a"));
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_set_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_function(hashk, algo);
+
+    /* Hsieh is disabled most of the time for patent issues */
+    if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
+      continue;
+
+    if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
+      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_CUSTOM:
+    case HASHKIT_HASH_MAX:
+    default:
+      list= NULL;
+      break;
+    }
+
+    // Now we make sure we did set the hash correctly.
+    if (list)
+    {
+      for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+      {
+        uint32_t hash_val;
+
+        hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
+        test_true(list[x] == hash_val);
+      }
+    }
+    else
+    {
+      return TEST_FAILURE;
+    }
+  }
+
+  return TEST_SUCCESS;
+}
+
+static uint32_t hash_test_function(const char *string, size_t string_length, void *context)
+{
+  (void)context;
+  return libhashkit_md5(string, string_length);
+}
+
+static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk)
+{
+  hashkit_return_t rc;
+  uint32_t x;
+  const char **ptr;
+
+
+  rc= hashkit_set_custom_function(hashk, hash_test_function, NULL);
+  test_true(rc == HASHKIT_SUCCESS);
+
+  for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
+  {
+    uint32_t hash_val;
+
+    hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr));
+    test_true(md5_values[x] == hash_val);
+  }
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_set_distribution_function_test(hashkit_st *hashk)
+{
+  for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++) 
+  {
+    hashkit_return_t rc;
+
+    rc= hashkit_set_distribution_function(hashk, algo);
+
+    /* Hsieh is disabled most of the time for patent issues */
+    if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_HSIEH)
+      continue;
+
+    if (rc == HASHKIT_FAILURE && algo == HASHKIT_HASH_CUSTOM)
+      continue;
+
+    test_true(rc == HASHKIT_SUCCESS);
+  }
+
+  return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_set_custom_distribution_function_test(hashkit_st *hashk)
+{
+  hashkit_return_t rc;
+
+  rc= hashkit_set_custom_distribution_function(hashk, hash_test_function, NULL);
+  test_true(rc == HASHKIT_SUCCESS);
+
+  return TEST_SUCCESS;
+}
+
+
+static test_return_t hashkit_get_function_test(hashkit_st *hashk)
+{
+  for (hashkit_hash_algorithm_t algo = HASHKIT_HASH_DEFAULT; algo < HASHKIT_HASH_MAX; algo++) 
+  {
+    hashkit_return_t rc;
+
+    if (HASHKIT_HASH_CUSTOM || HASHKIT_HASH_HSIEH)
+      continue;
+
+    rc= hashkit_set_function(hashk, algo);
+    test_true(rc == HASHKIT_SUCCESS);
+
+    test_true(hashkit_get_function(hashk) == algo);
+  }
+  return TEST_SUCCESS;
+}
+
+static test_return_t hashkit_compare_test(hashkit_st *hashk)
+{
+  hashkit_st *clone;
+
+  clone= hashkit_clone(NULL, hashk);
+
+  test_true(hashkit_compare(clone, hashk));
+  hashkit_free(clone);
+
+  return TEST_SUCCESS;
+}
+
+test_st hashkit_st_functions[] ={
+  {"hashkit_digest", 0, (test_callback_fn)hashkit_digest_test},
+  {"hashkit_set_function", 0, (test_callback_fn)hashkit_set_function_test},
+  {"hashkit_set_custom_function", 0, (test_callback_fn)hashkit_set_custom_function_test},
+  {"hashkit_get_function", 0, (test_callback_fn)hashkit_get_function_test},
+  {"hashkit_set_distribution_function", 0, (test_callback_fn)hashkit_set_distribution_function_test},
+  {"hashkit_set_custom_distribution_function", 0, (test_callback_fn)hashkit_set_custom_distribution_function_test},
+  {"hashkit_compare", 0, (test_callback_fn)hashkit_compare_test},
+  {0, 0, 0}
+};
+
+static test_return_t libhashkit_digest_test(hashkit_st *hashk)
+{
+  uint32_t value;
+
+  (void)hashk;
+
+  value= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT);
+
+  return TEST_SUCCESS;
+}
+
+test_st library_functions[] ={
+  {"libhashkit_digest", 0, (test_callback_fn)libhashkit_digest_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 },
@@ -323,8 +525,10 @@ 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}
 };
 
@@ -344,25 +548,12 @@ void *world_create(test_return_t *error)
     return NULL;
   }
 
-  // First we test if hashk is even valid
-  if (hashkit_is_initialized(hashk_ptr) == false)
-  {
-    *error= TEST_FAILURE;
-    return NULL;
-  }
-
   if (hashkit_is_allocated(hashk_ptr) == true)
   {
     *error= TEST_FAILURE;
     return NULL;
   }
 
-  if (hashk_ptr->continuum != NULL)
-  {
-    *error= TEST_FAILURE;
-    return NULL;
-  }
-
   *error= TEST_SUCCESS;
 
   return hashk_ptr;
@@ -372,7 +563,6 @@ void *world_create(test_return_t *error)
 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);