1 | /*
|
---|
2 | * memoryusageobserverunittest.cpp
|
---|
3 | */
|
---|
4 |
|
---|
5 |
|
---|
6 | // include config.h
|
---|
7 | #ifdef HAVE_CONFIG_H
|
---|
8 | #include <config.h>
|
---|
9 | #endif
|
---|
10 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | #include <cppunit/CompilerOutputter.h>
|
---|
14 | #include <cppunit/extensions/TestFactoryRegistry.h>
|
---|
15 | #include <cppunit/ui/text/TestRunner.h>
|
---|
16 |
|
---|
17 | #include "memoryallocator.hpp"
|
---|
18 | #include "memoryusageobserver.hpp"
|
---|
19 | #include "memoryusageobserverunittest.hpp"
|
---|
20 |
|
---|
21 | #ifdef HAVE_TESTRUNNER
|
---|
22 | #include "UnitTestMain.hpp"
|
---|
23 | #endif /*HAVE_TESTRUNNER*/
|
---|
24 |
|
---|
25 | /********************************************** Test classes **************************************/
|
---|
26 | // Registers the fixture into the 'registry'
|
---|
27 | CPPUNIT_TEST_SUITE_REGISTRATION( MemoryUsageObserverTest );
|
---|
28 |
|
---|
29 |
|
---|
30 | void MemoryUsageObserverTest::setUp()
|
---|
31 | {
|
---|
32 | };
|
---|
33 |
|
---|
34 |
|
---|
35 | void MemoryUsageObserverTest::tearDown()
|
---|
36 | {
|
---|
37 | MemoryUsageObserver::purgeInstance();
|
---|
38 | logger::purgeInstance();
|
---|
39 | errorLogger::purgeInstance();
|
---|
40 | };
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * UnitTest for getInstance()
|
---|
44 | */
|
---|
45 | void MemoryUsageObserverTest::getInstanceReturnsMemoryUsageObserverTest()
|
---|
46 | {
|
---|
47 | CPPUNIT_ASSERT_EQUAL(true, MemoryUsageObserver::getInstance() != NULL);
|
---|
48 | };
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * UnitTest for getInstance()
|
---|
52 | */
|
---|
53 | void MemoryUsageObserverTest::getInstanceTwiceReturnsSameInstanceTest()
|
---|
54 | {
|
---|
55 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::getInstanceTwiceReturnsSameInstanceTest - i");
|
---|
56 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
57 | CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
58 | CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
59 | };
|
---|
60 |
|
---|
61 | /**
|
---|
62 | * UnitTest for getInstance()
|
---|
63 | */
|
---|
64 | void MemoryUsageObserverTest::getInstanceAfterPurgeInstanceReturnsNewInstanceTest()
|
---|
65 | {
|
---|
66 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::getInstanceAfterPurgeInstanceReturnsNewInstanceTest - i");
|
---|
67 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
68 | CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
69 | MemoryUsageObserver::purgeInstance();
|
---|
70 | CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
71 | };
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * UnitTest for addMemory() and removeMemory()
|
---|
75 | */
|
---|
76 | void MemoryUsageObserverTest::addAndRemoveMemoryTest()
|
---|
77 | {
|
---|
78 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::addAndRemoveMemoryTest - i");
|
---|
79 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
80 | CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
81 | MemoryUsageObserver::getInstance()->removeMemory(i);
|
---|
82 | CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
83 | Free(i);
|
---|
84 | };
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * UnitTest for addMemory() and removeMemory()
|
---|
88 | */
|
---|
89 | void MemoryUsageObserverTest::removeNonTrackedMemoryDoesNotCauseACrashTest()
|
---|
90 | {
|
---|
91 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::removeNonTrackedMemoryDoesNotCauseACrashTest - i");
|
---|
92 | MemoryUsageObserver::getInstance()->removeMemory(i);
|
---|
93 | CPPUNIT_ASSERT_EQUAL((size_t) 0, MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
94 | Free(i);
|
---|
95 | };
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * UnitTest for addMemory() and removeMemory()
|
---|
99 | */
|
---|
100 | void MemoryUsageObserverTest::addMemoryTwiceTest()
|
---|
101 | {
|
---|
102 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::addMemoryTwiceTest - i");
|
---|
103 | int* j = Malloc<int>(1, "MemoryUsageObserverTest::addMemoryTwiceTest - j");
|
---|
104 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
105 | MemoryUsageObserver::getInstance()->addMemory(j, sizeof(int));
|
---|
106 | CPPUNIT_ASSERT_EQUAL(2 * sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
107 | Free(i);
|
---|
108 | Free(j);
|
---|
109 | };
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * UnitTest for addMemory() and removeMemory()
|
---|
113 | */
|
---|
114 | void MemoryUsageObserverTest::addMemoryAndChangeSizeOfAddedMemoryTest()
|
---|
115 | {
|
---|
116 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::addMemoryAndChangeSizeOfAddedMemoryTest - i");
|
---|
117 | MemoryUsageObserver::getInstance()->addMemory(i, 2 * sizeof(int));
|
---|
118 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
119 | CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getUsedMemorySize());
|
---|
120 | Free(i);
|
---|
121 | };
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * UnitTest for getMaximumUsedMemory()
|
---|
125 | */
|
---|
126 | void MemoryUsageObserverTest::addMemoryChangeSizeOfAddedMemoryAndGetMaximumSizeTest()
|
---|
127 | {
|
---|
128 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::addMemoryChangeSizeOfAddedMemoryAndGetMaximumSizeTest - i");
|
---|
129 | MemoryUsageObserver::getInstance()->addMemory(i, 2 * sizeof(int));
|
---|
130 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
131 | CPPUNIT_ASSERT_EQUAL(2 * sizeof(int), MemoryUsageObserver::getInstance()->getMaximumUsedMemory());
|
---|
132 | Free(i);
|
---|
133 | };
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * UnitTest for getMaximumUsedMemory()
|
---|
137 | */
|
---|
138 | void MemoryUsageObserverTest::addMemoryRemoveMemoryAndGetMaximumSizeTest()
|
---|
139 | {
|
---|
140 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::addMemoryRemoveMemoryAndGetMaximumSizeTest - i");
|
---|
141 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
142 | MemoryUsageObserver::getInstance()->removeMemory(i);
|
---|
143 | CPPUNIT_ASSERT_EQUAL(sizeof(int), MemoryUsageObserver::getInstance()->getMaximumUsedMemory());
|
---|
144 | Free(i);
|
---|
145 | };
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * UnitTest for getPointersToAllocatedMemory()
|
---|
149 | */
|
---|
150 | void MemoryUsageObserverTest::getPointersToAllocatedMemoryTest()
|
---|
151 | {
|
---|
152 | int* i = Malloc<int>(1, "MemoryUsageObserverTest::getPointersToAllocatedMemoryTest - i");
|
---|
153 | MemoryUsageObserver::getInstance()->addMemory(i, sizeof(int));
|
---|
154 | CPPUNIT_ASSERT_EQUAL(i, (int*) MemoryUsageObserver::getInstance()->getPointersToAllocatedMemory().begin()->first);
|
---|
155 | Free(i);
|
---|
156 | };
|
---|