Parent Directory | Revision Log
Revision 17 - (view) (download)
1 : | ian | 17 | #include <amxmodx> |
2 : | |||
3 : | |||
4 : | new __testnumber; | ||
5 : | new errcount; | ||
6 : | new __testfunc[32]; | ||
7 : | new __testfuncnum; | ||
8 : | |||
9 : | enum TestType | ||
10 : | { | ||
11 : | TT_Equal = 0, | ||
12 : | TT_LessThan, | ||
13 : | TT_GreaterThan, | ||
14 : | TT_LessThanEqual, | ||
15 : | TT_GreaterThanEqual, | ||
16 : | TT_NotEqual | ||
17 : | }; | ||
18 : | |||
19 : | new TestWords[6][] = { | ||
20 : | "==", | ||
21 : | "<", | ||
22 : | ">", | ||
23 : | "<=", | ||
24 : | ">=", | ||
25 : | "!=" | ||
26 : | }; | ||
27 : | |||
28 : | |||
29 : | |||
30 : | stock test(A,B=0,TestType:Type=TT_Equal) | ||
31 : | { | ||
32 : | ++__testnumber; | ||
33 : | |||
34 : | new passed=0; | ||
35 : | |||
36 : | switch (Type) | ||
37 : | { | ||
38 : | case TT_Equal: if (A==B) passed=1; | ||
39 : | case TT_LessThan: if (A<B) passed=1; | ||
40 : | case TT_GreaterThan: if (A>B) passed=1; | ||
41 : | case TT_LessThanEqual: if (A<=B) passed=1; | ||
42 : | case TT_GreaterThanEqual: if (A>=B) passed=1; | ||
43 : | case TT_NotEqual: if (A!=B) passed=1; | ||
44 : | } | ||
45 : | |||
46 : | if (!passed) | ||
47 : | { | ||
48 : | log_amx("Failed test #%d (%d %s %d)",__testnumber,A,TestWords[_:Type],B); | ||
49 : | errcount++; | ||
50 : | } | ||
51 : | } | ||
52 : | stock starttests(const startfunc[]) | ||
53 : | { | ||
54 : | __testnumber=0; | ||
55 : | errcount=0; | ||
56 : | __testfuncnum=1; | ||
57 : | server_print("Starting tests..."); | ||
58 : | formatex(__testfunc,sizeof(__testfunc)-1,"%s",startfunc); | ||
59 : | |||
60 : | new func[32]; | ||
61 : | formatex(func,sizeof(func)-1,"%s%d",__testfunc,__testfuncnum++); | ||
62 : | set_task(0.1,func); | ||
63 : | } | ||
64 : | |||
65 : | stock showres() | ||
66 : | { | ||
67 : | if (errcount==0) | ||
68 : | { | ||
69 : | new func[32]; | ||
70 : | formatex(func,sizeof(func)-1,"%s%d",__testfunc,__testfuncnum++); | ||
71 : | if (get_func_id(func)==-1) | ||
72 : | { | ||
73 : | server_print("All tests ok!"); | ||
74 : | } | ||
75 : | else | ||
76 : | { | ||
77 : | server_print("Test ok, moving on..."); | ||
78 : | |||
79 : | set_task(0.1,func); | ||
80 : | } | ||
81 : | } | ||
82 : | else | ||
83 : | { | ||
84 : | server_print("Test failed, aborting."); | ||
85 : | } | ||
86 : | } | ||
87 : | |||
88 : | |||
89 : | |||
90 : | public plugin_init() | ||
91 : | { | ||
92 : | register_srvcmd("arraytest","arraytest"); | ||
93 : | } | ||
94 : | |||
95 : | public arraytest() | ||
96 : | { | ||
97 : | starttests("arraytest"); | ||
98 : | } | ||
99 : | public arraytest1() | ||
100 : | { | ||
101 : | server_print("Testing 1000 iterations of 1-cell arrays..."); | ||
102 : | |||
103 : | new Float:f; | ||
104 : | new Array:a=ArrayCreate(1); | ||
105 : | for (new i=0; i<1000; i++) | ||
106 : | { | ||
107 : | f=float(i); | ||
108 : | ArrayPushCell(a,f); | ||
109 : | } | ||
110 : | new Float:r; | ||
111 : | for (new i=0; i<1000; i++) | ||
112 : | { | ||
113 : | f=float(i); | ||
114 : | ArrayGetCell(a,i,r); | ||
115 : | |||
116 : | // This is normally bad for float "casting", but in this case it should be fine. | ||
117 : | test(_:f, _:r); | ||
118 : | |||
119 : | |||
120 : | // Reset with inversed values | ||
121 : | new g=_:f; | ||
122 : | g=~g; | ||
123 : | |||
124 : | ArraySetCell(a,i,g); | ||
125 : | |||
126 : | ArrayGetCell(a,i,r); | ||
127 : | |||
128 : | test(g, _:r); | ||
129 : | |||
130 : | } | ||
131 : | |||
132 : | ArrayDestroy(a); | ||
133 : | |||
134 : | showres(); | ||
135 : | } | ||
136 : | stock bool:checkarray(const a[], const b[], size) | ||
137 : | { | ||
138 : | while (size--) | ||
139 : | { | ||
140 : | if (a[size]!=b[size]) | ||
141 : | { | ||
142 : | return false; | ||
143 : | } | ||
144 : | } | ||
145 : | |||
146 : | return true; | ||
147 : | } | ||
148 : | stock invarray(a[],size) | ||
149 : | { | ||
150 : | while (size--) | ||
151 : | { | ||
152 : | a[size] = ~a[size]; | ||
153 : | } | ||
154 : | |||
155 : | } | ||
156 : | public arraytest2() | ||
157 : | { | ||
158 : | server_print("Testing 1000 iterations of 40-cell arrays..."); | ||
159 : | |||
160 : | new Array:a=ArrayCreate(40); | ||
161 : | new buff[40]; | ||
162 : | new buffb[40]; | ||
163 : | for (new i=0; i<1000; i++) | ||
164 : | { | ||
165 : | arrayset(buff,i,sizeof(buff)); | ||
166 : | |||
167 : | ArrayPushArray(a, buff); | ||
168 : | } | ||
169 : | for (new i=0; i<1000; i++) | ||
170 : | { | ||
171 : | arrayset(buff, i, sizeof(buff)); | ||
172 : | |||
173 : | ArrayGetArray(a, i, buffb); | ||
174 : | |||
175 : | test(_:checkarray(buff,buffb,sizeof(buff)),1); | ||
176 : | |||
177 : | // Now overwrite the array with inversed value | ||
178 : | invarray(buff,sizeof(buff)); | ||
179 : | |||
180 : | ArraySetArray(a, i, buff); | ||
181 : | |||
182 : | ArrayGetArray(a, i, buffb); | ||
183 : | |||
184 : | test(_:checkarray(buff,buffb,sizeof(buff)),1); | ||
185 : | } | ||
186 : | |||
187 : | ArrayDestroy(a); | ||
188 : | |||
189 : | showres(); | ||
190 : | } | ||
191 : | public arraytest3() | ||
192 : | { | ||
193 : | server_print("Testing 1000 iterations of strings..."); | ||
194 : | |||
195 : | // The string is 10 long, the string we're trying to pass is 20 long. | ||
196 : | |||
197 : | new Array:a=ArrayCreate(10); | ||
198 : | |||
199 : | new buff[20]="1234567890abcdefghi"; | ||
200 : | new buffb[20]; | ||
201 : | |||
202 : | for (new i=0; i<1000; i++) | ||
203 : | { | ||
204 : | ArrayPushString(a, buff); | ||
205 : | } | ||
206 : | for (new i=0; i<1000; i++) | ||
207 : | { | ||
208 : | ArrayGetString(a, i, buffb, sizeof(buffb)-1); | ||
209 : | |||
210 : | test(strcmp(buffb,"123456789"),0); | ||
211 : | |||
212 : | ArraySetString(a, i, "9876543210"); | ||
213 : | |||
214 : | ArrayGetString(a, i, buffb, sizeof(buffb)-1); | ||
215 : | |||
216 : | test(strcmp(buffb,"987654321"),0); | ||
217 : | |||
218 : | buffb[0]=0; | ||
219 : | |||
220 : | formatex(buffb,sizeof(buffb)-1,"%S", ArrayGetStringHandle(a, i)); | ||
221 : | |||
222 : | test(strcmp(buffb, "987654321"),0); | ||
223 : | } | ||
224 : | |||
225 : | ArrayDestroy(a); | ||
226 : | |||
227 : | showres(); | ||
228 : | } | ||
229 : | |||
230 : | public sortcallback(Array:a, b, c) | ||
231 : | { | ||
232 : | static stra[40]; | ||
233 : | static strb[40]; | ||
234 : | |||
235 : | ArrayGetString(a, b, stra, sizeof(stra)-1); | ||
236 : | ArrayGetString(a, c, strb, sizeof(strb)-1); | ||
237 : | return strcmp(stra,strb); | ||
238 : | } | ||
239 : | public arraytest4() | ||
240 : | { | ||
241 : | server_print("Testing sorting function..."); | ||
242 : | |||
243 : | new Array:a=ArrayCreate(40); | ||
244 : | |||
245 : | ArrayPushString(a, "z"); | ||
246 : | ArrayPushString(a, "yz"); | ||
247 : | ArrayPushString(a, "xyz"); | ||
248 : | ArrayPushString(a, "wxyz"); | ||
249 : | ArrayPushString(a, "vwxyz"); | ||
250 : | ArrayPushString(a, "uvwxyz"); | ||
251 : | ArrayPushString(a, "tuvwxyz"); | ||
252 : | ArrayPushString(a, "stuvwxyz"); | ||
253 : | ArrayPushString(a, "rstuvwxyz"); | ||
254 : | ArrayPushString(a, "qrstuvwxyz"); | ||
255 : | ArrayPushString(a, "pqrstuvwxyz"); | ||
256 : | ArrayPushString(a, "opqrstuvwxyz"); | ||
257 : | ArrayPushString(a, "nopqrstuvwxyz"); | ||
258 : | ArrayPushString(a, "mnopqrstuvwxyz"); | ||
259 : | ArrayPushString(a, "lmnopqrstuvwxyz"); | ||
260 : | ArrayPushString(a, "klmnopqrstuvwxyz"); | ||
261 : | ArrayPushString(a, "jklmnopqrstuvwxyz"); | ||
262 : | ArrayPushString(a, "ijklmnopqrstuvwxyz"); | ||
263 : | ArrayPushString(a, "hijklmnopqrstuvwxyz"); | ||
264 : | ArrayPushString(a, "ghijklmnopqrstuvwxyz"); | ||
265 : | ArrayPushString(a, "fghijklmnopqrstuvwxyz"); | ||
266 : | ArrayPushString(a, "efghijklmnopqrstuvwxyz"); | ||
267 : | ArrayPushString(a, "defghijklmnopqrstuvwxyz"); | ||
268 : | ArrayPushString(a, "cdefghijklmnopqrstuvwxyz"); | ||
269 : | ArrayPushString(a, "bcdefghijklmnopqrstuvwxyz"); | ||
270 : | ArrayPushString(a, "abcdefghijklmnopqrstuvwxyz"); | ||
271 : | |||
272 : | new OldSize=ArraySize(a); | ||
273 : | |||
274 : | ArraySort(a, "sortcallback"); | ||
275 : | |||
276 : | test(ArraySize(a),OldSize); | ||
277 : | |||
278 : | new buff[40]; | ||
279 : | |||
280 : | ArrayGetString(a,0,buff,sizeof(buff)-1); | ||
281 : | |||
282 : | test(strcmp(buff,"abcdefghijklmnopqrstuvwxyz"),0); | ||
283 : | |||
284 : | ArrayGetString(a,25,buff,sizeof(buff)-1); | ||
285 : | |||
286 : | test(strcmp(buff,"z"),0); | ||
287 : | |||
288 : | |||
289 : | new start='a'; | ||
290 : | |||
291 : | for (new i=0;i<OldSize;i++) | ||
292 : | { | ||
293 : | ArrayGetString(a,i,buff,sizeof(buff)-1) | ||
294 : | |||
295 : | test(buff[0],start++); | ||
296 : | } | ||
297 : | |||
298 : | showres(); | ||
299 : | } | ||
300 : | public arraytest5() | ||
301 : | { | ||
302 : | server_print("Testing ArrayDeleteItem()..."); | ||
303 : | new Array:a=ArrayCreate(1); | ||
304 : | |||
305 : | new v; | ||
306 : | |||
307 : | for (new i=0; i<1000; i++) | ||
308 : | { | ||
309 : | ArrayPushCell(a, i); | ||
310 : | } | ||
311 : | for (new i=ArraySize(a) - 1; i>=0 ; i--) | ||
312 : | { | ||
313 : | if (i % 2 == 0) | ||
314 : | { | ||
315 : | ArrayDeleteItem(a, i); | ||
316 : | } | ||
317 : | } | ||
318 : | test(ArraySize(a), 500); | ||
319 : | for (new i=0; i< 500; i++) | ||
320 : | { | ||
321 : | ArrayGetCell(a, i, v); | ||
322 : | |||
323 : | // All items should be incrementing odd numbers | ||
324 : | test(((i + 1) * 2) - 1, v); | ||
325 : | |||
326 : | // All remaining entries should be odd | ||
327 : | test((v & 1), 1); | ||
328 : | } | ||
329 : | ArrayDestroy(a); | ||
330 : | |||
331 : | a=ArrayCreate(1); | ||
332 : | // Repeat the same test, but check even numbers | ||
333 : | for (new i=0; i<1000; i++) | ||
334 : | { | ||
335 : | ArrayPushCell(a, i); | ||
336 : | } | ||
337 : | for (new i=ArraySize(a) - 1; i>=0 ; i--) | ||
338 : | { | ||
339 : | if (i % 2 == 1) | ||
340 : | { | ||
341 : | ArrayDeleteItem(a, i); | ||
342 : | } | ||
343 : | } | ||
344 : | test(ArraySize(a), 500); | ||
345 : | for (new i=0; i< 500; i++) | ||
346 : | { | ||
347 : | ArrayGetCell(a, i, v); | ||
348 : | |||
349 : | // All items should be incrementing even numbers | ||
350 : | test(((i + 1) * 2) - 2, v); | ||
351 : | |||
352 : | // All remaining entries should be even | ||
353 : | test((v & 1), 0); | ||
354 : | } | ||
355 : | ArrayDestroy(a); | ||
356 : | |||
357 : | showres(); | ||
358 : | } | ||
359 : | public arraytest6() | ||
360 : | { | ||
361 : | server_print("Testing ArrayInsertCellAfter()..."); | ||
362 : | |||
363 : | new Array:a=ArrayCreate(1); | ||
364 : | |||
365 : | for (new i=0; i<10;i++) | ||
366 : | { | ||
367 : | ArrayPushCell(a, i); | ||
368 : | new item=ArraySize(a)-1; | ||
369 : | for (new j=0; j<10; j++) | ||
370 : | { | ||
371 : | ArrayInsertCellAfter(a, item + j, j); | ||
372 : | } | ||
373 : | } | ||
374 : | |||
375 : | test(ArraySize(a), 110); | ||
376 : | |||
377 : | new v; | ||
378 : | for (new i=0; i<110; i++) | ||
379 : | { | ||
380 : | ArrayGetCell(a, i, v); | ||
381 : | |||
382 : | test(v, i / 10); | ||
383 : | for (new j=0; j<10; j++) | ||
384 : | { | ||
385 : | ArrayGetCell(a, ++i, v); | ||
386 : | test(v, j); | ||
387 : | } | ||
388 : | } | ||
389 : | |||
390 : | |||
391 : | ArrayDestroy(a); | ||
392 : | |||
393 : | showres(); | ||
394 : | } | ||
395 : | public arraytest7() | ||
396 : | { | ||
397 : | server_print("Testing ArrayInsertCellBefore()..."); | ||
398 : | |||
399 : | new Array:a=ArrayCreate(1); | ||
400 : | |||
401 : | for (new i=0; i<10;i++) | ||
402 : | { | ||
403 : | ArrayPushCell(a, i); | ||
404 : | new item=ArraySize(a)-1; | ||
405 : | for (new j=0; j<10; j++) | ||
406 : | { | ||
407 : | ArrayInsertCellBefore(a, item, j); | ||
408 : | } | ||
409 : | } | ||
410 : | |||
411 : | test(ArraySize(a), 110); | ||
412 : | |||
413 : | new v; | ||
414 : | for (new i=0; i<110; i++) | ||
415 : | { | ||
416 : | for (new j=9; j>=0; j--) | ||
417 : | { | ||
418 : | ArrayGetCell(a, i++, v); | ||
419 : | test(v, j); | ||
420 : | } | ||
421 : | |||
422 : | ArrayGetCell(a, i, v); | ||
423 : | |||
424 : | test(v, (i - 10) / 10); | ||
425 : | } | ||
426 : | |||
427 : | |||
428 : | ArrayDestroy(a); | ||
429 : | |||
430 : | showres(); | ||
431 : | } | ||
432 : | public arraytest8() | ||
433 : | { | ||
434 : | server_print("Testing ArraySwap()..."); | ||
435 : | new Array:a=ArrayCreate(1); | ||
436 : | |||
437 : | for (new i=0; i<10; i++) | ||
438 : | { | ||
439 : | ArrayPushCell(a, i); | ||
440 : | } | ||
441 : | for (new i=0; i<5; i++) | ||
442 : | { | ||
443 : | ArraySwap(a, i, (10 - (i + 1))); | ||
444 : | } | ||
445 : | new v; | ||
446 : | for (new i=0; i<5; i++) | ||
447 : | { | ||
448 : | ArrayGetCell(a, i, v); | ||
449 : | |||
450 : | test(v, (10 - (i + 1))); | ||
451 : | } | ||
452 : | |||
453 : | ArrayDestroy(a); | ||
454 : | |||
455 : | showres(); | ||
456 : | } |
Contact | ViewVC Help |
Powered by ViewVC 1.0.4 |