2 * Copyright 2016 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
10 #include "src/core/SkLRUCache.h"
11 #include "tests/Test.h"
14 Value(int value, int* counter)
28 DEF_TEST(LRUCacheSequential, r) {
31 static const int kSize = 100;
32 SkLRUCache<int, std::unique_ptr<Value>> test(kSize);
33 for (int i = 1; i < kSize * 2; i++) {
34 REPORTER_ASSERT(r, !test.find(i));
35 test.insert(i, std::make_unique<Value>(i * i, &instances));
36 REPORTER_ASSERT(r, test.find(i));
37 REPORTER_ASSERT(r, i * i == (*test.find(i))->fValue);
39 REPORTER_ASSERT(r, kSize == instances);
40 REPORTER_ASSERT(r, !test.find(i - kSize));
42 REPORTER_ASSERT(r, i == instances);
44 REPORTER_ASSERT(r, (int) test.count() == instances);
47 REPORTER_ASSERT(r, 0 == instances);
50 DEF_TEST(LRUCacheRandom, r) {
53 int seq[] = { 0, 1, 2, 3, 4, 1, 6, 2, 7, 5, 3, 2, 2, 3, 1, 7 };
54 int expected[] = { 7, 1, 3, 2, 5 };
55 static const int kSize = 5;
56 SkLRUCache<int, std::unique_ptr<Value>> test(kSize);
57 for (int i = 0; i < (int) (sizeof(seq) / sizeof(int)); i++) {
60 test.insert(k, std::make_unique<Value>(k, &instances));
63 REPORTER_ASSERT(r, kSize == instances);
64 REPORTER_ASSERT(r, kSize == test.count());
65 for (int i = 0; i < kSize; i++) {
67 REPORTER_ASSERT(r, test.find(k));
68 REPORTER_ASSERT(r, k == (*test.find(k))->fValue);
71 REPORTER_ASSERT(r, 0 == instances);