1 : // Copyright 2011 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 : #include "syzygy/core/address.h"
16 :
17 : #include "gtest/gtest.h"
18 : #include "syzygy/core/unittest_util.h"
19 :
20 : namespace core {
21 :
22 E : TEST(AddressTest, DefaultInitialization) {
23 E : EXPECT_EQ(0, RelativeAddress().value());
24 E : EXPECT_EQ(0, AbsoluteAddress().value());
25 E : EXPECT_EQ(0, FileOffsetAddress().value());
26 E : }
27 :
28 E : TEST(AddressTest, CreateInitialialized) {
29 E : const size_t kAddress = 0xCAFEBABE;
30 E : EXPECT_EQ(kAddress, RelativeAddress(kAddress).value());
31 E : EXPECT_EQ(kAddress, AbsoluteAddress(kAddress).value());
32 E : EXPECT_EQ(kAddress, FileOffsetAddress(kAddress).value());
33 E : }
34 :
35 E : TEST(AddressTest, Operators) {
36 E : const RelativeAddress kOne(1);
37 E : const RelativeAddress kTwo(2);
38 E : const RelativeAddress kThree(3);
39 :
40 E : EXPECT_TRUE(kOne < kTwo);
41 E : EXPECT_FALSE(kOne < kOne);
42 E : EXPECT_FALSE(kTwo < kOne);
43 :
44 E : EXPECT_TRUE(kOne <= kOne);
45 E : EXPECT_TRUE(kOne <= kTwo);
46 E : EXPECT_FALSE(kTwo <= kOne);
47 :
48 E : EXPECT_FALSE(kOne > kTwo);
49 E : EXPECT_TRUE(kTwo > kOne);
50 :
51 E : RelativeAddress addr(kOne);
52 :
53 E : EXPECT_TRUE(kOne == addr);
54 E : EXPECT_FALSE(addr == kTwo);
55 :
56 E : EXPECT_TRUE(kOne + 1 == kTwo);
57 E : EXPECT_TRUE(kOne == kTwo - 1);
58 E : EXPECT_EQ(1, kTwo - kOne);
59 :
60 E : EXPECT_EQ(1, addr.value());
61 E : addr.set_value(2);
62 E : EXPECT_EQ(2, addr.value());
63 :
64 E : addr += 1;
65 E : EXPECT_TRUE(addr == kThree);
66 E : addr -= 1;
67 E : EXPECT_TRUE(addr == kTwo);
68 E : }
69 :
70 E : TEST(AddressTest, AlignUp) {
71 E : const RelativeAddress one(1);
72 E : const RelativeAddress two(2);
73 E : const RelativeAddress four(4);
74 E : const RelativeAddress eight(8);
75 E : const RelativeAddress sixteen(16);
76 :
77 E : EXPECT_EQ(one.AlignUp(1), one);
78 E : EXPECT_EQ(one.AlignUp(2), two);
79 E : EXPECT_EQ(one.AlignUp(4), four);
80 E : EXPECT_EQ(one.AlignUp(8), eight);
81 E : EXPECT_EQ(one.AlignUp(16), sixteen);
82 :
83 E : EXPECT_TRUE(one.AlignUp(1).IsAligned(1));
84 E : EXPECT_TRUE(one.AlignUp(2).IsAligned(2));
85 E : EXPECT_TRUE(one.AlignUp(4).IsAligned(4));
86 E : EXPECT_TRUE(one.AlignUp(8).IsAligned(8));
87 E : EXPECT_TRUE(one.AlignUp(16).IsAligned(16));
88 E : }
89 :
90 E : TEST(AddressTest, GetAlignment) {
91 E : const uint32 max_alignment = 0x80000000;
92 :
93 E : const RelativeAddress zero(0);
94 E : EXPECT_EQ(max_alignment, zero.GetAlignment());
95 E : const RelativeAddress one(1);
96 :
97 E : for (uint32 i = 1; i < max_alignment; i <<= 1) {
98 E : RelativeAddress address(i);
99 E : EXPECT_EQ(i, address.GetAlignment());
100 E : }
101 :
102 E : RelativeAddress max_address(max_alignment);
103 E : EXPECT_EQ(max_alignment, max_address.GetAlignment());
104 E : }
105 :
106 E : TEST(AddressTest, Serialization) {
107 E : const RelativeAddress address(42);
108 :
109 E : EXPECT_TRUE(testing::TestSerialization(address));
110 E : }
111 :
112 : } // namespace core
|