2 * Copyright (c) 2019 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 "loco/IR/CanonicalNode.h"
19 #include <gtest/gtest.h>
21 TEST(CanonicalNodeTest, visitor_with_user_default_impl)
23 struct MyVisitor final : public loco::CanonicalNodeVisitor<uint32_t>
25 // This visitor returns 128 if it visits a Forward node.
26 uint32_t visit(const loco::Forward *) final { return 128; }
28 // Otherwise, this visitor returns 256.
29 uint32_t visit(const loco::Node *) final { return 256; }
32 loco::Forward forward;
33 loco::ConstGen constgen;
37 ASSERT_EQ(128, forward.accept(&v));
38 ASSERT_EQ(256, constgen.accept(&v));
41 TEST(CanonicalNodeTest, visitor)
43 struct CountingVisitor final : public loco::CanonicalNodeVisitor<uint32_t>
45 uint32_t visit(const loco::Forward *) final { return 1; }
48 // Visitor can visit constant nodes
49 const loco::Forward node;
53 ASSERT_EQ(1, node.accept(&v));
56 TEST(CanonicalNodeTest, mutable_visitor)
58 struct ResetForward final : public loco::CanonicalNodeMutableVisitor<void>
60 void visit(loco::Forward *node) final { node->input(nullptr); }
64 loco::Forward forward_node;
66 forward_node.input(&pull_node);
69 forward_node.accept(&v);
71 ASSERT_EQ(nullptr, forward_node.input());