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 21 #include "ir/named.h" 22 #include "utils/signal.h" 23 #include "common/common_test.h" 24 25 using std::cout; 26 using std::endl; 27 using std::string; 28 29 namespace mindspore { 30 class TestSignal : public UT::Common { 31 public: 32 TestSignal() {} 33 }; 34 35 struct signals { 36 Signal<void(int, float, std::string)> signal; 37 Signal<void(std::shared_ptr<Named>)> signal1; 38 }; 39 40 class A { 41 public: 42 A() {} 43 explicit A(signals *sigs) : sigs_(sigs) { 44 sigs_->signal.connect(this, &A::FuncA); 45 sigs_->signal1.connect(this, &A::Funct); 46 printf("conn:%p\n", this); 47 i = std::make_shared<int>(1); 48 } 49 void Funct(std::shared_ptr<Named> a) {} 50 virtual void FuncA(int v1, float v2, std::string str) { printf("A: --%d--%f--%s--\n", v1, v2, str.c_str()); } 51 52 private: 53 signals *sigs_; 54 std::shared_ptr<int> i; 55 }; 56 57 class Ca : public A { 58 public: 59 Ca() {} 60 explicit Ca(signals *sigs) : A(sigs) { printf("conn C:%p\n", this); } 61 void FuncA(int v1, float v2, std::string str) { printf("C: --%d--%f--%s--\n", v1, v2, str.c_str()); } 62 }; 63 64 class B : public A { 65 public: 66 B() {} 67 explicit B(signals *sigs) : A(sigs) { printf("conn B:%p\n", this); } 68 void FuncA(int v1, float v2, std::string str) { printf("B: --%d--%f--%s--\n", v1, v2, str.c_str()); } 69 }; 70 71 TEST_F(TestSignal, test_common) { 72 A objA; 73 B objB; 74 Ca objC; 75 76 Signal<void(int, float, std::string)> signal; 77 78 signal.connect(&objA, &A::FuncA); 79 signal.connect(&objB, &B::FuncA); 80 signal.connect(&objC, &Ca::FuncA); 81 signal(20, 20, "Signal-Slot test"); 82 } 83 84 TEST_F(TestSignal, test_sigs) { 85 signals sigs; 86 A objA(&sigs); 87 B objB(&sigs); 88 Ca objC(&sigs); 89 90 sigs.signal.connect(&objA, &A::FuncA); 91 sigs.signal.connect(&objB, &B::FuncA); 92 sigs.signal.connect(&objC, &Ca::FuncA); 93 sigs.signal(20, 20, "sigs Signal-Slot test"); 94 } 95 96 TEST_F(TestSignal, test_sigs_Named) { 97 signals sigs; 98 A objA(&sigs); 99 B objB(&sigs); 100 Ca objC(&sigs); 101 102 sigs.signal(10, 20, "Signal-Slot test"); 103 std::shared_ptr<Named> a; 104 sigs.signal1(a); 105 } 106 107 } // namespace mindspore 108