1 : // Copyright 2012 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Unittests for reorder::orderers::ExplicitOrderer.
16 :
17 : #include "syzygy/reorder/orderers/explicit_orderer.h"
18 :
19 : #include "gmock/gmock.h"
20 : #include "gtest/gtest.h"
21 : #include "syzygy/reorder/order_generator_test.h"
22 :
23 : namespace reorder {
24 : namespace orderers {
25 :
26 : namespace {
27 :
28 : using block_graph::BlockGraph;
29 : using block_graph::OrderedBlockGraph;
30 : using block_graph::ConstBlockVector;
31 : using testing::ContainerEq;
32 :
33 : typedef Reorderer::Order::BlockSpec BlockSpec;
34 : typedef Reorderer::Order::BlockSpecVector BlockSpecVector;
35 :
36 : class ExplicitOrdererTest : public testing::Test {
37 : public:
38 E : ExplicitOrdererTest() { }
39 :
40 E : virtual void SetUp() {
41 E : sections_.push_back(block_graph_.AddSection("0", 0));
42 E : sections_.push_back(block_graph_.AddSection("1", 0));
43 :
44 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "0"));
45 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "1"));
46 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "2"));
47 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "3"));
48 :
49 E : blocks_[0]->set_section(sections_[0]->id());
50 E : blocks_[1]->set_section(sections_[0]->id());
51 E : blocks_[2]->set_section(sections_[1]->id());
52 E : blocks_[3]->set_section(sections_[1]->id());
53 E : }
54 :
55 : Reorderer::Order order_;
56 : BlockGraph block_graph_;
57 :
58 : std::vector<BlockGraph::Section*> sections_;
59 : block_graph::BlockVector blocks_;
60 : };
61 :
62 : template<typename Container>
63 E : ConstBlockVector ToBlockVector(const Container& container) {
64 E : return ConstBlockVector(container.begin(), container.end());
65 E : }
66 :
67 : template<>
68 : ConstBlockVector ToBlockVector<BlockSpecVector>(
69 E : const BlockSpecVector& container) {
70 E : ConstBlockVector result;
71 E : result.reserve(container.size());
72 E : for (size_t i = 0; i < container.size(); ++i)
73 E : result.push_back(container[i].block);
74 E : return result;
75 E : }
76 :
77 : } // namespace
78 :
79 E : TEST_F(ExplicitOrdererTest, FailsWithInvalidSection) {
80 E : order_.sections.resize(1);
81 E : order_.sections[0].id = 0xCCCCCCCC;
82 E : order_.sections[0].name = sections_[0]->name();
83 E : order_.sections[0].characteristics = sections_[0]->characteristics();
84 E : order_.sections[0].blocks.push_back(BlockSpec(blocks_[0]));
85 :
86 E : OrderedBlockGraph obg(&block_graph_);
87 E : ExplicitOrderer orderer(&order_);
88 E : EXPECT_FALSE(orderer.OrderBlockGraph(&obg, NULL));
89 E : }
90 :
91 E : TEST_F(ExplicitOrdererTest, FailsWithInvalidBlock) {
92 E : order_.sections.resize(1);
93 E : order_.sections[0].id = sections_[0]->id();
94 E : order_.sections[0].name = sections_[0]->name();
95 E : order_.sections[0].characteristics = sections_[0]->characteristics();
96 E : order_.sections[0].blocks.push_back(BlockSpec(blocks_[0]));
97 : order_.sections[0].blocks.push_back(
98 E : BlockSpec(reinterpret_cast<BlockGraph::Block*>(0xCCCCCCCC)));
99 :
100 E : OrderedBlockGraph obg(&block_graph_);
101 E : ExplicitOrderer orderer(&order_);
102 E : EXPECT_FALSE(orderer.OrderBlockGraph(&obg, NULL));
103 E : }
104 :
105 E : TEST_F(ExplicitOrdererTest, OrderIsAsExpected) {
106 E : order_.sections.resize(2);
107 :
108 E : order_.sections[0].id = sections_[0]->id();
109 E : order_.sections[0].name = sections_[0]->name();
110 E : order_.sections[0].characteristics = sections_[0]->characteristics();
111 E : order_.sections[0].blocks.push_back(BlockSpec(blocks_[2]));
112 E : order_.sections[0].blocks.push_back(BlockSpec(blocks_[3]));
113 E : order_.sections[0].blocks.push_back(BlockSpec(blocks_[1]));
114 :
115 E : order_.sections[1].id = sections_[1]->id();
116 E : order_.sections[1].name = sections_[1]->name();
117 E : order_.sections[1].characteristics = sections_[1]->characteristics();
118 E : order_.sections[1].blocks.push_back(BlockSpec(blocks_[0]));
119 :
120 E : OrderedBlockGraph obg(&block_graph_);
121 E : ExplicitOrderer orderer(&order_);
122 E : ASSERT_TRUE(orderer.OrderBlockGraph(&obg, NULL));
123 :
124 : EXPECT_THAT(ToBlockVector(order_.sections[0].blocks),
125 : ContainerEq(ToBlockVector(
126 E : obg.ordered_section(sections_[0]).ordered_blocks())));
127 : EXPECT_THAT(ToBlockVector(order_.sections[1].blocks),
128 : ContainerEq(ToBlockVector(
129 E : obg.ordered_section(sections_[1]).ordered_blocks())));
130 E : }
131 :
132 E : TEST_F(ExplicitOrdererTest, BasicBlockOrderFails) {
133 E : order_.sections.resize(1);
134 :
135 E : order_.sections[0].id = sections_[0]->id();
136 E : order_.sections[0].name = sections_[0]->name();
137 E : order_.sections[0].characteristics = sections_[0]->characteristics();
138 E : order_.sections[0].blocks.push_back(BlockSpec(blocks_[2]));
139 E : order_.sections[0].blocks[0].basic_block_offsets.push_back(0);
140 E : order_.sections[0].blocks[0].basic_block_offsets.push_back(10);
141 :
142 E : OrderedBlockGraph obg(&block_graph_);
143 E : ExplicitOrderer orderer(&order_);
144 E : ASSERT_FALSE(orderer.OrderBlockGraph(&obg, NULL));
145 E : }
146 :
147 : } // namespace orderers
148 : } // namespace reorder
|