File: scoped_ptr_expiring_cache_unittest.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (165 lines) | stat: -rw-r--r-- 4,945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <algorithm>
#include <vector>

#include "base/memory/ref_counted.h"
#include "chrome/browser/android/thumbnail/scoped_ptr_expiring_cache.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

#define MAX_CACHE_SIZE 5u

namespace {

unsigned int GenerateValue(unsigned int key) {
  return (key * key * 127) % 133 + key * 23;
}

class MockObject {
 public:
  static scoped_ptr<MockObject> Create(unsigned int key) {
    return make_scoped_ptr(new MockObject(key));
  }

  unsigned int value() const { return value_; }

 private:
  explicit MockObject(unsigned int key) : value_(GenerateValue(key)) {}
  unsigned int value_;

  DISALLOW_COPY_AND_ASSIGN(MockObject);
};

}  // namespace

typedef testing::Test ScopedPtrExpiringCacheTest;
typedef ScopedPtrExpiringCache<unsigned int, MockObject>
    TestScopedPtrExpiringCache;

TEST_F(ScopedPtrExpiringCacheTest, SimplePutAndGet) {
  TestScopedPtrExpiringCache cache(MAX_CACHE_SIZE);
  EXPECT_EQ(MAX_CACHE_SIZE, cache.MaximumCacheSize());
  EXPECT_EQ(0u, cache.size());

  for (unsigned int i = 0; i < MAX_CACHE_SIZE; i++) {
    cache.Put(i, MockObject::Create(i).Pass());
  }

  EXPECT_EQ(MAX_CACHE_SIZE, cache.size());

  unsigned int next_key = MAX_CACHE_SIZE;

  // One cache entry should have been evicted.
  cache.Put(next_key, MockObject::Create(next_key).Pass());
  EXPECT_EQ(MAX_CACHE_SIZE, cache.size());

  size_t cached_count = 0;
  for (unsigned int i = 0; i < MAX_CACHE_SIZE + 1; i++) {
    if (cache.Get(i)) {
      EXPECT_EQ(GenerateValue(i), cache.Get(i)->value());
      cached_count++;
    }
  }

  EXPECT_EQ(MAX_CACHE_SIZE, cached_count);

  // Test Get as membership test.
  cached_count = 0;
  for (unsigned int i = 0; i < MAX_CACHE_SIZE + 1; i++) {
    if (cache.Get(i))
      cached_count++;
  }
  EXPECT_EQ(MAX_CACHE_SIZE, cached_count);

  cache.Clear();
  EXPECT_EQ(0u, cache.size());

  for (unsigned int i = 0; i < MAX_CACHE_SIZE + 1; i++) {
    EXPECT_EQ(NULL, cache.Get(i));
  }
}

// The eviction policy is least-recently-used, where we define used as insertion
// into the cache.  We test that the first to be evicted is the first entry
// inserted into the cache.
TEST_F(ScopedPtrExpiringCacheTest, EvictedEntry) {
  TestScopedPtrExpiringCache cache(MAX_CACHE_SIZE);
  for (unsigned int i = 0; i < MAX_CACHE_SIZE; i++) {
    cache.Put(i, MockObject::Create(i).Pass());
  }

  unsigned int next_key = MAX_CACHE_SIZE;
  cache.Put(next_key, MockObject::Create(next_key).Pass());
  EXPECT_EQ(MAX_CACHE_SIZE, cache.size());
  EXPECT_EQ(GenerateValue(next_key), cache.Get(next_key)->value());

  // The first inserted entry should have been evicted.
  EXPECT_EQ(NULL, cache.Get(0));

  // The rest of the content should be present.
  for (unsigned int i = 1; i < MAX_CACHE_SIZE; i++) {
    EXPECT_TRUE(cache.Get(i) != NULL);
  }

  next_key++;

  // The first candidate to be evicted is the head of the iterator.
  unsigned int head_key = cache.begin()->first;
  EXPECT_TRUE(cache.Get(head_key) != NULL);
  cache.Put(next_key, MockObject::Create(next_key).Pass());

  EXPECT_NE(cache.begin()->first, head_key);
  EXPECT_EQ(NULL, cache.Get(head_key));
}

TEST_F(ScopedPtrExpiringCacheTest, RetainedEntry) {
  TestScopedPtrExpiringCache cache(MAX_CACHE_SIZE);
  for (unsigned int i = 0; i < MAX_CACHE_SIZE; i++) {
    cache.Put(i, MockObject::Create(i).Pass());
  }

  // Add (cache size - 1)-entries.
  for (unsigned int i = 0; i < MAX_CACHE_SIZE; i++) {
    EXPECT_TRUE(cache.Get(i) != NULL);
  }

  for (unsigned int i = MAX_CACHE_SIZE; i < 2 * MAX_CACHE_SIZE - 1; i++) {
    cache.Put(i, MockObject::Create(i).Pass());
  }

  EXPECT_EQ(MAX_CACHE_SIZE, cache.size());

  for (unsigned int i = 0; i < MAX_CACHE_SIZE - 1; i++) {
    EXPECT_EQ(NULL, cache.Get(i));
  }

  // The only retained entry (from the first round of insertion) is the last to
  // be inserted.
  EXPECT_TRUE(cache.Get(MAX_CACHE_SIZE - 1) != NULL);
}

// Test that the iterator order is the insertion order.  The first element of
// the iterator is the oldest entry in the cache.
TEST_F(ScopedPtrExpiringCacheTest, Iterator) {
  TestScopedPtrExpiringCache cache(MAX_CACHE_SIZE);
  std::vector<unsigned int> test_keys;
  for (unsigned int i = 0; i < MAX_CACHE_SIZE; i++) {
    test_keys.push_back(i);
  }
  std::random_shuffle(test_keys.begin(), test_keys.end());

  for (unsigned int i = 0; i < MAX_CACHE_SIZE; i++) {
    cache.Put(test_keys[i], MockObject::Create(test_keys[i]).Pass());
  }

  TestScopedPtrExpiringCache::iterator cache_iter = cache.begin();
  std::vector<unsigned int>::iterator key_iter = test_keys.begin();
  while (cache_iter != cache.end() && key_iter != test_keys.end()) {
    EXPECT_EQ(cache_iter->first, *key_iter);
    cache_iter++;
    key_iter++;
  }
}