From dac48f2dbe34915755f8f4f7f88419f47dc9e27f Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Wed, 20 Jan 2010 18:06:18 -0800 Subject: [PATCH] C++ interface bits for libhashkit --- .bzrignore | 3 + libhashkit/common.h | 13 ++- libhashkit/digest.c | 56 +++++++++++ libhashkit/digest.h | 30 ++++++ libhashkit/function.c | 47 --------- libhashkit/function.h | 10 -- libhashkit/hashkit.c | 9 ++ libhashkit/hashkit.h | 90 ++++++++++++++--- libhashkit/include.am | 16 ++-- libhashkit/types.h | 4 + libmemcached/hash.c | 4 +- libmemcached/hosts.c | 4 +- libmemcached/memcached.c | 4 +- libmemcached/memcached.h | 128 +++++++++++++++++++++---- libmemcached/types.h | 2 +- tests/hash_plus.cc | 197 ++++++++++++++++++++++++++++++++++++++ tests/hashkit_functions.c | 16 ++-- tests/include.am | 25 ++++- 18 files changed, 539 insertions(+), 119 deletions(-) create mode 100644 libhashkit/digest.c create mode 100644 libhashkit/digest.h create mode 100644 tests/hash_plus.cc diff --git a/.bzrignore b/.bzrignore index 81f3b402..c78c4048 100644 --- a/.bzrignore +++ b/.bzrignore @@ -174,3 +174,6 @@ tests/testapp tests/testhashkit tests/testplus tests/udptest +tests/hashplus +patch +patch2 diff --git a/libhashkit/common.h b/libhashkit/common.h index 0a7ac845..b2aaf0e2 100644 --- a/libhashkit/common.h +++ b/libhashkit/common.h @@ -6,14 +6,13 @@ * the COPYING file in the parent directory for full text. */ -/** - * @file - * @brief System Include Files - */ - #ifndef HASHKIT_COMMON_H #define HASHKIT_COMMON_H +#ifdef __cplusplus +extern "C" { +#endif + #include "config.h" #include @@ -30,4 +29,8 @@ void md5_signature(const unsigned char *key, unsigned int length, unsigned char HASHKIT_LOCAL int update_continuum(hashkit_st *hashkit); +#ifdef __cplusplus +} +#endif + #endif /* HASHKIT_COMMON_H */ diff --git a/libhashkit/digest.c b/libhashkit/digest.c new file mode 100644 index 00000000..bca6b5b5 --- /dev/null +++ b/libhashkit/digest.c @@ -0,0 +1,56 @@ +/* HashKit + * Copyright (C) 2010 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + */ + +#include "common.h" + +uint32_t hashkit_digest(const hashkit_st *self, const char *key, size_t key_length) +{ + return self->base_hash.function(key, key_length, self->base_hash.context); +} + +uint32_t libhashkit_digest(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm) +{ + switch (hash_algorithm) + { + case HASHKIT_HASH_DEFAULT: + return libhashkit_one_at_a_time(key, key_length); + case HASHKIT_HASH_MD5: + return libhashkit_md5(key, key_length); + case HASHKIT_HASH_CRC: + return libhashkit_crc32(key, key_length); + case HASHKIT_HASH_FNV1_64: + return libhashkit_fnv1_64(key, key_length); + case HASHKIT_HASH_FNV1A_64: + return libhashkit_fnv1a_64(key, key_length); + case HASHKIT_HASH_FNV1_32: + return libhashkit_fnv1_32(key, key_length); + case HASHKIT_HASH_FNV1A_32: + return libhashkit_fnv1a_32(key, key_length); + case HASHKIT_HASH_HSIEH: +#ifdef HAVE_HSIEH_HASH + return libhashkit_hsieh(key, key_length); +#else + return 1; +#endif + case HASHKIT_HASH_MURMUR: + return libhashkit_murmur(key, key_length); + case HASHKIT_HASH_JENKINS: + return libhashkit_jenkins(key, key_length); + case HASHKIT_HASH_CUSTOM: + case HASHKIT_HASH_MAX: + default: +#ifdef HAVE_DEBUG + fprintf(stderr, "hashkit_hash_t was extended but libhashkit_generate_value was not updated\n"); + fflush(stderr); + assert(0); +#endif + break; + } + + return 1; +} diff --git a/libhashkit/digest.h b/libhashkit/digest.h new file mode 100644 index 00000000..271ddfcd --- /dev/null +++ b/libhashkit/digest.h @@ -0,0 +1,30 @@ +/* HashKit + * Copyright (C) 2010 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + */ + +#ifndef HASHKIT_DIGEST_H +#define HASHKIT_DIGEST_H + +#ifdef __cplusplus +extern "C" { +#endif + +HASHKIT_API +uint32_t hashkit_digest(const hashkit_st *self, const char *key, size_t key_length); + +/** + This is a utilitly function provided so that you can directly access hashes with a hashkit_st. +*/ + +HASHKIT_API +uint32_t libhashkit_digest(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm); + +#ifdef __cplusplus +} +#endif + +#endif /* HASHKIT_DIGEST_H */ diff --git a/libhashkit/function.c b/libhashkit/function.c index 445859d2..2b22590b 100644 --- a/libhashkit/function.c +++ b/libhashkit/function.c @@ -8,11 +8,6 @@ #include "common.h" -uint32_t hashkit_generate_value(const hashkit_st *self, const char *key, size_t key_length) -{ - return self->base_hash.function(key, key_length, self->base_hash.context); -} - static hashkit_return_t _set_function(struct hashkit_function_st *self, hashkit_hash_algorithm_t hash_algorithm) { switch (hash_algorithm) @@ -153,45 +148,3 @@ hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *sel { return get_function_type(self->distribution_hash.function); } - -uint32_t libhashkit_generate_value(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm) -{ - switch (hash_algorithm) - { - case HASHKIT_HASH_DEFAULT: - return libhashkit_one_at_a_time(key, key_length); - case HASHKIT_HASH_MD5: - return libhashkit_md5(key, key_length); - case HASHKIT_HASH_CRC: - return libhashkit_crc32(key, key_length); - case HASHKIT_HASH_FNV1_64: - return libhashkit_fnv1_64(key, key_length); - case HASHKIT_HASH_FNV1A_64: - return libhashkit_fnv1a_64(key, key_length); - case HASHKIT_HASH_FNV1_32: - return libhashkit_fnv1_32(key, key_length); - case HASHKIT_HASH_FNV1A_32: - return libhashkit_fnv1a_32(key, key_length); - case HASHKIT_HASH_HSIEH: -#ifdef HAVE_HSIEH_HASH - return libhashkit_hsieh(key, key_length); -#else - return 1; -#endif - case HASHKIT_HASH_MURMUR: - return libhashkit_murmur(key, key_length); - case HASHKIT_HASH_JENKINS: - return libhashkit_jenkins(key, key_length); - case HASHKIT_HASH_CUSTOM: - case HASHKIT_HASH_MAX: - default: -#ifdef HAVE_DEBUG - fprintf(stderr, "hashkit_hash_t was extended but libhashkit_generate_value was not updated\n"); - fflush(stderr); - assert(0); -#endif - break; - } - - return 1; -} diff --git a/libhashkit/function.h b/libhashkit/function.h index 8a550ec6..56fcc795 100644 --- a/libhashkit/function.h +++ b/libhashkit/function.h @@ -13,9 +13,6 @@ extern "C" { #endif -HASHKIT_API -uint32_t hashkit_generate_value(const hashkit_st *self, const char *key, size_t key_length); - /** This sets/gets the default function we will be using. */ @@ -40,13 +37,6 @@ hashkit_return_t hashkit_set_custom_distribution_function(hashkit_st *self, hash HASHKIT_API hashkit_hash_algorithm_t hashkit_get_distribution_function(const hashkit_st *self); -/** - This is a utilitly function provided so that you can directly access hashes with a hashkit_st. -*/ - -HASHKIT_API -uint32_t libhashkit_generate_value(const char *key, size_t key_length, hashkit_hash_algorithm_t hash_algorithm); - #ifdef __cplusplus } #endif diff --git a/libhashkit/hashkit.c b/libhashkit/hashkit.c index 47c9e580..c58ab69f 100644 --- a/libhashkit/hashkit.c +++ b/libhashkit/hashkit.c @@ -106,3 +106,12 @@ bool hashkit_compare(const hashkit_st *first, const hashkit_st *second) return false; } + +#ifdef __cplusplus +hashkit_st operator=(const hashkit_st& source) +{ + hashkit_clone(this, &source); + + return *this; +} +#endif diff --git a/libhashkit/hashkit.h b/libhashkit/hashkit.h index 1def040d..fe6c148d 100644 --- a/libhashkit/hashkit.h +++ b/libhashkit/hashkit.h @@ -1,5 +1,5 @@ /* HashKit - * Copyright (C) 2009 Brian Aker + * Copyright (C) 2009-2010 Brian Aker * All rights reserved. * * Use and distribution licensed under the BSD license. See @@ -9,6 +9,7 @@ #ifndef HASHKIT_H #define HASHKIT_H + #if !defined(__cplusplus) # include #endif @@ -18,13 +19,32 @@ #include #include #include +#include #include #include #ifdef __cplusplus + +#include + extern "C" { #endif +HASHKIT_API +hashkit_st *hashkit_create(hashkit_st *hash); + +HASHKIT_API +hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *ptr); + +HASHKIT_API +bool hashkit_compare(const hashkit_st *first, const hashkit_st *second); + +HASHKIT_API +void hashkit_free(hashkit_st *hash); + +#define hashkit_is_allocated(__object) ((__object)->options.is_allocated) +#define hashkit_is_initialized(__object) ((__object)->options.is_initialized) + struct hashkit_st { struct hashkit_function_st { @@ -39,27 +59,69 @@ struct hashkit_st struct { bool is_allocated:1; } options; -}; -HASHKIT_API -hashkit_st *hashkit_create(hashkit_st *hash); +#ifdef __cplusplus -HASHKIT_API -hashkit_st *hashkit_clone(hashkit_st *destination, const hashkit_st *ptr); + hashkit_st() : + base_hash(), + distribution_hash(), + flags(), + options() + { + hashkit_create(this); + } -HASHKIT_API -bool hashkit_compare(const hashkit_st *first, const hashkit_st *second); + hashkit_st(const hashkit_st& source) : + base_hash(), + distribution_hash(), + flags(), + options() + { + hashkit_clone(this, &source); + } -HASHKIT_API -void hashkit_free(hashkit_st *hash); + hashkit_st& operator=(const hashkit_st& source) + { + hashkit_clone(this, &source); -#define hashkit_is_allocated(__object) ((__object)->options.is_allocated) -#define hashkit_is_initialized(__object) ((__object)->options.is_initialized) + return *this; + } + + friend bool operator==(const hashkit_st &left, const hashkit_st &right) + { + return hashkit_compare(&left, &right); + } + + uint32_t digest(std::string& str) + { + return hashkit_digest(this, str.c_str(), str.length()); + } + + uint32_t digest(const char *key, size_t key_length) + { + return hashkit_digest(this, key, key_length); + } + + hashkit_return_t set_function(hashkit_hash_algorithm_t hash_algorithm) + { + return hashkit_set_function(this, hash_algorithm); + } -/** @} */ + hashkit_return_t set_distribution_function(hashkit_hash_algorithm_t hash_algorithm) + { + return hashkit_set_function(this, hash_algorithm); + } + + ~hashkit_st() + { + hashkit_free(this); + } +#endif +}; #ifdef __cplusplus -} +} // extern "C" #endif + #endif /* HASHKIT_H */ diff --git a/libhashkit/include.am b/libhashkit/include.am index 97aac9da..57819852 100644 --- a/libhashkit/include.am +++ b/libhashkit/include.am @@ -12,13 +12,14 @@ lib_LTLIBRARIES+= libhashkit/libhashkit.la nobase_include_HEADERS+= \ - libhashkit/algorithm.h \ - libhashkit/behavior.h \ - libhashkit/function.h \ - libhashkit/hashkit.h \ - libhashkit/strerror.h \ - libhashkit/types.h \ - libhashkit/visibility.h + libhashkit/algorithm.h \ + libhashkit/behavior.h \ + libhashkit/digest.h \ + libhashkit/function.h \ + libhashkit/hashkit.h \ + libhashkit/strerror.h \ + libhashkit/types.h \ + libhashkit/visibility.h noinst_HEADERS+= \ libhashkit/common.h @@ -28,6 +29,7 @@ libhashkit_libhashkit_la_SOURCES= \ libhashkit/behavior.c \ libhashkit/crc32.c \ libhashkit/fnv.c \ + libhashkit/digest.c \ libhashkit/function.c \ libhashkit/hashkit.c \ libhashkit/jenkins.c \ diff --git a/libhashkit/types.h b/libhashkit/types.h index b2e5f798..6b7e29b2 100644 --- a/libhashkit/types.h +++ b/libhashkit/types.h @@ -11,6 +11,9 @@ #define HASHKIT_TYPES_H #ifdef __cplusplus + +typedef struct hashkit_st Hashkit; + extern "C" { #endif @@ -49,6 +52,7 @@ typedef enum typedef struct hashkit_st hashkit_st; + typedef uint32_t (*hashkit_hash_fn)(const char *key, size_t key_length, void *context); #ifdef __cplusplus diff --git a/libmemcached/hash.c b/libmemcached/hash.c index e6458c25..392155c0 100644 --- a/libmemcached/hash.c +++ b/libmemcached/hash.c @@ -3,7 +3,7 @@ uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm) { - return libhashkit_generate_value(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm); + return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm); } uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length) @@ -16,7 +16,7 @@ uint32_t generate_hash(memcached_st *ptr, const char *key, size_t key_length) if (memcached_server_count(ptr) == 1) return 0; - hash= hashkit_generate_value(&ptr->hashkit, key, key_length); + hash= hashkit_digest(&ptr->hashkit, key, key_length); WATCHPOINT_ASSERT(hash); return hash; diff --git a/libmemcached/hosts.c b/libmemcached/hosts.c index 6853845a..ea02e7d4 100644 --- a/libmemcached/hosts.c +++ b/libmemcached/hosts.c @@ -222,7 +222,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } else { - value= hashkit_generate_value(&ptr->distribution_hashkit, sort_host, sort_host_length); + value= hashkit_digest(&ptr->distribution_hashkit, sort_host, sort_host_length); ptr->continuum[continuum_index].index= host_index; ptr->continuum[continuum_index++].value= value; } @@ -266,7 +266,7 @@ static memcached_return_t update_continuum(memcached_st *ptr) } else { - value= hashkit_generate_value(&ptr->distribution_hashkit, sort_host, sort_host_length); + value= hashkit_digest(&ptr->distribution_hashkit, sort_host, sort_host_length); ptr->continuum[continuum_index].index= host_index; ptr->continuum[continuum_index++].value= value; } diff --git a/libmemcached/memcached.c b/libmemcached/memcached.c index be084199..6df260c6 100644 --- a/libmemcached/memcached.c +++ b/libmemcached/memcached.c @@ -184,7 +184,7 @@ void memcached_free(memcached_st *ptr) If source is NULL the call is the same as if a memcached_create() was called. */ -memcached_st *memcached_clone(memcached_st *clone, memcached_st *source) +memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source) { memcached_return_t rc= MEMCACHED_SUCCESS; memcached_st *new_clone; @@ -271,7 +271,7 @@ memcached_st *memcached_clone(memcached_st *clone, memcached_st *source) } if (source->on_clone) - source->on_clone(source, new_clone); + source->on_clone(new_clone, source); return new_clone; } diff --git a/libmemcached/memcached.h b/libmemcached/memcached.h index a98a8c82..62d3b1dc 100644 --- a/libmemcached/memcached.h +++ b/libmemcached/memcached.h @@ -56,6 +56,25 @@ extern "C" { #endif +LIBMEMCACHED_API +void memcached_servers_reset(memcached_st *ptr); + +LIBMEMCACHED_API +memcached_st *memcached_create(memcached_st *ptr); + +LIBMEMCACHED_API +void memcached_free(memcached_st *ptr); + +LIBMEMCACHED_API +memcached_st *memcached_clone(memcached_st *clone, const memcached_st *ptr); + +LIBMEMCACHED_API +void *memcached_get_user_data(const memcached_st *ptr); + +LIBMEMCACHED_API +void *memcached_set_user_data(memcached_st *ptr, void *data); + + struct memcached_st { /** @note these are static and should not change without a call to behavior. @@ -127,27 +146,104 @@ struct memcached_st { struct { bool is_allocated:1; } options; -}; -LIBMEMCACHED_API -void memcached_servers_reset(memcached_st *ptr); +#ifdef __cplusplus + memcached_st() : + state(), + flags(), + distribution(), + hashkit(), + continuum_points_counter(), + number_of_hosts(), + servers(), + last_disconnected_server(), + snd_timeout(), + rcv_timeout(), + server_failure_limit(), + io_msg_watermark(), + io_bytes_watermark(), + io_key_prefetch(), + cached_errno(), + poll_timeout(), + connect_timeout(), + retry_timeout(), + continuum_count(), + send_size(), + recv_size(), + user_data(), + next_distribution_rebuild(), + prefix_key_length(), + number_of_replicas(), + distribution_hashkit(), + result(), + continuum(), + allocators(), + on_clone(), + on_cleanup(), + get_key_failure(), + delete_trigger(), + callbacks(), + prefix_key(), + options() + { + memcached_create(this); + } -/* Public API */ + ~memcached_st() + { + memcached_free(this); + } -LIBMEMCACHED_API -memcached_st *memcached_create(memcached_st *ptr); + memcached_st(const memcached_st& source) : + state(), + flags(), + distribution(), + hashkit(), + continuum_points_counter(), + number_of_hosts(), + servers(), + last_disconnected_server(), + snd_timeout(), + rcv_timeout(), + server_failure_limit(), + io_msg_watermark(), + io_bytes_watermark(), + io_key_prefetch(), + cached_errno(), + poll_timeout(), + connect_timeout(), + retry_timeout(), + continuum_count(), + send_size(), + recv_size(), + user_data(), + next_distribution_rebuild(), + prefix_key_length(), + number_of_replicas(), + distribution_hashkit(), + result(), + continuum(), + allocators(), + on_clone(), + on_cleanup(), + get_key_failure(), + delete_trigger(), + callbacks(), + prefix_key(), + options() + { + memcached_clone(this, &source); + } -LIBMEMCACHED_API -void memcached_free(memcached_st *ptr); + memcached_st& operator=(const memcached_st& source) + { + memcached_clone(this, &source); -LIBMEMCACHED_API -memcached_st *memcached_clone(memcached_st *clone, memcached_st *ptr); + return *this; + } -LIBMEMCACHED_API -void *memcached_get_user_data(const memcached_st *ptr); - -LIBMEMCACHED_API -void *memcached_set_user_data(memcached_st *ptr, void *data); +#endif +}; // Local Only Inline static inline memcached_server_st *memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key) @@ -157,7 +253,7 @@ static inline memcached_server_st *memcached_server_instance_fetch(memcached_st #ifdef __cplusplus -} +} // extern "C" #endif diff --git a/libmemcached/types.h b/libmemcached/types.h index 28182cd9..0c78eb30 100644 --- a/libmemcached/types.h +++ b/libmemcached/types.h @@ -24,7 +24,7 @@ typedef struct memcached_string_st memcached_string_st; typedef struct memcached_server_st memcached_server_st; typedef struct memcached_continuum_item_st memcached_continuum_item_st; -typedef memcached_return_t (*memcached_clone_fn)(memcached_st *parent, memcached_st *clone); +typedef memcached_return_t (*memcached_clone_fn)(memcached_st *destination, const memcached_st *source); typedef memcached_return_t (*memcached_cleanup_fn)(const memcached_st *ptr); /** diff --git a/tests/hash_plus.cc b/tests/hash_plus.cc new file mode 100644 index 00000000..cbf2fdf9 --- /dev/null +++ b/tests/hash_plus.cc @@ -0,0 +1,197 @@ +/* + C++ to libhashkit +*/ +#include "test.h" +#include +#include +#include + +#include + +#include "hash_results.h" + +static test_return_t exists_test(void *obj __attribute__((unused))) +{ + Hashkit hashk; + + return TEST_SUCCESS; +} + +static test_return_t new_test(void *obj __attribute__((unused))) +{ + Hashkit *hashk= new Hashkit; + + (void)hashk; + + delete hashk; + + return TEST_SUCCESS; +} + +static test_return_t copy_test(void *obj __attribute__((unused))) +{ + Hashkit *hashk= new Hashkit; + Hashkit *copy(hashk); + + (void)copy; + + delete hashk; + + return TEST_SUCCESS; +} + +static test_return_t assign_test(void *obj __attribute__((unused))) +{ + Hashkit hashk; + Hashkit copy; + + copy= hashk; + + (void)copy; + + return TEST_SUCCESS; +} + +static test_return_t digest_test(void *obj __attribute__((unused))) +{ + Hashkit hashk; + uint32_t value; + + value= hashk.digest("Foo", sizeof("Foo")); + + return TEST_SUCCESS; +} + +static test_return_t set_function_test(void *obj __attribute__((unused))) +{ + Hashkit hashk; + hashkit_hash_algorithm_t algo_list[]= { + HASHKIT_HASH_DEFAULT, + HASHKIT_HASH_MD5, + HASHKIT_HASH_CRC, + HASHKIT_HASH_FNV1_64, + HASHKIT_HASH_FNV1A_64, + HASHKIT_HASH_FNV1_32, + HASHKIT_HASH_FNV1A_32, + HASHKIT_HASH_MURMUR, + HASHKIT_HASH_JENKINS, + HASHKIT_HASH_MAX + }; + hashkit_hash_algorithm_t *algo; + + + for (algo= algo_list; *algo != HASHKIT_HASH_MAX; algo++) + { + hashkit_return_t rc; + uint32_t x; + const char **ptr; + uint32_t *list; + + rc= hashk.set_function(*algo); + + 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; + 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= hashk.digest(*ptr, strlen(*ptr)); + test_true(list[x] == hash_val); + } + } + + return TEST_SUCCESS; +} + +static test_return_t set_distribution_function_test(void *obj __attribute__((unused))) +{ + Hashkit hashk; + hashkit_return_t rc; + + rc= hashk.set_distribution_function(HASHKIT_HASH_CUSTOM); + test_true(rc == HASHKIT_FAILURE); + + rc= hashk.set_distribution_function(HASHKIT_HASH_JENKINS); + test_true(rc == HASHKIT_SUCCESS); + + return TEST_SUCCESS; +} + +static test_return_t compare_function_test(void *obj __attribute__((unused))) +{ + Hashkit a, b; + + b= a; + + test_true(a == b); + + b.set_function(HASHKIT_HASH_MURMUR); + + test_false(a == b); + test_true(b == b); + test_true(a == a); + + return TEST_SUCCESS; +} + +test_st basic[] ={ + { "exists", 0, exists_test }, + { "new", 0, new_test }, + { "copy", 0, copy_test }, + { "assign", 0, assign_test }, + { "digest", 0, digest_test }, + { "set_function", 0, set_function_test }, + { "set_distribution_function", 0, set_distribution_function_test }, + { "compare", 0, compare_function_test }, + { 0, 0, 0} +}; + +collection_st collection[] ={ + {"basic", 0, 0, basic}, + {0, 0, 0, 0} +}; + +void get_world(world_st *world) +{ + world->collections= collection; +} diff --git a/tests/hashkit_functions.c b/tests/hashkit_functions.c index 2642f76a..1d548f4a 100644 --- a/tests/hashkit_functions.c +++ b/tests/hashkit_functions.c @@ -297,10 +297,10 @@ test_st allocation[]= { {0, 0, 0} }; -static test_return_t hashkit_generate_value_test(hashkit_st *hashk) +static test_return_t hashkit_digest_test(hashkit_st *hashk) { uint32_t value; - value= hashkit_generate_value(hashk, "a", sizeof("a")); + value= hashkit_digest(hashk, "a", sizeof("a")); return TEST_SUCCESS; } @@ -369,7 +369,7 @@ static test_return_t hashkit_set_function_test(hashkit_st *hashk) { uint32_t hash_val; - hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr)); + hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr)); test_true(list[x] == hash_val); } } @@ -397,7 +397,7 @@ static test_return_t hashkit_set_custom_function_test(hashkit_st *hashk) { uint32_t hash_val; - hash_val= hashkit_generate_value(hashk, *ptr, strlen(*ptr)); + hash_val= hashkit_digest(hashk, *ptr, strlen(*ptr)); test_true(md5_values[x] == hash_val); } @@ -465,7 +465,7 @@ static test_return_t hashkit_compare_test(hashkit_st *hashk) } test_st hashkit_st_functions[] ={ - {"hashkit_generate_value", 0, (test_callback_fn)hashkit_generate_value_test}, + {"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}, @@ -475,19 +475,19 @@ test_st hashkit_st_functions[] ={ {0, 0, 0} }; -static test_return_t libhashkit_generate_value_test(hashkit_st *hashk) +static test_return_t libhashkit_digest_test(hashkit_st *hashk) { uint32_t value; (void)hashk; - value= libhashkit_generate_value("a", sizeof("a"), HASHKIT_HASH_DEFAULT); + value= libhashkit_digest("a", sizeof("a"), HASHKIT_HASH_DEFAULT); return TEST_SUCCESS; } test_st library_functions[] ={ - {"libhashkit_generate_value", 0, (test_callback_fn)libhashkit_generate_value_test}, + {"libhashkit_digest", 0, (test_callback_fn)libhashkit_digest_test}, {0, 0, 0} }; diff --git a/tests/include.am b/tests/include.am index bbfaebd5..f1d00ef0 100644 --- a/tests/include.am +++ b/tests/include.am @@ -36,6 +36,7 @@ noinst_HEADERS+= \ noinst_PROGRAMS+= \ tests/atomsmasher \ + tests/hashplus \ tests/startservers \ tests/testapp \ tests/testhashkit \ @@ -77,6 +78,10 @@ tests_testhashkit_SOURCES = tests/hashkit_functions.c tests_testhashkit_LDADD = tests/libtest.la libhashkit/libhashkit.la tests_testhashkit_DEPENDENCIES = $(tests_testhashkit_LDADD) +tests_hashplus_SOURCES = tests/hash_plus.cc +tests_hashplus_LDADD = $(tests_testhashkit_LDADD) +tests_hashplus_DEPENDENCIES = $(tests_testhashkit_LDADD) + test: test-docs test-mem test-hash memcapable echo "Tests completed" @@ -199,7 +204,9 @@ MEMSLAP_COMMAND= clients/memslap $(COLLECTION) $(SUITE) MEM_COMMAND= tests/testapp $(COLLECTION) $(SUITE) -PLUS_COMMAND= tests/testplus $(COLLECTION) $(SUITE) +MEMPLUS_COMMAND= tests/testplus $(COLLECTION) $(SUITE) + +HASHPLUS_COMMAND= tests/hashplus $(COLLECTION) $(SUITE) ATOM_COMMAND= tests/atomsmasher $(COLLECTION) $(SUITE) @@ -212,15 +219,17 @@ test-atom: tests/atomsmasher $(ATOM_COMMAND) test-plus: tests/testplus - $(PLUS_COMMAND) + $(MEMPLUS_COMMAND) test-hash: tests/testhashkit $(HASH_COMMAND) +test-hashplus: tests/hashplus + $(HASHPLUS_COMMAND) + pahole-mem: tests/testapp $(PAHOLE_COMMAND) $(MEM_COMMAND) - gdb-mem: tests/testapp $(DEBUG_COMMAND) $(MEM_COMMAND) @@ -228,11 +237,14 @@ gdb-atom: tests/atomsmasher $(DEBUG_COMMAND) $(ATOM_COMMAND) gdb-plus: tests/testplus - $(DEBUG_COMMAND) $(PLUS_COMMAND) + $(DEBUG_COMMAND) $(MEMPLUS_COMMAND) gdb-hash: tests/testhashkit $(DEBUG_COMMAND) $(HASH_COMMAND) +gdb-hashplus: tests/hashplus + $(DEBUG_COMMAND) $(HASHPLUS_COMMAND) + gdb-memslap: clients/memslap $(DEBUG_COMMAND) $(MEMSLAP_COMMAND) @@ -243,11 +255,14 @@ valgrind-atom: tests/atomsmasher $(VALGRIND_COMMAND) $(ATOM_COMMAND) valgrind-plus: tests/testplus - $(VALGRIND_COMMAND) $(PLUS_COMMAND) + $(VALGRIND_COMMAND) $(MEMPLUS_COMMAND) valgrind-hash: tests/testhashkit $(VALGRIND_COMMAND) $(HASH_COMMAND) +valgrind-hashplus: tests/hashplus + $(VALGRIND_COMMAND) $(HASHPLUS_COMMAND) + valgrind-memslap: clients/memslap $(VALGRIND_COMMAND) $(MEMSLAP_COMMAND) -- 2.30.2