Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: cshapiro@google.com (Carl Shapiro) |
| 3 | |
| 4 | #include "src/space.h" |
| 5 | |
| 6 | #include "gtest/gtest.h" |
| 7 | |
| 8 | #include "src/globals.h" |
| 9 | #include "src/scoped_ptr.h" |
| 10 | |
| 11 | namespace art { |
| 12 | |
| 13 | TEST(SpaceTest, Init) { |
| 14 | { |
| 15 | // Less than |
| 16 | scoped_ptr<Space> space(Space::Create(16 * MB, 32 * MB)); |
| 17 | EXPECT_TRUE(space != NULL); |
| 18 | } |
| 19 | { |
| 20 | // Equal to |
| 21 | scoped_ptr<Space> space(Space::Create(16 * MB, 16 * MB)); |
| 22 | EXPECT_TRUE(space != NULL); |
| 23 | } |
| 24 | { |
| 25 | // Greater than |
| 26 | scoped_ptr<Space> space(Space::Create(32 * MB, 16 * MB)); |
| 27 | EXPECT_TRUE(space == NULL); |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | TEST(SpaceTest, AllocAndFree) { |
| 32 | scoped_ptr<Space> space(Space::Create(4 * MB, 16 * MB)); |
| 33 | ASSERT_TRUE(space != NULL); |
| 34 | |
| 35 | // Succeeds, fits without adjusting the max allowed footprint. |
| 36 | void* ptr1 = space->AllocWithoutGrowth(1 * MB); |
| 37 | EXPECT_TRUE(ptr1 != NULL); |
| 38 | |
| 39 | // Fails, requires a higher allowed footprint. |
| 40 | void* ptr2 = space->AllocWithoutGrowth(8 * MB); |
| 41 | EXPECT_TRUE(ptr2 == NULL); |
| 42 | |
| 43 | // Succeeds, adjusts the footprint. |
| 44 | void* ptr3 = space->AllocWithGrowth(8 * MB); |
| 45 | EXPECT_TRUE(ptr3 != NULL); |
| 46 | |
| 47 | // Fails, requires a higher allowed footprint. |
| 48 | void* ptr4 = space->AllocWithoutGrowth(8 * MB); |
| 49 | EXPECT_FALSE(ptr4 != NULL); |
| 50 | |
| 51 | // Also fails, requires a higher allowed footprint. |
| 52 | void* ptr5 = space->AllocWithGrowth(8 * MB); |
| 53 | EXPECT_FALSE(ptr5 != NULL); |
| 54 | |
| 55 | // Release some memory. |
| 56 | size_t free3 = space->Free(ptr3); |
| 57 | EXPECT_LE(8U * MB, free3); |
| 58 | |
| 59 | // Succeeds, now that memory has been freed. |
| 60 | void* ptr6 = space->AllocWithGrowth(9 * MB); |
| 61 | EXPECT_TRUE(ptr6 != NULL); |
| 62 | |
| 63 | // Final clean up. |
| 64 | size_t free1 = space->Free(ptr1); |
| 65 | EXPECT_LE(1U * MB, free1); |
| 66 | } |
| 67 | |
| 68 | } // namespace art |