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 iteration primitives.
16 :
17 : #include "syzygy/block_graph/transforms/trim_transform.h"
18 :
19 : #include "gtest/gtest.h"
20 :
21 : namespace block_graph {
22 : namespace transforms {
23 :
24 : namespace {
25 :
26 : static const size_t kPtrSize = 4;
27 : static const uint8 kDummyData[] = { 0xaa, 0xbb, 0xcc, 0xdd };
28 :
29 : } // namespace
30 :
31 E : TEST(TrimTransformTest, BlocksAreTrimmed) {
32 E : BlockGraph bg;
33 :
34 : // These blocks have no references. The first should be trimmed and the
35 : // second left alone.
36 E : BlockGraph::Block* b1 = bg.AddBlock(BlockGraph::CODE_BLOCK, 10, "b1");
37 E : BlockGraph::Block* b2 = bg.AddBlock(BlockGraph::CODE_BLOCK, 10, "b2");
38 E : b1->SetData(kDummyData, sizeof(kDummyData));
39 E : b1->ResizeData(10);
40 E : b2->SetData(kDummyData, sizeof(kDummyData));
41 :
42 : // These blocks have references. The first should be trimmed, the second
43 : // left alone and the third should be extended.
44 E : BlockGraph::Block* b3 = bg.AddBlock(BlockGraph::CODE_BLOCK, 10, "b3");
45 E : BlockGraph::Block* b4 = bg.AddBlock(BlockGraph::CODE_BLOCK, 10, "b4");
46 E : BlockGraph::Block* b5 = bg.AddBlock(BlockGraph::CODE_BLOCK, 10, "b5");
47 E : b3->ResizeData(10);
48 : b3->SetReference(0, BlockGraph::Reference(BlockGraph::RELATIVE_REF,
49 E : kPtrSize, b1, 0, 0));
50 E : b4->ResizeData(kPtrSize);
51 : b4->SetReference(0, BlockGraph::Reference(BlockGraph::RELATIVE_REF,
52 E : kPtrSize, b1, 0, 0));
53 : b5->SetReference(0, BlockGraph::Reference(BlockGraph::RELATIVE_REF,
54 E : kPtrSize, b1, 0, 0));
55 :
56 E : TrimTransform trim_transform;
57 E : EXPECT_TRUE(ApplyBlockGraphTransform(&trim_transform, &bg, b1));
58 E : EXPECT_EQ(sizeof(kDummyData), b1->data_size());
59 E : EXPECT_EQ(sizeof(kDummyData), b2->data_size());
60 E : EXPECT_EQ(kPtrSize, b3->data_size());
61 E : EXPECT_EQ(kPtrSize, b4->data_size());
62 E : EXPECT_EQ(kPtrSize, b5->data_size());
63 E : }
64 :
65 : } // namespace transforms
66 : } // namespace block_graph
|