summaryrefslogtreecommitdiff
path: root/libutils/LruCache_test.cpp
blob: 8b16947bef4734819cbf4e79cad6a6b848151c6c (plain)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>

#include <android/log.h>
#include <gtest/gtest.h>
#include <utils/JenkinsHash.h>
#include <utils/LruCache.h>

namespace {

typedef int SimpleKey;
typedef const char* StringValue;

struct ComplexKey {
    int k;

    explicit ComplexKey(int k) : k(k) {
        instanceCount += 1;
    }

    ComplexKey(const ComplexKey& other) : k(other.k) {
        instanceCount += 1;
    }

    ~ComplexKey() {
        instanceCount -= 1;
    }

    bool operator ==(const ComplexKey& other) const {
        return k == other.k;
    }

    bool operator !=(const ComplexKey& other) const {
        return k != other.k;
    }

    static ssize_t instanceCount;
};

ssize_t ComplexKey::instanceCount = 0;

struct ComplexValue {
    int v;

    explicit ComplexValue(int v) : v(v) {
        instanceCount += 1;
    }

    ComplexValue(const ComplexValue& other) : v(other.v) {
        instanceCount += 1;
    }

    ~ComplexValue() {
        instanceCount -= 1;
    }

    static ssize_t instanceCount;
};

ssize_t ComplexValue::instanceCount = 0;

struct KeyWithPointer {
    int *ptr;
    bool operator ==(const KeyWithPointer& other) const {
        return *ptr == *other.ptr;
    }
};

struct KeyFailsOnCopy : public ComplexKey {
    public:
    KeyFailsOnCopy(const KeyFailsOnCopy& key) : ComplexKey(key) {
        ADD_FAILURE();
    }
    KeyFailsOnCopy(int key) : ComplexKey(key) { }
};

} // namespace


namespace android {

typedef LruCache<ComplexKey, ComplexValue> ComplexCache;

template<> inline android::hash_t hash_type(const ComplexKey& value) {
    return hash_type(value.k);
}

template<> inline android::hash_t hash_type(const KeyWithPointer& value) {
    return hash_type(*value.ptr);
}

template<> inline android::hash_t hash_type(const KeyFailsOnCopy& value) {
    return hash_type<ComplexKey>(value);
}

class EntryRemovedCallback : public OnEntryRemoved<SimpleKey, StringValue> {
public:
    EntryRemovedCallback() : callbackCount(0), lastKey(-1), lastValue(nullptr) { }
    ~EntryRemovedCallback() {}
    void operator()(SimpleKey& k, StringValue& v) {
        callbackCount += 1;
        lastKey = k;
        lastValue = v;
    }
    ssize_t callbackCount;
    SimpleKey lastKey;
    StringValue lastValue;
};

class InvalidateKeyCallback : public OnEntryRemoved<KeyWithPointer, StringValue> {
public:
    void operator()(KeyWithPointer& k, StringValue&) {
        delete k.ptr;
        k.ptr = nullptr;
    }
};

class LruCacheTest : public testing::Test {
protected:
    virtual void SetUp() {
        ComplexKey::instanceCount = 0;
        ComplexValue::instanceCount = 0;
    }

    virtual void TearDown() {
        ASSERT_NO_FATAL_FAILURE(assertInstanceCount(0, 0));
    }

