mru_cache_unittest.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // Copyright (c) 2011 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. #include "butil/basictypes.h"
  5. #include "butil/containers/mru_cache.h"
  6. #include <gtest/gtest.h>
  7. namespace {
  8. int cached_item_live_count = 0;
  9. struct CachedItem {
  10. CachedItem() : value(0) {
  11. cached_item_live_count++;
  12. }
  13. explicit CachedItem(int new_value) : value(new_value) {
  14. cached_item_live_count++;
  15. }
  16. explicit CachedItem(const CachedItem& other) : value(other.value) {
  17. cached_item_live_count++;
  18. }
  19. ~CachedItem() {
  20. cached_item_live_count--;
  21. }
  22. int value;
  23. };
  24. } // namespace
  25. TEST(MRUCacheTest, Basic) {
  26. typedef butil::MRUCache<int, CachedItem> Cache;
  27. Cache cache(Cache::NO_AUTO_EVICT);
  28. // Check failure conditions
  29. {
  30. CachedItem test_item;
  31. EXPECT_TRUE(cache.Get(0) == cache.end());
  32. EXPECT_TRUE(cache.Peek(0) == cache.end());
  33. }
  34. static const int kItem1Key = 5;
  35. CachedItem item1(10);
  36. Cache::iterator inserted_item = cache.Put(kItem1Key, item1);
  37. EXPECT_EQ(1U, cache.size());
  38. // Check that item1 was properly inserted.
  39. {
  40. Cache::iterator found = cache.Get(kItem1Key);
  41. EXPECT_TRUE(inserted_item == cache.begin());
  42. EXPECT_TRUE(found != cache.end());
  43. found = cache.Peek(kItem1Key);
  44. EXPECT_TRUE(found != cache.end());
  45. EXPECT_EQ(kItem1Key, found->first);
  46. EXPECT_EQ(item1.value, found->second.value);
  47. }
  48. static const int kItem2Key = 7;
  49. CachedItem item2(12);
  50. cache.Put(kItem2Key, item2);
  51. EXPECT_EQ(2U, cache.size());
  52. // Check that item1 is the oldest since item2 was added afterwards.
  53. {
  54. Cache::reverse_iterator oldest = cache.rbegin();
  55. ASSERT_TRUE(oldest != cache.rend());
  56. EXPECT_EQ(kItem1Key, oldest->first);
  57. EXPECT_EQ(item1.value, oldest->second.value);
  58. }
  59. // Check that item1 is still accessible by key.
  60. {
  61. Cache::iterator test_item = cache.Get(kItem1Key);
  62. ASSERT_TRUE(test_item != cache.end());
  63. EXPECT_EQ(kItem1Key, test_item->first);
  64. EXPECT_EQ(item1.value, test_item->second.value);
  65. }
  66. // Check that retrieving item1 pushed item2 to oldest.
  67. {
  68. Cache::reverse_iterator oldest = cache.rbegin();
  69. ASSERT_TRUE(oldest != cache.rend());
  70. EXPECT_EQ(kItem2Key, oldest->first);
  71. EXPECT_EQ(item2.value, oldest->second.value);
  72. }
  73. // Remove the oldest item and check that item1 is now the only member.
  74. {
  75. Cache::reverse_iterator next = cache.Erase(cache.rbegin());
  76. EXPECT_EQ(1U, cache.size());
  77. EXPECT_TRUE(next == cache.rbegin());
  78. EXPECT_EQ(kItem1Key, next->first);
  79. EXPECT_EQ(item1.value, next->second.value);
  80. cache.Erase(cache.begin());
  81. EXPECT_EQ(0U, cache.size());
  82. }
  83. // Check that Clear() works properly.
  84. cache.Put(kItem1Key, item1);
  85. cache.Put(kItem2Key, item2);
  86. EXPECT_EQ(2U, cache.size());
  87. cache.Clear();
  88. EXPECT_EQ(0U, cache.size());
  89. }
  90. TEST(MRUCacheTest, GetVsPeek) {
  91. typedef butil::MRUCache<int, CachedItem> Cache;
  92. Cache cache(Cache::NO_AUTO_EVICT);
  93. static const int kItem1Key = 1;
  94. CachedItem item1(10);
  95. cache.Put(kItem1Key, item1);
  96. static const int kItem2Key = 2;
  97. CachedItem item2(20);
  98. cache.Put(kItem2Key, item2);
  99. // This should do nothing since the size is bigger than the number of items.
  100. cache.ShrinkToSize(100);
  101. // Check that item1 starts out as oldest
  102. {
  103. Cache::reverse_iterator iter = cache.rbegin();
  104. ASSERT_TRUE(iter != cache.rend());
  105. EXPECT_EQ(kItem1Key, iter->first);
  106. EXPECT_EQ(item1.value, iter->second.value);
  107. }
  108. // Check that Peek doesn't change ordering
  109. {
  110. Cache::iterator peekiter = cache.Peek(kItem1Key);
  111. ASSERT_TRUE(peekiter != cache.end());
  112. Cache::reverse_iterator iter = cache.rbegin();
  113. ASSERT_TRUE(iter != cache.rend());
  114. EXPECT_EQ(kItem1Key, iter->first);
  115. EXPECT_EQ(item1.value, iter->second.value);
  116. }
  117. }
  118. TEST(MRUCacheTest, KeyReplacement) {
  119. typedef butil::MRUCache<int, CachedItem> Cache;
  120. Cache cache(Cache::NO_AUTO_EVICT);
  121. static const int kItem1Key = 1;
  122. CachedItem item1(10);
  123. cache.Put(kItem1Key, item1);
  124. static const int kItem2Key = 2;
  125. CachedItem item2(20);
  126. cache.Put(kItem2Key, item2);
  127. static const int kItem3Key = 3;
  128. CachedItem item3(30);
  129. cache.Put(kItem3Key, item3);
  130. static const int kItem4Key = 4;
  131. CachedItem item4(40);
  132. cache.Put(kItem4Key, item4);
  133. CachedItem item5(50);
  134. cache.Put(kItem3Key, item5);
  135. EXPECT_EQ(4U, cache.size());
  136. for (int i = 0; i < 3; ++i) {
  137. Cache::reverse_iterator iter = cache.rbegin();
  138. ASSERT_TRUE(iter != cache.rend());
  139. }
  140. // Make it so only the most important element is there.
  141. cache.ShrinkToSize(1);
  142. Cache::iterator iter = cache.begin();
  143. EXPECT_EQ(kItem3Key, iter->first);
  144. EXPECT_EQ(item5.value, iter->second.value);
  145. }
  146. // Make sure that the owning version release its pointers properly.
  147. TEST(MRUCacheTest, Owning) {
  148. typedef butil::OwningMRUCache<int, CachedItem*> Cache;
  149. Cache cache(Cache::NO_AUTO_EVICT);
  150. int initial_count = cached_item_live_count;
  151. // First insert and item and then overwrite it.
  152. static const int kItem1Key = 1;
  153. cache.Put(kItem1Key, new CachedItem(20));
  154. cache.Put(kItem1Key, new CachedItem(22));
  155. // There should still be one item, and one extra live item.
  156. Cache::iterator iter = cache.Get(kItem1Key);
  157. EXPECT_EQ(1U, cache.size());
  158. EXPECT_TRUE(iter != cache.end());
  159. EXPECT_EQ(initial_count + 1, cached_item_live_count);
  160. // Now remove it.
  161. cache.Erase(cache.begin());
  162. EXPECT_EQ(initial_count, cached_item_live_count);
  163. // Now try another cache that goes out of scope to make sure its pointers
  164. // go away.
  165. {
  166. Cache cache2(Cache::NO_AUTO_EVICT);
  167. cache2.Put(1, new CachedItem(20));
  168. cache2.Put(2, new CachedItem(20));
  169. }
  170. // There should be no objects leaked.
  171. EXPECT_EQ(initial_count, cached_item_live_count);
  172. // Check that Clear() also frees things correctly.
  173. {
  174. Cache cache2(Cache::NO_AUTO_EVICT);
  175. cache2.Put(1, new CachedItem(20));
  176. cache2.Put(2, new CachedItem(20));
  177. EXPECT_EQ(initial_count + 2, cached_item_live_count);
  178. cache2.Clear();
  179. EXPECT_EQ(initial_count, cached_item_live_count);
  180. }
  181. }
  182. TEST(MRUCacheTest, AutoEvict) {
  183. typedef butil::OwningMRUCache<int, CachedItem*> Cache;
  184. static const Cache::size_type kMaxSize = 3;
  185. int initial_count = cached_item_live_count;
  186. {
  187. Cache cache(kMaxSize);
  188. static const int kItem1Key = 1, kItem2Key = 2, kItem3Key = 3, kItem4Key = 4;
  189. cache.Put(kItem1Key, new CachedItem(20));
  190. cache.Put(kItem2Key, new CachedItem(21));
  191. cache.Put(kItem3Key, new CachedItem(22));
  192. cache.Put(kItem4Key, new CachedItem(23));
  193. // The cache should only have kMaxSize items in it even though we inserted
  194. // more.
  195. EXPECT_EQ(kMaxSize, cache.size());
  196. }
  197. // There should be no objects leaked.
  198. EXPECT_EQ(initial_count, cached_item_live_count);
  199. }
  200. TEST(MRUCacheTest, HashingMRUCache) {
  201. // Very simple test to make sure that the hashing cache works correctly.
  202. typedef butil::HashingMRUCache<std::string, CachedItem> Cache;
  203. Cache cache(Cache::NO_AUTO_EVICT);
  204. CachedItem one(1);
  205. cache.Put("First", one);
  206. CachedItem two(2);
  207. cache.Put("Second", two);
  208. EXPECT_EQ(one.value, cache.Get("First")->second.value);
  209. EXPECT_EQ(two.value, cache.Get("Second")->second.value);
  210. cache.ShrinkToSize(1);
  211. EXPECT_EQ(two.value, cache.Get("Second")->second.value);
  212. EXPECT_TRUE(cache.Get("First") == cache.end());
  213. }