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 <memory> | |
6 #include <vector> | |
Kevin M
2016/04/13 23:29:52
Add newline after <vector>
nyquist
2016/04/14 19:33:23
Done.
| |
7 #include "base/logging.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "blimp/common/blob_cache/blob_cache.h" | |
11 #include "blimp/common/blob_cache/infinite_blob_cache.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace blimp { | |
15 namespace { | |
16 | |
17 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.
| |
18 scoped_refptr<RefCountedVector> entry(new RefCountedVector); | |
19 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?
| |
20 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.
| |
21 return entry; | |
22 } | |
23 | |
24 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.
| |
25 scoped_refptr<RefCountedVector> b) { | |
26 if (a.get()->data.size() != b.get()->data.size()) | |
27 return false; | |
28 for (size_t i = 0; i < a.get()->data.size(); ++i) { | |
29 if (static_cast<uint8_t>(a.get()->data[i]) != b.get()->data[i]) | |
30 return false; | |
31 } | |
32 return true; | |
33 } | |
34 | |
Kevin M
2016/04/13 23:29:52
Also test duplicate puts
nyquist
2016/04/14 19:33:22
Done.
| |
35 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.
| |
36 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.
| |
37 base::WrapUnique(new InfiniteBlobCache); | |
38 | |
39 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.
| |
40 EXPECT_EQ(nullptr, cache->Get("foo")); | |
41 | |
42 const char* data = "\xde\xad\xbe\xef"; | |
43 scoped_refptr<RefCountedVector> entry = Create(data, 4); | |
44 cache->Put("foo", entry); | |
45 | |
46 EXPECT_TRUE(cache->Contains("foo")); | |
47 EXPECT_FALSE(cache->Contains("bar")); | |
48 | |
49 scoped_refptr<RefCountedVector> out = cache->Get("foo"); | |
50 | |
51 EXPECT_TRUE(Equal(entry, out)); | |
52 } | |
53 | |
54 TEST(InfiniteBlobCacheTest, EnsureCacheGivesReferences) { | |
55 std::unique_ptr<InfiniteBlobCache> cache = | |
56 base::WrapUnique(new InfiniteBlobCache); | |
57 | |
58 const char* data = "\xde\xad\xbe\xef"; | |
59 scoped_refptr<RefCountedVector> entry1 = Create(data, 4); | |
60 scoped_refptr<RefCountedVector> entry2 = Create(data, 4); | |
61 EXPECT_TRUE(Equal(entry1, entry2)); | |
62 | |
63 cache->Put("foo", entry1); | |
64 scoped_refptr<RefCountedVector> out1 = cache->Get("foo"); | |
65 | |
66 // Change a single value of what was put into the cache. | |
67 entry1.get()->data[1] = '\x00'; | |
68 | |
69 scoped_refptr<RefCountedVector> out2 = cache->Get("foo"); | |
70 | |
71 EXPECT_TRUE(Equal(out1, out2)); | |
72 EXPECT_TRUE(Equal(entry1, out1)); | |
73 EXPECT_FALSE(Equal(entry2, out2)); | |
74 | |
75 // Change it back and ensure values now match. | |
76 out1.get()->data[1] = '\xad'; | |
77 EXPECT_TRUE(Equal(out1, out2)); | |
78 EXPECT_TRUE(Equal(entry2, out1)); | |
79 } | |
80 | |
81 } // namespace | |
82 } // namespace blimp | |
OLD | NEW |