| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, 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> |
| 11 |
#include <mrpt/typemeta/TTypeName.h> |
| 12 |
#include <mrpt/typemeta/TTypeName_stl.h> |
| 13 |
|
| 14 |
#include <iostream> |
| 15 |
#include <memory> // shared_ptr |
| 16 |
|
| 17 |
struct MyFooClass |
| 18 |
{ |
| 19 |
using Ptr = std::shared_ptr<MyFooClass>; |
| 20 |
}; |
| 21 |
namespace MyNS |
| 22 |
{ |
| 23 |
struct MyBarClass |
| 24 |
{ |
| 25 |
}; |
| 26 |
struct MyBarClass2 |
| 27 |
{ |
| 28 |
DECLARE_TTYPENAME_CLASSNAME(MyNS::MyBarClass2) |
| 29 |
}; |
| 30 |
} // namespace MyNS |
| 31 |
|
| 32 |
DECLARE_CUSTOM_TTYPENAME(MyFooClass); |
| 33 |
DECLARE_CUSTOM_TTYPENAME(MyNS::MyBarClass); |
| 34 |
|
| 35 |
#define TST_FOR_TYPE(__TSTTYPE) 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 << "\n Computed type is: " \ |
| 42 |
<< TTypeName<__TSTTYPE, __TSTTYPE2ndpart>::get().c_str() << endl; |
| 43 |
|
| 44 |
TEST(TTypeName, types2str) |
| 45 |
{ |
| 46 |
using namespace mrpt::typemeta; |
| 47 |
using namespace std; |
| 48 |
|
| 49 |
// Basic types: |
| 50 |
TST_FOR_TYPE(int32_t); |
| 51 |
TST_FOR_TYPE(uint8_t); |
| 52 |
TST_FOR_TYPE(float); |
| 53 |
TST_FOR_TYPE(double); |
| 54 |
|
| 55 |
// STL: |
| 56 |
TST_FOR_TYPE(std::string); |
| 57 |
TST_FOR_TYPE(std::vector<double>); |
| 58 |
TST_FOR_TYPE(std::vector<int32_t>); |
| 59 |
TST_FOR_TYPE(std::set<double>); |
| 60 |
TST_FOR_TYPE(std::set<std::vector<double>>); |
| 61 |
|
| 62 |
TST_FOR_TYPE2(std::pair<int32_t, int32_t>); |
| 63 |
TST_FOR_TYPE2(std::map<double, std::set<int32_t>>); |
| 64 |
TST_FOR_TYPE2(std::array<double, 3>); |
| 65 |
|
| 66 |
// User-defined types: |
| 67 |
TST_FOR_TYPE(MyFooClass); |
| 68 |
TST_FOR_TYPE(MyNS::MyBarClass); |
| 69 |
TST_FOR_TYPE(MyNS::MyBarClass2); |
| 70 |
TST_FOR_TYPE(std::vector<MyFooClass>); |
| 71 |
TST_FOR_TYPE(std::set<MyNS::MyBarClass>); |
| 72 |
TST_FOR_TYPE(std::set<MyNS::MyBarClass2>); |
| 73 |
TST_FOR_TYPE2(std::vector<std::array<MyNS::MyBarClass, 10>>); |
| 74 |
} |
| 75 |
|
| 76 |
TEST(TTypeName, types2str_shared_ptr) |
| 77 |
{ |
| 78 |
using namespace mrpt::typemeta; |
| 79 |
|
| 80 |
TST_FOR_TYPE(std::shared_ptr<MyFooClass>); |
| 81 |
TST_FOR_TYPE(std::vector<std::shared_ptr<MyFooClass>>); |
| 82 |
EXPECT_STREQ("std::shared_ptr<MyFooClass>", TTypeName<MyFooClass::Ptr>::get().c_str()); |
| 83 |
} |
| 84 |
|
| 85 |
TEST(TTypeName, types2stdstring) |
| 86 |
{ |
| 87 |
using namespace mrpt::typemeta; |
| 88 |
using namespace std; |
| 89 |
|
| 90 |
auto st1 = TTypeName<uint8_t>::get(); |
| 91 |
const std::string s1(st1.c_str()); |
| 92 |
const std::string s2("uint8_t"); |
| 93 |
EXPECT_STREQ(s1.c_str(), s2.c_str()); |
| 94 |
} |