Chromium Code Reviews| Index: blimp/common/blob_cache/infinite_blob_cache_unittest.cc |
| diff --git a/blimp/common/blob_cache/infinite_blob_cache_unittest.cc b/blimp/common/blob_cache/infinite_blob_cache_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..56e23768cf2aa7f00d9317243e2442a519fd757a |
| --- /dev/null |
| +++ b/blimp/common/blob_cache/infinite_blob_cache_unittest.cc |
| @@ -0,0 +1,98 @@ |
| +// Copyright 2015 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 <algorithm> |
| +#include <memory> |
| +#include <vector> |
| + |
| +#include "base/logging.h" |
| +#include "base/memory/ptr_util.h" |
| +#include "base/memory/ref_counted.h" |
| +#include "blimp/common/blob_cache/blob_cache.h" |
| +#include "blimp/common/blob_cache/infinite_blob_cache.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace blimp { |
| +namespace { |
| + |
| +const std::string kFoo("foo"); |
| +const std::string kBar("bar"); |
| +const std::string kDeadbeef("\xde\xad\xbe\xef"); |
| +const std::string kForbiddenCode("\x4b\x1d\xc0\xd3"); |
| + |
| +scoped_refptr<RefCountedVector> Create(const std::string& data) { |
|
Kevin M
2016/04/14 20:08:30
CreateRefCountedVector?
nyquist
2016/04/15 01:27:12
Done.
I don't see how this improves readability t
|
| + scoped_refptr<RefCountedVector> entry(new RefCountedVector); |
| + entry->data.assign(data.c_str(), data.c_str() + data.length()); |
| + return entry; |
| +} |
| + |
| +bool Equal(scoped_refptr<RefCountedVector> a, |
| + scoped_refptr<RefCountedVector> b) { |
| + return a->data == b->data; |
| +} |
| + |
| +class InfiniteBlobCacheTest : public testing::Test { |
| + public: |
| + InfiniteBlobCacheTest() : cache_(base::WrapUnique(new InfiniteBlobCache)) {} |
| + ~InfiniteBlobCacheTest() override {} |
| + |
| + protected: |
| + std::unique_ptr<InfiniteBlobCache> cache_; |
| +}; |
| + |
| +TEST_F(InfiniteBlobCacheTest, SimplePutContainsAndGetOperations) { |
| + EXPECT_FALSE(cache_->Contains(kFoo)); |
| + EXPECT_EQ(nullptr, cache_->Get(kFoo)); |
| + |
| + scoped_refptr<RefCountedVector> entry = Create(kDeadbeef); |
| + cache_->Put(kFoo, entry); |
| + |
| + EXPECT_TRUE(cache_->Contains(kFoo)); |
| + EXPECT_FALSE(cache_->Contains(kBar)); |
| + |
| + scoped_refptr<RefCountedVector> out = cache_->Get(kFoo); |
| + |
| + EXPECT_TRUE(Equal(entry, out)); |
| +} |
| + |
| +TEST_F(InfiniteBlobCacheTest, EnsureCacheGivesReferences) { |
| + scoped_refptr<RefCountedVector> entry1 = Create(kDeadbeef); |
| + scoped_refptr<RefCountedVector> entry2 = Create(kDeadbeef); |
| + EXPECT_TRUE(Equal(entry1, entry2)); |
| + |
| + cache_->Put(kFoo, entry1); |
| + scoped_refptr<RefCountedVector> out1 = cache_->Get(kFoo); |
| + |
| + // Change a single value of what was put into the cache. |
| + entry1->data[1] = '\x00'; |
| + |
| + scoped_refptr<RefCountedVector> out2 = cache_->Get(kFoo); |
| + |
| + EXPECT_TRUE(Equal(out1, out2)); |
| + EXPECT_TRUE(Equal(entry1, out1)); |
| + EXPECT_FALSE(Equal(entry2, out2)); |
| + |
| + // Change it back and ensure values now match. |
| + out1->data[1] = '\xad'; |
| + EXPECT_TRUE(Equal(out1, out2)); |
| + EXPECT_TRUE(Equal(entry2, out1)); |
| +} |
| + |
| +TEST_F(InfiniteBlobCacheTest, TestDuplicatePut) { |
| + scoped_refptr<RefCountedVector> first = Create(kDeadbeef); |
| + scoped_refptr<RefCountedVector> duplicate = Create(kForbiddenCode); |
| + cache_->Put(kFoo, first); |
| + |
| + scoped_refptr<RefCountedVector> out1 = cache_->Get(kFoo); |
| + EXPECT_TRUE(Equal(first, out1)); |
| + |
| + // The second put should be ignored and retrieving kFoo should still retrieve |
| + // the first item. |
| + cache_->Put(kFoo, duplicate); |
| + scoped_refptr<RefCountedVector> out2 = cache_->Get(kFoo); |
| + EXPECT_TRUE(Equal(first, out2)); |
| +} |
| + |
| +} // namespace |
| +} // namespace blimp |