Cache.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "pch.h"
  2. #include "CppUnitTest.h"
  3. #include <Cache.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( 100, []( int i ) {
  13. return i;
  14. }, Framework::CacheCleanupStrategy::RANDOM );
  15. Assert::IsTrue( cache.getCurrentSize() == 0, L"getCurrentSize() on empty cache should be 0" );
  16. Assert::IsTrue( cache.getMaxSize() == 100, L"getMaxSize() on empty cache" );
  17. }
  18. TEST_METHOD( PutTest )
  19. {
  20. Framework::Cache<int, int> cache( 2, []( int i ) {
  21. return i;
  22. }, Framework::CacheCleanupStrategy::RANDOM );
  23. cache.put( 0, 100 );
  24. cache.put( 1, 10 );
  25. cache.put( 2, 1000 );
  26. Assert::IsTrue( cache.getCurrentSize() == 2, L"unexpected count of elements in cache" );
  27. Assert::IsTrue( cache.has( 0 ) ? cache.get( 0 ) == 100 : 1, L"invalid value at key 0 in cache after adding elements" );
  28. Assert::IsTrue( cache.has( 1 ) ? cache.get( 1 ) == 10 : 1, L"invalid value at key 1 in cache after adding elements" );
  29. Assert::IsTrue( cache.get( 2 ) == 1000, L"invalid value at key 2 in cache after adding elements" );
  30. int count = 0;
  31. for( Framework::MapEntry< int, int > i : cache )
  32. {
  33. if( i.getKey() == 0 )
  34. Assert::IsTrue( i.getValue() == 100, L"invalid value at key 0 in cache after adding elements" );
  35. else if( i.getKey() == 1 )
  36. Assert::IsTrue( i.getValue() == 10, L"invalid value at key 1 in cache after adding elements" );
  37. else if( i.getKey() == 2 )
  38. Assert::IsTrue( i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements" );
  39. else
  40. Assert::Fail( L"invalid key in cache after adding elements" );
  41. count++;
  42. }
  43. Assert::IsTrue( count == 2, L"unexpected count of elements in cache" );
  44. }
  45. TEST_METHOD( OldestTest )
  46. {
  47. Framework::Cache<int, int> cache( 2, []( int i ) {
  48. return i;
  49. }, Framework::CacheCleanupStrategy::OLDEST );
  50. cache.put( 0, 100 );
  51. Sleep( 1000 );
  52. cache.put( 1, 10 );
  53. Sleep( 1000 );
  54. cache.put( 2, 1000 );
  55. Assert::IsTrue( cache.getCurrentSize() == 2, L"unexpected count of elements in cache" );
  56. Assert::IsFalse( cache.has( 0 ), L"invalid value at key 0 in cache after adding elements" );
  57. Assert::IsTrue( cache.has( 1 ) && cache.get( 1 ) == 10, L"invalid value at key 1 in cache after adding elements" );
  58. Assert::IsTrue( cache.get( 2 ) == 1000, L"invalid value at key 2 in cache after adding elements" );
  59. int count = 0;
  60. for( Framework::MapEntry< int, int > i : cache )
  61. {
  62. if( i.getKey() == 1 )
  63. Assert::IsTrue( i.getValue() == 10, L"invalid value at key 1 in cache after adding elements" );
  64. else if( i.getKey() == 2 )
  65. Assert::IsTrue( i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements" );
  66. else
  67. Assert::Fail( L"invalid key in cache after adding elements" );
  68. count++;
  69. }
  70. Assert::IsTrue( count == 2, L"unexpected count of elements in cache" );
  71. }
  72. TEST_METHOD( LongestNotUsedTest )
  73. {
  74. Framework::Cache<int, int> cache( 2, []( int i ) {
  75. return i;
  76. }, Framework::CacheCleanupStrategy::LONGEST_NOT_USED );
  77. cache.put( 0, 100 );
  78. Sleep( 1000 );
  79. cache.put( 1, 10 );
  80. Sleep( 1000 );
  81. cache.get( 0 );
  82. cache.put( 2, 1000 );
  83. Assert::IsTrue( cache.getCurrentSize() == 2, L"unexpected count of elements in cache" );
  84. Assert::IsTrue( cache.has( 0 ) && cache.get( 0 ) == 100, L"invalid value at key 0 in cache after adding elements" );
  85. Assert::IsFalse( cache.has( 1 ), L"invalid value at key 1 in cache after adding elements" );
  86. Assert::IsTrue( cache.get( 2 ) == 1000, L"invalid value at key 2 in cache after adding elements" );
  87. int count = 0;
  88. for( Framework::MapEntry< int, int > i : cache )
  89. {
  90. if( i.getKey() == 0 )
  91. Assert::IsTrue( i.getValue() == 100, L"invalid value at key 0 in cache after adding elements" );
  92. else if( i.getKey() == 2 )
  93. Assert::IsTrue( i.getValue() == 1000, L"invalid value at key 2 in cache after adding elements" );
  94. else
  95. Assert::Fail( L"invalid key in cache after adding elements" );
  96. count++;
  97. }
  98. Assert::IsTrue( count == 2, L"unexpected count of elements in cache" );
  99. }
  100. };
  101. }