blob: 72ef6b6a4ccd28cf205bcaf1e4a7a0d812702113 [file] [log] [blame]
Dmitry Petrochenko135016a2014-04-03 14:35:54 +07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "stack_indirect_reference_table.h"
18#include "gtest/gtest.h"
19
20namespace art {
21
22// Test the offsets computed for members of StackIndirectReferenceTable. Because of cross-compiling
23// it is impossible the use OFFSETOF_MEMBER, so we do some reasonable computations ourselves. This
24// test checks whether we do the right thing.
25TEST(StackIndirectReferenceTableTest, Offsets) {
26 // As the members of StackIndirectReferenceTable are private, we cannot use OFFSETOF_MEMBER
27 // here. So do the inverse: set some data, and access it through pointers created from the offsets.
28
29 StackIndirectReferenceTable test_table(reinterpret_cast<mirror::Object*>(0x1234));
30 test_table.SetLink(reinterpret_cast<StackIndirectReferenceTable*>(0x5678));
31 test_table.SetNumberOfReferences(0x9ABC);
32
33 byte* table_base_ptr = reinterpret_cast<byte*>(&test_table);
34
35 {
36 uintptr_t* link_ptr = reinterpret_cast<uintptr_t*>(table_base_ptr +
37 StackIndirectReferenceTable::LinkOffset(kPointerSize));
38 EXPECT_EQ(*link_ptr, static_cast<size_t>(0x5678));
39 }
40
41 {
42 uint32_t* num_ptr = reinterpret_cast<uint32_t*>(table_base_ptr +
43 StackIndirectReferenceTable::NumberOfReferencesOffset(kPointerSize));
44 EXPECT_EQ(*num_ptr, static_cast<size_t>(0x9ABC));
45 }
46
47 {
48 // Assume sizeof(StackReference<mirror::Object>) == sizeof(uint32_t)
49 // TODO: How can we make this assumption-less but still access directly and fully?
50 EXPECT_EQ(sizeof(StackReference<mirror::Object>), sizeof(uint32_t));
51
52 uint32_t* ref_ptr = reinterpret_cast<uint32_t*>(table_base_ptr +
53 StackIndirectReferenceTable::ReferencesOffset(kPointerSize));
54 EXPECT_EQ(*ref_ptr, static_cast<uint32_t>(0x1234));
55 }
56}
57
58} // namespace art