gtest_any.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2023 Gaël Écorchard, KM Robotics s.r.o. - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <charconv> // std::{from_chars,from_chars_result},
14 #include <string>
15 #include <system_error> // std::errc.
16 
17 #include <gtest/gtest.h>
18 
20 
21 using namespace BT;
22 
23 TEST(Any, Empty)
24 {
25  {
26  Any a;
27  EXPECT_TRUE(a.empty());
28  }
29 
30  {
31  Any a(42);
32  EXPECT_FALSE(a.empty());
33  }
34 }
35 
36 TEST(Any, IsType)
37 {
38  // Boolean.
39  {
40  Any a(true);
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>());
46  }
47 
48  // Signed int.
49  {
50  Any a(42);
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>());
56  }
57 
58  // unsigned int.
59  {
60  Any a(42u);
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>());
66  }
67 
68  // Double.
69  {
70  Any a(42.0);
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>());
76  }
77 
78  // String.
79  {
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>());
86  }
87 }
88 
89 TEST(Any, Cast)
90 {
91  // Boolean.
92  {
93  Any a(true);
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");
99 
100  Any b(false);
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");
106  }
107 
108  // Signed int.
109  {
110  Any a(42);
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");
115 
116  Any b(-43);
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");
121 
122  Any c(0);
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");
128 
129  Any d(1);
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");
135  }
136 
137  // unsigned int.
138  {
139  Any a(43u);
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");
145 
146  Any b(0u);
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");
153 
154  Any c(1u);
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");
161  }
162 
163  // Double.
164  {
165  Any a(44.0);
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>();
172  double a_val;
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);
177 #endif
178 
179  Any b(44.1);
180  EXPECT_TRUE(b.cast<bool>());
181  EXPECT_EQ(typeid(b.cast<bool>()), typeid(bool));
182  // EXPECT_EQ(b.cast<int>(), 44);?
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>();
187  double b_val;
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);
192 #endif
193 
194  Any c(44.9);
195  EXPECT_TRUE(c.cast<bool>());
196  // EXPECT_EQ(c.cast<int>(), 44);?
197  EXPECT_ANY_THROW(auto res = c.cast<int>());
198  EXPECT_EQ(c.cast<double>(), 44.9);
199 
200  Any d(-45.0);
201  EXPECT_TRUE(c.cast<bool>());
202  // EXPECT_EQ(c.cast<int>(), -45);?
203  EXPECT_ANY_THROW(auto res = c.cast<int>());
204 
205  Any e(0.0);
206  EXPECT_FALSE(e.cast<bool>());
207  EXPECT_EQ(e.cast<int>(), 0);
208  }
209 
210  // String.
211  {
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");
217 
218  Any b(std::string{ "true" });
219  // EXPECT_TRUE(b.cast<bool>());?
220  EXPECT_ANY_THROW(auto res = b.cast<bool>());
221 
222  Any c(std::string{ "false" });
223  // EXPECT_FALSE(c.cast<bool>());?
224  EXPECT_ANY_THROW(auto res = c.cast<bool>());
225 
226  Any d(std::string{ "0" });
227  // EXPECT_FALSE(d.cast<bool>());?
228  EXPECT_EQ(d.cast<int>(), 0);
229  EXPECT_EQ(d.cast<double>(), 0.0);
230 
231  Any e(std::string{ "1" });
232  // EXPECT_TRUE(e.cast<bool>());?
233  EXPECT_EQ(e.cast<int>(), 1);
234  EXPECT_EQ(e.cast<double>(), 1.0);
235 
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);
240 
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);
245  }
246 
247  {
248  std::vector<int> v{ 1, 2, 3 };
249  Any a(v);
250  EXPECT_EQ(a.cast<std::vector<int>>(), v);
251  }
252 }
BT
Definition: ex01_wrap_legacy.cpp:29
BT::Any
Definition: safe_any.hpp:36
TEST
TEST(Any, Empty)
Definition: gtest_any.cpp:23
BT::Any::isType
bool isType() const
Definition: safe_any.hpp:131
BT::Any::empty
bool empty() const noexcept
Definition: safe_any.hpp:207
safe_any.hpp
BT::Any::cast
T cast() const
Definition: safe_any.hpp:146
f
static FILE * f
Definition: minitrace.cpp:74


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16