File: hashtable_test.cpp

package info (click to toggle)
pd-vstplugin 0.6.1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 2,008 kB
  • sloc: cpp: 22,783; lisp: 2,860; makefile: 37; sh: 26
file content (80 lines) | stat: -rw-r--r-- 2,380 bytes parent folder | download | duplicates (2)
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
72
73
74
75
76
77
78
79
80
#include "HashTable.h"

#include <cstdlib>
#include <iostream>
#include <random>
#include <string>
#include <string_view>
#include <unordered_map>

constexpr size_t iterCount = 100;
constexpr size_t elementCount = 10000;
constexpr size_t maxStringSize = 64;

std::string randomString(std::mt19937& mt) {
    std::uniform_int_distribution<size_t> d(0, maxStringSize);
    std::uniform_int_distribution<int> d2(0, 127);
    std::string result;

    auto size = d(mt);
    for (int i = 0; i < size; ++i) {
        result.push_back(d2(mt));
    }
    // std::cout << "randomString: " << result << std::endl;
    return result;
}

int randomInt(std::mt19937& mt) {
    std::uniform_int_distribution<int> d;
    return d(mt);
}

int main(int argc, const char *argv[]) {
    std::random_device rd;
    std::mt19937 mt(rd());

    for (int i = 0; i < iterCount; ++i) {
        std::unordered_map<std::string, int> source;
        vst::HashTable<std::string, int, std::string_view> dest;

        for (int i = 0; i < elementCount; ++i) {
            source.emplace(randomString(mt), randomInt(mt));
        }

        for (auto& [key, value] : source) {
            if (!dest.insert(key, value)) {
                std::cout << "could not insert key '" << key << "'!" << std::endl;
                return EXIT_FAILURE;
            }
        }

        for (auto& [key, value] : source) {
            auto result = dest.find(key);
            if (!result) {
                std::cout << "could not find key '" << key << "'!" << std::endl;
                return EXIT_FAILURE;
            }
            if (*result != value) {
                std::cout << "values (" << value << ", " << *result << ") do not match!" << std::endl;
                return EXIT_FAILURE;
            }
        }

        for (int i = 0; i < elementCount; ++i) {
            // make a key that is not contained in 'source'
            std::string key;
            do {
                key = randomString(mt);
            } while (source.count(key));

            if (dest.find(key)) {
                std::cout << "found key '" << key << "' that has not been inserted!" << std::endl;
                return EXIT_FAILURE;
            }
        }
    }

    std::cout << "all tests succeeded!" << std::endl;

    return EXIT_SUCCESS;
}