| 1 | /* | 
|---|
| 2 | * memoryallocatorunittest.cpp | 
|---|
| 3 | */ | 
|---|
| 4 |  | 
|---|
| 5 |  | 
|---|
| 6 | using namespace std; | 
|---|
| 7 |  | 
|---|
| 8 | #include <cppunit/CompilerOutputter.h> | 
|---|
| 9 | #include <cppunit/extensions/TestFactoryRegistry.h> | 
|---|
| 10 | #include <cppunit/ui/text/TestRunner.h> | 
|---|
| 11 |  | 
|---|
| 12 | #include "memoryallocator.hpp" | 
|---|
| 13 | #include "memoryallocatorunittest.hpp" | 
|---|
| 14 | #include "memoryusageobserver.hpp" | 
|---|
| 15 | #include "Helpers/helpers.hpp" | 
|---|
| 16 | #include "Helpers/Log.hpp" | 
|---|
| 17 | #include "defs.hpp" | 
|---|
| 18 |  | 
|---|
| 19 | #ifdef HAVE_TESTRUNNER | 
|---|
| 20 | #include "UnitTestMain.hpp" | 
|---|
| 21 | #endif /*HAVE_TESTRUNNER*/ | 
|---|
| 22 |  | 
|---|
| 23 | /********************************************** Test classes **************************************/ | 
|---|
| 24 |  | 
|---|
| 25 | // Registers the fixture into the 'registry' | 
|---|
| 26 | CPPUNIT_TEST_SUITE_REGISTRATION( MemoryAllocatorTest ); | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | void MemoryAllocatorTest::setUp() | 
|---|
| 30 | { | 
|---|
| 31 | }; | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 | void MemoryAllocatorTest::tearDown() | 
|---|
| 35 | { | 
|---|
| 36 | MemoryUsageObserver::getInstance()->purgeInstance(); | 
|---|
| 37 | logger::purgeInstance(); | 
|---|
| 38 | }; | 
|---|
| 39 |  | 
|---|
| 40 | /** | 
|---|
| 41 | * UnitTest for Malloc() | 
|---|
| 42 | */ | 
|---|
| 43 | void MemoryAllocatorTest::MallocTest() | 
|---|
| 44 | { | 
|---|
| 45 | int* buffer1 = NULL; | 
|---|
| 46 | buffer1 = Malloc<int>(1, ""); | 
|---|
| 47 | Free(&buffer1); | 
|---|
| 48 |  | 
|---|
| 49 | long* buffer2 = NULL; | 
|---|
| 50 | buffer2 = Malloc<long>(1, ""); | 
|---|
| 51 | Free(&buffer2); | 
|---|
| 52 |  | 
|---|
| 53 | char* buffer3 = NULL; | 
|---|
| 54 | buffer3 = Malloc<char>(4, ""); | 
|---|
| 55 | DoLog(0) && (Log() << Verbose(0) << buffer3 << endl); | 
|---|
| 56 | Free(&buffer3); | 
|---|
| 57 |  | 
|---|
| 58 | char** buffer4 = NULL; | 
|---|
| 59 | buffer4 = Malloc<char*>(10, ""); | 
|---|
| 60 | for (int i=0;i<10;i++) | 
|---|
| 61 | buffer4[i] = NULL; | 
|---|
| 62 | Free(&buffer4); | 
|---|
| 63 | }; | 
|---|
| 64 |  | 
|---|
| 65 | /** | 
|---|
| 66 | * UnitTest for Calloc() | 
|---|
| 67 | */ | 
|---|
| 68 | void MemoryAllocatorTest::CallocTest() | 
|---|
| 69 | { | 
|---|
| 70 | int* buffer1 = NULL; | 
|---|
| 71 | buffer1 = Calloc<int>(10, ""); | 
|---|
| 72 | Free(&buffer1); | 
|---|
| 73 |  | 
|---|
| 74 | long* buffer2 = NULL; | 
|---|
| 75 | buffer2 = Calloc<long>(10, ""); | 
|---|
| 76 | Free(&buffer2); | 
|---|
| 77 |  | 
|---|
| 78 | char** buffer3 = NULL; | 
|---|
| 79 | buffer3 = Calloc<char *>(10, ""); | 
|---|
| 80 | for (int i=0;i<10;i++) | 
|---|
| 81 | buffer3[i] = NULL; | 
|---|
| 82 | Free(&buffer3); | 
|---|
| 83 | }; | 
|---|
| 84 |  | 
|---|
| 85 | /** | 
|---|
| 86 | * UnitTest for ReAlloc() | 
|---|
| 87 | */ | 
|---|
| 88 | void MemoryAllocatorTest::ReAllocTest() | 
|---|
| 89 | { | 
|---|
| 90 | int* buffer1 = NULL; | 
|---|
| 91 | buffer1 = Malloc<int>(1, ""); | 
|---|
| 92 | buffer1 = ReAlloc<int>(buffer1, 2, ""); | 
|---|
| 93 | Free(&buffer1); | 
|---|
| 94 |  | 
|---|
| 95 | int* buffer2 = NULL; | 
|---|
| 96 | buffer2 = ReAlloc<int>(buffer2, 2, ""); | 
|---|
| 97 | Free(&buffer2); | 
|---|
| 98 | }; | 
|---|
| 99 |  | 
|---|
| 100 | /** | 
|---|
| 101 | * UnitTest for Free() | 
|---|
| 102 | */ | 
|---|
| 103 | void MemoryAllocatorTest::FreeTest() | 
|---|
| 104 | { | 
|---|
| 105 | char** buffer1 = NULL; | 
|---|
| 106 | Free(buffer1); | 
|---|
| 107 |  | 
|---|
| 108 | int** buffer2 = NULL; | 
|---|
| 109 | Free(buffer2); | 
|---|
| 110 | }; | 
|---|