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..d461d38d73a43228c88f3bfcf2dd9d7e8a240a86 |
| --- /dev/null |
| +++ b/blimp/common/blob_cache/infinite_blob_cache_unittest.cc |
| @@ -0,0 +1,82 @@ |
| +// 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 <memory> |
| +#include <vector> |
|
Kevin M
2016/04/13 23:29:52
Add newline after <vector>
nyquist
2016/04/14 19:33:23
Done.
|
| +#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 { |
| + |
| +scoped_refptr<RefCountedVector> Create(const char* data, size_t length) { |
|
Kevin M
2016/04/13 23:29:52
Just take a const std::string&?
nyquist
2016/04/14 19:33:23
Done.
|
| + scoped_refptr<RefCountedVector> entry(new RefCountedVector); |
| + for (size_t i = 0; i < length; ++i) |
|
vmpstr
2016/04/13 23:48:02
does "entry->data.assign(data, data + length);" wo
nyquist
2016/04/14 19:33:23
Done, I think?
|
| + entry.get()->data.push_back(static_cast<uint8_t>(data[i])); |
|
vmpstr
2016/04/13 23:48:02
you don't need .get()
nyquist
2016/04/14 19:33:22
Done.
|
| + return entry; |
| +} |
| + |
| +bool Equal(scoped_refptr<RefCountedVector> a, |
|
Kevin M
2016/04/13 23:29:52
You can use std::equal() to check if the vectors a
vmpstr
2016/04/13 23:48:02
Yeah.. isn't this just "a->data == b->data"?
nyquist
2016/04/14 19:33:23
Done.
|
| + scoped_refptr<RefCountedVector> b) { |
| + if (a.get()->data.size() != b.get()->data.size()) |
| + return false; |
| + for (size_t i = 0; i < a.get()->data.size(); ++i) { |
| + if (static_cast<uint8_t>(a.get()->data[i]) != b.get()->data[i]) |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
|
Kevin M
2016/04/13 23:29:52
Also test duplicate puts
nyquist
2016/04/14 19:33:22
Done.
|
| +TEST(InfiniteBlobCacheTest, SimplePutContainsAndGetOperations) { |
|
Kevin M
2016/04/13 23:29:52
Convention: create a testing::Test class and use T
nyquist
2016/04/14 19:33:23
Done.
|
| + std::unique_ptr<InfiniteBlobCache> cache = |
|
Kevin M
2016/04/13 23:29:52
Move cache into testing base class
nyquist
2016/04/14 19:33:22
Done.
|
| + base::WrapUnique(new InfiniteBlobCache); |
| + |
| + EXPECT_FALSE(cache->Contains("foo")); |
|
Kevin M
2016/04/13 23:29:52
Move string literals into kConstants
nyquist
2016/04/14 19:33:22
Done.
|
| + EXPECT_EQ(nullptr, cache->Get("foo")); |
| + |
| + const char* data = "\xde\xad\xbe\xef"; |
| + scoped_refptr<RefCountedVector> entry = Create(data, 4); |
| + cache->Put("foo", entry); |
| + |
| + EXPECT_TRUE(cache->Contains("foo")); |
| + EXPECT_FALSE(cache->Contains("bar")); |
| + |
| + scoped_refptr<RefCountedVector> out = cache->Get("foo"); |
| + |
| + EXPECT_TRUE(Equal(entry, out)); |
| +} |
| + |
| +TEST(InfiniteBlobCacheTest, EnsureCacheGivesReferences) { |
| + std::unique_ptr<InfiniteBlobCache> cache = |
| + base::WrapUnique(new InfiniteBlobCache); |
| + |
| + const char* data = "\xde\xad\xbe\xef"; |
| + scoped_refptr<RefCountedVector> entry1 = Create(data, 4); |
| + scoped_refptr<RefCountedVector> entry2 = Create(data, 4); |
| + EXPECT_TRUE(Equal(entry1, entry2)); |
| + |
| + cache->Put("foo", entry1); |
| + scoped_refptr<RefCountedVector> out1 = cache->Get("foo"); |
| + |
| + // Change a single value of what was put into the cache. |
| + entry1.get()->data[1] = '\x00'; |
| + |
| + scoped_refptr<RefCountedVector> out2 = cache->Get("foo"); |
| + |
| + EXPECT_TRUE(Equal(out1, out2)); |
| + EXPECT_TRUE(Equal(entry1, out1)); |
| + EXPECT_FALSE(Equal(entry2, out2)); |
| + |
| + // Change it back and ensure values now match. |
| + out1.get()->data[1] = '\xad'; |
| + EXPECT_TRUE(Equal(out1, out2)); |
| + EXPECT_TRUE(Equal(entry2, out1)); |
| +} |
| + |
| +} // namespace |
| +} // namespace blimp |