1 #include <gtest/gtest.h> 5 #include <c10/util/Registry.h> 19 C10_DECLARE_REGISTRY(FooRegistry,
Foo,
int);
20 C10_DEFINE_REGISTRY(FooRegistry,
Foo,
int);
21 #define REGISTER_FOO(clsname) C10_REGISTER_CLASS(FooRegistry, clsname, clsname) 25 explicit Bar(
int x) :
Foo(x) {
39 TEST(RegistryTest, CanRunCreator) {
40 std::unique_ptr<Foo> bar(FooRegistry()->Create(
"Bar", 1));
41 EXPECT_TRUE(bar !=
nullptr) <<
"Cannot create bar.";
42 std::unique_ptr<Foo> another_bar(FooRegistry()->Create(
"AnotherBar", 1));
43 EXPECT_TRUE(another_bar !=
nullptr);
46 TEST(RegistryTest, ReturnNullOnNonExistingCreator) {
47 EXPECT_EQ(FooRegistry()->Create(
"Non-existing bar", 1),
nullptr);
51 void RegisterFooDefault() {
52 C10_REGISTER_CLASS_WITH_PRIORITY(
53 FooRegistry, FooWithPriority, c10::REGISTRY_DEFAULT,
Foo);
56 void RegisterFooDefaultAgain() {
57 C10_REGISTER_CLASS_WITH_PRIORITY(
58 FooRegistry, FooWithPriority, c10::REGISTRY_DEFAULT,
Foo);
61 void RegisterFooBarFallback() {
62 C10_REGISTER_CLASS_WITH_PRIORITY(
63 FooRegistry, FooWithPriority, c10::REGISTRY_FALLBACK,
Bar);
66 void RegisterFooBarPreferred() {
67 C10_REGISTER_CLASS_WITH_PRIORITY(
68 FooRegistry, FooWithPriority, c10::REGISTRY_PREFERRED,
Bar);
71 TEST(RegistryTest, RegistryPriorities) {
72 FooRegistry()->SetTerminate(
false);
76 EXPECT_THROW(RegisterFooDefaultAgain(), std::runtime_error);
80 RegisterFooBarFallback();
81 std::unique_ptr<Foo> bar1(FooRegistry()->Create(
"FooWithPriority", 1));
82 EXPECT_EQ(dynamic_cast<Bar*>(bar1.get()),
nullptr);
85 RegisterFooBarPreferred();
86 std::unique_ptr<Foo> bar2(FooRegistry()->Create(
"FooWithPriority", 1));
87 EXPECT_NE(dynamic_cast<Bar*>(bar2.get()),
nullptr);