1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
#include <gtest/gtest.h>
#include <iostream>
#include <memory>
#include <c10/util/Registry.h>
// Note: we use a different namespace to test if the macros defined in
// Registry.h actually works with a different namespace from c10.
namespace c10_test {
class Foo {
public:
explicit Foo(int x) {
// LOG(INFO) << "Foo " << x;
}
virtual ~Foo() = default;
};
C10_DECLARE_REGISTRY(FooRegistry, Foo, int);
C10_DEFINE_REGISTRY(FooRegistry, Foo, int);
#define REGISTER_FOO(clsname) C10_REGISTER_CLASS(FooRegistry, clsname, clsname)
class Bar : public Foo {
public:
explicit Bar(int x) : Foo(x) {
// LOG(INFO) << "Bar " << x;
}
};
REGISTER_FOO(Bar);
class AnotherBar : public Foo {
public:
explicit AnotherBar(int x) : Foo(x) {
// LOG(INFO) << "AnotherBar " << x;
}
};
REGISTER_FOO(AnotherBar);
TEST(RegistryTest, CanRunCreator) {
std::unique_ptr<Foo> bar(FooRegistry()->Create("Bar", 1));
EXPECT_TRUE(bar != nullptr) << "Cannot create bar.";
std::unique_ptr<Foo> another_bar(FooRegistry()->Create("AnotherBar", 1));
EXPECT_TRUE(another_bar != nullptr);
}
TEST(RegistryTest, ReturnNullOnNonExistingCreator) {
EXPECT_EQ(FooRegistry()->Create("Non-existing bar", 1), nullptr);
}
// C10_REGISTER_CLASS_WITH_PRIORITY defines static variable
void RegisterFooDefault() {
C10_REGISTER_CLASS_WITH_PRIORITY(
FooRegistry, FooWithPriority, c10::REGISTRY_DEFAULT, Foo);
}
void RegisterFooDefaultAgain() {
C10_REGISTER_CLASS_WITH_PRIORITY(
FooRegistry, FooWithPriority, c10::REGISTRY_DEFAULT, Foo);
}
void RegisterFooBarFallback() {
C10_REGISTER_CLASS_WITH_PRIORITY(
FooRegistry, FooWithPriority, c10::REGISTRY_FALLBACK, Bar);
}
void RegisterFooBarPreferred() {
C10_REGISTER_CLASS_WITH_PRIORITY(
FooRegistry, FooWithPriority, c10::REGISTRY_PREFERRED, Bar);
}
TEST(RegistryTest, RegistryPriorities) {
FooRegistry()->SetTerminate(false);
RegisterFooDefault();
// throws because Foo is already registered with default priority
// NOLINTNEXTLINE(hicpp-avoid-goto,cppcoreguidelines-avoid-goto)
EXPECT_THROW(RegisterFooDefaultAgain(), std::runtime_error);
#ifdef __GXX_RTTI
// not going to register Bar because Foo is registered with Default priority
RegisterFooBarFallback();
std::unique_ptr<Foo> bar1(FooRegistry()->Create("FooWithPriority", 1));
EXPECT_EQ(dynamic_cast<Bar*>(bar1.get()), nullptr);
// will register Bar because of higher priority
RegisterFooBarPreferred();
std::unique_ptr<Foo> bar2(FooRegistry()->Create("FooWithPriority", 1));
EXPECT_NE(dynamic_cast<Bar*>(bar2.get()), nullptr);
#endif
}
} // namespace c10_test
|