Index: blimp/common/blob_cache/in_memory_blob_cache_unittest.cc |
diff --git a/blimp/common/blob_cache/in_memory_blob_cache_unittest.cc b/blimp/common/blob_cache/in_memory_blob_cache_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f759cf571029188d8c32484ef1ae6ae04481a641 |
--- /dev/null |
+++ b/blimp/common/blob_cache/in_memory_blob_cache_unittest.cc |
@@ -0,0 +1,100 @@ |
+// 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/in_memory_blob_cache.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace blimp { |
+namespace { |
+ |
+const std::string kFoo("foo"); |
Kevin M
2016/04/15 17:28:51
kConstants must be POD types (in this case, const
nyquist
2016/04/16 00:25:30
Done.
|
+const std::string kBar("bar"); |
+const std::string kDeadbeef("\xde\xad\xbe\xef"); |
+const std::string kForbiddenCode("\x4b\x1d\xc0\xd3"); |
+ |
+scoped_refptr<RefCountedVector> CreateRefCountedVector( |
+ const std::string& data) { |
+ 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; |
vmpstr
2016/04/15 18:18:54
:) Do you really need an Equal function?
nyquist
2016/04/16 00:25:30
Done.
|
+} |
+ |
+class InMemoryBlobCacheTest : public testing::Test { |
+ public: |
+ InMemoryBlobCacheTest() : cache_(base::WrapUnique(new InMemoryBlobCache)) {} |
vmpstr
2016/04/15 18:18:54
Why not just:
InMemoryBlobCache cache_;?
nyquist
2016/04/16 00:25:30
Done.
|
+ ~InMemoryBlobCacheTest() override {} |
+ |
+ protected: |
+ std::unique_ptr<InMemoryBlobCache> cache_; |
+}; |
+ |
+TEST_F(InMemoryBlobCacheTest, SimplePutContainsAndGetOperations) { |
+ EXPECT_FALSE(cache_->Contains(kFoo)); |
+ EXPECT_EQ(nullptr, cache_->Get(kFoo)); |
+ |
+ scoped_refptr<RefCountedVector> entry = CreateRefCountedVector(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(InMemoryBlobCacheTest, EnsureCacheGivesReferences) { |
+ scoped_refptr<RefCountedVector> entry1 = CreateRefCountedVector(kDeadbeef); |
+ scoped_refptr<RefCountedVector> entry2 = CreateRefCountedVector(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(InMemoryBlobCacheTest, TestDuplicatePut) { |
+ scoped_refptr<RefCountedVector> first = CreateRefCountedVector(kDeadbeef); |
+ scoped_refptr<RefCountedVector> duplicate = |
+ CreateRefCountedVector(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 |