flush
[awesomized/libmemcached] / testing / tests / hashkit / basic.cpp
1 #include "testing/lib/common.hpp"
2 #include "testing/fixtures/hashes.hpp"
3
4 #include "libhashkit-1.0/hashkit.hpp"
5
6 TEST_CASE("hashkit") {
7 hashkit_st st, *hp = hashkit_create(nullptr);
8 Hashkit stack;
9 Hashkit *heap = new Hashkit;
10
11 REQUIRE(hashkit_create(&st));
12 REQUIRE(hp);
13
14 SECTION("can copy") {
15 Hashkit stack_copy(stack);
16 Hashkit *heap_copy(heap);
17 hashkit_st st_copy, *st_ptr;
18
19 (void) stack_copy;
20 (void) heap_copy;
21
22 st_ptr = hashkit_clone(&st_copy, &st);
23 REQUIRE(st_ptr == &st_copy);
24 REQUIRE(hashkit_compare(st_ptr, &st_copy));
25
26 SUCCEED("OK");
27 }
28
29 SECTION("can assign") {
30 Hashkit stack_copy;
31
32 stack_copy = stack;
33 (void) stack_copy;
34
35 SUCCEED("OK");
36 }
37
38 SECTION("can digest default") {
39 REQUIRE(2297466611U == stack.digest(S("apple")));
40 REQUIRE(2297466611U == hashkit_digest(&st, S("apple")));
41 }
42
43 SECTION("can set hash function") {
44 for (int f = HASHKIT_HASH_DEFAULT; f < HASHKIT_HASH_MAX; ++f) {
45 auto h = static_cast<hashkit_hash_algorithm_t>(f);
46
47 if (h == HASHKIT_HASH_CUSTOM) {
48 continue;
49 }
50 if (!libhashkit_has_algorithm(h)) {
51 WARN("hashkit algorithm not enabled: " << libhashkit_string_hash(h) << " (" << f << ")");
52 continue;
53 }
54
55 INFO("hash: " << libhashkit_string_hash(h));
56
57 REQUIRE(HASHKIT_SUCCESS == stack.set_function(h));
58 REQUIRE(HASHKIT_SUCCESS == hashkit_set_function(&st, h));
59
60 DYNAMIC_SECTION("can digest hash function: " << libhashkit_string_hash(h)) {
61 auto n = 0;
62
63 for (auto i : input) {
64 CHECK(output[f][n] == stack.digest(S(i)));
65 CHECK(output[f][n] == hashkit_digest(&st, S(i)));
66 CHECK(output[f][n] == libhashkit_digest(S(i), h));
67 ++n;
68 }
69 }
70 }
71 }
72
73 SECTION("is comparable") {
74 REQUIRE(*heap == stack);
75 REQUIRE(hashkit_compare(&st, hp));
76
77 stack.set_function(HASHKIT_HASH_MD5);
78 hashkit_set_function(&st, HASHKIT_HASH_MD5);
79
80 REQUIRE_FALSE(*heap == stack);
81 REQUIRE_FALSE(hashkit_compare(&st, hp));
82 }
83
84 delete heap;
85 hashkit_free(&st);
86 hashkit_free(hp);
87 }