File: fnv.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (50 lines) | stat: -rw-r--r-- 1,350 bytes parent folder | download
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
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE hash.fnv

#include "caf/hash/fnv.hpp"

#include "core-test.hpp"

#include <string>

using namespace caf;

using namespace std::string_literals;

template <class... Ts>
auto fnv32_hash(Ts&&... xs) {
  return hash::fnv<uint32_t>::compute(std::forward<Ts>(xs)...);
}

template <class... Ts>
auto fnv64_hash(Ts&&... xs) {
  return hash::fnv<uint64_t>::compute(std::forward<Ts>(xs)...);
}

CAF_TEST(FNV hashes build incrementally) {
  hash::fnv<uint32_t> f;
  CHECK_EQ(f.result, 0x811C9DC5u);
  f.value('a');
  CHECK_EQ(f.result, 0xE40C292Cu);
  f.value('b');
  CHECK_EQ(f.result, 0x4D2505CAu);
  f.value('c');
  CHECK_EQ(f.result, 0x1A47E90Bu);
  f.value('d');
  CHECK_EQ(f.result, 0xCE3479BDu);
}

CAF_TEST(FNV supports uint32 hashing) {
  CHECK_EQ(fnv32_hash(), 0x811C9DC5u);
  CHECK_EQ(fnv32_hash("abcd"s), 0xCE3479BDu);
  CHECK_EQ(fnv32_hash("C++ Actor Framework"s), 0x2FF91FE5u);
}

CAF_TEST(FNV supports uint64 hashing) {
  CHECK_EQ(fnv64_hash(), 0xCBF29CE484222325ull);
  CHECK_EQ(fnv64_hash("abcd"s), 0xFC179F83EE0724DDull);
  CHECK_EQ(fnv64_hash("C++ Actor Framework"s), 0xA229A760C3AF69C5ull);
}