Merge branch 'lua_versions' into main/rendor-staging
[ryzomcore.git] / nel / tools / nel_unit_test / ut_misc_debug.h
blob3301784cd51dde991d740ead46054ea40b4ac6f1
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
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.
8 //
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/>.
17 #ifndef UT_MISC_DEBUG
18 #define UT_MISC_DEBUG
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
30 class CFoo1
32 public:
33 NL_INSTANCE_COUNTER_DECL(CFoo1);
35 NL_INSTANCE_COUNTER_IMPL(CFoo1);
37 class CFoo2
39 public:
40 NL_INSTANCE_COUNTER_DECL(CFoo2);
43 NL_INSTANCE_COUNTER_IMPL(CFoo2);
45 class CFoo3 : public CFoo2
47 public:
48 NL_INSTANCE_COUNTER_DECL(CFoo3);
51 NL_INSTANCE_COUNTER_IMPL(CFoo3);
54 class CUTMiscDebug : public Test::Suite
56 public:
57 CUTMiscDebug()
59 TEST_ADD(CUTMiscDebug::testInstanceCounter)
60 TEST_ADD(CUTMiscDebug::testInstanceCounterOutput)
63 private:
64 void testInstanceCounter()
66 sint32 n;
68 CFoo1 foo1;
70 sint32 n = NL_GET_INSTANCE_COUNTER(CFoo1);
71 TEST_ASSERT(n == 1);
72 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
73 TEST_ASSERT(n == 1);
75 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
77 n = NL_GET_INSTANCE_COUNTER(CFoo1);
78 TEST_ASSERT(n == 1);
79 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
80 TEST_ASSERT(n == 0);
82 n = NL_GET_INSTANCE_COUNTER(CFoo1);
83 TEST_ASSERT(n == 0);
84 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
85 TEST_ASSERT(n == -1);
88 CFoo1 foo1;
89 CFoo1 other(foo1);
91 sint32 n = NL_GET_INSTANCE_COUNTER(CFoo1);
92 TEST_ASSERT(n == 2);
93 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
94 TEST_ASSERT(n == 1);
96 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
98 n = NL_GET_INSTANCE_COUNTER(CFoo1);
99 TEST_ASSERT(n == 2);
100 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
101 TEST_ASSERT(n == 0);
103 n = NL_GET_INSTANCE_COUNTER(CFoo1);
104 TEST_ASSERT(n == 0);
105 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
106 TEST_ASSERT(n == -2);
109 CFoo1 foo1;
110 CFoo1 other;
112 foo1 = other;
114 sint32 n = NL_GET_INSTANCE_COUNTER(CFoo1);
115 TEST_ASSERT(n == 2);
116 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
117 TEST_ASSERT(n == 0);
119 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
121 n = NL_GET_INSTANCE_COUNTER(CFoo1);
122 TEST_ASSERT(n == 2);
123 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
124 TEST_ASSERT(n == 0);
126 n = NL_GET_INSTANCE_COUNTER(CFoo1);
127 TEST_ASSERT(n == 0);
128 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
129 TEST_ASSERT(n == -2);
131 CFoo1 *foo1s[10];
132 CFoo2 *foo2s[10];
133 CFoo3 *foo3s[10];
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);
144 TEST_ASSERT(n == 8);
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);
158 TEST_ASSERT(n == 0);
159 n = NL_GET_INSTANCE_COUNTER(CFoo2);
160 TEST_ASSERT(n == 20);
161 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo2);
162 TEST_ASSERT(n == 0);
163 n = NL_GET_INSTANCE_COUNTER(CFoo3);
164 TEST_ASSERT(n == 10);
165 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo3);
166 TEST_ASSERT(n == 0);
168 for (uint i=0; i<5; ++i)
170 delete foo1s[i];
171 delete foo2s[i];
172 delete foo3s[i];
174 n = NL_GET_INSTANCE_COUNTER(CFoo1);
175 TEST_ASSERT(n == 5);
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);
183 TEST_ASSERT(n == 5);
184 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo3);
185 TEST_ASSERT(n == -5);
186 for (uint i=5; i<10; ++i)
188 delete foo1s[i];
189 delete foo2s[i];
190 delete foo3s[i];
192 n = NL_GET_INSTANCE_COUNTER(CFoo1);
193 TEST_ASSERT(n == 0);
194 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo1);
195 TEST_ASSERT(n == -10);
196 n = NL_GET_INSTANCE_COUNTER(CFoo2);
197 TEST_ASSERT(n == 0);
198 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo2);
199 TEST_ASSERT(n == -20);
200 n = NL_GET_INSTANCE_COUNTER(CFoo3);
201 TEST_ASSERT(n == 0);
202 n = NL_GET_INSTANCE_COUNTER_DELTA(CFoo3);
203 TEST_ASSERT(n == -10);
206 void testInstanceCounterOutput()
208 NLMISC::CInstanceCounterManager::getInstance().resetDeltaCounter();
210 CFoo1 *foo1s[10];
211 CFoo2 *foo2s[10];
212 CFoo3 *foo3s[10];
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);
234 #endif