diff options
author | Zeno Albisser <zeno.albisser@digia.com> | 2013-08-15 21:46:11 +0200 |
---|---|---|
committer | Zeno Albisser <zeno.albisser@digia.com> | 2013-08-15 21:46:11 +0200 |
commit | 679147eead574d186ebf3069647b4c23e8ccace6 (patch) | |
tree | fc247a0ac8ff119f7c8550879ebb6d3dd8d1ff69 /chromium/content/browser/loader/resource_buffer_unittest.cc | |
download | qtwebengine-chromium-679147eead574d186ebf3069647b4c23e8ccace6.tar.gz |
Initial import.
Diffstat (limited to 'chromium/content/browser/loader/resource_buffer_unittest.cc')
-rw-r--r-- | chromium/content/browser/loader/resource_buffer_unittest.cc | 137 |
1 files changed, 137 insertions, 0 deletions
diff --git a/chromium/content/browser/loader/resource_buffer_unittest.cc b/chromium/content/browser/loader/resource_buffer_unittest.cc new file mode 100644 index 00000000000..a9e90431544 --- /dev/null +++ b/chromium/content/browser/loader/resource_buffer_unittest.cc @@ -0,0 +1,137 @@ +// Copyright (c) 2012 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/browser/loader/resource_buffer.h" +#include "testing/gtest/include/gtest/gtest.h" + +namespace content { + +TEST(ResourceBufferTest, BasicAllocations) { + scoped_refptr<ResourceBuffer> buf = new ResourceBuffer(); + EXPECT_TRUE(buf->Initialize(100, 5, 10)); + EXPECT_TRUE(buf->CanAllocate()); + + // First allocation + { + int size; + char* ptr = buf->Allocate(&size); + EXPECT_TRUE(ptr); + EXPECT_EQ(10, size); + EXPECT_TRUE(buf->CanAllocate()); + + EXPECT_EQ(0, buf->GetLastAllocationOffset()); + + buf->ShrinkLastAllocation(2); // Less than our min allocation size. + EXPECT_EQ(0, buf->GetLastAllocationOffset()); + EXPECT_TRUE(buf->CanAllocate()); + } + + // Second allocation + { + int size; + char* ptr = buf->Allocate(&size); + EXPECT_TRUE(ptr); + EXPECT_EQ(10, size); + EXPECT_TRUE(buf->CanAllocate()); + + EXPECT_EQ(5, buf->GetLastAllocationOffset()); + + buf->ShrinkLastAllocation(4); + EXPECT_EQ(5, buf->GetLastAllocationOffset()); + + EXPECT_TRUE(buf->CanAllocate()); + } +} + +TEST(ResourceBufferTest, AllocateAndRecycle) { + scoped_refptr<ResourceBuffer> buf = new ResourceBuffer(); + EXPECT_TRUE(buf->Initialize(100, 5, 10)); + + int size; + + buf->Allocate(&size); + EXPECT_EQ(0, buf->GetLastAllocationOffset()); + + buf->RecycleLeastRecentlyAllocated(); + + // Offset should again be 0. + buf->Allocate(&size); + EXPECT_EQ(0, buf->GetLastAllocationOffset()); +} + +TEST(ResourceBufferTest, WrapAround) { + scoped_refptr<ResourceBuffer> buf = new ResourceBuffer(); + EXPECT_TRUE(buf->Initialize(20, 10, 10)); + + int size; + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + // Create hole at the beginnning. Next allocation should go there. + buf->RecycleLeastRecentlyAllocated(); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + EXPECT_EQ(0, buf->GetLastAllocationOffset()); +} + +TEST(ResourceBufferTest, WrapAround2) { + scoped_refptr<ResourceBuffer> buf = new ResourceBuffer(); + EXPECT_TRUE(buf->Initialize(30, 10, 10)); + + int size; + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + EXPECT_FALSE(buf->CanAllocate()); + + // Create holes at first and second slots. + buf->RecycleLeastRecentlyAllocated(); + buf->RecycleLeastRecentlyAllocated(); + + EXPECT_TRUE(buf->CanAllocate()); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + EXPECT_EQ(0, buf->GetLastAllocationOffset()); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + EXPECT_EQ(10, buf->GetLastAllocationOffset()); + + EXPECT_FALSE(buf->CanAllocate()); +} + +TEST(ResourceBufferTest, Full) { + scoped_refptr<ResourceBuffer> buf = new ResourceBuffer(); + EXPECT_TRUE(buf->Initialize(20, 10, 10)); + + int size; + buf->Allocate(&size); + EXPECT_EQ(10, size); + + buf->Allocate(&size); + EXPECT_EQ(10, size); + + // Full. + EXPECT_FALSE(buf->CanAllocate()); + + // Still full, even if there is a small hole at the end. + buf->ShrinkLastAllocation(5); + EXPECT_FALSE(buf->CanAllocate()); +} + +} // namespace content |