test_reflex_object.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <atomic>
  22. #include <chrono>
  23. #include <condition_variable>
  24. #include <future>
  25. #include <memory>
  26. #include <mutex>
  27. #include <string>
  28. #include <thread>
  29. #include "preproc.hpp"
  30. #include "reflex_object.h"
  31. namespace cnstream {
  32. class ReflexObjectTest : public ReflexObject {
  33. public:
  34. DECLARE_REFLEX_OBJECT_EX(ReflexObjectTest, ReflexObject);
  35. };
  36. IMPLEMENT_REFLEX_OBJECT_EX(ReflexObjectTest, ReflexObject);
  37. class A : public ReflexObjectEx<A> {
  38. public:
  39. virtual ~A() {}
  40. };
  41. class AChild : public A {
  42. DECLARE_REFLEX_OBJECT_EX(AChild, A);
  43. };
  44. IMPLEMENT_REFLEX_OBJECT_EX(AChild, A);
  45. class B : public ReflexObjectEx<B> {
  46. public:
  47. virtual ~B() {}
  48. };
  49. class Bchild : public B {
  50. DECLARE_REFLEX_OBJECT_EX(Bchild, B);
  51. };
  52. IMPLEMENT_REFLEX_OBJECT_EX(Bchild, B);
  53. TEST(Inferencer, ReflexObject_CreateObject) {
  54. // already registered
  55. EXPECT_NE(ReflexObject::CreateObject("ReflexObjectTest"), nullptr);
  56. EXPECT_EQ(ReflexObject::CreateObject("ReflexObject"), nullptr);
  57. ClassInfo<ReflexObject> info(std::string("ReflexObject"), ObjectConstructor<ReflexObject>([]() {
  58. return reinterpret_cast<ReflexObject*>(new ReflexObjectTest);
  59. }));
  60. ObjectConstructor<ReflexObject> base_constructor;
  61. base_constructor = [info]() { return reinterpret_cast<ReflexObject*>(info.constructor()()); };
  62. ClassInfo<ReflexObject> base_info(info.name(), base_constructor);
  63. EXPECT_EQ(ReflexObject::Register(base_info), true);
  64. EXPECT_NE(ReflexObject::CreateObject("ReflexObject"), nullptr);
  65. ReflexObject::Remove("ReflexObject");
  66. }
  67. TEST(Inferencer, ReflexObject_Register) {
  68. // reference to static ClassInfo
  69. ClassInfo<ReflexObject>& info = ReflexObjectTest::sclass_info;
  70. EXPECT_EQ(ReflexObject::Register(info), false);
  71. ClassInfo<ReflexObject> info1(std::string("ReflexObject_test"), ObjectConstructor<ReflexObject>([]() {
  72. return reinterpret_cast<ReflexObject*>(new ReflexObjectTest);
  73. }));
  74. ObjectConstructor<ReflexObject> base_constructor;
  75. base_constructor = [info1]() { return reinterpret_cast<ReflexObject*>(info1.constructor()()); };
  76. ClassInfo<ReflexObject> base_info(info1.name(), base_constructor);
  77. EXPECT_EQ(ReflexObject::Register(base_info), true);
  78. ReflexObject::Remove("ReflexObject_test");
  79. }
  80. TEST(Inferencer, ReflexObjectEx_CreateObject) {
  81. EXPECT_EQ(ReflexObjectEx<A>::CreateObject("Bchild"), nullptr);
  82. EXPECT_NE(nullptr, ReflexObjectEx<A>::CreateObject("AChild"));
  83. }
  84. } // namespace cnstream