- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cpp
More file actions
Latest commit
213 lines (196 loc) · 3.81 KB
/
Copy pathtest.cpp
File metadata and controls
213 lines (196 loc) · 3.81 KB
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
#defineNewnew(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
#defineCRTDBG_MAP_ALLOC
#include<stdlib.h>
#include<crtdbg.h>
#endif
#defineEXPECT(expect, actual) \
do {\
if (expect != actual)\
fprintf(stderr, "%s:%d: \n", __FILE__, __LINE__);\
}while(0)
MEMORY_POOL_NAMESPACE_USE
classTest
{
public:
Test(int a, double b)
{
v = a;
d = b;
}
int v;
double d;
};
classTestLarge
{
public:
TestLarge(int a)
{
l1 = a;
}
longlong l1; longlong l2; longlong l3; longlong l4;
longlong l5; longlong l6; longlong l7; longlong l8;
longlong l9; longlong l10; longlong l11; longlong l12;
longlong l13; longlong l14; longlong l15; longlong l16;
longlong l17; longlong l18; longlong l19; longlong l20;
longlong l21; longlong l22; longlong l23; longlong l24;
longlong l25; longlong l26; longlong l27; longlong l28;
longlong l29; longlong l30; longlong l31; longlong l32;
longlong l33; longlong l34; longlong l35; longlong l36;
longlong l37; longlong l38; longlong l39; longlong l40;
longlong l41; longlong l42; longlong l43; longlong l44;
};
voidtest1()
{
auto p = MemoryTool::getInstance()->safeMalloc<int>();
*p = 5;
MemoryTool::getInstance()->safeFree(p);
EXPECT(p, NULL);
}
voidtest2()
{
auto p = MemoryTool::getInstance()->safeMalloc<Test>(1, 2.0);
EXPECT(p->v, 1);
EXPECT(p->d, 2.0);
MemoryTool::getInstance()->safeFree(p);
}
voidtestLarge()
{
auto p = MemoryTool::getInstance()->safeMalloc<TestLarge>(5);
EXPECT(p->l1, 5);
MemoryTool::getInstance()->safeFree(p);
}
voidtestMacro()
{
// 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);
}
voidtestGC()
{
size_t i;
for (i = 0; i < 128; ++i)
{
int *p = newint;
*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>
voidtestEffect()
{
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 = newint;
*p = 5;
delete p;
}
{
TestLarge *q = newTestLarge(5);
delete q;
}
}
end = clock();
std::cout << start << " - " << end << std::endl;
auto diff2 = end - start;
if (diff1 >= diff2)
{
EXPECT(1, 2);
}
}
voidtestMallocPerformance()
{
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 = newint;
*p = 5;
}
{
TestLarge *q = newTestLarge(5);
q->l11 = 15;
}
}
end = clock();
std::cout << start << " - " << end << std::endl;
auto diff2 = end - start;
if (diff1 >= diff2)
{
EXPECT(1, 2);
}
}
#endif
intmain()
{
test1();
test2();
testLarge();
testMacro();
testGC();
#ifdef _MSC_VER
testEffect();
testMallocPerformance();
#endif
deleteMemoryTool::getInstance();
#ifdef _MSC_VER
_CrtDumpMemoryLeaks();
system("pause");
#else
#endif
return0;
}