OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 "blimp/common/blob_cache/test_util.h" | |
15 #include "testing/gtest/include/gtest/gtest.h" | |
16 | |
17 namespace blimp { | |
18 namespace { | |
19 | |
20 const char kFoo[] = "foo"; | |
21 const char kBar[] = "bar"; | |
22 const char kDeadbeef[] = "\xde\xad\xbe\xef"; | |
23 const char kForbiddenCode[] = "\x4b\x1d\xc0\xd3"; | |
24 | |
25 class InMemoryBlobCacheTest : public testing::Test { | |
26 public: | |
27 InMemoryBlobCacheTest() {} | |
28 ~InMemoryBlobCacheTest() override {} | |
29 | |
30 protected: | |
31 InMemoryBlobCache cache_; | |
32 }; | |
33 | |
34 TEST_F(InMemoryBlobCacheTest, SimplePutContainsAndGetOperations) { | |
35 EXPECT_FALSE(cache_.Contains(kFoo)); | |
36 EXPECT_FALSE(cache_.Get(kFoo)); | |
37 EXPECT_FALSE(cache_.Contains(kBar)); | |
38 EXPECT_FALSE(cache_.Get(kBar)); | |
39 | |
40 BlobDataPtr blob_data_1 = CreateBlobDataPtr(kDeadbeef); | |
41 cache_.Put(kFoo, blob_data_1); | |
42 | |
43 EXPECT_TRUE(cache_.Contains(kFoo)); | |
44 EXPECT_FALSE(cache_.Contains(kBar)); | |
45 | |
46 BlobDataPtr blob_data_2 = CreateBlobDataPtr(kDeadbeef); | |
47 cache_.Put(kBar, blob_data_2); | |
48 | |
49 EXPECT_EQ(blob_data_1, cache_.Get(kFoo)); | |
50 EXPECT_EQ(blob_data_2, cache_.Get(kBar)); | |
51 | |
52 auto cache_state = cache_.GetCachedBlobIds(); | |
53 EXPECT_EQ(2u, cache_state.size()); | |
54 EXPECT_EQ(kBar, cache_state[0]); | |
55 EXPECT_EQ(kFoo, cache_state[1]); | |
56 } | |
57 | |
58 TEST_F(InMemoryBlobCacheTest, TestDuplicatePut) { | |
59 BlobDataPtr first = CreateBlobDataPtr(kDeadbeef); | |
60 BlobDataPtr duplicate = CreateBlobDataPtr(kForbiddenCode); | |
61 cache_.Put(kFoo, first); | |
62 | |
63 BlobDataPtr out1 = cache_.Get(kFoo); | |
64 EXPECT_EQ(first, out1); | |
65 | |
66 // The second put should be ignored and retrieving kFoo should still retrieve | |
67 // the first item. | |
68 cache_.Put(kFoo, duplicate); | |
69 BlobDataPtr out2 = cache_.Get(kFoo); | |
70 EXPECT_EQ(first, out2); | |
71 | |
72 auto cache_state = cache_.GetCachedBlobIds(); | |
73 EXPECT_EQ(1u, cache_state.size()); | |
74 EXPECT_EQ(kFoo, cache_state[0]); | |
75 } | |
76 | |
77 } // namespace | |
78 } // namespace blimp | |
OLD | NEW |