bin: consolidate clients
[awesomized/libmemcached] / test / tests / memcached / generate_hash.cpp
1 #include "test/lib/common.hpp"
2 #include "test/fixtures/hashes.hpp"
3
4 static constexpr const uint32_t md5_hosts[] = {4U, 1U, 0U, 1U, 4U, 2U, 0U, 3U, 0U, 0U, 3U, 1U, 0U, 0U, 1U, 3U, 0U, 0U, 0U, 3U, 1U, 0U, 4U, 4U, 3U, 1U};
5 static constexpr const uint32_t crc_hosts[] = {2U, 4U, 1U, 0U, 2U, 4U, 4U, 4U, 1U, 2U, 3U, 4U, 3U, 4U, 1U, 3U, 3U, 2U, 0U, 0U, 0U, 1U, 2U, 4U, 0U, 4U};
6 static constexpr const uint32_t *hosts[] = {nullptr, md5_hosts, crc_hosts};
7
8 TEST_CASE("memcached_generate_hash") {
9 MemcachedPtr memc(memcached(S("--server=localhost:1 --server=localhost:2 --server=localhost:3 --server=localhost:4 --server=localhost5 --DISTRIBUTION=modula")));
10
11 REQUIRE(*memc);
12
13 SECTION("generate hash value") {
14 for (int f = MEMCACHED_HASH_DEFAULT; f < MEMCACHED_HASH_MAX; ++f) {
15 auto h = static_cast<memcached_hash_t>(f);
16
17 if (h == MEMCACHED_HASH_CUSTOM) {
18 continue;
19 }
20 if (MEMCACHED_SUCCESS != memcached_behavior_set_key_hash(*memc, h)) {
21 WARN("hash algorithm not enabled: " << libmemcached_string_hash(h) << " (" << f << ")");
22 continue;
23 }
24
25 INFO("hash: " << libmemcached_string_hash(h));
26
27 auto n = 0;
28 for (auto i : input) {
29 CHECK(output[f][n] == memcached_generate_hash_value(S(i), h));
30 ++n;
31 }
32 }
33 }
34
35 SECTION("generate hash") {
36 auto hash = GENERATE(as<memcached_hash_t>{}, MEMCACHED_HASH_MD5, MEMCACHED_HASH_CRC);
37
38 INFO("hash: " << libmemcached_string_hash(hash));
39 REQUIRE(MEMCACHED_SUCCESS == memcached_behavior_set_key_hash(*memc, hash));
40
41 auto n = 0;
42 for (auto i : input) {
43 CHECK(hosts[hash][n] == memcached_generate_hash(*memc, S(i)));
44 ++n;
45 }
46 }
47 }