1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/loader/resource_buffer.h"
6 #include "testing/gtest/include/gtest/gtest.h"
10 TEST(ResourceBufferTest
, BasicAllocations
) {
11 scoped_refptr
<ResourceBuffer
> buf
= new ResourceBuffer();
12 EXPECT_TRUE(buf
->Initialize(100, 5, 10));
13 EXPECT_TRUE(buf
->CanAllocate());
18 char* ptr
= buf
->Allocate(&size
);
21 EXPECT_TRUE(buf
->CanAllocate());
23 EXPECT_EQ(0, buf
->GetLastAllocationOffset());
25 buf
->ShrinkLastAllocation(2); // Less than our min allocation size.
26 EXPECT_EQ(0, buf
->GetLastAllocationOffset());
27 EXPECT_TRUE(buf
->CanAllocate());
33 char* ptr
= buf
->Allocate(&size
);
36 EXPECT_TRUE(buf
->CanAllocate());
38 EXPECT_EQ(5, buf
->GetLastAllocationOffset());
40 buf
->ShrinkLastAllocation(4);
41 EXPECT_EQ(5, buf
->GetLastAllocationOffset());
43 EXPECT_TRUE(buf
->CanAllocate());
47 TEST(ResourceBufferTest
, AllocateAndRecycle
) {
48 scoped_refptr
<ResourceBuffer
> buf
= new ResourceBuffer();
49 EXPECT_TRUE(buf
->Initialize(100, 5, 10));
54 EXPECT_EQ(0, buf
->GetLastAllocationOffset());
56 buf
->RecycleLeastRecentlyAllocated();
58 // Offset should again be 0.
60 EXPECT_EQ(0, buf
->GetLastAllocationOffset());
63 TEST(ResourceBufferTest
, WrapAround
) {
64 scoped_refptr
<ResourceBuffer
> buf
= new ResourceBuffer();
65 EXPECT_TRUE(buf
->Initialize(20, 10, 10));
75 // Create hole at the beginnning. Next allocation should go there.
76 buf
->RecycleLeastRecentlyAllocated();
81 EXPECT_EQ(0, buf
->GetLastAllocationOffset());
84 TEST(ResourceBufferTest
, WrapAround2
) {
85 scoped_refptr
<ResourceBuffer
> buf
= new ResourceBuffer();
86 EXPECT_TRUE(buf
->Initialize(30, 10, 10));
99 EXPECT_FALSE(buf
->CanAllocate());
101 // Create holes at first and second slots.
102 buf
->RecycleLeastRecentlyAllocated();
103 buf
->RecycleLeastRecentlyAllocated();
105 EXPECT_TRUE(buf
->CanAllocate());
107 buf
->Allocate(&size
);
109 EXPECT_EQ(0, buf
->GetLastAllocationOffset());
111 buf
->Allocate(&size
);
113 EXPECT_EQ(10, buf
->GetLastAllocationOffset());
115 EXPECT_FALSE(buf
->CanAllocate());
118 TEST(ResourceBufferTest
, Full
) {
119 scoped_refptr
<ResourceBuffer
> buf
= new ResourceBuffer();
120 EXPECT_TRUE(buf
->Initialize(20, 10, 10));
123 buf
->Allocate(&size
);
126 buf
->Allocate(&size
);
130 EXPECT_FALSE(buf
->CanAllocate());
132 // Still full, even if there is a small hole at the end.
133 buf
->ShrinkLastAllocation(5);
134 EXPECT_FALSE(buf
->CanAllocate());
137 } // namespace content