1 : // Copyright 2012 Google Inc.
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 :
22 : namespace reorder {
23 : namespace orderers {
24 :
25 : namespace {
26 :
27 : using block_graph::BlockGraph;
28 : using block_graph::OrderedBlockGraph;
29 : using testing::ContainerEq;
30 :
31 : class ExplicitOrdererTest : public testing::Test {
32 : public:
33 E : ExplicitOrdererTest() { }
34 :
35 E : virtual void SetUp() {
36 E : sections_.push_back(block_graph_.AddSection("0", 0));
37 E : sections_.push_back(block_graph_.AddSection("1", 0));
38 :
39 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "0"));
40 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "1"));
41 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "2"));
42 E : blocks_.push_back(block_graph_.AddBlock(BlockGraph::DATA_BLOCK, 10, "3"));
43 :
44 E : blocks_[0]->set_section(sections_[0]->id());
45 E : blocks_[1]->set_section(sections_[0]->id());
46 E : blocks_[2]->set_section(sections_[1]->id());
47 E : blocks_[3]->set_section(sections_[1]->id());
48 E : }
49 :
50 : Reorderer::Order order_;
51 : BlockGraph block_graph_;
52 :
53 : std::vector<BlockGraph::Section*> sections_;
54 : block_graph::BlockVector blocks_;
55 : };
56 :
57 : template<typename Container>
58 E : Reorderer::Order::BlockList ToBlockList(const Container& container) {
59 E : return Reorderer::Order::BlockList(container.begin(), container.end());
60 E : }
61 :
62 : } // namespace
63 :
64 E : TEST_F(ExplicitOrdererTest, FailsWithInvalidSection) {
65 E : order_.section_block_lists[0xCCCCCCCC].push_back(blocks_[0]);
66 :
67 E : OrderedBlockGraph obg(&block_graph_);
68 E : ExplicitOrderer orderer(&order_);
69 E : EXPECT_FALSE(orderer.OrderBlockGraph(&obg, NULL));
70 E : }
71 :
72 E : TEST_F(ExplicitOrdererTest, FailsWithInvalidBlock) {
73 E : BlockGraph::SectionId sid = sections_[0]->id();
74 E : order_.section_block_lists[sid].push_back(blocks_[0]);
75 : order_.section_block_lists[sid].push_back(
76 E : reinterpret_cast<BlockGraph::Block*>(0xCCCCCCCC));
77 :
78 E : OrderedBlockGraph obg(&block_graph_);
79 E : ExplicitOrderer orderer(&order_);
80 E : EXPECT_FALSE(orderer.OrderBlockGraph(&obg, NULL));
81 E : }
82 :
83 E : TEST_F(ExplicitOrdererTest, OrderIsAsExpected) {
84 E : BlockGraph::SectionId sid0 = sections_[0]->id();
85 E : BlockGraph::SectionId sid1 = sections_[1]->id();
86 :
87 E : order_.section_block_lists[sid0].push_back(blocks_[2]);
88 E : order_.section_block_lists[sid0].push_back(blocks_[3]);
89 E : order_.section_block_lists[sid0].push_back(blocks_[1]);
90 E : order_.section_block_lists[sid1].push_back(blocks_[0]);
91 :
92 E : OrderedBlockGraph obg(&block_graph_);
93 E : ExplicitOrderer orderer(&order_);
94 E : EXPECT_TRUE(orderer.OrderBlockGraph(&obg, NULL));
95 :
96 : EXPECT_THAT(order_.section_block_lists[sid0],
97 : ContainerEq(ToBlockList(
98 E : obg.ordered_section(sections_[0]).ordered_blocks())));
99 : EXPECT_THAT(order_.section_block_lists[sid1],
100 : ContainerEq(ToBlockList(
101 E : obg.ordered_section(sections_[1]).ordered_blocks())));
102 E : }
103 :
104 : } // namespace orderers
105 : } // namespace reorder
|