blob: 81a0d915aa983714be2697a22614d5ba5bdfff8c [file] [log] [blame]
Nicolas Geoffray818f2102014-02-18 16:43:35 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
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 "base/stringprintf.h"
18#include "builder.h"
19#include "dex_instruction.h"
20#include "nodes.h"
21#include "pretty_printer.h"
22#include "utils/arena_allocator.h"
23
24#include "gtest/gtest.h"
25
26namespace art {
27
28const uint16_t data[] = { Instruction::RETURN_VOID };
29
30const char* expected =
31 "BasicBlock 0\n"
32 " Goto\n"
33 "BasicBlock 1\n"
34 " ReturnVoid\n"
35 "BasicBlock 2\n"
36 " Exit\n";
37
38class StringPrettyPrinter : public HPrettyPrinter {
39 public:
40 explicit StringPrettyPrinter(HGraph* graph) : HPrettyPrinter(graph), str_("") { }
41
42 virtual void PrintInt(int value) {
43 str_ += StringPrintf("%d", value);
44 }
45
46 virtual void PrintString(const char* value) {
47 str_ += value;
48 }
49
50 virtual void PrintNewLine() {
51 str_ += '\n';
52 }
53
54 void Clear() { str_.clear(); }
55
56 std::string str() const { return str_; }
57
58 private:
59 std::string str_;
60 DISALLOW_COPY_AND_ASSIGN(StringPrettyPrinter);
61};
62
63TEST(OptimizerTest, ReturnVoid) {
64 ArenaPool pool;
65 ArenaAllocator allocator(&pool);
66 HGraphBuilder builder(&allocator);
67 HGraph* graph = builder.BuildGraph(data, data + 1);
68 ASSERT_NE(graph, nullptr);
69 StringPrettyPrinter printer(graph);
70 printer.VisitInsertionOrder();
71 ASSERT_STREQ(expected, printer.str().c_str());
72
73 const GrowableArray<HBasicBlock*>* blocks = graph->blocks();
74 ASSERT_EQ(blocks->Get(0)->predecessors()->Size(), (size_t)0);
75 ASSERT_EQ(blocks->Get(1)->predecessors()->Size(), (size_t)1);
76 ASSERT_EQ(blocks->Get(1)->predecessors()->Get(0), blocks->Get(0));
77 ASSERT_EQ(blocks->Get(2)->predecessors()->Size(), (size_t)1);
78 ASSERT_EQ(blocks->Get(2)->predecessors()->Get(0), blocks->Get(1));
79
80 ASSERT_EQ(blocks->Get(0)->successors()->Size(), (size_t)1);
81 ASSERT_EQ(blocks->Get(1)->successors()->Get(0), blocks->Get(2));
82 ASSERT_EQ(blocks->Get(1)->successors()->Size(), (size_t)1);
83 ASSERT_EQ(blocks->Get(1)->successors()->Get(0), blocks->Get(2));
84 ASSERT_EQ(blocks->Get(2)->successors()->Size(), (size_t)0);
85}
86
87} // namespace art