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