aboutsummaryrefslogtreecommitdiff
path: root/util/cache_test.cc
diff options
context:
space:
mode:
Diffstat (limited to 'util/cache_test.cc')
-rw-r--r--util/cache_test.cc39
1 files changed, 28 insertions, 11 deletions
diff --git a/util/cache_test.cc b/util/cache_test.cc
index dbab988..8a7f1c4 100644
--- a/util/cache_test.cc
+++ b/util/cache_test.cc
@@ -32,7 +32,7 @@ class CacheTest {
current_->deleted_values_.push_back(DecodeValue(v));
}
- static const int kCacheSize = 100;
+ static const int kCacheSize = 1000;
std::vector<int> deleted_keys_;
std::vector<int> deleted_values_;
Cache* cache_;
@@ -137,23 +137,40 @@ TEST(CacheTest, EvictionPolicy) {
Insert(200, 201);
// Frequently used entry must be kept around
- for (int i = 0; i < kCacheSize; i++) {
+ for (int i = 0; i < kCacheSize + 100; i++) {
Insert(1000+i, 2000+i);
ASSERT_EQ(2000+i, Lookup(1000+i));
ASSERT_EQ(101, Lookup(100));
}
ASSERT_EQ(101, Lookup(100));
- ASSERT_EQ(2, deleted_keys_.size());
- ASSERT_EQ(200, deleted_keys_[0]);
- ASSERT_EQ(201, deleted_values_[0]);
+ ASSERT_EQ(-1, Lookup(200));
}
-TEST(CacheTest, HeavyEntry) {
- Insert(100, 101);
- Insert(200, 201, kCacheSize);
- ASSERT_EQ(1, deleted_keys_.size());
- ASSERT_EQ(100, deleted_keys_[0]);
- ASSERT_EQ(101, deleted_values_[0]);
+TEST(CacheTest, HeavyEntries) {
+ // Add a bunch of light and heavy entries and then count the combined
+ // size of items still in the cache, which must be approximately the
+ // same as the total capacity.
+ const int kLight = 1;
+ const int kHeavy = 10;
+ int added = 0;
+ int index = 0;
+ while (added < 2*kCacheSize) {
+ const int weight = (index & 1) ? kLight : kHeavy;
+ Insert(index, 1000+index, weight);
+ added += weight;
+ index++;
+ }
+
+ int cached_weight = 0;
+ for (int i = 0; i < index; i++) {
+ const int weight = (i & 1 ? kLight : kHeavy);
+ int r = Lookup(i);
+ if (r >= 0) {
+ cached_weight += weight;
+ ASSERT_EQ(1000+i, r);
+ }
+ }
+ ASSERT_LE(cached_weight, kCacheSize + kCacheSize/10);
}
TEST(CacheTest, NewId) {