-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cpp
213 lines (196 loc) · 3.81 KB
/
test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "MemoryTool.h"
#include "iostream"
#ifdef _MSC_VER
#ifdef _DEBUG
#define New new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#define CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#endif
#define EXPECT(expect, actual) \
do {\
if (expect != actual)\
fprintf(stderr, "%s:%d: \n", __FILE__, __LINE__);\
}while(0)
MEMORY_POOL_NAMESPACE_USE
class Test
{
public:
Test(int a, double b)
{
v = a;
d = b;
}
int v;
double d;
};
class TestLarge
{
public:
TestLarge(int a)
{
l1 = a;
}
long long l1; long long l2; long long l3; long long l4;
long long l5; long long l6; long long l7; long long l8;
long long l9; long long l10; long long l11; long long l12;
long long l13; long long l14; long long l15; long long l16;
long long l17; long long l18; long long l19; long long l20;
long long l21; long long l22; long long l23; long long l24;
long long l25; long long l26; long long l27; long long l28;
long long l29; long long l30; long long l31; long long l32;
long long l33; long long l34; long long l35; long long l36;
long long l37; long long l38; long long l39; long long l40;
long long l41; long long l42; long long l43; long long l44;
};
void test1()
{
auto p = MemoryTool::getInstance()->safeMalloc<int>();
*p = 5;
MemoryTool::getInstance()->safeFree(p);
EXPECT(p, NULL);
}
void test2()
{
auto p = MemoryTool::getInstance()->safeMalloc<Test>(1, 2.0);
EXPECT(p->v, 1);
EXPECT(p->d, 2.0);
MemoryTool::getInstance()->safeFree(p);
}
void testLarge()
{
auto p = MemoryTool::getInstance()->safeMalloc<TestLarge>(5);
EXPECT(p->l1, 5);
MemoryTool::getInstance()->safeFree(p);
}
void testMacro()
{
// no params construct
auto p1 = NEW(int);
*p1 = 1;
EXPECT(*p1, 1);
DELET(p1);
// multiple params construct
auto p2 = NEW(Test, 1, 2.0);
EXPECT(p2->v, 1);
EXPECT(p2->d, 2.0);
DELET(p2);
auto p3 = NEW(TestLarge, 5);
EXPECT(p3->l1, 5);
DELET(p3);
}
void testGC()
{
size_t i;
for (i = 0; i < 128; ++i)
{
int *p = new int;
*p = 65535;
DELET(p);
}
EXPECT(MemoryTool::getInstance()->getPoolColSize(0), i + NODE_MAX_SIZE);
MemoryTool::getInstance()->gc();
EXPECT(MemoryTool::getInstance()->getPoolColSize(0), NODE_MAX_SIZE);
}
#ifdef _MSC_VER
#include <time.h>
void testEffect()
{
size_t i;
clock_t start, end;
start = clock();
for (i = 0; i < 10000; ++i)
{
{
auto p = MemoryTool::getInstance()->safeMalloc<int>();
*p = 5;
MemoryTool::getInstance()->safeFree(p);
}
{
auto q = MemoryTool::getInstance()->safeMalloc<TestLarge>(5);
MemoryTool::getInstance()->safeFree(q);
}
}
end = clock();
auto diff1 = end - start;
std::cout << start << " - " << end << std::endl;
start = clock();
for (i = 0; i < 10000; ++i)
{
{
int *p = new int;
*p = 5;
delete p;
}
{
TestLarge *q = new TestLarge(5);
delete q;
}
}
end = clock();
std::cout << start << " - " << end << std::endl;
auto diff2 = end - start;
if (diff1 >= diff2)
{
EXPECT(1, 2);
}
}
void testMallocPerformance()
{
clock_t start, end;
start = clock();
size_t i;
for (i = 0; i < 10000; ++i)
{
{
auto p = MemoryTool::getInstance()->safeMalloc<int>();
*p = 5;
}
{
auto q = MemoryTool::getInstance()->safeMalloc<TestLarge>(5);
}
}
end = clock();
auto diff1 = end - start;
std::cout << start << " - " << end << std::endl;
start = clock();
for (i = 0; i < 10000; ++i)
{
{
int *p = new int;
*p = 5;
}
{
TestLarge *q = new TestLarge(5);
q->l11 = 15;
}
}
end = clock();
std::cout << start << " - " << end << std::endl;
auto diff2 = end - start;
if (diff1 >= diff2)
{
EXPECT(1, 2);
}
}
#endif
int main()
{
test1();
test2();
testLarge();
testMacro();
testGC();
#ifdef _MSC_VER
testEffect();
testMallocPerformance();
#endif
delete MemoryTool::getInstance();
#ifdef _MSC_VER
_CrtDumpMemoryLeaks();
system("pause");
#else
#endif
return 0;
}