blob: 73840214b1a3b58b0a65a6ed08bc92916bc244c0 [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"
8#include "scoped_ptr.h"
Carl Shapiro69759ea2011-07-21 18:13:35 -07009
10namespace art {
11
12TEST(SpaceTest, Init) {
13 {
14 // Less than
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070015 scoped_ptr<Space> space(Space::Create(16 * MB, 32 * MB, NULL));
Carl Shapiro69759ea2011-07-21 18:13:35 -070016 EXPECT_TRUE(space != NULL);
17 }
18 {
19 // Equal to
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070020 scoped_ptr<Space> space(Space::Create(16 * MB, 16 * MB, NULL));
Carl Shapiro69759ea2011-07-21 18:13:35 -070021 EXPECT_TRUE(space != NULL);
22 }
23 {
24 // Greater than
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070025 scoped_ptr<Space> space(Space::Create(32 * MB, 16 * MB, NULL));
Carl Shapiro69759ea2011-07-21 18:13:35 -070026 EXPECT_TRUE(space == NULL);
27 }
28}
29
30TEST(SpaceTest, AllocAndFree) {
Brian Carlstrom4a289ed2011-08-16 17:17:49 -070031 scoped_ptr<Space> space(Space::Create(4 * MB, 16 * MB, NULL));
Carl Shapiro69759ea2011-07-21 18:13:35 -070032 ASSERT_TRUE(space != NULL);
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