aboutsummaryrefslogtreecommitdiff
path: root/tests/compressed_vector.cpp
blob: a5d76e4a2e0194d2f2be4d23d18a44eb56d72e26 (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
72
#include "compressed_vector.hpp"

#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <vector>
#include <string>
#include <stdexcept>

int main()
{
    compressed_vector<uint8_t, 3> cv;
    compressed_vector<uint8_t, 3> cv2;
    std::vector<uint8_t> v;
    srand(time(NULL));
    for (int i = 0; i < 100; ++i)
    {
        int r = rand() % 8;
        cv.push_back(r);
        v.push_back(r);
    }
    for (int i = 0; i < 100; ++i)
    {
        if (cv.get(i) != v[i])
        {
            printf("%u <=> %u @ %d\n", cv.get(i), v[i], i);
            throw std::runtime_error(std::to_string(__LINE__));
        }
    }
    for (int i = 0; i < 1000; ++i)
    {
        if (rand() % 3)
        {
            int r = rand() % 8;
            cv.push_back(r);
            v.push_back(r);
        }
        else
        {
            if (cv.back() != v.back())
                throw std::runtime_error(std::to_string(__LINE__));
            cv.pop_back();
            v.pop_back();
        }
    }
    if (cv.size() != v.size())
        throw std::runtime_error(std::to_string(__LINE__));
    for (size_t i = 0; i < v.size(); ++i)
    {
        if (cv.get(i) != v[i])
        {
            printf("%u <=> %u @ %lu\n", cv.get(i), v[i], i);
            throw std::runtime_error(std::to_string(__LINE__));
        }
        cv2.push_back(cv.get(i));
    }
    for (size_t i = 0; i < v.size(); ++i)
    {
        if (cv.get(i) != cv2.get(i))
        {
            throw std::runtime_error(std::to_string(__LINE__));
        }
    }
    size_t h1 = compressed_vector_hash<uint8_t, 3>{}(cv);
    size_t h2 = compressed_vector_hash<uint8_t, 3>{}(cv2);
    if (h1 != h2)
    {
        printf("%lu <=> %lu\n", h1, h2);
        throw std::runtime_error(std::to_string(__LINE__));
    }
    return 0;
}