amxmodx/plugins/testsuite/arraytest.sma

753 lines
14 KiB
Plaintext
Raw Normal View History

// vim: set ts=4 sw=4 tw=99 noet:
//
// AMX Mod X, based on AMX Mod by Aleksander Naszko ("OLO").
// Copyright (C) The AMX Mod X Development Team.
//
// This software is licensed under the GNU General Public License, version 3 or higher.
// Additional exceptions apply. For full license details, see LICENSE.txt or visit:
// https://alliedmods.net/amxmodx-license
#include <amxmodx>
new __testnumber;
new errcount;
new __testfunc[32];
new __testfuncnum;
enum TestType
{
TT_Equal = 0,
TT_LessThan,
TT_GreaterThan,
TT_LessThanEqual,
TT_GreaterThanEqual,
TT_NotEqual
};
new TestWords[6][] = {
"==",
"<",
">",
"<=",
">=",
"!="
};
2014-07-25 02:25:31 +04:00
stock test(any:A, any:B = 0, TestType:Type = TT_Equal)
{
++__testnumber;
2014-07-25 02:25:31 +04:00
new passed=0;
2014-07-25 02:25:31 +04:00
switch (Type)
{
2014-07-25 02:25:31 +04:00
case TT_Equal: if (A == B) passed = 1;
case TT_LessThan: if (A < B) passed = 1;
case TT_GreaterThan: if (A > B) passed = 1;
case TT_LessThanEqual: if (A <= B) passed = 1;
case TT_GreaterThanEqual: if (A >= B) passed = 1;
case TT_NotEqual: if (A != B) passed =1;
}
2014-07-25 02:25:31 +04:00
if (!passed)
{
2014-07-25 02:25:31 +04:00
log_amx("Failed test #%d (%d %s %d)", __testnumber, A, TestWords[_:Type], B);
errcount++;
}
}
stock starttests(const startfunc[])
{
2014-07-25 02:25:31 +04:00
__testnumber = 0;
errcount = 0;
__testfuncnum = 1;
server_print("Starting tests...");
2014-07-25 02:25:31 +04:00
formatex(__testfunc, charsmax(__testfunc), "%s", startfunc);
new func[32];
2014-07-25 02:25:31 +04:00
formatex(func, charsmax(func), "%s%d", __testfunc, __testfuncnum++);
set_task(0.1, func);
}
stock showres()
{
if (errcount==0)
{
new func[32];
2014-07-25 02:25:31 +04:00
formatex(func, charsmax(func), "%s%d", __testfunc, __testfuncnum++);
if (get_func_id(func) == -1)
{
server_print("All tests ok!");
}
else
{
server_print("Test ok, moving on...");
2014-07-25 02:25:31 +04:00
set_task(0.1, func);
}
}
else
{
server_print("Test failed, aborting.");
}
}
public plugin_init()
{
register_srvcmd("arraytest","arraytest");
}
public arraytest()
{
starttests("arraytest");
}
public arraytest1()
{
server_print("Testing 1000 iterations of 1-cell arrays...");
2014-07-25 02:25:31 +04:00
new Float:f;
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
2008-04-14 23:52:11 +04:00
if (a == Invalid_Array)
{
}
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
2014-07-25 02:25:31 +04:00
f = float(i);
ArrayPushCell(a,f);
}
2014-07-25 02:25:31 +04:00
new Float:r;
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
2014-07-25 02:25:31 +04:00
f = float(i);
r = Float:ArrayGetCell(a, i);
// This is normally bad for float "casting", but in this case it should be fine.
test(_:f, _:r);
2014-07-25 02:25:31 +04:00
// Reset with inversed values
new g=_:f;
g=~g;
2014-07-25 02:25:31 +04:00
ArraySetCell(a, i, g);
2014-07-25 02:25:31 +04:00
r = Float:ArrayGetCell(a,i);
test(g, _:r);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
stock bool:checkarray(const a[], const b[], size)
{
while (size--)
{
2014-07-25 02:25:31 +04:00
if (a[size] != b[size])
{
return false;
}
}
2014-07-25 02:25:31 +04:00
return true;
}
2014-07-25 02:25:31 +04:00
stock invarray(a[],size)
{
while (size--)
{
a[size] = ~a[size];
}
2014-07-25 02:25:31 +04:00
}
public arraytest2()
{
server_print("Testing 1000 iterations of 40-cell arrays...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(40);
new buff[40];
new buffb[40];
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
2014-07-25 02:25:31 +04:00
arrayset(buff, i, sizeof(buff));
ArrayPushArray(a, buff);
}
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
arrayset(buff, i, sizeof(buff));
ArrayGetArray(a, i, buffb);
2014-07-25 02:25:31 +04:00
test(_:checkarray(buff, buffb, sizeof(buff)), 1);
// Now overwrite the array with inversed value
2014-07-25 02:25:31 +04:00
invarray(buff, sizeof(buff));
ArraySetArray(a, i, buff);
ArrayGetArray(a, i, buffb);
2014-07-25 02:25:31 +04:00
test(_:checkarray(buff, buffb, sizeof(buff)), 1);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest3()
{
server_print("Testing 1000 iterations of strings...");
2014-07-25 02:25:31 +04:00
// The string is 10 long, the string we're trying to pass is 20 long.
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(10);
new buff[20] = "1234567890abcdefghi";
new buffb[20];
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
ArrayPushString(a, buff);
}
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
2014-07-25 02:25:31 +04:00
ArrayGetString(a, i, buffb, charsmax(buffb));
test(strcmp(buffb,"123456789"),0);
2014-07-25 02:25:31 +04:00
ArraySetString(a, i, "9876543210");
2014-07-25 02:25:31 +04:00
ArrayGetString(a, i, buffb, charsmax(buffb));
test(strcmp(buffb,"987654321"),0);
2014-07-25 02:25:31 +04:00
buffb[0] = EOS;
formatex(buffb, charsmax(buffb),"%a", ArrayGetStringHandle(a, i));
test(strcmp(buffb, "987654321"),0);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
public sortcallback(Array:a, b, c)
{
static stra[40];
static strb[40];
2014-07-25 02:25:31 +04:00
ArrayGetString(a, b, stra, charsmax(stra));
ArrayGetString(a, c, strb, charsmax(strb));
return strcmp(stra,strb);
}
2014-07-25 02:25:31 +04:00
public arraytest4()
{
server_print("Testing sorting function...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(40);
ArrayPushString(a, "z");
ArrayPushString(a, "yz");
ArrayPushString(a, "xyz");
ArrayPushString(a, "wxyz");
ArrayPushString(a, "vwxyz");
ArrayPushString(a, "uvwxyz");
ArrayPushString(a, "tuvwxyz");
ArrayPushString(a, "stuvwxyz");
ArrayPushString(a, "rstuvwxyz");
ArrayPushString(a, "qrstuvwxyz");
ArrayPushString(a, "pqrstuvwxyz");
ArrayPushString(a, "opqrstuvwxyz");
ArrayPushString(a, "nopqrstuvwxyz");
ArrayPushString(a, "mnopqrstuvwxyz");
ArrayPushString(a, "lmnopqrstuvwxyz");
ArrayPushString(a, "klmnopqrstuvwxyz");
ArrayPushString(a, "jklmnopqrstuvwxyz");
ArrayPushString(a, "ijklmnopqrstuvwxyz");
ArrayPushString(a, "hijklmnopqrstuvwxyz");
ArrayPushString(a, "ghijklmnopqrstuvwxyz");
ArrayPushString(a, "fghijklmnopqrstuvwxyz");
ArrayPushString(a, "efghijklmnopqrstuvwxyz");
ArrayPushString(a, "defghijklmnopqrstuvwxyz");
ArrayPushString(a, "cdefghijklmnopqrstuvwxyz");
ArrayPushString(a, "bcdefghijklmnopqrstuvwxyz");
ArrayPushString(a, "abcdefghijklmnopqrstuvwxyz");
2014-07-25 02:25:31 +04:00
new OldSize = ArraySize(a);
ArraySort(a, "sortcallback");
2014-07-25 02:25:31 +04:00
test(ArraySize(a), OldSize);
2014-07-25 02:25:31 +04:00
new buff[40];
ArrayGetString(a, 0, buff, charsmax(buff));
test(strcmp(buff,"abcdefghijklmnopqrstuvwxyz"),0);
2014-07-25 02:25:31 +04:00
ArrayGetString(a, 25, buff, charsmax(buff));
test(strcmp(buff, "z"), 0);
new start = 'a';
for (new i = 0; i < OldSize; i++)
{
2014-07-25 02:25:31 +04:00
ArrayGetString(a, i, buff, charsmax(buff));
test(buff[0], start++);
}
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest5()
{
server_print("Testing ArrayDeleteItem()...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
new v;
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
ArrayPushCell(a, i);
}
2014-07-25 02:25:31 +04:00
for (new i = ArraySize(a) - 1; i >= 0; i--)
{
if (i % 2 == 0)
{
ArrayDeleteItem(a, i);
}
}
2014-07-25 02:25:31 +04:00
test(ArraySize(a), 500);
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 500; i++)
{
2014-07-25 02:25:31 +04:00
v = ArrayGetCell(a, i);
// All items should be incrementing odd numbers
test(((i + 1) * 2) - 1, v);
// All remaining entries should be odd
test((v & 1), 1);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
a = ArrayCreate(1);
// Repeat the same test, but check even numbers
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 1000; i++)
{
ArrayPushCell(a, i);
}
2014-07-25 02:25:31 +04:00
for (new i = ArraySize(a) - 1; i >= 0 ; i--)
{
if (i % 2 == 1)
{
ArrayDeleteItem(a, i);
}
}
2014-07-25 02:25:31 +04:00
test(ArraySize(a), 500);
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 500; i++)
{
2014-07-25 02:25:31 +04:00
v = ArrayGetCell(a, i);
// All items should be incrementing even numbers
test(((i + 1) * 2) - 2, v);
// All remaining entries should be even
test((v & 1), 0);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest6()
{
server_print("Testing ArrayInsertCellAfter()...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
for (new i = 0; i < 10;i++)
{
ArrayPushCell(a, i);
2014-07-25 02:25:31 +04:00
new item = ArraySize(a) - 1;
for (new j = 0; j < 10; j++)
{
ArrayInsertCellAfter(a, item + j, j);
}
}
2014-07-25 02:25:31 +04:00
test(ArraySize(a), 110);
2014-07-25 02:25:31 +04:00
new v;
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 110; i++)
{
2014-07-25 02:25:31 +04:00
v = ArrayGetCell(a, i);
test(v, i / 10);
2014-07-25 02:25:31 +04:00
for (new j = 0; j < 10; j++)
{
2014-07-25 02:25:31 +04:00
v = ArrayGetCell(a, ++i);
test(v, j);
}
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest7()
{
2014-07-25 02:25:31 +04:00
server_print("Testing ArrayInsertStringAfter()...");
new Array:a = ArrayCreate(4);
new buffer[4];
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 10;i++)
{
2014-07-25 02:25:31 +04:00
formatex(buffer, charsmax(buffer), "%d", i);
ArrayPushString(a, buffer);
new item = ArraySize(a) - 1;
for (new j = 0; j < 10; j++)
{
2014-07-25 02:25:31 +04:00
formatex(buffer, charsmax(buffer), "%d", j);
ArrayInsertStringAfter(a, item + j, buffer);
}
}
2014-07-25 02:25:31 +04:00
test(ArraySize(a), 110);
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 110; i++)
{
2014-07-25 02:25:31 +04:00
ArrayGetString(a, i, buffer, charsmax(buffer));
test(str_to_num(buffer), i / 10);
for (new j = 0; j < 10; j++)
{
2014-07-25 02:25:31 +04:00
ArrayGetString(a, ++i, buffer, charsmax(buffer));
test(str_to_num(buffer), j);
}
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest8()
2014-07-25 02:25:31 +04:00
{
server_print("Testing ArrayInsertCellBefore()...");
new Array:a = ArrayCreate(1);
for (new i = 0; i < 10; i++)
{
new item = ArrayPushCell(a, i);
for (new j = 0; j < 10; j++)
{
ArrayInsertCellBefore(a, item, j);
}
}
test(ArraySize(a), 110);
for (new i = 0; i < 110; i++)
{
for (new j = 9; j >= 0; j--)
{
test(ArrayGetCell(a, i++), j);
}
test(ArrayGetCell(a, i), (i - 10) / 10);
}
ArrayDestroy(a);
showres();
}
public arraytest9()
{
server_print("Testing ArrayInsertStringBefore()...");
new buffer[4];
new Array:a = ArrayCreate(4);
for (new i = 0; i < 10; i++)
{
formatex(buffer, charsmax(buffer), "%d", i);
new item = ArrayPushString(a, buffer);
for (new j = 0; j < 10; j++)
{
formatex(buffer, charsmax(buffer), "%d", j);
ArrayInsertStringBefore(a, item, buffer);
}
}
test(ArraySize(a), 110);
for (new i = 0; i < 110; i++)
{
for (new j = 9; j >= 0; j--)
{
ArrayGetString(a, i++, buffer, charsmax(buffer));
test(str_to_num(buffer), j);
}
ArrayGetString(a, i, buffer, charsmax(buffer));
test(str_to_num(buffer), (i - 10) / 10);
}
ArrayDestroy(a);
showres();
}
public arraytest10()
{
server_print("Testing ArraySwap()...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
for (new i = 0; i < 10; i++)
{
ArrayPushCell(a, i);
}
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 5; i++)
{
ArraySwap(a, i, (10 - (i + 1)));
}
2014-07-25 02:25:31 +04:00
new v;
2014-07-25 02:25:31 +04:00
for (new i = 0; i < 5; i++)
{
2014-07-25 02:25:31 +04:00
v = ArrayGetCell(a, i);
test(v, (10 - (i + 1)));
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public Sortcallbackex_string(Array:a, const b[], const c[], d)
{
2014-07-25 02:25:31 +04:00
return strcmp(b, c);
}
2014-07-25 02:25:31 +04:00
public arraytest11()
{
server_print("Testing (new) sorting function with string...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(40);
ArrayPushString(a, "z");
ArrayPushString(a, "yz");
ArrayPushString(a, "xyz");
ArrayPushString(a, "wxyz");
ArrayPushString(a, "vwxyz");
ArrayPushString(a, "uvwxyz");
ArrayPushString(a, "tuvwxyz");
ArrayPushString(a, "stuvwxyz");
ArrayPushString(a, "rstuvwxyz");
ArrayPushString(a, "qrstuvwxyz");
ArrayPushString(a, "pqrstuvwxyz");
ArrayPushString(a, "opqrstuvwxyz");
ArrayPushString(a, "nopqrstuvwxyz");
ArrayPushString(a, "mnopqrstuvwxyz");
ArrayPushString(a, "lmnopqrstuvwxyz");
ArrayPushString(a, "klmnopqrstuvwxyz");
ArrayPushString(a, "jklmnopqrstuvwxyz");
ArrayPushString(a, "ijklmnopqrstuvwxyz");
ArrayPushString(a, "hijklmnopqrstuvwxyz");
ArrayPushString(a, "ghijklmnopqrstuvwxyz");
ArrayPushString(a, "fghijklmnopqrstuvwxyz");
ArrayPushString(a, "efghijklmnopqrstuvwxyz");
ArrayPushString(a, "defghijklmnopqrstuvwxyz");
ArrayPushString(a, "cdefghijklmnopqrstuvwxyz");
ArrayPushString(a, "bcdefghijklmnopqrstuvwxyz");
ArrayPushString(a, "abcdefghijklmnopqrstuvwxyz");
2014-07-25 02:25:31 +04:00
new OldSize = ArraySize(a);
ArraySortEx(a, "Sortcallbackex_string");
test(ArraySize(a), OldSize);
new buff[40];
2014-07-25 02:25:31 +04:00
ArrayGetString(a, 0, buff, charsmax(buff));
test(strcmp(buff, "abcdefghijklmnopqrstuvwxyz"), 0);
2014-07-25 02:25:31 +04:00
ArrayGetString(a, 25, buff, charsmax(buff));
test(strcmp(buff, "z"),0);
2014-07-25 02:25:31 +04:00
new start = 'a';
for (new i = 0; i < OldSize; i++)
{
2014-07-25 02:25:31 +04:00
ArrayGetString(a, i, buff, charsmax(buff))
test(buff[0], start++);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public Sortcallbackex_int(Array:a, const b, const c, d)
{
return b < c ? -1 : 1;
}
2014-07-25 02:25:31 +04:00
public arraytest12()
{
server_print("Testing (new) sorting function with integer...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
ArrayPushCell(a, 8);
ArrayPushCell(a, 1);
ArrayPushCell(a, 3);
ArrayPushCell(a, 5);
ArrayPushCell(a, 7);
ArrayPushCell(a, 2);
ArrayPushCell(a, 9);
ArrayPushCell(a, 4);
ArrayPushCell(a, 10);
ArrayPushCell(a, 6);
2014-07-25 02:25:31 +04:00
new OldSize = ArraySize(a);
2014-07-25 02:25:31 +04:00
ArraySortEx(a, "Sortcallbackex_int");
test(ArraySize(a), OldSize);
test(ArrayGetCell(a, 0), 1);
test(ArrayGetCell(a, 9), 10);
for (new i = 0; i < OldSize; i++)
{
2014-07-25 02:25:31 +04:00
test(ArrayGetCell(a, i), i+1);
}
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
2014-07-25 02:25:31 +04:00
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest13()
{
server_print("Testing cloning function...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
2014-07-25 02:25:31 +04:00
ArrayPushCell(a, 42);
ArrayPushCell(a, 9);
ArrayPushCell(a, -1);
ArrayPushCell(a, 0);
ArrayPushCell(a, 5);
ArrayPushCell(a, 10);
ArrayPushCell(a, 15);
ArrayPushCell(a, 6.5);
2014-07-25 02:25:31 +04:00
new Array:b = ArrayClone(a);
2014-07-25 02:25:31 +04:00
ArrayPushCell(b, 48);
ArrayPushCell(b, 3.14);
2014-07-25 02:25:31 +04:00
test(a, b, TT_NotEqual);
test(ArraySize(a), ArraySize(b) - 2);
test(ArrayGetCell(b, 0), 42);
test(ArrayGetCell(b, 2), -1);
test(ArrayGetCell(b, 7), 6.5);
test(ArrayGetCell(b, 9), 3.14);
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
ArrayDestroy(b);
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest14()
{
server_print("Testing resizing function...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(16);
2014-07-25 02:25:31 +04:00
ArrayPushString(a, "egg");
2014-07-25 02:25:31 +04:00
ArrayResize(a, 50);
ArrayPushString(a, "boileregg");
2014-07-25 02:25:31 +04:00
ArraySetString(a, 50, "no more egg v2");
2014-07-25 02:25:31 +04:00
new buffer[16];
ArrayGetString(a, 50, buffer, charsmax(buffer));
2014-07-25 02:25:31 +04:00
test(ArraySize(a), 50 + 1);
test(strcmp(buffer, "no more egg v2"), 0);
ArrayDestroy(a);
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest15()
{
server_print("Testing finding string in array...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(16);
2014-07-25 02:25:31 +04:00
ArrayPushString(a, "z");
ArrayPushString(a, "egg");
ArrayPushString(a, "boilerplate");
ArrayPushString(a, "amxmodx");
ArrayPushString(a, "something");
ArrayPushString(a, "");
ArrayPushString(a, "eggeggeggeggeggeggegg");
2014-07-25 02:25:31 +04:00
test(ArrayFindString(a, "egg"), 1);
test(ArrayFindString(a, "doh"), -1);
test(ArrayFindString(a, "something"), 4);
test(ArrayFindString(a, "eggeggeggeggegg"), 6);
test(ArrayFindString(a, ""), 5);
test(ArrayFindString(a, "zz"), -1);
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
showres();
}
2014-07-25 02:25:31 +04:00
public arraytest16()
{
server_print("Testing finding value in array...");
2014-07-25 02:25:31 +04:00
new Array:a = ArrayCreate(1);
2014-07-25 02:25:31 +04:00
ArrayPushCell(a, 2);
ArrayPushCell(a, 1);
ArrayPushCell(a, 5);
ArrayPushCell(a, 3.14);
ArrayPushCell(a, -1);
2014-07-25 02:25:31 +04:00
test(ArrayFindValue(a, -1), 4);
test(ArrayFindValue(a, 2), 0);
test(ArrayFindValue(a, 3), -1);
test(ArrayFindValue(a, 3.14), 3);
2014-07-25 02:25:31 +04:00
ArrayDestroy(a);
showres();
}