Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / loco / src / IR / CanonicalNode.test.cpp
1 /*
2  * Copyright (c) 2019 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 "loco/IR/CanonicalNode.h"
18
19 #include <gtest/gtest.h>
20
21 TEST(CanonicalNodeTest, visitor_with_user_default_impl)
22 {
23   struct MyVisitor final : public loco::CanonicalNodeVisitor<uint32_t>
24   {
25     // This visitor returns 128 if it visits a Forward node.
26     uint32_t visit(const loco::Forward *) final { return 128; }
27
28     // Otherwise, this visitor returns 256.
29     uint32_t visit(const loco::Node *) final { return 256; }
30   };
31
32   loco::Forward forward;
33   loco::ConstGen constgen;
34
35   MyVisitor v;
36
37   ASSERT_EQ(128, forward.accept(&v));
38   ASSERT_EQ(256, constgen.accept(&v));
39 }
40
41 TEST(CanonicalNodeTest, visitor)
42 {
43   struct CountingVisitor final : public loco::CanonicalNodeVisitor<uint32_t>
44   {
45     uint32_t visit(const loco::Forward *) final { return 1; }
46   };
47
48   // Visitor can visit constant nodes
49   const loco::Forward node;
50
51   CountingVisitor v;
52
53   ASSERT_EQ(1, node.accept(&v));
54 }
55
56 TEST(CanonicalNodeTest, mutable_visitor)
57 {
58   struct ResetForward final : public loco::CanonicalNodeMutableVisitor<void>
59   {
60     void visit(loco::Forward *node) final { node->input(nullptr); }
61   };
62
63   loco::Pull pull_node;
64   loco::Forward forward_node;
65
66   forward_node.input(&pull_node);
67
68   ResetForward v;
69   forward_node.accept(&v);
70
71   ASSERT_EQ(nullptr, forward_node.input());
72 }