blob: 2e6932dcb0e0a201f911f824fdfaba61fe01f013 [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
5#include "gtest/gtest.h"
6
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07007#include "globals.h"
Elliott Hughes90a33692011-08-30 13:27:07 -07008#include "UniquePtr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07009
10namespace art {
11
12TEST(SpaceTest, Init) {
13 {
14 // Less than
Elliott Hughes90a33692011-08-30 13:27:07 -070015 UniquePtr<Space> space(Space::Create(16 * MB, 32 * MB, NULL));
16 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070017 }
18 {
19 // Equal to
Elliott Hughes90a33692011-08-30 13:27:07 -070020 UniquePtr<Space> space(Space::Create(16 * MB, 16 * MB, NULL));
21 EXPECT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070022 }
23 {
24 // Greater than
Elliott Hughes90a33692011-08-30 13:27:07 -070025 UniquePtr<Space> space(Space::Create(32 * MB, 16 * MB, NULL));
26 EXPECT_TRUE(space.get() == NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070027 }
28}
29
30TEST(SpaceTest, AllocAndFree) {
Elliott Hughes90a33692011-08-30 13:27:07 -070031 UniquePtr<Space> space(Space::Create(4 * MB, 16 * MB, NULL));
32 ASSERT_TRUE(space.get() != NULL);
Carl Shapiro69759ea2011-07-21 18:13:35 -070033
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