Cache.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "pch.h"
  2. #include <Cache.h>
  3. #include "CppUnitTest.h"
  4. using namespace Microsoft::VisualStudio::CppUnitTestFramework;
  5. namespace FrameworkTests
  6. {
  7. TEST_CLASS (CacheTests)
  8. {
  9. public:
  10. TEST_METHOD (EmtptyTest)
  11. {
  12. Framework::Cache<int, int> cache(
  13. 100,
  14. [](int i) { return i; },
  15. Framework::CacheCleanupStrategy::RANDOM);
  16. Assert::IsTrue(cache.getCurrentSize() == 0,
  17. L"getCurrentSize() on empty cache should be 0");
  18. Assert::IsTrue(
  19. cache.getMaxSize() == 100, L"getMaxSize() on empty cache");
  20. }
  21. TEST_METHOD (PutTest)
  22. {
  23. Framework::Cache<int, int> cache(
  24. 2,
  25. [](int i) { return i; },
  26. Framework::CacheCleanupStrategy::RANDOM);
  27. cache.put(0, 100);
  28. cache.put(1, 10);
  29. cache.put(2, 1000);
  30. Assert::IsTrue(cache.getCurrentSize() == 2,
  31. L"unexpected count of elements in cache");
  32. Assert::IsTrue(cache.has(0) ? cache.get(0) == 100 : 1,
  33. L"invalid value at key 0 in cache after adding elements");
  34. Assert::IsTrue(cache.has(1) ? cache.get(1) == 10 : 1,
  35. L"invalid value at key 1 in cache after adding elements");
  36. Assert::IsTrue(cache.get(2) == 1000,
  37. L"invalid value at key 2 in cache after adding elements");
  38. int count = 0;
  39. for (Framework::MapEntry<int, int> i : cache)
  40. {
  41. if (i.getKey() == 0)
  42. Assert::IsTrue(i.getValue() == 100,
  43. L"invalid value at key 0 in cache after adding "
  44. L"elements");
  45. else if (i.getKey() == 1)
  46. Assert::IsTrue(i.getValue() == 10,
  47. L"invalid value at key 1 in cache after adding "
  48. L"elements");
  49. else if (i.getKey() == 2)
  50. Assert::IsTrue(i.getValue() == 1000,
  51. L"invalid value at key 2 in cache after adding "
  52. L"elements");
  53. else
  54. Assert::Fail(L"invalid key in cache after adding elements");
  55. count++;
  56. }
  57. Assert::IsTrue(
  58. count == 2, L"unexpected count of elements in cache");
  59. }
  60. TEST_METHOD (OldestTest)
  61. {
  62. Framework::Cache<int, int> cache(
  63. 2,
  64. [](int i) { return i; },
  65. Framework::CacheCleanupStrategy::OLDEST);
  66. cache.put(0, 100);
  67. Sleep(1000);
  68. cache.put(1, 10);
  69. Sleep(1000);
  70. cache.put(2, 1000);
  71. Assert::IsTrue(cache.getCurrentSize() == 2,
  72. L"unexpected count of elements in cache");
  73. Assert::IsFalse(cache.has(0),
  74. L"invalid value at key 0 in cache after adding elements");
  75. Assert::IsTrue(cache.has(1) && cache.get(1) == 10,
  76. L"invalid value at key 1 in cache after adding elements");
  77. Assert::IsTrue(cache.get(2) == 1000,
  78. L"invalid value at key 2 in cache after adding elements");
  79. int count = 0;
  80. for (Framework::MapEntry<int, int> i : cache)
  81. {
  82. if (i.getKey() == 1)
  83. Assert::IsTrue(i.getValue() == 10,
  84. L"invalid value at key 1 in cache after adding "
  85. L"elements");
  86. else if (i.getKey() == 2)
  87. Assert::IsTrue(i.getValue() == 1000,
  88. L"invalid value at key 2 in cache after adding "
  89. L"elements");
  90. else
  91. Assert::Fail(L"invalid key in cache after adding elements");
  92. count++;
  93. }
  94. Assert::IsTrue(
  95. count == 2, L"unexpected count of elements in cache");
  96. }
  97. TEST_METHOD (LongestNotUsedTest)
  98. {
  99. Framework::Cache<int, int> cache(
  100. 2,
  101. [](int i) { return i; },
  102. Framework::CacheCleanupStrategy::LONGEST_NOT_USED);
  103. cache.put(0, 100);
  104. Sleep(1000);
  105. cache.put(1, 10);
  106. Sleep(1000);
  107. cache.get(0);
  108. cache.put(2, 1000);
  109. Assert::IsTrue(cache.getCurrentSize() == 2,
  110. L"unexpected count of elements in cache");
  111. Assert::IsTrue(cache.has(0) && cache.get(0) == 100,
  112. L"invalid value at key 0 in cache after adding elements");
  113. Assert::IsFalse(cache.has(1),
  114. L"invalid value at key 1 in cache after adding elements");
  115. Assert::IsTrue(cache.get(2) == 1000,
  116. L"invalid value at key 2 in cache after adding elements");
  117. int count = 0;
  118. for (Framework::MapEntry<int, int> i : cache)
  119. {
  120. if (i.getKey() == 0)
  121. Assert::IsTrue(i.getValue() == 100,
  122. L"invalid value at key 0 in cache after adding "
  123. L"elements");
  124. else if (i.getKey() == 2)
  125. Assert::IsTrue(i.getValue() == 1000,
  126. L"invalid value at key 2 in cache after adding "
  127. L"elements");
  128. else
  129. Assert::Fail(L"invalid key in cache after adding elements");
  130. count++;
  131. }
  132. Assert::IsTrue(
  133. count == 2, L"unexpected count of elements in cache");
  134. }
  135. };
  136. } // namespace FrameworkTests