LRUCacheTest.cpp 2.0 KB

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