Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / browser / loader / resource_buffer_unittest.cc
bloba9e90431544dcf148ec3d6d3ea0db94b3d0b0bd3
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"
8 namespace content {
10 TEST(ResourceBufferTest, BasicAllocations) {
11 scoped_refptr<ResourceBuffer> buf = new ResourceBuffer();
12 EXPECT_TRUE(buf->Initialize(100, 5, 10));
13 EXPECT_TRUE(buf->CanAllocate());
15 // First allocation
17 int size;
18 char* ptr = buf->Allocate(&size);
19 EXPECT_TRUE(ptr);
20 EXPECT_EQ(10, 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());
30 // Second allocation
32 int size;
33 char* ptr = buf->Allocate(&size);
34 EXPECT_TRUE(ptr);
35 EXPECT_EQ(10, 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));
51 int size;
53 buf->Allocate(&size);
54 EXPECT_EQ(0, buf->GetLastAllocationOffset());
56 buf->RecycleLeastRecentlyAllocated();
58 // Offset should again be 0.
59 buf->Allocate(&size);
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));
67 int size;
69 buf->Allocate(&size);
70 EXPECT_EQ(10, size);
72 buf->Allocate(&size);
73 EXPECT_EQ(10, size);
75 // Create hole at the beginnning. Next allocation should go there.
76 buf->RecycleLeastRecentlyAllocated();
78 buf->Allocate(&size);
79 EXPECT_EQ(10, size);
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));
88 int size;
90 buf->Allocate(&size);
91 EXPECT_EQ(10, size);
93 buf->Allocate(&size);
94 EXPECT_EQ(10, size);
96 buf->Allocate(&size);
97 EXPECT_EQ(10, size);
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);
108 EXPECT_EQ(10, size);
109 EXPECT_EQ(0, buf->GetLastAllocationOffset());
111 buf->Allocate(&size);
112 EXPECT_EQ(10, 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));
122 int size;
123 buf->Allocate(&size);
124 EXPECT_EQ(10, size);
126 buf->Allocate(&size);
127 EXPECT_EQ(10, size);
129 // Full.
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