2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
20 #include "ir/verifier/Verifier.h"
21 #include "cpp14/memory.h"
22 #include "../MockNode.h"
29 using IndexSet = neurun::model::OperandIndexSequence;
30 using Mock = neurun_test::graph::SimpleMock;
32 } // namespace anonymous
34 TEST(graph_operand_usedef, usedef_test)
36 neurun::graph::Graph graph;
37 neurun::graph::verifier::DAGChecker verifier;
39 neurun::model::Shape shape(3);
40 neurun::model::TypeInfo type{neurun::ir::DataType::INT32};
43 auto input_operand = graph.addOperand(shape, type);
44 auto output_operand = graph.addOperand(shape, type);
46 graph.addInput(input_operand);
47 graph.addOutput(output_operand);
50 auto operand_index1 = graph.addOperand(shape, type);
51 auto mocknode_index1 = graph.addOperation(
52 nnfw::cpp14::make_unique<Mock>(IndexSet{input_operand}, IndexSet{operand_index1}));
55 auto operand_index2 = graph.addOperand(shape, type);
56 auto mocknode_index2 = graph.addOperation(
57 nnfw::cpp14::make_unique<Mock>(IndexSet{input_operand}, IndexSet{operand_index2}));
59 // MockNode3(two input)
60 auto multiinput_index = graph.addOperation(nnfw::cpp14::make_unique<Mock>(
61 IndexSet{operand_index1, operand_index2}, IndexSet{output_operand}));
63 graph.finishBuilding();
65 ASSERT_EQ(verifier.verify(graph), true);
68 ASSERT_EQ(graph.operands().at(operand_index1).getDef().contains(mocknode_index1), true);
69 ASSERT_EQ(graph.operands().at(operand_index2).getDef().contains(mocknode_index2), true);
70 ASSERT_EQ(graph.operands().at(output_operand).getDef().contains(multiinput_index), true);
72 ASSERT_EQ(graph.operands().at(operand_index1).getDef().contains(mocknode_index2), false);
73 ASSERT_EQ(graph.operands().at(operand_index1).getDef().contains(multiinput_index), false);
76 ASSERT_EQ(graph.operands().at(input_operand).getUses().contains(mocknode_index1), true);
77 ASSERT_EQ(graph.operands().at(input_operand).getUses().contains(mocknode_index2), true);
78 ASSERT_EQ(graph.operands().at(input_operand).getUses().contains(multiinput_index), false);
79 ASSERT_EQ(graph.operands().at(operand_index1).getUses().contains(multiinput_index), true);
80 ASSERT_EQ(graph.operands().at(operand_index2).getUses().contains(multiinput_index), true);
82 ASSERT_EQ(graph.operands().at(input_operand).getUses().size(), 2);
83 ASSERT_EQ(graph.operands().at(operand_index1).getUses().size(), 1);
84 ASSERT_EQ(graph.operands().at(output_operand).getUses().size(), 0);