Main MRPT website > C++ reference for MRPT 1.9.9
typename_unittest.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
12 #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 }
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:
54  TST_FOR_TYPE(float);
55  TST_FOR_TYPE(double);
56 
57  // STL:
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:65
TEST(TTypeName, types2str)
unsigned char uint8_t
Definition: rptypes.h:41
#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:100
GLsizei const GLchar ** string
Definition: glext.h:4101
__int32 int32_t
Definition: rptypes.h:46
[example-define-class]
static constexpr auto get()
Definition: TTypeName.h:67



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019