forked from ad-freiburg/qlever
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParametersTest.cpp
134 lines (110 loc) · 4.5 KB
/
ParametersTest.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Created by johannes on 07.06.21.
//
#include <gtest/gtest.h>
#include "util/MemorySize/MemorySize.h"
#include "util/Parameters.h"
using namespace ad_utility;
using namespace memory_literals;
using namespace detail::parameterShortNames;
using namespace std::chrono_literals;
TEST(Parameters, First) {
using FloatParameter = Float<"Float">;
using IntParameter = SizeT<"SizeT">;
using DoubleParameter = Double<"Double">;
using BoolParameter = Bool<"Bool">;
using BoolParameter2 = Bool<"Bool2">; // to test both results of toString
Parameters pack2(FloatParameter{2.0f}, IntParameter{3ull},
DoubleParameter{42.1}, BoolParameter{true},
BoolParameter2{true});
ASSERT_EQ(3ul, pack2.get<"SizeT">());
ASSERT_FLOAT_EQ(2.0, pack2.get<"Float">());
ASSERT_DOUBLE_EQ(42.1, pack2.get<"Double">());
ASSERT_TRUE(pack2.get<"Bool">());
pack2.set("Float", "42.0");
ASSERT_EQ(3ul, pack2.get<"SizeT">());
ASSERT_FLOAT_EQ(42.0, pack2.get<"Float">());
pack2.set("Double", "16.2");
ASSERT_DOUBLE_EQ(16.2, pack2.get<"Double">());
pack2.set("SizeT", "134");
ASSERT_EQ(pack2.get<"SizeT">(), 134);
pack2.set("Bool", "false");
ASSERT_FALSE(pack2.get<"Bool">());
ASSERT_THROW(pack2.set("NoKey", "24.1"), std::runtime_error);
// TODO<joka921>: Make this unit test work.
// ASSERT_THROW(pack2.set("Float", "24.nofloat1"), std::runtime_error);
auto map = pack2.toMap();
ASSERT_EQ(5ul, map.size());
ASSERT_EQ("134", map.at("SizeT"));
ASSERT_EQ("42.000000", map.at("Float"));
ASSERT_EQ("16.200000", map.at("Double"));
ASSERT_EQ("false", map.at("Bool"));
ASSERT_EQ("true", map.at("Bool2"));
}
// Basic test, if the parameter for `MemorySize` works.
TEST(Parameters, MemorySizeParameter) {
// Compare a given `MemorySizeParameter` with a given `MemorySize`.
auto compareWithMemorySize = [](const auto& parameter,
const MemorySize& expectedValue) {
ASSERT_EQ(expectedValue.getBytes(), parameter.get().getBytes());
ASSERT_STREQ(expectedValue.asString().c_str(),
parameter.toString().c_str());
};
MemorySizeParameter<"Memory"> m(6_GB);
compareWithMemorySize(m, 6_GB);
m.set(6_MB);
compareWithMemorySize(m, 6_MB);
m.setFromString("6 TB");
compareWithMemorySize(m, 6_TB);
// Test, if it works with `Parameters`.
Parameters pack(Float<"Float">{2.0f}, SizeT<"SizeT">{3ull},
Double<"Double">{42.1}, MemorySizeParameter<"Memory">{6_GB});
ASSERT_EQ(pack.get<"Memory">().getBytes(), (6_GB).getBytes());
pack.set("Memory", "6 MB");
ASSERT_EQ(pack.get<"Memory">().getBytes(), (6_MB).getBytes());
}
// Basic test, if the concept works.
TEST(Parameters, ParameterConcept) {
// Test the parameter short names.
static_assert(IsParameter<Float<"Float">>);
static_assert(IsParameter<Double<"Double">>);
static_assert(IsParameter<SizeT<"SizeT">>);
static_assert(IsParameter<String<"String">>);
static_assert(IsParameter<MemorySizeParameter<"MemorySizeParameter">>);
static_assert(IsParameter<Bool<"Bool">>);
static_assert(
IsParameter<DurationParameter<std::chrono::seconds, "Seconds">>);
// Test some other random types.
static_assert(!IsParameter<std::string>);
static_assert(!IsParameter<ParameterName>);
}
// _____________________________________________________________________________
TEST(Parameter, verifyParameterConstraint) {
Parameter<size_t, szt, toString, "test"> parameter{42};
EXPECT_NO_THROW(parameter.set(1337));
// Check constraint is tested for existing value
EXPECT_THROW(parameter.setParameterConstraint(
[](const auto& value, std::string_view name) {
EXPECT_EQ(value, 1337);
EXPECT_EQ(name, "test");
throw std::runtime_error{"Test"};
}),
std::runtime_error);
// Assert constraint was not set
EXPECT_NO_THROW(parameter.set(0));
EXPECT_EQ(parameter.get(), 0);
parameter.setParameterConstraint([](const auto& value, std::string_view) {
if (value != 0) {
throw std::runtime_error{"Test"};
}
});
EXPECT_THROW(parameter.set(1), std::runtime_error);
EXPECT_EQ(parameter.get(), 0);
}
// _____________________________________________________________________________
TEST(Parameter, verifyDurationParameterSerializationWorks) {
DurationParameter<std::chrono::seconds, "Seconds"> durationParameter{0s};
EXPECT_EQ(durationParameter.toString(), "0s");
durationParameter.setFromString("10s");
EXPECT_EQ(durationParameter.get(), 10s);
}