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