/************************************************************************* * Copyright (C) [2019] by Cambricon, Inc. All rights reserved * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. *************************************************************************/ #include #include #include #include #include #include #include #include #include #include "preproc.hpp" #include "reflex_object.h" namespace cnstream { class ReflexObjectTest : public ReflexObject { public: DECLARE_REFLEX_OBJECT_EX(ReflexObjectTest, ReflexObject); }; IMPLEMENT_REFLEX_OBJECT_EX(ReflexObjectTest, ReflexObject); class A : public ReflexObjectEx { public: virtual ~A() {} }; class AChild : public A { DECLARE_REFLEX_OBJECT_EX(AChild, A); }; IMPLEMENT_REFLEX_OBJECT_EX(AChild, A); class B : public ReflexObjectEx { public: virtual ~B() {} }; class Bchild : public B { DECLARE_REFLEX_OBJECT_EX(Bchild, B); }; IMPLEMENT_REFLEX_OBJECT_EX(Bchild, B); TEST(Inferencer, ReflexObject_CreateObject) { // already registered EXPECT_NE(ReflexObject::CreateObject("ReflexObjectTest"), nullptr); EXPECT_EQ(ReflexObject::CreateObject("ReflexObject"), nullptr); ClassInfo info(std::string("ReflexObject"), ObjectConstructor([]() { return reinterpret_cast(new ReflexObjectTest); })); ObjectConstructor base_constructor; base_constructor = [info]() { return reinterpret_cast(info.constructor()()); }; ClassInfo base_info(info.name(), base_constructor); EXPECT_EQ(ReflexObject::Register(base_info), true); EXPECT_NE(ReflexObject::CreateObject("ReflexObject"), nullptr); ReflexObject::Remove("ReflexObject"); } TEST(Inferencer, ReflexObject_Register) { // reference to static ClassInfo ClassInfo& info = ReflexObjectTest::sclass_info; EXPECT_EQ(ReflexObject::Register(info), false); ClassInfo info1(std::string("ReflexObject_test"), ObjectConstructor([]() { return reinterpret_cast(new ReflexObjectTest); })); ObjectConstructor base_constructor; base_constructor = [info1]() { return reinterpret_cast(info1.constructor()()); }; ClassInfo base_info(info1.name(), base_constructor); EXPECT_EQ(ReflexObject::Register(base_info), true); ReflexObject::Remove("ReflexObject_test"); } TEST(Inferencer, ReflexObjectEx_CreateObject) { EXPECT_EQ(ReflexObjectEx::CreateObject("Bchild"), nullptr); EXPECT_NE(nullptr, ReflexObjectEx::CreateObject("AChild")); } } // namespace cnstream