-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_performance.cpp
105 lines (86 loc) · 2.83 KB
/
test_performance.cpp
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <gtest/gtest.h>
#include <random>
#include "const_mapper.hpp"
using namespace const_mapper;
namespace {
static constexpr auto loop = 10000000;
}
TEST(Performance, ref_std_unordered_map) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution distrib(0, 9);
const auto map = std::unordered_map<std::uint8_t, int>{
{0, 0}, {1, -1}, {2, -2}, {3, -3}, {4, -4}, {5, -5}, {6, -6}, {7, -7}, {8, -8}, {9, -9},
};
for (auto j = 0; j < loop; ++j) {
auto i = distrib(gen);
auto expected = -i;
auto value = map.at(static_cast<std::uint8_t>(i));
EXPECT_EQ(value, expected);
}
}
TEST(Performance, ref_std_map) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution distrib(0, 9);
const auto map = std::map<std::uint8_t, int>{
{0, 0}, {1, -1}, {2, -2}, {3, -3}, {4, -4}, {5, -5}, {6, -6}, {7, -7}, {8, -8}, {9, -9},
};
for (auto j = 0; j < loop; ++j) {
auto i = distrib(gen);
auto expected = -i;
auto value = map.at(static_cast<std::uint8_t>(i));
EXPECT_EQ(value, expected);
}
}
TEST(Performance, const_mapper_to) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution distrib(0, 9);
constexpr auto map = ConstMapper<10, std::uint8_t, int>{
{{{0, 0}, {1, -1}, {2, -2}, {3, -3}, {4, -4}, {5, -5}, {6, -6}, {7, -7}, {8, -8}, {9, -9}}}};
for (auto j = 0; j < loop; ++j) {
auto i = distrib(gen);
auto expected = -i;
auto value = map.to<int, std::uint8_t>(static_cast<std::uint8_t>(i));
EXPECT_EQ(value, expected);
}
}
TEST(Performance, const_mapper_pattern_match) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution distrib(0, 9);
constexpr auto map = ConstMapper<10, std::uint8_t, int>{
{{{0, 0}, {1, -1}, {2, -2}, {3, -3}, {4, -4}, {5, -5}, {6, -6}, {7, -7}, {8, -8}, {9, -9}}}};
for (auto j = 0; j < loop; ++j) {
auto i = distrib(gen);
auto expected = -i;
auto key = static_cast<std::uint8_t>(i);
auto value = map.pattern_match(std::make_tuple(key, Result{}));
EXPECT_EQ(value, expected);
}
}
TEST(Performance, const_mapper_pattern_match_pick_up_2_values) {
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution distrib(0, 9);
constexpr auto map = ConstMapper<10, std::uint8_t, std::uint16_t, int>{{{
{0, 0, 0},
{1, 2, -1},
{2, 4, -2},
{3, 6, -3},
{4, 8, -4},
{5, 10, -5},
{6, 12, -6},
{7, 14, -7},
{8, 16, -8},
{9, 18, -9},
}}};
for (auto j = 0; j < loop; ++j) {
auto i = distrib(gen);
auto expected = std::make_tuple<std::uint16_t, int>(static_cast<std::uint16_t>(2 * i), -i);
auto key = static_cast<std::uint8_t>(i);
auto value = map.pattern_match(std::make_tuple(key, Result{}, Result{}));
EXPECT_EQ(value, expected);
}
}