    void assertInstanceCount(ssize_t keys, ssize_t values) {
        if (keys != ComplexKey::instanceCount || values != ComplexValue::instanceCount) {
            FAIL() << "Expected " << keys << " keys and " << values << " values "
                    "but there were actually " << ComplexKey::instanceCount << " keys and "
                    << ComplexValue::instanceCount << " values";
        }
    }
};

TEST_F(LruCacheTest, Empty) {
    LruCache<SimpleKey, StringValue> cache(100);

    EXPECT_EQ(nullptr, cache.get(0));
    EXPECT_EQ(0u, cache.size());
}

TEST_F(LruCacheTest, Simple) {
    LruCache<SimpleKey, StringValue> cache(100);

    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");
    EXPECT_STREQ("one", cache.get(1));
    EXPECT_STREQ("two", cache.get(2));
    EXPECT_STREQ("three", cache.get(3));
    EXPECT_EQ(3u, cache.size());
}

TEST_F(LruCacheTest, MaxCapacity) {
    LruCache<SimpleKey, StringValue> cache(2);

    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");
    EXPECT_EQ(nullptr, cache.get(1));
    EXPECT_STREQ("two", cache.get(2));
    EXPECT_STREQ("three", cache.get(3));
    EXPECT_EQ(2u, cache.size());
}

TEST_F(LruCacheTest, RemoveLru) {
    LruCache<SimpleKey, StringValue> cache(100);

    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");
    cache.removeOldest();
    EXPECT_EQ(nullptr, cache.get(1));
    EXPECT_STREQ("two", cache.get(2));
    EXPECT_STREQ("three", cache.get(3));
    EXPECT_EQ(2u, cache.size());
}

TEST_F(LruCacheTest, GetUpdatesLru) {
    LruCache<SimpleKey, StringValue> cache(100);

    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");
    EXPECT_STREQ("one", cache.get(1));
    cache.removeOldest();
    EXPECT_STREQ("one", cache.get(1));
    EXPECT_EQ(nullptr, cache.get(2));
    EXPECT_STREQ("three", cache.get(3));
    EXPECT_EQ(2u, cache.size());
}

uint32_t hash_int(int x) {
    return JenkinsHashWhiten(JenkinsHashMix(0, x));
}

TEST_F(LruCacheTest, StressTest) {
    const size_t kCacheSize = 512;
    LruCache<SimpleKey, StringValue> cache(512);
    const size_t kNumKeys = 16 * 1024;
    const size_t kNumIters = 100000;
    char* strings[kNumKeys];

    for (size_t i = 0; i < kNumKeys; i++) {
        strings[i] = (char *)malloc(16);
        sprintf(strings[i], "%zu", i);
    }

    srandom(12345);
    int hitCount = 0;
    for (size_t i = 0; i < kNumIters; i++) {
        int index = random() % kNumKeys;
        uint32_t key = hash_int(index);
        const char *val = cache.get(key);
        if (val != nullptr) {
            EXPECT_EQ(strings[index], val);
            hitCount++;
        } else {
            cache.put(key, strings[index]);
        }
    }
    size_t expectedHitCount = kNumIters * kCacheSize / kNumKeys;
    EXPECT_LT(int(expectedHitCount * 0.9), hitCount);
    EXPECT_GT(int(expectedHitCount * 1.1), hitCount);
    EXPECT_EQ(kCacheSize, cache.size());

    for (size_t i = 0; i < kNumKeys; i++) {
        free((void *)strings[i]);
    }
}

TEST_F(LruCacheTest, NoLeak) {
    ComplexCache cache(100);

    cache.put(ComplexKey(0), ComplexValue(0));
    cache.put(ComplexKey(1), ComplexValue(1));
    EXPECT_EQ(2U, cache.size());
    assertInstanceCount(2, 3);  // the member mNullValue counts as an instance
}

TEST_F(LruCacheTest, Clear) {
    ComplexCache cache(100);

    cache.put(ComplexKey(0), ComplexValue(0));
    cache.put(ComplexKey(1), ComplexValue(1));
    EXPECT_EQ(2U, cache.size());
    assertInstanceCount(2, 3);
    cache.clear();
    assertInstanceCount(0, 1);
}

TEST_F(LruCacheTest, ClearNoDoubleFree) {
    {
        ComplexCache cache(100);

        cache.put(ComplexKey(0), ComplexValue(0));
        cache.put(ComplexKey(1), ComplexValue(1));
        EXPECT_EQ(2U, cache.size());
        assertInstanceCount(2, 3);
        cache.removeOldest();
        cache.clear();
        assertInstanceCount(0, 1);
    }
    assertInstanceCount(0, 0);
}

TEST_F(LruCacheTest, ClearReuseOk) {
    ComplexCache cache(100);

    cache.put(ComplexKey(0), ComplexValue(0));
    cache.put(ComplexKey(1), ComplexValue(1));
    EXPECT_EQ(2U, cache.size());
    assertInstanceCount(2, 3);
    cache.clear();
    assertInstanceCount(0, 1);
    cache.put(ComplexKey(0), ComplexValue(0));
    cache.put(ComplexKey(1), ComplexValue(1));
    EXPECT_EQ(2U, cache.size());
    assertInstanceCount(2, 3);
}

TEST_F(LruCacheTest, Callback) {
    EntryRemovedCallback callback;
    LruCache<SimpleKey, StringValue> cache(100);
    cache.setOnEntryRemovedListener(&callback);

    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");
    EXPECT_EQ(3U, cache.size());
    cache.removeOldest();
    EXPECT_EQ(1, callback.callbackCount);
    EXPECT_EQ(1, callback.lastKey);
    EXPECT_STREQ("one", callback.lastValue);
}

TEST_F(LruCacheTest, CallbackOnClear) {
    EntryRemovedCallback callback;
    LruCache<SimpleKey, StringValue> cache(100);
    cache.setOnEntryRemovedListener(&callback);

    cache.put(1, "one");
    cache.put(2, "two");
    cache.put(3, "three");
    EXPECT_EQ(3U, cache.size());
    cache.clear();
    EXPECT_EQ(3, callback.callbackCount);
}

TEST_F(LruCacheTest, CallbackRemovesKeyWorksOK) {
    InvalidateKeyCallback callback;
    LruCache<KeyWithPointer, StringValue> cache(1);
    cache.setOnEntryRemovedListener(&callback);
    KeyWithPointer key1;
    key1.ptr = new int(1);
    KeyWithPointer key2;
    key2.ptr = new int(2);

    cache.put(key1, "one");
    // As the size of the cache is 1, the put will call the callback.
    // Make sure everything goes smoothly even if the callback invalidates
    // the key (b/24785286)
    cache.put(key2, "two");
    EXPECT_EQ(1U, cache.size());
    EXPECT_STREQ("two", cache.get(key2));
    cache.clear();
}

TEST_F(LruCacheTest, IteratorCheck) {
    LruCache<int, int> cache(100);

    cache.put(1, 4);
    cache.put(2, 5);
    cache.put(3, 6);
    EXPECT_EQ(3U, cache.size());

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        int v = it.value();
        // Check we haven't seen the value before.
        EXPECT_TRUE(returnedValues.find(v) == returnedValues.end());
        returnedValues.insert(v);
    }
    EXPECT_EQ(std::unordered_set<int>({4, 5, 6}), returnedValues);
}

