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