aboutsummaryrefslogtreecommitdiff
path: root/test/native/hash.cc
blob: 8aa0a35e32bab3132aa05d18f4ea1cf77d352e40 (plain) (blame)
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
// Copyright (c) 2022 Johannes Stoelp

#include <types.h>
#include <utils.h>

#include <filesystem>
#include <fstream>
#include <gtest/gtest.h>
#include <iostream>
#include <vector>

using ID = std::array<u8, 6>;
namespace fs = std::filesystem;

std::vector<ID> read_blob() {
    if (!fs::exists("blob")) {
        std::system("dd if=/dev/urandom of=blob count=16 bs=1M");
    }

    std::vector<ID> ids;
    ids.reserve(fs::file_size("blob") / sizeof(ID));

    auto ifs = std::ifstream("blob");
    assert(ifs.is_open());
    ID id;
    while (ifs.read((char*)id.data(), sizeof(id))) {
        ids.push_back(id);
    }

    return ids;
}

TEST(hash, uniform_distribuation) {
    constexpr usize BUCKETS = 64;
    constexpr float BUCKET_SIZE = static_cast<float>(100) / BUCKETS;  // Bucket size in percent.
    constexpr float BUCKET_ERR = BUCKET_SIZE * 0.05 /* 5% */;         // Allowed distribution error.

    const auto ids = read_blob();

    usize cnt[BUCKETS] = {0};
    for (const auto& id : ids) {
        u32 h = hash(id.data(), id.size());
        cnt[h % BUCKETS] += 1;
    }

    for (usize b = 0; b < BUCKETS; ++b) {
        const float dist = static_cast<float>(cnt[b]) / ids.size() * 100;
        ASSERT_GT(dist, BUCKET_SIZE - BUCKET_ERR);
        ASSERT_LT(dist, BUCKET_SIZE + BUCKET_ERR);
        // printf("bucket %2ld: %5.2f (%ld)\n", b, dist, cnt[b]);
    }
}

TEST(hash, DISABLED_collisions) {
    const auto ids = read_blob();

    std::unordered_map<u32, usize> hits;
    for (const auto& id : ids) {
        u32 h = hash(id.data(), id.size());
        hits[h] = hits[h] + 1;
    }

    usize collisions = 0;
    for (const auto& hit : hits) {
        if (hit.second > 1) {
            ++collisions;
        }
    }

    printf("Hashed %ld values got %ld collisions\n", ids.size(), collisions);
}