MRPT  2.0.5
typename_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include <gtest/gtest.h>
13 #include <iostream>
14 #include <memory> // shared_ptr
15 
16 struct MyFooClass
17 {
19 };
20 namespace MyNS
21 {
22 struct MyBarClass
23 {
24 };
26 {
28 };
29 } // namespace MyNS
30 
33 
34 #define TST_FOR_TYPE(__TSTTYPE) \
35  EXPECT_STREQ(#__TSTTYPE, TTypeName<__TSTTYPE>::get().c_str())
36 
37 // templates with a "," in its name break all our and gtest macros:
38 #define TST_FOR_TYPE2(__TSTTYPE, __TSTTYPE2ndpart) \
39  if (std::string(#__TSTTYPE "," #__TSTTYPE2ndpart) != \
40  TTypeName<__TSTTYPE, __TSTTYPE2ndpart>::get().c_str()) \
41  GTEST_FAIL() << "Failed: " << #__TSTTYPE "," #__TSTTYPE2ndpart \
42  << "\n Computed type is: " \
43  << TTypeName<__TSTTYPE, __TSTTYPE2ndpart>::get().c_str() \
44  << endl;
45 
46 TEST(TTypeName, types2str)
47 {
48  using namespace mrpt::typemeta;
49  using namespace std;
50 
51  // Basic types:
52  TST_FOR_TYPE(int32_t);
53  TST_FOR_TYPE(uint8_t);
54  TST_FOR_TYPE(float);
55  TST_FOR_TYPE(double);
56 
57  // STL:
58  TST_FOR_TYPE(std::string);
59  TST_FOR_TYPE(std::vector<double>);
60  TST_FOR_TYPE(std::vector<int32_t>);
61  TST_FOR_TYPE(std::set<double>);
62  TST_FOR_TYPE(std::set<std::vector<double>>);
63 
64  TST_FOR_TYPE2(std::pair<int32_t, int32_t>);
65  TST_FOR_TYPE2(std::map<double, std::set<int32_t>>);
66  TST_FOR_TYPE2(std::array<double, 3>);
67 
68  // User-defined types:
72  TST_FOR_TYPE(std::vector<MyFooClass>);
73  TST_FOR_TYPE(std::set<MyNS::MyBarClass>);
74  TST_FOR_TYPE(std::set<MyNS::MyBarClass2>);
75  TST_FOR_TYPE2(std::vector<std::array<MyNS::MyBarClass, 10>>);
76 }
77 
78 TEST(TTypeName, types2str_shared_ptr)
79 {
80  using namespace mrpt::typemeta;
81 
84  EXPECT_STREQ(
85  "std::shared_ptr<MyFooClass>",
87 }
88 
89 TEST(TTypeName, types2stdstring)
90 {
91  using namespace mrpt::typemeta;
92  using namespace std;
93 
94  auto st1 = TTypeName<uint8_t>::get();
95  const std::string s1(st1.c_str());
96  const std::string s2("uint8_t");
97  EXPECT_STREQ(s1.c_str(), s2.c_str());
98 }
DECLARE_CUSTOM_TTYPENAME(MyFooClass)
STL namespace.
[example typename]
A template to obtain the type of its argument as a string at compile time.
Definition: TTypeName.h:69
TEST(TTypeName, types2str)
#define TST_FOR_TYPE2(__TSTTYPE, __TSTTYPE2ndpart)
#define TST_FOR_TYPE(__TSTTYPE)
#define DECLARE_TTYPENAME_CLASSNAME(_CLASSNAME)
Like DECLARE_CUSTOM_TTYPENAME(), but for use within the class declaration body.
Definition: TTypeName.h:104
[example-define-class]
static constexpr auto get()
Definition: TTypeName.h:71



Page generated by Doxygen 1.8.14 for MRPT 2.0.5 Git: eda5ade6c Tue Aug 4 12:48:50 2020 +0200 at mar ago 4 13:00:11 CEST 2020