1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <nel/misc/debug.h>
21 #include <nel/misc/dynloadlib.h>
23 class CMiscUnitTestNelLibrary
: public NLMISC::INelLibrary
{
24 void onLibraryLoaded(bool firstTime
) { }
25 void onLibraryUnloaded(bool lastTime
) { }
27 NLMISC_DECL_PURE_LIB(CMiscUnitTestNelLibrary
);
29 // Test suite for CInstanceCounter
33 NL_INSTANCE_COUNTER_DECL(CFoo1
);
35 NL_INSTANCE_COUNTER_IMPL(CFoo1
);
40 NL_INSTANCE_COUNTER_DECL(CFoo2
);
43 NL_INSTANCE_COUNTER_IMPL(CFoo2
);
45 class CFoo3
: public CFoo2
48 NL_INSTANCE_COUNTER_DECL(CFoo3
);
51 NL_INSTANCE_COUNTER_IMPL(CFoo3
);
54 class CUTMiscDebug
: public Test::Suite
59 TEST_ADD(CUTMiscDebug::testInstanceCounter
)
60 TEST_ADD(CUTMiscDebug::testInstanceCounterOutput
)
64 void testInstanceCounter()
70 sint32 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
72 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
75 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
77 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
79 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
82 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
84 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
91 sint32 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
93 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
96 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
98 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
100 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
103 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
105 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
106 TEST_ASSERT(n
== -2);
114 sint32 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
116 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
119 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
121 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
123 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
126 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
128 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
129 TEST_ASSERT(n
== -2);
134 for (uint i
=0; i
<10; ++i
)
136 foo1s
[i
] = new CFoo1
;
137 foo2s
[i
] = new CFoo2
;
138 foo3s
[i
] = new CFoo3
;
141 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
142 TEST_ASSERT(n
== 10);
143 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
145 n
= NL_GET_INSTANCE_COUNTER(CFoo2
);
146 TEST_ASSERT(n
== 20);
147 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo2
);
148 TEST_ASSERT(n
== 20);
149 n
= NL_GET_INSTANCE_COUNTER(CFoo3
);
150 TEST_ASSERT(n
== 10);
151 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo3
);
152 TEST_ASSERT(n
== 10);
154 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
155 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
156 TEST_ASSERT(n
== 10);
157 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
159 n
= NL_GET_INSTANCE_COUNTER(CFoo2
);
160 TEST_ASSERT(n
== 20);
161 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo2
);
163 n
= NL_GET_INSTANCE_COUNTER(CFoo3
);
164 TEST_ASSERT(n
== 10);
165 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo3
);
168 for (uint i
=0; i
<5; ++i
)
174 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
176 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
177 TEST_ASSERT(n
== -5);
178 n
= NL_GET_INSTANCE_COUNTER(CFoo2
);
179 TEST_ASSERT(n
== 10);
180 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo2
);
181 TEST_ASSERT(n
== -10);
182 n
= NL_GET_INSTANCE_COUNTER(CFoo3
);
184 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo3
);
185 TEST_ASSERT(n
== -5);
186 for (uint i
=5; i
<10; ++i
)
192 n
= NL_GET_INSTANCE_COUNTER(CFoo1
);
194 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo1
);
195 TEST_ASSERT(n
== -10);
196 n
= NL_GET_INSTANCE_COUNTER(CFoo2
);
198 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo2
);
199 TEST_ASSERT(n
== -20);
200 n
= NL_GET_INSTANCE_COUNTER(CFoo3
);
202 n
= NL_GET_INSTANCE_COUNTER_DELTA(CFoo3
);
203 TEST_ASSERT(n
== -10);
206 void testInstanceCounterOutput()
208 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
213 for (uint i
=0; i
<10; ++i
)
215 foo1s
[i
] = new CFoo1
;
216 foo2s
[i
] = new CFoo2
;
217 foo3s
[i
] = new CFoo3
;
221 string ref
= "Listing 3 Instance counters :\n"
222 " Class 'CFoo1 ', \t 10 instances, \t 10 delta\n"
223 " Class 'CFoo2 ', \t 20 instances, \t 20 delta\n"
224 " Class 'CFoo3 ', \t 10 instances, \t 10 delta\n";
226 string ret
= NLMISC::CInstanceCounterManager::getInstance().displayCounters();
228 nlinfo("%s", ref
.c_str());
229 nlinfo("%s", ret
.c_str());
230 TEST_ASSERT(ref
== ret
);