/* * memoryusageobserverunittest.cpp */ using namespace std; #include #include #include #include "memoryallocator.hpp" #include "memoryusageobserver.hpp" #include "memoryusageobserverunittest.hpp" #ifdef HAVE_TESTRUNNER #include "UnitTestMain.hpp" #endif /*HAVE_TESTRUNNER*/ /********************************************** Test classes **************************************/ // Registers the fixture into the 'registry' CPPUNIT_TEST_SUITE_REGISTRATION( MemoryUsageObserverTest ); void MemoryUsageObserverTest::setUp() { }; void MemoryUsageObserverTest::tearDown() { MemoryUsageObserver::purgeInstance(); logger::purgeInstance(); errorLogger::purgeInstance(); }; /** * UnitTest for getInstance() */ void MemoryUsageObserverTest::getInstanceReturnsMemoryUsageObserverTest() { CPPUNIT_ASSERT_EQUAL(true, MemoryUsageObserver::getInstance() != NULL); }; /** * UnitTest for getInstance() */ void MemoryUsageObserverTest::getInstanceTwiceReturnsSameInstanceTest() { int* i = Malloc(1, "MemoryUsageObserverTest::getInstanceTwiceReturnsSameInstanceTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize()); CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize()); }; /** * UnitTest for getInstance() */ void MemoryUsageObserverTest::getInstanceAfterPurgeInstanceReturnsNewInstanceTest() { int* i = Malloc(1, "MemoryUsageObserverTest::getInstanceAfterPurgeInstanceReturnsNewInstanceTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize()); MemoryUsageObserver::purgeInstance(); CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize()); }; /** * UnitTest for addMemory() and removeMemory() */ void MemoryUsageObserverTest::addAndRemoveMemoryTest() { int* i = Malloc(1, "MemoryUsageObserverTest::addAndRemoveMemoryTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize()); MemoryUsageObserver::getInstance()->removeMemory(i); CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize()); Free(i); }; /** * UnitTest for addMemory() and removeMemory() */ void MemoryUsageObserverTest::removeNonTrackedMemoryDoesNotCauseACrashTest() { int* i = Malloc(1, "MemoryUsageObserverTest::removeNonTrackedMemoryDoesNotCauseACrashTest - i"); MemoryUsageObserver::getInstance()->removeMemory(i); CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize()); Free(i); }; /** * UnitTest for addMemory() and removeMemory() */ void MemoryUsageObserverTest::addMemoryTwiceTest() { int* i = Malloc(1, "MemoryUsageObserverTest::addMemoryTwiceTest - i"); int* j = Malloc(1, "MemoryUsageObserverTest::addMemoryTwiceTest - j"); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); MemoryUsageObserver::getInstance()->addMemory(j, sizeof(int)); CPPUNIT_ASSERT_EQUAL(2 * sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize()); Free(i); Free(j); }; /** * UnitTest for addMemory() and removeMemory() */ void MemoryUsageObserverTest::addMemoryAndChangeSizeOfAddedMemoryTest() { int* i = Malloc(1, "MemoryUsageObserverTest::addMemoryAndChangeSizeOfAddedMemoryTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, 2 * sizeof(int)); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize()); Free(i); }; /** * UnitTest for getMaximumUsedMemory() */ void MemoryUsageObserverTest::addMemoryChangeSizeOfAddedMemoryAndGetMaximumSizeTest() { int* i = Malloc(1, "MemoryUsageObserverTest::addMemoryChangeSizeOfAddedMemoryAndGetMaximumSizeTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, 2 * sizeof(int)); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); CPPUNIT_ASSERT_EQUAL(2 * sizeof(int), MemoryUsageObserver::getInstance()->getMaximumUsedMemory()); Free(i); }; /** * UnitTest for getMaximumUsedMemory() */ void MemoryUsageObserverTest::addMemoryRemoveMemoryAndGetMaximumSizeTest() { int* i = Malloc(1, "MemoryUsageObserverTest::addMemoryRemoveMemoryAndGetMaximumSizeTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); MemoryUsageObserver::getInstance()->removeMemory(i); CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getMaximumUsedMemory()); Free(i); }; /** * UnitTest for getPointersToAllocatedMemory() */ void MemoryUsageObserverTest::getPointersToAllocatedMemoryTest() { int* i = Malloc(1, "MemoryUsageObserverTest::getPointersToAllocatedMemoryTest - i"); MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int)); CPPUNIT_ASSERT_EQUAL(i, (int*) MemoryUsageObserver::getInstance()->getPointersToAllocatedMemory().begin()->first); Free(i); };