blob: 88df24d9acd5f6c77484b4737be4920907b61605 [file] [log] [blame]
Nicolas Geoffray4e3d23a2014-05-22 18:32:45 +01001/*
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 "nodes.h"
18#include "parallel_move_resolver.h"
19#include "utils/arena_allocator.h"
20
21#include "gtest/gtest.h"
22
23namespace art {
24
25class TestParallelMoveResolver : public ParallelMoveResolver {
26 public:
27 explicit TestParallelMoveResolver(ArenaAllocator* allocator) : ParallelMoveResolver(allocator) {}
28
29 virtual void EmitMove(size_t index) {
30 MoveOperands* move = moves_.Get(index);
31 if (!message_.str().empty()) {
32 message_ << " ";
33 }
34 message_ << "("
35 << move->GetSource().reg().RegId()
36 << " -> "
37 << move->GetDestination().reg().RegId()
38 << ")";
39 }
40
41 virtual void EmitSwap(size_t index) {
42 MoveOperands* move = moves_.Get(index);
43 if (!message_.str().empty()) {
44 message_ << " ";
45 }
46 message_ << "("
47 << move->GetSource().reg().RegId()
48 << " <-> "
49 << move->GetDestination().reg().RegId()
50 << ")";
51 }
52
53 std::string GetMessage() const {
54 return message_.str();
55 }
56
57 private:
58 std::ostringstream message_;
59
60
61 DISALLOW_COPY_AND_ASSIGN(TestParallelMoveResolver);
62};
63
64static HParallelMove* BuildParallelMove(ArenaAllocator* allocator,
65 const size_t operands[][2],
66 size_t number_of_moves) {
67 HParallelMove* moves = new (allocator) HParallelMove(allocator);
68 for (size_t i = 0; i < number_of_moves; ++i) {
69 moves->AddMove(new (allocator) MoveOperands(
70 Location::RegisterLocation(ManagedRegister(operands[i][0])),
71 Location::RegisterLocation(ManagedRegister(operands[i][1]))));
72 }
73 return moves;
74}
75
76TEST(ParallelMoveTest, Dependency) {
77 ArenaPool pool;
78 ArenaAllocator allocator(&pool);
79
80 {
81 TestParallelMoveResolver resolver(&allocator);
82 static constexpr size_t moves[][2] = {{0, 1}, {1, 2}};
83 resolver.EmitNativeCode(BuildParallelMove(&allocator, moves, arraysize(moves)));
84 ASSERT_STREQ("(1 -> 2) (0 -> 1)", resolver.GetMessage().c_str());
85 }
86
87 {
88 TestParallelMoveResolver resolver(&allocator);
89 static constexpr size_t moves[][2] = {{0, 1}, {1, 2}, {2, 3}, {1, 4}};
90 resolver.EmitNativeCode(BuildParallelMove(&allocator, moves, arraysize(moves)));
91 ASSERT_STREQ("(2 -> 3) (1 -> 2) (1 -> 4) (0 -> 1)", resolver.GetMessage().c_str());
92 }
93}
94
95TEST(ParallelMoveTest, Swap) {
96 ArenaPool pool;
97 ArenaAllocator allocator(&pool);
98
99 {
100 TestParallelMoveResolver resolver(&allocator);
101 static constexpr size_t moves[][2] = {{0, 1}, {1, 0}};
102 resolver.EmitNativeCode(BuildParallelMove(&allocator, moves, arraysize(moves)));
103 ASSERT_STREQ("(1 <-> 0)", resolver.GetMessage().c_str());
104 }
105
106 {
107 TestParallelMoveResolver resolver(&allocator);
108 static constexpr size_t moves[][2] = {{0, 1}, {1, 2}, {1, 0}};
109 resolver.EmitNativeCode(BuildParallelMove(&allocator, moves, arraysize(moves)));
110 ASSERT_STREQ("(1 -> 2) (1 <-> 0)", resolver.GetMessage().c_str());
111 }
112
113 {
114 TestParallelMoveResolver resolver(&allocator);
115 static constexpr size_t moves[][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 1}};
116 resolver.EmitNativeCode(BuildParallelMove(&allocator, moves, arraysize(moves)));
117 ASSERT_STREQ("(4 <-> 1) (3 <-> 4) (2 <-> 3) (0 -> 1)", resolver.GetMessage().c_str());
118 }
119
120 {
121 TestParallelMoveResolver resolver(&allocator);
122 static constexpr size_t moves[][2] = {{0, 1}, {1, 2}, {2, 3}, {3, 4}, {4, 1}, {5, 4}};
123 resolver.EmitNativeCode(BuildParallelMove(&allocator, moves, arraysize(moves)));
124 ASSERT_STREQ("(4 <-> 1) (3 <-> 4) (2 <-> 3) (0 -> 1) (5 -> 4)", resolver.GetMessage().c_str());
125 }
126}
127
128} // namespace art