1 #include "c10/util/typeid.h" 2 #include <gtest/gtest.h> 9 class TypeMetaTestFoo {};
10 class TypeMetaTestBar {};
13 CAFFE_KNOWN_TYPE(TypeMetaTestFoo);
14 CAFFE_KNOWN_TYPE(TypeMetaTestBar);
18 TEST(TypeMetaTest, TypeMetaStatic) {
19 EXPECT_EQ(TypeMeta::ItemSize<int>(),
sizeof(
int));
20 EXPECT_EQ(TypeMeta::ItemSize<float>(),
sizeof(
float));
21 EXPECT_EQ(TypeMeta::ItemSize<TypeMetaTestFoo>(),
sizeof(TypeMetaTestFoo));
22 EXPECT_EQ(TypeMeta::ItemSize<TypeMetaTestBar>(),
sizeof(TypeMetaTestBar));
23 EXPECT_NE(TypeMeta::Id<int>(), TypeMeta::Id<float>());
24 EXPECT_NE(TypeMeta::Id<int>(), TypeMeta::Id<TypeMetaTestFoo>());
25 EXPECT_NE(TypeMeta::Id<TypeMetaTestFoo>(), TypeMeta::Id<TypeMetaTestBar>());
26 EXPECT_EQ(TypeMeta::Id<int>(), TypeMeta::Id<int>());
27 EXPECT_EQ(TypeMeta::Id<TypeMetaTestFoo>(), TypeMeta::Id<TypeMetaTestFoo>());
30 TEST(TypeMetaTest, Names) {
32 EXPECT_TRUE(
string(null_meta.name()) ==
"nullptr (uninitialized)");
33 TypeMeta int_meta = TypeMeta::Make<int>();
34 EXPECT_TRUE(
string(int_meta.name()) ==
"int");
36 TypeMeta string_meta = TypeMeta::Make<string>();
39 string(string_meta.name()) !=
typeid(
string).name());
41 string(string_meta.name()) ==
c10::demangle(
typeid(
string).name()));
45 TEST(TypeMetaTest, TypeMeta) {
46 TypeMeta int_meta = TypeMeta::Make<int>();
47 TypeMeta float_meta = TypeMeta::Make<float>();
48 TypeMeta foo_meta = TypeMeta::Make<TypeMetaTestFoo>();
49 TypeMeta bar_meta = TypeMeta::Make<TypeMetaTestBar>();
51 TypeMeta another_int_meta = TypeMeta::Make<int>();
52 TypeMeta another_foo_meta = TypeMeta::Make<TypeMetaTestFoo>();
54 EXPECT_EQ(int_meta, another_int_meta);
55 EXPECT_EQ(foo_meta, another_foo_meta);
56 EXPECT_NE(int_meta, float_meta);
57 EXPECT_NE(int_meta, foo_meta);
58 EXPECT_NE(foo_meta, bar_meta);
59 EXPECT_TRUE(int_meta.Match<
int>());
60 EXPECT_TRUE(foo_meta.Match<TypeMetaTestFoo>());
61 EXPECT_FALSE(int_meta.Match<
float>());
62 EXPECT_FALSE(int_meta.Match<TypeMetaTestFoo>());
63 EXPECT_FALSE(foo_meta.Match<
int>());
64 EXPECT_FALSE(foo_meta.Match<TypeMetaTestBar>());
65 EXPECT_EQ(int_meta.id(), TypeMeta::Id<int>());
66 EXPECT_EQ(float_meta.id(), TypeMeta::Id<float>());
67 EXPECT_EQ(foo_meta.id(), TypeMeta::Id<TypeMetaTestFoo>());
68 EXPECT_EQ(bar_meta.id(), TypeMeta::Id<TypeMetaTestBar>());
69 EXPECT_EQ(int_meta.itemsize(), TypeMeta::ItemSize<int>());
70 EXPECT_EQ(float_meta.itemsize(), TypeMeta::ItemSize<float>());
71 EXPECT_EQ(foo_meta.itemsize(), TypeMeta::ItemSize<TypeMetaTestFoo>());
72 EXPECT_EQ(bar_meta.itemsize(), TypeMeta::ItemSize<TypeMetaTestBar>());
73 EXPECT_STREQ(int_meta.name(),
"int");
74 EXPECT_STREQ(float_meta.name(),
"float");
77 std::string(foo_meta.name()).find(
"TypeMetaTestFoo"), std::string::npos);
79 std::string(bar_meta.name()).find(
"TypeMetaTestBar"), std::string::npos);
84 class ClassAllowAssignment {
86 ClassAllowAssignment() : x(42) {}
87 ClassAllowAssignment(
const ClassAllowAssignment& src) : x(src.x) {}
88 ClassAllowAssignment& operator=(
const ClassAllowAssignment& src) =
default;
92 class ClassNoAssignment {
94 ClassNoAssignment() : x(42) {}
95 ClassNoAssignment(
const ClassNoAssignment& src) =
delete;
96 ClassNoAssignment& operator=(
const ClassNoAssignment& src) =
delete;
101 CAFFE_KNOWN_TYPE(ClassAllowAssignment);
102 CAFFE_KNOWN_TYPE(ClassNoAssignment);
106 TEST(TypeMetaTest, CtorDtorAndCopy) {
107 TypeMeta fundamental_meta = TypeMeta::Make<int>();
108 EXPECT_EQ(fundamental_meta.placementNew(),
nullptr);
109 EXPECT_EQ(fundamental_meta.placementDelete(),
nullptr);
110 EXPECT_EQ(fundamental_meta.copy(),
nullptr);
112 TypeMeta meta_a = TypeMeta::Make<ClassAllowAssignment>();
113 EXPECT_TRUE(meta_a.placementNew() !=
nullptr);
114 EXPECT_TRUE(meta_a.placementDelete() !=
nullptr);
115 EXPECT_TRUE(meta_a.copy() !=
nullptr);
116 ClassAllowAssignment src;
118 ClassAllowAssignment dst;
119 EXPECT_EQ(dst.x, 42);
120 meta_a.copy()(&src, &dst, 1);
121 EXPECT_EQ(dst.x, 10);
123 TypeMeta meta_b = TypeMeta::Make<ClassNoAssignment>();
125 EXPECT_TRUE(meta_b.placementNew() !=
nullptr);
126 EXPECT_TRUE(meta_b.placementDelete() !=
nullptr);
131 EXPECT_EQ(meta_b.copy(), &(detail::_CopyNotAllowed<ClassNoAssignment>));
135 TEST(TypeMetaTest, Float16IsNotUint16) {
136 EXPECT_NE(TypeMeta::Id<uint16_t>(), TypeMeta::Id<at::Half>());
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
std::string demangle(const char *name)
Utility to demangle a C++ symbol name.