1 : // Copyright 2015 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/refinery/core/addressed_data.h"
16 :
17 : #include "gtest/gtest.h"
18 :
19 : namespace refinery {
20 :
21 E : TEST(AddressedDataTest, BasicTest) {
22 : // Create an address range.
23 E : const Address kAddress = 80ULL;
24 E : const char kBuffer[] = "abcdef";
25 E : const AddressRange range(kAddress, sizeof(kBuffer));
26 E : AddressedData data(range, reinterpret_cast<const void*>(kBuffer));
27 :
28 : // Retrieving from outside the range fails.
29 : char retrieved;
30 E : ASSERT_FALSE(data.GetAt(kAddress - 1, &retrieved));
31 E : ASSERT_FALSE(data.GetAt(kAddress + sizeof(kBuffer), &retrieved));
32 :
33 : // Retrieving the head succeeds.
34 E : retrieved = '-';
35 E : ASSERT_TRUE(data.GetAt(kAddress, &retrieved));
36 E : ASSERT_EQ('a', retrieved);
37 :
38 : // Retrieving into the range succeeds.
39 E : retrieved = '-';
40 E : ASSERT_TRUE(data.GetAt(kAddress + 5, &retrieved));
41 E : ASSERT_EQ('f', retrieved);
42 E : }
43 :
44 E : TEST(AddressedDataTest, Slice) {
45 E : const Address kAddress = 80ULL;
46 E : const char kBuffer[] = "0123456789";
47 E : const AddressRange range(kAddress, sizeof(kBuffer));
48 :
49 E : AddressedData data(range, reinterpret_cast<const void*>(kBuffer));
50 :
51 E : AddressedData slice;
52 : // Starting a slice past the end should fail.
53 E : EXPECT_FALSE(data.Slice(sizeof(kBuffer) + 1, 1, &slice));
54 : // Slicing length past the end should fail.
55 E : EXPECT_FALSE(data.Slice(0, sizeof(kBuffer) + 1, &slice));
56 :
57 : // A zero slice is OK.
58 E : EXPECT_TRUE(data.Slice(sizeof(kBuffer), 0, &slice));
59 :
60 : // Test that valid slicing works.
61 E : EXPECT_TRUE(data.Slice(1, 1, &slice));
62 E : char retrieved = '-';
63 E : EXPECT_TRUE(slice.GetAt(kAddress + 1, &retrieved));
64 E : EXPECT_EQ('1', retrieved);
65 :
66 E : EXPECT_FALSE(slice.GetAt(kAddress + 2, &retrieved));
67 E : }
68 :
69 : } // namespace refinery
|