TEST_F(LruCacheTest, EmptyCacheIterator) {
    // Check that nothing crashes...
    LruCache<int, int> cache(100);

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        returnedValues.insert(it.value());
    }
    EXPECT_EQ(std::unordered_set<int>(), returnedValues);
}

TEST_F(LruCacheTest, OneElementCacheIterator) {
    // Check that nothing crashes...
    LruCache<int, int> cache(100);
    cache.put(1, 2);

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        returnedValues.insert(it.value());
    }
    EXPECT_EQ(std::unordered_set<int>({ 2 }), returnedValues);
}

TEST_F(LruCacheTest, OneElementCacheRemove) {
    LruCache<int, int> cache(100);
    cache.put(1, 2);

    cache.remove(1);

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        returnedValues.insert(it.value());
    }
    EXPECT_EQ(std::unordered_set<int>({ }), returnedValues);
}

TEST_F(LruCacheTest, Remove) {
    LruCache<int, int> cache(100);
    cache.put(1, 4);
    cache.put(2, 5);
    cache.put(3, 6);

    cache.remove(2);

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        returnedValues.insert(it.value());
    }
    EXPECT_EQ(std::unordered_set<int>({ 4, 6 }), returnedValues);
}

TEST_F(LruCacheTest, RemoveYoungest) {
    LruCache<int, int> cache(100);
    cache.put(1, 4);
    cache.put(2, 5);
    cache.put(3, 6);

    cache.remove(3);

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        returnedValues.insert(it.value());
    }
    EXPECT_EQ(std::unordered_set<int>({ 4, 5 }), returnedValues);
}

TEST_F(LruCacheTest, RemoveNonMember) {
    LruCache<int, int> cache(100);
    cache.put(1, 4);
    cache.put(2, 5);
    cache.put(3, 6);

    cache.remove(7);

    LruCache<int, int>::Iterator it(cache);
    std::unordered_set<int> returnedValues;
    while (it.next()) {
        returnedValues.insert(it.value());
    }
    EXPECT_EQ(std::unordered_set<int>({ 4, 5, 6 }), returnedValues);
}

TEST_F(LruCacheTest, DontCopyKeyInGet) {
    LruCache<KeyFailsOnCopy, KeyFailsOnCopy> cache(1);
    // Check that get doesn't copy the key
    cache.get(KeyFailsOnCopy(0));
}

}