15 #include <system_error>
17 #include <gtest/gtest.h>
27 EXPECT_TRUE(a.
empty());
32 EXPECT_FALSE(a.
empty());
41 EXPECT_TRUE(a.
isType<
bool>());
42 EXPECT_FALSE(a.
isType<
int>());
43 EXPECT_FALSE(a.
isType<
unsigned int>());
44 EXPECT_FALSE(a.
isType<
double>());
45 EXPECT_FALSE(a.
isType<std::string>());
51 EXPECT_FALSE(a.
isType<
bool>());
52 EXPECT_TRUE(a.
isType<
int>());
53 EXPECT_FALSE(a.
isType<
unsigned int>());
54 EXPECT_FALSE(a.
isType<
double>());
55 EXPECT_FALSE(a.
isType<std::string>());
61 EXPECT_FALSE(a.
isType<
bool>());
62 EXPECT_FALSE(a.
isType<
int>());
63 EXPECT_TRUE(a.
isType<
unsigned int>());
64 EXPECT_FALSE(a.
isType<
double>());
65 EXPECT_FALSE(a.
isType<std::string>());
71 EXPECT_FALSE(a.
isType<
bool>());
72 EXPECT_FALSE(a.
isType<
int>());
73 EXPECT_FALSE(a.
isType<
unsigned int>());
74 EXPECT_TRUE(a.
isType<
double>());
75 EXPECT_FALSE(a.
isType<std::string>());
80 Any a(std::string{
"forty-two" });
81 EXPECT_FALSE(a.isType<
bool>());
82 EXPECT_FALSE(a.isType<
int>());
83 EXPECT_FALSE(a.isType<
unsigned int>());
84 EXPECT_FALSE(a.isType<
double>());
85 EXPECT_TRUE(a.isType<std::string>());
94 EXPECT_TRUE(a.
cast<
bool>());
95 EXPECT_EQ(
typeid(a.
cast<
bool>()),
typeid(
bool));
96 EXPECT_EQ(a.
cast<
int>(), 1);
97 EXPECT_EQ(a.
cast<
bool>(), 1.0);
98 EXPECT_EQ(a.
cast<std::string>(),
"1");
101 EXPECT_FALSE(b.
cast<
bool>());
102 EXPECT_EQ(
typeid(b.
cast<
bool>()),
typeid(
bool));
103 EXPECT_EQ(b.
cast<
int>(), 0);
104 EXPECT_EQ(b.
cast<
double>(), 0.0);
105 EXPECT_EQ(b.
cast<std::string>(),
"0");
111 EXPECT_ANY_THROW(
auto res = a.
cast<
bool>());
112 EXPECT_EQ(a.
cast<
int>(), 42);
113 EXPECT_EQ(a.
cast<
double>(), 42.0);
114 EXPECT_EQ(a.
cast<std::string>(),
"42");
117 EXPECT_ANY_THROW(
auto res = b.
cast<
bool>());
118 EXPECT_EQ(b.
cast<
int>(), -43);
119 EXPECT_EQ(b.
cast<
double>(), -43.0);
120 EXPECT_EQ(b.
cast<std::string>(),
"-43");
123 EXPECT_FALSE(c.
cast<
bool>());
124 EXPECT_EQ(
typeid(c.
cast<
bool>()),
typeid(
bool));
125 EXPECT_EQ(c.
cast<
int>(), 0);
126 EXPECT_EQ(c.
cast<
double>(), 0.0);
127 EXPECT_EQ(c.
cast<std::string>(),
"0");
130 EXPECT_TRUE(d.
cast<
bool>());
131 EXPECT_EQ(
typeid(d.
cast<
bool>()),
typeid(
bool));
132 EXPECT_EQ(d.
cast<
int>(), 1);
133 EXPECT_EQ(d.
cast<
double>(), 1.0);
134 EXPECT_EQ(d.
cast<std::string>(),
"1");
140 EXPECT_ANY_THROW(
auto res = a.
cast<
bool>());
141 EXPECT_EQ(a.
cast<
unsigned int>(), 43u);
142 EXPECT_EQ(a.
cast<
int>(), 43);
143 EXPECT_EQ(a.
cast<
double>(), 43.0);
144 EXPECT_EQ(a.
cast<std::string>(),
"43");
147 EXPECT_FALSE(b.
cast<
bool>());
148 EXPECT_EQ(
typeid(b.
cast<
bool>()),
typeid(
bool));
149 EXPECT_EQ(b.
cast<
unsigned int>(), 0u);
150 EXPECT_EQ(b.
cast<
int>(), 0);
151 EXPECT_EQ(b.
cast<
double>(), 0.0);
152 EXPECT_EQ(b.
cast<std::string>(),
"0");
155 EXPECT_TRUE(c.
cast<
bool>());
156 EXPECT_EQ(
typeid(c.
cast<
bool>()),
typeid(
bool));
157 EXPECT_EQ(c.
cast<
unsigned int>(), 1u);
158 EXPECT_EQ(c.
cast<
int>(), 1);
159 EXPECT_EQ(c.
cast<
double>(), 1.0);
160 EXPECT_EQ(c.
cast<std::string>(),
"1");
166 EXPECT_TRUE(a.
cast<
bool>());
167 EXPECT_EQ(
typeid(a.
cast<
bool>()),
typeid(
bool));
168 EXPECT_EQ(a.
cast<
int>(), 44);
169 EXPECT_EQ(a.
cast<
double>(), 44.0);
170 #if __cpp_lib_to_chars >= 201611L
171 const std::string a_str = a.
cast<std::string>();
173 const auto res = std::from_chars(a_str.data(), a_str.data() + a_str.size(), a_val,
174 std::chars_format::general);
175 EXPECT_TRUE(res.ec == std::errc{});
176 EXPECT_DOUBLE_EQ(a_val, 44.0);
180 EXPECT_TRUE(b.
cast<
bool>());
181 EXPECT_EQ(
typeid(b.
cast<
bool>()),
typeid(
bool));
183 EXPECT_ANY_THROW(
auto res = b.
cast<
int>());
184 EXPECT_EQ(b.
cast<
double>(), 44.1);
185 #if __cpp_lib_to_chars >= 201611L
186 const std::string b_str = b.
cast<std::string>();
188 const auto res2 = std::from_chars(b_str.data(), b_str.data() + b_str.size(), b_val,
189 std::chars_format::general);
190 EXPECT_TRUE(res2.ec == std::errc{});
191 EXPECT_DOUBLE_EQ(b_val, 44.1);
195 EXPECT_TRUE(c.
cast<
bool>());
197 EXPECT_ANY_THROW(
auto res = c.
cast<
int>());
198 EXPECT_EQ(c.
cast<
double>(), 44.9);
201 EXPECT_TRUE(c.
cast<
bool>());
203 EXPECT_ANY_THROW(
auto res = c.
cast<
int>());
206 EXPECT_FALSE(e.
cast<
bool>());
207 EXPECT_EQ(e.
cast<
int>(), 0);
212 Any a(std::string{
"fifty" });
213 EXPECT_ANY_THROW(
auto res = a.cast<
bool>());
214 EXPECT_ANY_THROW(
auto res = a.cast<
int>());
215 EXPECT_ANY_THROW(
auto res = a.cast<
double>());
216 EXPECT_EQ(a.cast<std::string>(),
"fifty");
218 Any b(std::string{
"true" });
220 EXPECT_ANY_THROW(
auto res = b.cast<
bool>());
222 Any c(std::string{
"false" });
224 EXPECT_ANY_THROW(
auto res = c.cast<
bool>());
226 Any d(std::string{
"0" });
228 EXPECT_EQ(d.cast<
int>(), 0);
229 EXPECT_EQ(d.cast<
double>(), 0.0);
231 Any e(std::string{
"1" });
233 EXPECT_EQ(e.cast<
int>(), 1);
234 EXPECT_EQ(e.cast<
double>(), 1.0);
236 Any f(std::string{
"51" });
237 EXPECT_ANY_THROW(
auto res =
f.cast<
bool>());
238 EXPECT_EQ(
f.cast<
int>(), 51);
239 EXPECT_EQ(
f.cast<
double>(), 51.0);
241 Any g(std::string{
"51.1" });
242 EXPECT_ANY_THROW(
auto res = g.cast<
bool>());
243 EXPECT_EQ(g.cast<
int>(), 51);
244 EXPECT_DOUBLE_EQ(g.cast<
double>(), 51.1);
248 std::vector<int> v{ 1, 2, 3 };
250 EXPECT_EQ(a.
cast<std::vector<int>>(), v);