You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

signal_test.cc 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  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. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <iostream>
  17. #include <sstream>
  18. #include <memory>
  19. #include <algorithm>
  20. #include "ir/named.h"
  21. #include "utils/signal.h"
  22. #include "common/common_test.h"
  23. using std::cout;
  24. using std::endl;
  25. using std::string;
  26. namespace mindspore {
  27. class TestSignal : public UT::Common {
  28. public:
  29. TestSignal() {}
  30. };
  31. struct signals {
  32. Signal<void(int, float, std::string)> signal;
  33. Signal<void(std::shared_ptr<Named>)> signal1;
  34. };
  35. class A {
  36. public:
  37. A() {}
  38. explicit A(signals *sigs) : sigs_(sigs) {
  39. sigs_->signal.connect(this, &A::FuncA);
  40. sigs_->signal1.connect(this, &A::Funct);
  41. printf("conn:%p\n", this);
  42. i = std::make_shared<int>(1);
  43. }
  44. void Funct(std::shared_ptr<Named> a) {}
  45. virtual void FuncA(int v1, float v2, std::string str) { printf("A: --%d--%f--%s--\n", v1, v2, str.c_str()); }
  46. private:
  47. signals *sigs_;
  48. std::shared_ptr<int> i;
  49. };
  50. class C : public A {
  51. public:
  52. C() {}
  53. explicit C(signals *sigs) : A(sigs) { printf("conn C:%p\n", this); }
  54. void FuncA(int v1, float v2, std::string str) { printf("C: --%d--%f--%s--\n", v1, v2, str.c_str()); }
  55. };
  56. class B : public A {
  57. public:
  58. B() {}
  59. explicit B(signals *sigs) : A(sigs) { printf("conn B:%p\n", this); }
  60. void FuncA(int v1, float v2, std::string str) { printf("B: --%d--%f--%s--\n", v1, v2, str.c_str()); }
  61. };
  62. TEST_F(TestSignal, test_common) {
  63. A objA;
  64. B objB;
  65. C objC;
  66. Signal<void(int, float, std::string)> signal;
  67. signal.connect(&objA, &A::FuncA);
  68. signal.connect(&objB, &B::FuncA);
  69. signal.connect(&objC, &C::FuncA);
  70. signal(20, 20, "Signal-Slot test");
  71. }
  72. TEST_F(TestSignal, test_sigs) {
  73. signals sigs;
  74. A objA(&sigs);
  75. B objB(&sigs);
  76. C objC(&sigs);
  77. sigs.signal.connect(&objA, &A::FuncA);
  78. sigs.signal.connect(&objB, &B::FuncA);
  79. sigs.signal.connect(&objC, &C::FuncA);
  80. sigs.signal(20, 20, "sigs Signal-Slot test");
  81. }
  82. TEST_F(TestSignal, test_sigs_Named) {
  83. signals sigs;
  84. A objA(&sigs);
  85. B objB(&sigs);
  86. C objC(&sigs);
  87. sigs.signal(10, 20, "Signal-Slot test");
  88. std::shared_ptr<Named> a;
  89. sigs.signal1(a);
  90. }
  91. } // namespace mindspore