blob: 0a6067c6556c0fe6f5655b34efcb06f1e71a7db3 [file] [log] [blame]
Carl Shapiro69759ea2011-07-21 18:13:35 -07001// Copyright 2011 Google Inc. All Rights Reserved.
Carl Shapiro69759ea2011-07-21 18:13:35 -07002
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07003#include "space.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07004
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -07005#include "common_test.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07006#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -07007#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07008
9namespace art {
10
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070011class SpaceTest : public CommonTest {};
12
13TEST_F(SpaceTest, Init) {
Carl Shapiro69759ea2011-07-21 18:13:35 -070014 {
15 // Less than
Elliott Hughes307f75d2011-10-12 18:04:40 -070016 UniquePtr<Space> space(Space::Create("test", 16 * MB, 32 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070017 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070018 }
19 {
20 // Equal to
Elliott Hughes307f75d2011-10-12 18:04:40 -070021 UniquePtr<Space> space(Space::Create("test", 16 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070022 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070023 }
24 {
25 // Greater than
Elliott Hughes307f75d2011-10-12 18:04:40 -070026 UniquePtr<Space> space(Space::Create("test", 32 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070027 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070028 }
29}
30
Brian Carlstrom9b7f2c22011-09-27 14:35:04 -070031TEST_F(SpaceTest, AllocAndFree) {
Elliott Hughes307f75d2011-10-12 18:04:40 -070032 UniquePtr<Space> space(Space::Create("test", 4 * MB, 16 * MB, NULL));
Elliott Hughes90a33692011-08-30 13:27:07 -070033 ASSERT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070034
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