MemoryPoolTest.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //
  2. // MemoryPoolTest.cpp
  3. //
  4. // Copyright (c) 2005-2006, Applied Informatics Software Engineering GmbH.
  5. // and Contributors.
  6. //
  7. // SPDX-License-Identifier: BSL-1.0
  8. //
  9. #include "MemoryPoolTest.h"
  10. #include "CppUnit/TestCaller.h"
  11. #include "CppUnit/TestSuite.h"
  12. #include "Poco/MemoryPool.h"
  13. #include <vector>
  14. using Poco::MemoryPool;
  15. MemoryPoolTest::MemoryPoolTest(const std::string& name): CppUnit::TestCase(name)
  16. {
  17. }
  18. MemoryPoolTest::~MemoryPoolTest()
  19. {
  20. }
  21. void MemoryPoolTest::testMemoryPool()
  22. {
  23. MemoryPool pool1(100, 0, 10);
  24. assertTrue (pool1.blockSize() == 100);
  25. assertTrue (pool1.allocated() == 0);
  26. assertTrue (pool1.available() == 0);
  27. std::vector<void*> ptrs;
  28. for (int i = 0; i < 10; ++i)
  29. {
  30. ptrs.push_back(pool1.get());
  31. assertTrue (pool1.allocated() == i + 1);
  32. assertTrue (pool1.available() == 0);
  33. }
  34. try
  35. {
  36. pool1.get();
  37. fail("pool exhausted - must throw exception");
  38. }
  39. catch (Poco::OutOfMemoryException&)
  40. {
  41. }
  42. int av = 0;
  43. for (std::vector<void*>::iterator it = ptrs.begin(); it != ptrs.end(); ++it)
  44. {
  45. pool1.release(*it);
  46. ++av;
  47. assertTrue (pool1.available() == av);
  48. }
  49. MemoryPool pool2(32, 5, 10);
  50. assertTrue (pool2.available() == 5);
  51. assertTrue (pool2.blockSize() == 32);
  52. assertTrue (pool2.allocated() == 5);
  53. }
  54. void MemoryPoolTest::setUp()
  55. {
  56. }
  57. void MemoryPoolTest::tearDown()
  58. {
  59. }
  60. CppUnit::Test* MemoryPoolTest::suite()
  61. {
  62. CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("MemoryPoolTest");
  63. CppUnit_addTest(pSuite, MemoryPoolTest, testMemoryPool);
  64. return pSuite;
  65. }