-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathunformat_test.cpp
160 lines (133 loc) · 2.93 KB
/
unformat_test.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include "unformat.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <random>
#include <limits>
TEST(Unformat, EmptyString)
{
{
std::string output("NotEmpty");
ay::unformat("", "{}", output);
ASSERT_EQ("", output);
}
{
std::string output("NotEmpty");
ay::unformat(" ", " {}", output);
ASSERT_EQ("", output);
}
{
std::string output("NotEmpty");
ay::unformat(" ", "{} ", output);
ASSERT_EQ("", output);
}
{
std::string output("NotEmpty");
ay::unformat(" ", " {} ", output);
ASSERT_EQ("", output);
}
}
TEST(Unformat, Basic)
{
// Basic test to extract a single integer
int output;
ay::unformat("4", "{}", output);
ASSERT_EQ(4, output);
ay::unformat(" 5 ", " {} ", output);
ASSERT_EQ(5, output);
ay::unformat(" 6", " {}", output);
ASSERT_EQ(6, output);
ay::unformat("7 ", "{} ", output);
ASSERT_EQ(7, output);
}
TEST(Unformat, TwoVars)
{
std::string name;
int age;
ay::unformat("Harry is 18 years old.", "{} is {} years old.", name, age);
ASSERT_EQ(std::string("Harry"), name);
ASSERT_EQ(18, age);
}
TEST(Unformat, ThreeVars)
{
std::string name;
int weight;
std::string supporting;
ay::unformat("gpmvdo (78) -> jjixrr, zacrh, smylfq, fdvtn", "{} ({}) -> {}", name, weight, supporting);
ASSERT_EQ(std::string("gpmvdo"), name);
ASSERT_EQ(std::string("jjixrr, zacrh, smylfq, fdvtn"), supporting);
ASSERT_EQ(78, weight);
}
namespace
{
constexpr static auto FUZZ_COUNT = 10000;
template <typename T>
void fuzzReal()
{
fuzz<T, std::uniform_real_distribution<T>>();
}
template <typename T>
void fuzzInt()
{
fuzz<T, std::uniform_int_distribution<T>>();
}
template <typename T, typename DISTRIBUTION>
void fuzz()
{
std::mt19937 engine;
DISTRIBUTION dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
constexpr auto format = ay::make_format("{}");
for (int i = 0; i < FUZZ_COUNT; i++)
{
const auto input = dist(engine);
T output;
std::stringstream stream;
stream << input;
ay::unformat(stream.str().c_str(), format, output);
T streamOutput;
stream >> streamOutput;
ASSERT_EQ(streamOutput, output) << i << ": Test that unformat and std::stringstream are equal at default precision";
ay::unformat(std::to_string(input).c_str(), format, output);
ASSERT_EQ(input, output) << i << ": Test that unformat is the exact lossless opposite of std::to_string";
}
}
}
TEST(Unformat, FuzzFloat)
{
fuzzReal<float>();
}
TEST(Unformat, FuzzDouble)
{
fuzzReal<double>();
}
TEST(Unformat, FuzzShort)
{
fuzzInt<short>();
}
TEST(Unformat, FuzzUnsignedShort)
{
fuzzInt<unsigned short>();
}
TEST(Unformat, FuzzInt)
{
fuzzInt<int>();
}
TEST(Unformat, FuzzUnsignedInt)
{
fuzzInt<unsigned int>();
}
TEST(Unformat, FuzzLong)
{
fuzzInt<long>();
}
TEST(Unformat, FuzzUnsignedLong)
{
fuzzInt<unsigned long>();
}
TEST(Unformat, FuzzLongLong)
{
fuzzInt<long long>();
}
TEST(Unformat, FuzzUnsignedLongLong)
{
fuzzInt<unsigned long long>();
}