小组成员:姚凯文(kevinyao0901),姜嘉琪
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
4.1 KiB

  1. // Copyright (c) 2011 The LevelDB 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. See the AUTHORS file for names of contributors.
  4. #include "leveldb/cache.h"
  5. #include <vector>
  6. #include "util/coding.h"
  7. #include "util/testharness.h"
  8. namespace leveldb {
  9. // Conversions between numeric keys/values and the types expected by Cache.
  10. static std::string EncodeKey(int k) {
  11. std::string result;
  12. PutFixed32(&result, k);
  13. return result;
  14. }
  15. static int DecodeKey(const Slice& k) {
  16. assert(k.size() == 4);
  17. return DecodeFixed32(k.data());
  18. }
  19. static void* EncodeValue(uintptr_t v) { return reinterpret_cast<void*>(v); }
  20. static int DecodeValue(void* v) { return reinterpret_cast<uintptr_t>(v); }
  21. class CacheTest {
  22. public:
  23. static CacheTest* current_;
  24. static void Deleter(const Slice& key, void* v) {
  25. current_->deleted_keys_.push_back(DecodeKey(key));
  26. current_->deleted_values_.push_back(DecodeValue(v));
  27. }
  28. static const int kCacheSize = 100;
  29. std::vector<int> deleted_keys_;
  30. std::vector<int> deleted_values_;
  31. Cache* cache_;
  32. CacheTest() : cache_(NewLRUCache(kCacheSize)) {
  33. current_ = this;
  34. }
  35. ~CacheTest() {
  36. delete cache_;
  37. }
  38. int Lookup(int key) {
  39. Cache::Handle* handle = cache_->Lookup(EncodeKey(key));
  40. const int r = (handle == NULL) ? -1 : DecodeValue(cache_->Value(handle));
  41. if (handle != NULL) {
  42. cache_->Release(handle);
  43. }
  44. return r;
  45. }
  46. void Insert(int key, int value, int charge = 1) {
  47. cache_->Release(cache_->Insert(EncodeKey(key), EncodeValue(value), charge,
  48. &CacheTest::Deleter));
  49. }
  50. void Erase(int key) {
  51. cache_->Erase(EncodeKey(key));
  52. }
  53. };
  54. CacheTest* CacheTest::current_;
  55. TEST(CacheTest, HitAndMiss) {
  56. ASSERT_EQ(-1, Lookup(100));
  57. Insert(100, 101);
  58. ASSERT_EQ(101, Lookup(100));
  59. ASSERT_EQ(-1, Lookup(200));
  60. ASSERT_EQ(-1, Lookup(300));
  61. Insert(200, 201);
  62. ASSERT_EQ(101, Lookup(100));
  63. ASSERT_EQ(201, Lookup(200));
  64. ASSERT_EQ(-1, Lookup(300));
  65. Insert(100, 102);
  66. ASSERT_EQ(102, Lookup(100));
  67. ASSERT_EQ(201, Lookup(200));
  68. ASSERT_EQ(-1, Lookup(300));
  69. ASSERT_EQ(1, deleted_keys_.size());
  70. ASSERT_EQ(100, deleted_keys_[0]);
  71. ASSERT_EQ(101, deleted_values_[0]);
  72. }
  73. TEST(CacheTest, Erase) {
  74. Erase(200);
  75. ASSERT_EQ(0, deleted_keys_.size());
  76. Insert(100, 101);
  77. Insert(200, 201);
  78. Erase(100);
  79. ASSERT_EQ(-1, Lookup(100));
  80. ASSERT_EQ(201, Lookup(200));
  81. ASSERT_EQ(1, deleted_keys_.size());
  82. ASSERT_EQ(100, deleted_keys_[0]);
  83. ASSERT_EQ(101, deleted_values_[0]);
  84. Erase(100);
  85. ASSERT_EQ(-1, Lookup(100));
  86. ASSERT_EQ(201, Lookup(200));
  87. ASSERT_EQ(1, deleted_keys_.size());
  88. }
  89. TEST(CacheTest, EntriesArePinned) {
  90. Insert(100, 101);
  91. Cache::Handle* h1 = cache_->Lookup(EncodeKey(100));
  92. ASSERT_EQ(101, DecodeValue(cache_->Value(h1)));
  93. Insert(100, 102);
  94. Cache::Handle* h2 = cache_->Lookup(EncodeKey(100));
  95. ASSERT_EQ(102, DecodeValue(cache_->Value(h2)));
  96. ASSERT_EQ(0, deleted_keys_.size());
  97. cache_->Release(h1);
  98. ASSERT_EQ(1, deleted_keys_.size());
  99. ASSERT_EQ(100, deleted_keys_[0]);
  100. ASSERT_EQ(101, deleted_values_[0]);
  101. Erase(100);
  102. ASSERT_EQ(-1, Lookup(100));
  103. ASSERT_EQ(1, deleted_keys_.size());
  104. cache_->Release(h2);
  105. ASSERT_EQ(2, deleted_keys_.size());
  106. ASSERT_EQ(100, deleted_keys_[1]);
  107. ASSERT_EQ(102, deleted_values_[1]);
  108. }
  109. TEST(CacheTest, EvictionPolicy) {
  110. Insert(100, 101);
  111. Insert(200, 201);
  112. // Frequently used entry must be kept around
  113. for (int i = 0; i < kCacheSize; i++) {
  114. Insert(1000+i, 2000+i);
  115. ASSERT_EQ(2000+i, Lookup(1000+i));
  116. ASSERT_EQ(101, Lookup(100));
  117. }
  118. ASSERT_EQ(101, Lookup(100));
  119. ASSERT_EQ(2, deleted_keys_.size());
  120. ASSERT_EQ(200, deleted_keys_[0]);
  121. ASSERT_EQ(201, deleted_values_[0]);
  122. }
  123. TEST(CacheTest, HeavyEntry) {
  124. Insert(100, 101);
  125. Insert(200, 201, kCacheSize);
  126. ASSERT_EQ(1, deleted_keys_.size());
  127. ASSERT_EQ(100, deleted_keys_[0]);
  128. ASSERT_EQ(101, deleted_values_[0]);
  129. }
  130. TEST(CacheTest, NewId) {
  131. uint64_t a = cache_->NewId();
  132. uint64_t b = cache_->NewId();
  133. ASSERT_NE(a, b);
  134. }
  135. }
  136. int main(int argc, char** argv) {
  137. return leveldb::test::RunAllTests();
  138. }