OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 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. | |
4 | |
5 #include <algorithm> | |
6 #include <memory> | |
7 #include <vector> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/ptr_util.h" | |
11 #include "base/memory/ref_counted.h" | |
12 #include "blimp/common/blob_cache/blob_cache.h" | |
13 #include "blimp/common/blob_cache/infinite_blob_cache.h" | |
14 #include "testing/gtest/include/gtest/gtest.h" | |
15 | |
16 namespace blimp { | |
17 namespace { | |
18 | |
19 const std::string kFoo("foo"); | |
20 const std::string kBar("bar"); | |
21 const std::string kDeadbeef("\xde\xad\xbe\xef"); | |
22 const std::string kForbiddenCode("\x4b\x1d\xc0\xd3"); | |
23 | |
24 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
| |
25 scoped_refptr<RefCountedVector> entry(new RefCountedVector); | |
26 entry->data.assign(data.c_str(), data.c_str() + data.length()); | |
27 return entry; | |
28 } | |
29 | |
30 bool Equal(scoped_refptr<RefCountedVector> a, | |
31 scoped_refptr<RefCountedVector> b) { | |
32 return a->data == b->data; | |
33 } | |
34 | |
35 class InfiniteBlobCacheTest : public testing::Test { | |
36 public: | |
37 InfiniteBlobCacheTest() : cache_(base::WrapUnique(new InfiniteBlobCache)) {} | |
38 ~InfiniteBlobCacheTest() override {} | |
39 | |
40 protected: | |
41 std::unique_ptr<InfiniteBlobCache> cache_; | |
42 }; | |
43 | |
44 TEST_F(InfiniteBlobCacheTest, SimplePutContainsAndGetOperations) { | |
45 EXPECT_FALSE(cache_->Contains(kFoo)); | |
46 EXPECT_EQ(nullptr, cache_->Get(kFoo)); | |
47 | |
48 scoped_refptr<RefCountedVector> entry = Create(kDeadbeef); | |
49 cache_->Put(kFoo, entry); | |
50 | |
51 EXPECT_TRUE(cache_->Contains(kFoo)); | |
52 EXPECT_FALSE(cache_->Contains(kBar)); | |
53 | |
54 scoped_refptr<RefCountedVector> out = cache_->Get(kFoo); | |
55 | |
56 EXPECT_TRUE(Equal(entry, out)); | |
57 } | |
58 | |
59 TEST_F(InfiniteBlobCacheTest, EnsureCacheGivesReferences) { | |
60 scoped_refptr<RefCountedVector> entry1 = Create(kDeadbeef); | |
61 scoped_refptr<RefCountedVector> entry2 = Create(kDeadbeef); | |
62 EXPECT_TRUE(Equal(entry1, entry2)); | |
63 | |
64 cache_->Put(kFoo, entry1); | |
65 scoped_refptr<RefCountedVector> out1 = cache_->Get(kFoo); | |
66 | |
67 // Change a single value of what was put into the cache. | |
68 entry1->data[1] = '\x00'; | |
69 | |
70 scoped_refptr<RefCountedVector> out2 = cache_->Get(kFoo); | |
71 | |
72 EXPECT_TRUE(Equal(out1, out2)); | |
73 EXPECT_TRUE(Equal(entry1, out1)); | |
74 EXPECT_FALSE(Equal(entry2, out2)); | |
75 | |
76 // Change it back and ensure values now match. | |
77 out1->data[1] = '\xad'; | |
78 EXPECT_TRUE(Equal(out1, out2)); | |
79 EXPECT_TRUE(Equal(entry2, out1)); | |
80 } | |
81 | |
82 TEST_F(InfiniteBlobCacheTest, TestDuplicatePut) { | |
83 scoped_refptr<RefCountedVector> first = Create(kDeadbeef); | |
84 scoped_refptr<RefCountedVector> duplicate = Create(kForbiddenCode); | |
85 cache_->Put(kFoo, first); | |
86 | |
87 scoped_refptr<RefCountedVector> out1 = cache_->Get(kFoo); | |
88 EXPECT_TRUE(Equal(first, out1)); | |
89 | |
90 // The second put should be ignored and retrieving kFoo should still retrieve | |
91 // the first item. | |
92 cache_->Put(kFoo, duplicate); | |
93 scoped_refptr<RefCountedVector> out2 = cache_->Get(kFoo); | |
94 EXPECT_TRUE(Equal(first, out2)); | |
95 } | |
96 | |
97 } // namespace | |
98 } // namespace blimp | |
OLD | NEW |