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 "locop/FormattedGraph.h"
18 #include "ExampleGraph.h"
20 #include <stdex/Memory.h>
22 #include <gtest/gtest.h>
24 TEST(LinearV1FormatterTest, simple)
26 auto bundle = make_bundle<PullPush>();
27 auto g = bundle->graph();
29 // TODO Validate the output (when the implementation becomes stable)
30 std::cout << locop::fmt<locop::LinearV1>(g) << std::endl;
35 TEST(LinearV1FormatterTest, user_defined_node_summary_builder)
37 struct MyAnnotation final : public loco::NodeAnnotation
42 auto bundle = make_bundle<PullPush>();
43 auto g = bundle->graph();
45 bundle->push->annot(stdex::make_unique<MyAnnotation>());
48 struct MyBuilder final : public locop::NodeSummaryBuilder
50 bool build(const loco::Node *node, locop::NodeSummary &s) const final
53 if (node->annot<MyAnnotation>())
55 s.comments().append("annotated");
57 s.state(locop::NodeSummary::State::PartiallyKnown);
62 struct MyFactory final : public locop::NodeSummaryBuilderFactory
64 std::unique_ptr<locop::NodeSummaryBuilder> create(const locop::SymbolTable *) const final
66 return stdex::make_unique<MyBuilder>();
70 std::cout << locop::fmt<locop::LinearV1>(g).with(stdex::make_unique<MyFactory>()) << std::endl;
72 // TODO Check whether MyBuilder actually sees all the nodes in a graph
76 // This test shows how to compose two node summary builders.
77 TEST(LinearV1FormatterTest, node_summary_builder_composition)
79 struct MyNode : public loco::FixedArity<0>::Mixin<loco::Node>
81 uint32_t opnum(void) const final { return 0; }
82 const loco::Dialect *dialect(void) const final { return nullptr; };
85 auto g = loco::make_graph();
87 auto user = g->nodes()->create<MyNode>();
89 auto push = g->nodes()->create<loco::Push>();
94 // TODO Reuse MyBuilder above
95 struct MyBuilder final : public locop::NodeSummaryBuilder
97 bool build(const loco::Node *node, locop::NodeSummary &s) const final
100 s.state(locop::NodeSummary::State::PartiallyKnown);
105 class CompositeBuilder final : public locop::NodeSummaryBuilder
108 CompositeBuilder(const locop::SymbolTable *tbl) : _tbl{tbl}
114 bool build(const loco::Node *node, locop::NodeSummary &s) const final
116 if (locop::CanonicalNodeSummaryBuilder(_tbl).build(node, s))
121 if (MyBuilder().build(node, s))
130 const locop::SymbolTable *_tbl;
133 struct MyFactory final : public locop::NodeSummaryBuilderFactory
135 std::unique_ptr<locop::NodeSummaryBuilder> create(const locop::SymbolTable *tbl) const final
137 return stdex::make_unique<CompositeBuilder>(tbl);
141 std::cout << locop::fmt<locop::LinearV1>(g).with(stdex::make_unique<MyFactory>()) << std::endl;
143 // TODO Check whether MyBuilder actually sees all the nodes in a graph