1 //
2 // Copyright 2017 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6 // SizedMRUCache_unittest.h: Unit tests for the sized MRU cached.
7
8 #include <gtest/gtest.h>
9
10 #include "libANGLE/SizedMRUCache.h"
11
12 namespace angle
13 {
14
15 using Blob = std::vector<uint8_t>;
16
MakeBlob(size_t size)17 Blob MakeBlob(size_t size)
18 {
19 Blob blob;
20 for (uint8_t value = 0; value < size; ++value)
21 {
22 blob.push_back(value);
23 }
24 return blob;
25 }
26
27 // Test a cache with a value that takes up maximum size.
TEST(SizedMRUCacheTest,MaxSizedValue)28 TEST(SizedMRUCacheTest, MaxSizedValue)
29 {
30 constexpr size_t kSize = 32;
31 SizedMRUCache<std::string, Blob> sizedCache(kSize);
32
33 EXPECT_TRUE(sizedCache.put("test", MakeBlob(kSize), kSize));
34 EXPECT_EQ(32u, sizedCache.size());
35 EXPECT_FALSE(sizedCache.empty());
36
37 EXPECT_TRUE(sizedCache.put("test2", MakeBlob(kSize), kSize));
38 EXPECT_EQ(32u, sizedCache.size());
39 EXPECT_FALSE(sizedCache.empty());
40
41 const Blob *blob = nullptr;
42 EXPECT_FALSE(sizedCache.get("test", &blob));
43
44 sizedCache.clear();
45 EXPECT_TRUE(sizedCache.empty());
46 }
47
48 // Test a cache with many small values, that it can handle unlimited inserts.
TEST(SizedMRUCacheTest,ManySmallValues)49 TEST(SizedMRUCacheTest, ManySmallValues)
50 {
51 constexpr size_t kSize = 32;
52 SizedMRUCache<size_t, size_t> sizedCache(kSize);
53
54 for (size_t value = 0; value < kSize; ++value)
55 {
56 size_t valueCopy = value;
57 EXPECT_TRUE(sizedCache.put(value, std::move(valueCopy), 1));
58
59 const size_t *qvalue = nullptr;
60 EXPECT_TRUE(sizedCache.get(value, &qvalue));
61 if (qvalue)
62 {
63 EXPECT_EQ(value, *qvalue);
64 }
65 }
66
67 EXPECT_EQ(32u, sizedCache.size());
68 EXPECT_FALSE(sizedCache.empty());
69
70 // Putting one element evicts the first element.
71 EXPECT_TRUE(sizedCache.put(kSize, std::move(static_cast<int>(kSize)), 1));
72
73 const size_t *qvalue = nullptr;
74 EXPECT_FALSE(sizedCache.get(0, &qvalue));
75
76 // Putting one large element cleans out the whole stack.
77 EXPECT_TRUE(sizedCache.put(kSize + 1, kSize + 1, kSize));
78 EXPECT_EQ(32u, sizedCache.size());
79 EXPECT_FALSE(sizedCache.empty());
80
81 for (size_t value = 0; value <= kSize; ++value)
82 {
83 EXPECT_FALSE(sizedCache.get(value, &qvalue));
84 }
85 EXPECT_TRUE(sizedCache.get(kSize + 1, &qvalue));
86 if (qvalue)
87 {
88 EXPECT_EQ(kSize + 1, *qvalue);
89 }
90
91 // Put a bunch of items in the cache sequentially.
92 for (size_t value = 0; value < kSize * 10; ++value)
93 {
94 size_t valueCopy = value;
95 EXPECT_TRUE(sizedCache.put(value, std::move(valueCopy), 1));
96 }
97
98 EXPECT_EQ(32u, sizedCache.size());
99 }
100
101 // Tests putting an oversize element.
TEST(SizedMRUCacheTest,OversizeValue)102 TEST(SizedMRUCacheTest, OversizeValue)
103 {
104 constexpr size_t kSize = 32;
105 SizedMRUCache<size_t, size_t> sizedCache(kSize);
106
107 EXPECT_FALSE(sizedCache.put(5, 5, 100));
108 }
109
110 } // namespace angle
111