PageRenderTime 92ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/labo/2009.08.17/poco-1.3.5-mingw/Foundation/testsuite/src/ExpireCacheTest.cpp

http://mingw-lib.googlecode.com/
C++ | 234 lines | 154 code | 40 blank | 40 comment | 21 complexity | a1b22ce4142eb5a674ec6681f4bc29f6 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, MPL-2.0-no-copyleft-exception
  1. //
  2. // ExpireCacheTest.cpp
  3. //
  4. // $Id: //poco/1.3/Foundation/testsuite/src/ExpireCacheTest.cpp#2 $
  5. //
  6. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  7. // and Contributors.
  8. //
  9. // Permission is hereby granted, free of charge, to any person or organization
  10. // obtaining a copy of the software and accompanying documentation covered by
  11. // this license (the "Software") to use, reproduce, display, distribute,
  12. // execute, and transmit the Software, and to prepare derivative works of the
  13. // Software, and to permit third-parties to whom the Software is furnished to
  14. // do so, all subject to the following:
  15. //
  16. // The copyright notices in the Software and this entire statement, including
  17. // the above license grant, this restriction and the following disclaimer,
  18. // must be included in all copies of the Software, in whole or in part, and
  19. // all derivative works of the Software, unless such copies or derivative
  20. // works are solely in the form of machine-executable object code generated by
  21. // a source language processor.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
  26. // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
  27. // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
  28. // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. //
  31. #include "ExpireCacheTest.h"
  32. #include "CppUnit/TestCaller.h"
  33. #include "CppUnit/TestSuite.h"
  34. #include "Poco/Exception.h"
  35. #include "Poco/ExpireCache.h"
  36. #include "Poco/AccessExpireCache.h"
  37. #include "Poco/Bugcheck.h"
  38. #include "Poco/Thread.h"
  39. using namespace Poco;
  40. #define DURSLEEP 250
  41. #define DURHALFSLEEP DURSLEEP / 2
  42. #define DURWAIT 300
  43. ExpireCacheTest::ExpireCacheTest(const std::string& name): CppUnit::TestCase(name)
  44. {
  45. }
  46. ExpireCacheTest::~ExpireCacheTest()
  47. {
  48. }
  49. void ExpireCacheTest::testClear()
  50. {
  51. ExpireCache<int, int> aCache(DURSLEEP);
  52. aCache.add(1, 2);
  53. aCache.add(3, 4);
  54. aCache.add(5, 6);
  55. assert (aCache.has(1));
  56. assert (aCache.has(3));
  57. assert (aCache.has(5));
  58. assert (*aCache.get(1) == 2);
  59. assert (*aCache.get(3) == 4);
  60. assert (*aCache.get(5) == 6);
  61. aCache.clear();
  62. assert (!aCache.has(1));
  63. assert (!aCache.has(3));
  64. assert (!aCache.has(5));
  65. }
  66. void ExpireCacheTest::testExpire0()
  67. {
  68. try
  69. {
  70. ExpireCache<int, int> aCache(24);
  71. failmsg("cache expire lower than 25 is illegal, test should fail");
  72. }
  73. catch (Poco::InvalidArgumentException&)
  74. {
  75. }
  76. }
  77. void ExpireCacheTest::testExpireN()
  78. {
  79. // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
  80. // 3-1|5 -> 5 gets removed
  81. ExpireCache<int, int> aCache(DURSLEEP);
  82. aCache.add(1, 2); // 1
  83. assert (aCache.has(1));
  84. SharedPtr<int> tmp = aCache.get(1);
  85. assert (!tmp.isNull());
  86. assert (*tmp == 2);
  87. assert (aCache.size() == 1);
  88. Thread::sleep(DURWAIT);
  89. assert (aCache.size() == 0);
  90. assert (!aCache.has(1));
  91. // tmp must still be valid, access it
  92. assert (*tmp == 2);
  93. tmp = aCache.get(1);
  94. assert (!tmp);
  95. aCache.add(1, 2); // 1
  96. Thread::sleep(DURHALFSLEEP);
  97. aCache.add(3, 4); // 3-1
  98. assert (aCache.has(1));
  99. assert (aCache.has(3));
  100. tmp = aCache.get(1);
  101. SharedPtr<int> tmp2 = aCache.get(3);
  102. assert (*tmp == 2);
  103. assert (*tmp2 == 4);
  104. Thread::sleep(DURHALFSLEEP+25); //3|1
  105. assert (!aCache.has(1));
  106. assert (aCache.has(3));
  107. assert (*tmp == 2); // 1-3
  108. assert (*tmp2 == 4); // 3-1
  109. tmp2 = aCache.get(3);
  110. assert (*tmp2 == 4);
  111. Thread::sleep(DURHALFSLEEP+25); //3|1
  112. assert (!aCache.has(3));
  113. assert (*tmp2 == 4);
  114. tmp = aCache.get(1);
  115. tmp2 = aCache.get(3);
  116. assert (!tmp);
  117. assert (!tmp2);
  118. // removing illegal entries should work too
  119. aCache.remove(666);
  120. aCache.clear();
  121. assert (!aCache.has(5));
  122. assert (!aCache.has(3));
  123. }
  124. void ExpireCacheTest::testDuplicateAdd()
  125. {
  126. ExpireCache<int, int> aCache(DURSLEEP);
  127. aCache.add(1, 2); // 1
  128. assert (aCache.has(1));
  129. assert (*aCache.get(1) == 2);
  130. aCache.add(1, 3);
  131. assert (aCache.has(1));
  132. assert (*aCache.get(1) == 3);
  133. }
  134. void ExpireCacheTest::testAccessExpireN()
  135. {
  136. // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
  137. // 3-1|5 -> 5 gets removed
  138. AccessExpireCache<int, int> aCache(DURSLEEP);
  139. aCache.add(1, 2); // 1
  140. assert (aCache.has(1));
  141. SharedPtr<int> tmp = aCache.get(1);
  142. assert (!tmp.isNull());
  143. assert (*tmp == 2);
  144. assert (aCache.size() == 1);
  145. Thread::sleep(DURWAIT);
  146. assert (aCache.size() == 0);
  147. assert (!aCache.has(1));
  148. // tmp must still be valid, access it
  149. assert (*tmp == 2);
  150. tmp = aCache.get(1);
  151. assert (!tmp);
  152. aCache.add(1, 2); // 1
  153. Thread::sleep(DURHALFSLEEP);
  154. aCache.add(3, 4); // 3-1
  155. assert (aCache.has(1));
  156. assert (aCache.has(3));
  157. Thread::sleep(DURHALFSLEEP+50); //3|1
  158. assert (!aCache.has(1));
  159. assert (*aCache.get(3) == 4);
  160. Thread::sleep(DURHALFSLEEP+25); //3|1
  161. assert (*aCache.get(3) == 4);
  162. // removing illegal entries should work too
  163. aCache.remove(666);
  164. aCache.clear();
  165. assert (!aCache.has(5));
  166. assert (!aCache.has(3));
  167. }
  168. void ExpireCacheTest::testExpireWithHas()
  169. {
  170. // 3-1 represents the cache sorted by age, elements get replaced at the end of the list
  171. // 3-1|5 -> 5 gets removed
  172. ExpireCache<int, int> aCache(DURSLEEP);
  173. aCache.add(1, 2); // 1
  174. assert (aCache.has(1));
  175. Thread::sleep(DURWAIT);
  176. assert (!aCache.has(1));
  177. }
  178. void ExpireCacheTest::setUp()
  179. {
  180. }
  181. void ExpireCacheTest::tearDown()
  182. {
  183. }
  184. CppUnit::Test* ExpireCacheTest::suite()
  185. {
  186. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("ExpireCacheTest");
  187. CppUnit_addTest(pSuite, ExpireCacheTest, testClear);
  188. CppUnit_addTest(pSuite, ExpireCacheTest, testExpire0);
  189. CppUnit_addTest(pSuite, ExpireCacheTest, testExpireN);
  190. CppUnit_addTest(pSuite, ExpireCacheTest, testDuplicateAdd);
  191. CppUnit_addTest(pSuite, ExpireCacheTest, testAccessExpireN);
  192. CppUnit_addTest(pSuite, ExpireCacheTest, testExpireWithHas);
  193. return pSuite;
  194. }