Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / test / graph / operand / UseDef.cc
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. 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  * 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
17 #include <gtest/gtest.h>
18
19 #include "ir/Graph.h"
20 #include "ir/verifier/Verifier.h"
21 #include <memory>
22 #include "../MockNode.h"
23
24 #include <typeindex>
25
26 namespace
27 {
28
29 using IndexSet = onert::ir::OperandIndexSequence;
30 using Mock = onert_test::ir::SimpleMock;
31
32 } // namespace
33
34 TEST(ir_Operand, neg_usedef)
35 {
36   onert::ir::Graph graph;
37   onert::ir::verifier::DAGChecker verifier;
38
39   onert::ir::Shape shape(3);
40   onert::ir::TypeInfo type{onert::ir::DataType::INT32};
41
42   // Model Input/Output
43   auto input_operand = graph.addOperand(shape, type);
44   auto output_operand = graph.addOperand(shape, type);
45
46   graph.addInput(input_operand);
47   graph.addOutput(output_operand);
48
49   // MockNode1
50   auto operand_index1 = graph.addOperand(shape, type);
51   auto mocknode_index1 =
52     graph.addOperation(std::make_unique<Mock>(IndexSet{input_operand}, IndexSet{operand_index1}));
53
54   // MockNode2
55   auto operand_index2 = graph.addOperand(shape, type);
56   auto mocknode_index2 =
57     graph.addOperation(std::make_unique<Mock>(IndexSet{input_operand}, IndexSet{operand_index2}));
58
59   // MockNode3(two input)
60   auto multiinput_index = graph.addOperation(
61     std::make_unique<Mock>(IndexSet{operand_index1, operand_index2}, IndexSet{output_operand}));
62
63   graph.finishBuilding();
64
65   ASSERT_TRUE(verifier.verify(graph));
66
67   // Check def
68   ASSERT_EQ(graph.operands().at(operand_index1).getDef(), mocknode_index1);
69   ASSERT_EQ(graph.operands().at(operand_index2).getDef(), mocknode_index2);
70   ASSERT_EQ(graph.operands().at(output_operand).getDef(), multiinput_index);
71
72   ASSERT_NE(graph.operands().at(operand_index1).getDef(), mocknode_index2);
73   ASSERT_NE(graph.operands().at(operand_index1).getDef(), multiinput_index);
74
75   // Check use
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);
81
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);
85 }