|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include <string> |
| 16 | +#include "gtest/gtest.h" |
| 17 | +#include "paddle/fluid/framework/ir/graph.h" |
| 18 | +#include "paddle/fluid/framework/ir/pass.h" |
| 19 | + |
| 20 | +namespace paddle { |
| 21 | +namespace framework { |
| 22 | +namespace ir { |
| 23 | + |
| 24 | +class RunnableOp { |
| 25 | + public: |
| 26 | + RunnableOp(Node* node, bool* alive) : node_(node), alive_(alive) { |
| 27 | + node_->WrappedBy(this); |
| 28 | + } |
| 29 | + |
| 30 | + virtual ~RunnableOp() { *alive_ = false; } |
| 31 | + |
| 32 | + private: |
| 33 | + Node* node_; |
| 34 | + bool* alive_; |
| 35 | +}; |
| 36 | + |
| 37 | +class RunnableOp2 { |
| 38 | + public: |
| 39 | + RunnableOp2(Node* node, bool* alive) : node_(node), alive_(alive) { |
| 40 | + node_->WrappedBy(this); |
| 41 | + } |
| 42 | + |
| 43 | + virtual ~RunnableOp2() { *alive_ = false; } |
| 44 | + |
| 45 | + private: |
| 46 | + Node* node_; |
| 47 | + bool* alive_; |
| 48 | +}; |
| 49 | + |
| 50 | +TEST(NodeTest, Basic) { |
| 51 | + bool alive1 = true; |
| 52 | + bool alive2 = true; |
| 53 | + std::unique_ptr<Node> n1(CreateNodeForTest("n1", Node::Type::kVariable)); |
| 54 | + std::unique_ptr<Node> n2(CreateNodeForTest("n2", Node::Type::kVariable)); |
| 55 | + |
| 56 | + EXPECT_FALSE(n1->IsWrappedBy<RunnableOp>()); |
| 57 | + EXPECT_FALSE(n1->IsWrappedBy<RunnableOp2>()); |
| 58 | + EXPECT_FALSE(n2->IsWrappedBy<RunnableOp>()); |
| 59 | + EXPECT_FALSE(n2->IsWrappedBy<RunnableOp2>()); |
| 60 | + |
| 61 | + new RunnableOp(n1.get(), &alive1); |
| 62 | + new RunnableOp2(n2.get(), &alive2); |
| 63 | + |
| 64 | + EXPECT_TRUE(n1->IsWrappedBy<RunnableOp>()); |
| 65 | + EXPECT_FALSE(n1->IsWrappedBy<RunnableOp2>()); |
| 66 | + EXPECT_FALSE(n2->IsWrappedBy<RunnableOp>()); |
| 67 | + EXPECT_TRUE(n2->IsWrappedBy<RunnableOp2>()); |
| 68 | + |
| 69 | + EXPECT_TRUE(alive1); |
| 70 | + EXPECT_TRUE(alive2); |
| 71 | + |
| 72 | + n1.reset(nullptr); |
| 73 | + n2.reset(nullptr); |
| 74 | + EXPECT_FALSE(alive1); |
| 75 | + EXPECT_FALSE(alive2); |
| 76 | +} |
| 77 | + |
| 78 | +} // namespace ir |
| 79 | +} // namespace framework |
| 80 | +} // namespace paddle |
0 commit comments