1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
C++ to libhashkit
*/
#include <config.h>
#include <libtest/test.hpp>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <libhashkit-1.0/hashkit.hpp>
using namespace libtest;
#include "tests/hash_results.h"
static test_return_t exists_test(void *)
{
Hashkit hashk;
(void)hashk;
return TEST_SUCCESS;
}
static test_return_t new_test(void *)
{
Hashkit *hashk= new Hashkit;
(void)hashk;
delete hashk;
return TEST_SUCCESS;
}
static test_return_t copy_test(void *)
{
Hashkit *hashk= new Hashkit;
Hashkit *copy(hashk);
(void)copy;
delete hashk;
return TEST_SUCCESS;
}
static test_return_t assign_test(void *)
{
Hashkit hashk;
Hashkit copy;
copy= hashk;
(void)copy;
return TEST_SUCCESS;
}
static test_return_t digest_test(void *)
{
Hashkit hashk;
test_true(hashk.digest("Foo", sizeof("Foo")));
return TEST_SUCCESS;
}
static test_return_t set_function_test(void *)
{
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
};
for (hashkit_hash_algorithm_t *algo= algo_list; *algo != HASHKIT_HASH_MAX; algo++)
{
hashkit_return_t rc= hashk.set_function(*algo);
if (rc == HASHKIT_INVALID_ARGUMENT)
{
continue;
}
test_compare(HASHKIT_SUCCESS, rc);
uint32_t *list;
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:
#ifdef WORDS_BIGENDIAN
continue;
#endif
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.
uint32_t x;
const char **ptr;
for (ptr= list_to_hash, x= 0; *ptr; ptr++, x++)
{
uint32_t hash_val;
hash_val= hashk.digest(*ptr, strlen(*ptr));
char buffer[1024];
snprintf(buffer, sizeof(buffer), "%lu %lus %s", (unsigned long)list[x], (unsigned long)hash_val, libhashkit_string_hash(*algo));
test_true_got(list[x] == hash_val, buffer);
}
}
return TEST_SUCCESS;
}
static test_return_t set_distribution_function_test(void *)
{
Hashkit hashk;
hashkit_return_t rc;
rc= hashk.set_distribution_function(HASHKIT_HASH_CUSTOM);
test_true_got(rc == HASHKIT_FAILURE or rc == HASHKIT_INVALID_ARGUMENT, hashkit_strerror(NULL, rc));
test_compare(HASHKIT_SUCCESS,
hashk.set_distribution_function(HASHKIT_HASH_JENKINS));
return TEST_SUCCESS;
}
static test_return_t compare_function_test(void *)
{
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, reinterpret_cast<test_callback_fn*>(exists_test) },
{ "new", 0, reinterpret_cast<test_callback_fn*>(new_test) },
{ "copy", 0, reinterpret_cast<test_callback_fn*>(copy_test) },
{ "assign", 0, reinterpret_cast<test_callback_fn*>(assign_test) },
{ "digest", 0, reinterpret_cast<test_callback_fn*>(digest_test) },
{ "set_function", 0, reinterpret_cast<test_callback_fn*>(set_function_test) },
{ "set_distribution_function", 0, reinterpret_cast<test_callback_fn*>(set_distribution_function_test) },
{ "compare", 0, reinterpret_cast<test_callback_fn*>(compare_function_test) },
{ 0, 0, 0}
};
collection_st collection[] ={
{"basic", 0, 0, basic},
{0, 0, 0, 0}
};
void get_world(Framework *world)
{
world->collections(collection);
}
|