1cb93a386Sopenharmony_ci/* 2cb93a386Sopenharmony_ci * Copyright 2016 Google Inc. 3cb93a386Sopenharmony_ci * 4cb93a386Sopenharmony_ci * Use of this source code is governed by a BSD-style license that can be 5cb93a386Sopenharmony_ci * found in the LICENSE file. 6cb93a386Sopenharmony_ci */ 7cb93a386Sopenharmony_ci 8cb93a386Sopenharmony_ci#include <memory> 9cb93a386Sopenharmony_ci 10cb93a386Sopenharmony_ci#include "src/core/SkLRUCache.h" 11cb93a386Sopenharmony_ci#include "tests/Test.h" 12cb93a386Sopenharmony_ci 13cb93a386Sopenharmony_cistruct Value { 14cb93a386Sopenharmony_ci Value(int value, int* counter) 15cb93a386Sopenharmony_ci : fValue(value) 16cb93a386Sopenharmony_ci , fCounter(counter) { 17cb93a386Sopenharmony_ci (*fCounter)++; 18cb93a386Sopenharmony_ci } 19cb93a386Sopenharmony_ci 20cb93a386Sopenharmony_ci ~Value() { 21cb93a386Sopenharmony_ci (*fCounter)--; 22cb93a386Sopenharmony_ci } 23cb93a386Sopenharmony_ci 24cb93a386Sopenharmony_ci int fValue; 25cb93a386Sopenharmony_ci int* fCounter; 26cb93a386Sopenharmony_ci}; 27cb93a386Sopenharmony_ci 28cb93a386Sopenharmony_ciDEF_TEST(LRUCacheSequential, r) { 29cb93a386Sopenharmony_ci int instances = 0; 30cb93a386Sopenharmony_ci { 31cb93a386Sopenharmony_ci static const int kSize = 100; 32cb93a386Sopenharmony_ci SkLRUCache<int, std::unique_ptr<Value>> test(kSize); 33cb93a386Sopenharmony_ci for (int i = 1; i < kSize * 2; i++) { 34cb93a386Sopenharmony_ci REPORTER_ASSERT(r, !test.find(i)); 35cb93a386Sopenharmony_ci test.insert(i, std::make_unique<Value>(i * i, &instances)); 36cb93a386Sopenharmony_ci REPORTER_ASSERT(r, test.find(i)); 37cb93a386Sopenharmony_ci REPORTER_ASSERT(r, i * i == (*test.find(i))->fValue); 38cb93a386Sopenharmony_ci if (i > kSize) { 39cb93a386Sopenharmony_ci REPORTER_ASSERT(r, kSize == instances); 40cb93a386Sopenharmony_ci REPORTER_ASSERT(r, !test.find(i - kSize)); 41cb93a386Sopenharmony_ci } else { 42cb93a386Sopenharmony_ci REPORTER_ASSERT(r, i == instances); 43cb93a386Sopenharmony_ci } 44cb93a386Sopenharmony_ci REPORTER_ASSERT(r, (int) test.count() == instances); 45cb93a386Sopenharmony_ci } 46cb93a386Sopenharmony_ci } 47cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 0 == instances); 48cb93a386Sopenharmony_ci} 49cb93a386Sopenharmony_ci 50cb93a386Sopenharmony_ciDEF_TEST(LRUCacheRandom, r) { 51cb93a386Sopenharmony_ci int instances = 0; 52cb93a386Sopenharmony_ci { 53cb93a386Sopenharmony_ci int seq[] = { 0, 1, 2, 3, 4, 1, 6, 2, 7, 5, 3, 2, 2, 3, 1, 7 }; 54cb93a386Sopenharmony_ci int expected[] = { 7, 1, 3, 2, 5 }; 55cb93a386Sopenharmony_ci static const int kSize = 5; 56cb93a386Sopenharmony_ci SkLRUCache<int, std::unique_ptr<Value>> test(kSize); 57cb93a386Sopenharmony_ci for (int i = 0; i < (int) (sizeof(seq) / sizeof(int)); i++) { 58cb93a386Sopenharmony_ci int k = seq[i]; 59cb93a386Sopenharmony_ci if (!test.find(k)) { 60cb93a386Sopenharmony_ci test.insert(k, std::make_unique<Value>(k, &instances)); 61cb93a386Sopenharmony_ci } 62cb93a386Sopenharmony_ci } 63cb93a386Sopenharmony_ci REPORTER_ASSERT(r, kSize == instances); 64cb93a386Sopenharmony_ci REPORTER_ASSERT(r, kSize == test.count()); 65cb93a386Sopenharmony_ci for (int i = 0; i < kSize; i++) { 66cb93a386Sopenharmony_ci int k = expected[i]; 67cb93a386Sopenharmony_ci REPORTER_ASSERT(r, test.find(k)); 68cb93a386Sopenharmony_ci REPORTER_ASSERT(r, k == (*test.find(k))->fValue); 69cb93a386Sopenharmony_ci } 70cb93a386Sopenharmony_ci } 71cb93a386Sopenharmony_ci REPORTER_ASSERT(r, 0 == instances); 72cb93a386Sopenharmony_ci} 73