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