Mathieu Chartier | cc236d7 | 2012-07-20 10:29:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2012 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 "space_bitmap.h" |
| 18 | |
| 19 | #include "common_test.h" |
| 20 | #include "dlmalloc.h" |
| 21 | #include "globals.h" |
| 22 | #include "UniquePtr.h" |
| 23 | |
| 24 | #include <stdint.h> |
| 25 | |
| 26 | namespace art { |
| 27 | |
| 28 | class SpaceBitmapTest : public CommonTest { |
| 29 | public: |
| 30 | }; |
| 31 | |
| 32 | TEST_F(SpaceBitmapTest, Init) { |
| 33 | byte* heap_begin = reinterpret_cast<byte*>(0x10000000); |
| 34 | size_t heap_capacity = 16 * MB; |
| 35 | UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test-bitmap", |
| 36 | heap_begin, heap_capacity)); |
| 37 | EXPECT_TRUE(space_bitmap.get() != NULL); |
| 38 | } |
| 39 | |
| 40 | class BitmapVerify { |
| 41 | public: |
| 42 | BitmapVerify(SpaceBitmap* bitmap, const Object* begin, const Object* end) |
| 43 | : bitmap_(bitmap), |
| 44 | begin_(begin), |
| 45 | end_(end) {} |
| 46 | |
| 47 | void operator ()(const Object* obj) { |
| 48 | EXPECT_TRUE(obj >= begin_); |
| 49 | EXPECT_TRUE(obj <= end_); |
| 50 | EXPECT_TRUE(bitmap_->Test(obj) == ((reinterpret_cast<uintptr_t>(obj) & 0xF) != 0)); |
| 51 | } |
| 52 | |
| 53 | SpaceBitmap* bitmap_; |
| 54 | const Object* begin_; |
| 55 | const Object* end_; |
| 56 | }; |
| 57 | |
| 58 | TEST_F(SpaceBitmapTest, ScanRange) { |
| 59 | byte* heap_begin = reinterpret_cast<byte*>(0x10000000); |
| 60 | size_t heap_capacity = 16 * MB; |
| 61 | |
| 62 | UniquePtr<SpaceBitmap> space_bitmap(SpaceBitmap::Create("test-bitmap", |
| 63 | heap_begin, heap_capacity)); |
| 64 | EXPECT_TRUE(space_bitmap.get() != NULL); |
| 65 | |
| 66 | // Set all the odd bits in the first BitsPerWord * 3 to one. |
| 67 | for (size_t j = 0;j < kBitsPerWord * 3; ++j) { |
| 68 | const Object* obj = reinterpret_cast<Object*>(heap_begin + j * SpaceBitmap::kAlignment); |
| 69 | if (reinterpret_cast<uintptr_t>(obj) & 0xF) { |
| 70 | space_bitmap->Set(obj); |
| 71 | } |
| 72 | } |
| 73 | // Try every possible starting bit in the first word. Then for each starting bit, try each |
| 74 | // possible length up to a maximum of kBitsPerWord * 2 - 1 bits. |
| 75 | // This handles all the cases, having runs which start and end on the same word, and different |
| 76 | // words. |
| 77 | for (size_t i = 0; i < static_cast<size_t>(kBitsPerWord); ++i) { |
| 78 | Object* start = reinterpret_cast<Object*>(heap_begin + i * SpaceBitmap::kAlignment); |
| 79 | for (size_t j = 0; j < static_cast<size_t>(kBitsPerWord * 2); ++j) { |
| 80 | Object* end = reinterpret_cast<Object*>(heap_begin + (i + j) * SpaceBitmap::kAlignment); |
| 81 | BitmapVerify(space_bitmap.get(), start, end); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | } // namespace art |