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 | |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 5 | #include "common_test.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 6 | #include "globals.h" |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 7 | #include "UniquePtr.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 8 | |
| 9 | namespace art { |
| 10 | |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 11 | class SpaceTest : public CommonTest {}; |
| 12 | |
| 13 | TEST_F(SpaceTest, Init) { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 14 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame^] | 15 | // Init < max == growth |
| 16 | UniquePtr<Space> space(Space::Create("test", 16 * MB, 32 * MB, 32 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 17 | EXPECT_TRUE(space.get() != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 18 | } |
| 19 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame^] | 20 | // Init == max == growth |
| 21 | UniquePtr<Space> space(Space::Create("test", 16 * MB, 16 * MB, 16 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 22 | EXPECT_TRUE(space.get() != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 23 | } |
| 24 | { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame^] | 25 | // Init > max == growth |
| 26 | UniquePtr<Space> space(Space::Create("test", 32 * MB, 16 * MB, 16 * MB, NULL)); |
| 27 | EXPECT_TRUE(space.get() == NULL); |
| 28 | } |
| 29 | { |
| 30 | // Growth == init < max |
| 31 | UniquePtr<Space> space(Space::Create("test", 16 * MB, 32 * MB, 16 * MB, NULL)); |
| 32 | EXPECT_TRUE(space.get() != NULL); |
| 33 | } |
| 34 | { |
| 35 | // Growth < init < max |
| 36 | UniquePtr<Space> space(Space::Create("test", 16 * MB, 32 * MB, 8 * MB, NULL)); |
| 37 | EXPECT_TRUE(space.get() == NULL); |
| 38 | } |
| 39 | { |
| 40 | // Init < growth < max |
| 41 | UniquePtr<Space> space(Space::Create("test", 8 * MB, 32 * MB, 16 * MB, NULL)); |
| 42 | EXPECT_TRUE(space.get() != NULL); |
| 43 | } |
| 44 | { |
| 45 | // Init < max < growth |
| 46 | UniquePtr<Space> space(Space::Create("test", 8 * MB, 16 * MB, 32 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 47 | EXPECT_TRUE(space.get() == NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Brian Carlstrom | 9b7f2c2 | 2011-09-27 14:35:04 -0700 | [diff] [blame] | 51 | TEST_F(SpaceTest, AllocAndFree) { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame^] | 52 | UniquePtr<Space> space(Space::Create("test", 4 * MB, 16 * MB, 16 * MB, NULL)); |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 53 | ASSERT_TRUE(space.get() != NULL); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 54 | |
| 55 | // Succeeds, fits without adjusting the max allowed footprint. |
| 56 | void* ptr1 = space->AllocWithoutGrowth(1 * MB); |
| 57 | EXPECT_TRUE(ptr1 != NULL); |
| 58 | |
| 59 | // Fails, requires a higher allowed footprint. |
| 60 | void* ptr2 = space->AllocWithoutGrowth(8 * MB); |
| 61 | EXPECT_TRUE(ptr2 == NULL); |
| 62 | |
| 63 | // Succeeds, adjusts the footprint. |
| 64 | void* ptr3 = space->AllocWithGrowth(8 * MB); |
| 65 | EXPECT_TRUE(ptr3 != NULL); |
| 66 | |
| 67 | // Fails, requires a higher allowed footprint. |
| 68 | void* ptr4 = space->AllocWithoutGrowth(8 * MB); |
| 69 | EXPECT_FALSE(ptr4 != NULL); |
| 70 | |
| 71 | // Also fails, requires a higher allowed footprint. |
| 72 | void* ptr5 = space->AllocWithGrowth(8 * MB); |
| 73 | EXPECT_FALSE(ptr5 != NULL); |
| 74 | |
| 75 | // Release some memory. |
| 76 | size_t free3 = space->Free(ptr3); |
| 77 | EXPECT_LE(8U * MB, free3); |
| 78 | |
| 79 | // Succeeds, now that memory has been freed. |
| 80 | void* ptr6 = space->AllocWithGrowth(9 * MB); |
| 81 | EXPECT_TRUE(ptr6 != NULL); |
| 82 | |
| 83 | // Final clean up. |
| 84 | size_t free1 = space->Free(ptr1); |
| 85 | EXPECT_LE(1U * MB, free1); |
| 86 | } |
| 87 | |
| 88 | } // namespace art |