Added amb1600: trie natives
This commit is contained in:
@ -22,6 +22,7 @@
|
||||
#include <vector>
|
||||
#include <sorting>
|
||||
#include <cellarray>
|
||||
#include <celltrie>
|
||||
#include <newmenus>
|
||||
|
||||
/* Function is called just after server activation.
|
||||
|
26
plugins/include/celltrie.inc
Normal file
26
plugins/include/celltrie.inc
Normal file
@ -0,0 +1,26 @@
|
||||
#if defined _celltrie_included
|
||||
#endinput
|
||||
#endif
|
||||
#define _celltrie_included
|
||||
|
||||
enum Trie
|
||||
{
|
||||
Invalid_Trie = 0
|
||||
};
|
||||
|
||||
|
||||
native Trie:TrieCreate();
|
||||
native TrieClear(Trie:handle);
|
||||
|
||||
native TrieSetCell(Trie:handle, const key[], any:value);
|
||||
native TrieSetString(Trie:handle, const key[], const value[]);
|
||||
native TrieSetArray(Trie:handle, const key[], const any:buffer[], size);
|
||||
|
||||
native bool:TrieGetCell(Trie:handle, const key[], &any:value);
|
||||
native bool:TrieGetString(Trie:handle, const key[], output[], outputsize);
|
||||
native bool:TrieGetArray(Trie:handle, const key[], any:output[], outputsize);
|
||||
|
||||
native bool:TrieDeleteKey(Trie:handle, const key[]);
|
||||
native bool:TrieKeyExists(Trie:handle, const key[]);
|
||||
native TrieDestroy(&Trie:handle);
|
||||
|
@ -102,6 +102,9 @@ public arraytest1()
|
||||
|
||||
new Float:f;
|
||||
new Array:a=ArrayCreate(1);
|
||||
if (a == Invalid_Array)
|
||||
{
|
||||
}
|
||||
for (new i=0; i<1000; i++)
|
||||
{
|
||||
f=float(i);
|
||||
|
169
plugins/testsuite/trietest.sma
Normal file
169
plugins/testsuite/trietest.sma
Normal file
@ -0,0 +1,169 @@
|
||||
#include <amxmodx>
|
||||
|
||||
|
||||
// These natives are only available in a debug build of amxmodx
|
||||
native TrieFreeCount();
|
||||
native TrieMallocCount();
|
||||
|
||||
new failcount = 0;
|
||||
new passcount = 0;
|
||||
public plugin_init()
|
||||
{
|
||||
register_plugin("Trie Test", AMXX_VERSION_STR, "AMXX Dev Team");
|
||||
register_srvcmd("trietest", "trietest");
|
||||
}
|
||||
|
||||
stock fail(const testname[])
|
||||
{
|
||||
server_print("[FAIL] %s", testname);
|
||||
|
||||
failcount++;
|
||||
}
|
||||
stock pass(const testname[])
|
||||
{
|
||||
server_print("[PASS] %s", testname);
|
||||
|
||||
passcount++;
|
||||
}
|
||||
stock done()
|
||||
{
|
||||
server_print("Finished. %d tests, %d failed", failcount + passcount, failcount);
|
||||
}
|
||||
stock check_frees()
|
||||
{
|
||||
if (TrieMallocCount() != TrieFreeCount())
|
||||
fail("free count == malloc count");
|
||||
|
||||
else
|
||||
pass("free count == malloc count");
|
||||
|
||||
server_print("malloc count: %d free count: %d", TrieMallocCount(), TrieFreeCount());
|
||||
}
|
||||
public trietest()
|
||||
{
|
||||
failcount = 0;
|
||||
passcount = 0;
|
||||
|
||||
new bool:ok = true;
|
||||
new Trie:t = TrieCreate();
|
||||
|
||||
new Trie:oldhandle = t; // Makes sure that the trie handle system recycles old handles
|
||||
|
||||
new key[32];
|
||||
for (new i = 0; i < 100; i++)
|
||||
{
|
||||
formatex(key, charsmax(key), "K%dK", i);
|
||||
TrieSetCell(t, key, i);
|
||||
}
|
||||
|
||||
for (new i = 0; i < 100; i++)
|
||||
{
|
||||
formatex(key, charsmax(key), "K%dK", i);
|
||||
new val;
|
||||
if (!TrieGetCell(t, key, val))
|
||||
{
|
||||
server_print("TrieGetCell(%d, '%s', %d) failed", t, key, val);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
else if (val != i)
|
||||
{
|
||||
server_print("val mismatch, expected: %d got: %d", i, val);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
}
|
||||
if (ok)
|
||||
pass("Cell tests");
|
||||
|
||||
else
|
||||
fail("Cell tests");
|
||||
|
||||
TrieClear(t);
|
||||
TrieDestroy(t);
|
||||
|
||||
t = TrieCreate();
|
||||
|
||||
if (t == oldhandle)
|
||||
pass("Recycle handles");
|
||||
|
||||
else
|
||||
fail("Recycle handles");
|
||||
|
||||
ok = true;
|
||||
for (new i = 0; i < 100; i++)
|
||||
{
|
||||
static val[32];
|
||||
formatex(key, charsmax(key), "K%dK", i);
|
||||
formatex(val, charsmax(val), "V%dV", i);
|
||||
TrieSetString(t, key, val);
|
||||
}
|
||||
|
||||
for (new i = 0; i < 100; i++)
|
||||
{
|
||||
formatex(key, charsmax(key), "K%dK", i);
|
||||
static val[32];
|
||||
static exp[32];
|
||||
formatex(exp, charsmax(exp), "V%dV", i);
|
||||
if (!TrieGetString(t, key, val, charsmax(val)))
|
||||
{
|
||||
server_print("TrieGetString(%d, '%s', %s) failed", t, key, val);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
else if (!equal(val, exp))
|
||||
{
|
||||
server_print("val mismatch, key: '%s' expected: '%s' got: '%s'", key, exp, val);
|
||||
ok = false;
|
||||
}
|
||||
|
||||
}
|
||||
if (ok)
|
||||
pass("String tests");
|
||||
|
||||
else
|
||||
fail("String tests");
|
||||
|
||||
TrieDestroy(t);
|
||||
|
||||
check_frees();
|
||||
|
||||
t = TrieCreate();
|
||||
ok = true;
|
||||
for (new i = 0; i < 1000; i++)
|
||||
{
|
||||
formatex(key, charsmax(key), "!%d!", i);
|
||||
TrieSetString(t, key, key);
|
||||
}
|
||||
for (new i = 0; i < 1000; i++)
|
||||
{
|
||||
formatex(key, charsmax(key), "!%d!", i);
|
||||
|
||||
if (!TrieKeyExists(t, key))
|
||||
{
|
||||
ok = false;
|
||||
server_print("Key '%s' does not exist", key);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!TrieDeleteKey(t, key))
|
||||
{
|
||||
server_print("Key '%s' could not be deleted", key);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (ok)
|
||||
pass("Exists/Delete");
|
||||
|
||||
else
|
||||
fail("Exists/Delete");
|
||||
|
||||
check_frees();
|
||||
TrieClear(t);
|
||||
TrieDestroy(t);
|
||||
check_frees();
|
||||
done();
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user