Parent Directory | Revision Log
Revision 44 - (view) (download)
1 : | ian | 44 | #include <amxmodx> |
2 : | |||
3 : | |||
4 : | // These natives are only available in a debug build of amxmodx | ||
5 : | native TrieFreeCount(); | ||
6 : | native TrieMallocCount(); | ||
7 : | |||
8 : | new failcount = 0; | ||
9 : | new passcount = 0; | ||
10 : | public plugin_init() | ||
11 : | { | ||
12 : | register_plugin("Trie Test", AMXX_VERSION_STR, "AMXX Dev Team"); | ||
13 : | register_srvcmd("trietest", "trietest"); | ||
14 : | } | ||
15 : | |||
16 : | stock fail(const testname[]) | ||
17 : | { | ||
18 : | server_print("[FAIL] %s", testname); | ||
19 : | |||
20 : | failcount++; | ||
21 : | } | ||
22 : | stock pass(const testname[]) | ||
23 : | { | ||
24 : | server_print("[PASS] %s", testname); | ||
25 : | |||
26 : | passcount++; | ||
27 : | } | ||
28 : | stock done() | ||
29 : | { | ||
30 : | server_print("Finished. %d tests, %d failed", failcount + passcount, failcount); | ||
31 : | } | ||
32 : | stock check_frees() | ||
33 : | { | ||
34 : | if (TrieMallocCount() != TrieFreeCount()) | ||
35 : | fail("free count == malloc count"); | ||
36 : | |||
37 : | else | ||
38 : | pass("free count == malloc count"); | ||
39 : | |||
40 : | server_print("malloc count: %d free count: %d", TrieMallocCount(), TrieFreeCount()); | ||
41 : | } | ||
42 : | public trietest() | ||
43 : | { | ||
44 : | failcount = 0; | ||
45 : | passcount = 0; | ||
46 : | |||
47 : | new bool:ok = true; | ||
48 : | new Trie:t = TrieCreate(); | ||
49 : | |||
50 : | new Trie:oldhandle = t; // Makes sure that the trie handle system recycles old handles | ||
51 : | |||
52 : | new key[32]; | ||
53 : | for (new i = 0; i < 100; i++) | ||
54 : | { | ||
55 : | formatex(key, charsmax(key), "K%dK", i); | ||
56 : | TrieSetCell(t, key, i); | ||
57 : | } | ||
58 : | |||
59 : | for (new i = 0; i < 100; i++) | ||
60 : | { | ||
61 : | formatex(key, charsmax(key), "K%dK", i); | ||
62 : | new val; | ||
63 : | if (!TrieGetCell(t, key, val)) | ||
64 : | { | ||
65 : | server_print("TrieGetCell(%d, '%s', %d) failed", t, key, val); | ||
66 : | ok = false; | ||
67 : | } | ||
68 : | |||
69 : | else if (val != i) | ||
70 : | { | ||
71 : | server_print("val mismatch, expected: %d got: %d", i, val); | ||
72 : | ok = false; | ||
73 : | } | ||
74 : | |||
75 : | } | ||
76 : | if (ok) | ||
77 : | pass("Cell tests"); | ||
78 : | |||
79 : | else | ||
80 : | fail("Cell tests"); | ||
81 : | |||
82 : | TrieClear(t); | ||
83 : | TrieDestroy(t); | ||
84 : | |||
85 : | t = TrieCreate(); | ||
86 : | |||
87 : | if (t == oldhandle) | ||
88 : | pass("Recycle handles"); | ||
89 : | |||
90 : | else | ||
91 : | fail("Recycle handles"); | ||
92 : | |||
93 : | ok = true; | ||
94 : | for (new i = 0; i < 100; i++) | ||
95 : | { | ||
96 : | static val[32]; | ||
97 : | formatex(key, charsmax(key), "K%dK", i); | ||
98 : | formatex(val, charsmax(val), "V%dV", i); | ||
99 : | TrieSetString(t, key, val); | ||
100 : | } | ||
101 : | |||
102 : | for (new i = 0; i < 100; i++) | ||
103 : | { | ||
104 : | formatex(key, charsmax(key), "K%dK", i); | ||
105 : | static val[32]; | ||
106 : | static exp[32]; | ||
107 : | formatex(exp, charsmax(exp), "V%dV", i); | ||
108 : | if (!TrieGetString(t, key, val, charsmax(val))) | ||
109 : | { | ||
110 : | server_print("TrieGetString(%d, '%s', %s) failed", t, key, val); | ||
111 : | ok = false; | ||
112 : | } | ||
113 : | |||
114 : | else if (!equal(val, exp)) | ||
115 : | { | ||
116 : | server_print("val mismatch, key: '%s' expected: '%s' got: '%s'", key, exp, val); | ||
117 : | ok = false; | ||
118 : | } | ||
119 : | |||
120 : | } | ||
121 : | if (ok) | ||
122 : | pass("String tests"); | ||
123 : | |||
124 : | else | ||
125 : | fail("String tests"); | ||
126 : | |||
127 : | TrieDestroy(t); | ||
128 : | |||
129 : | check_frees(); | ||
130 : | |||
131 : | t = TrieCreate(); | ||
132 : | ok = true; | ||
133 : | for (new i = 0; i < 1000; i++) | ||
134 : | { | ||
135 : | formatex(key, charsmax(key), "!%d!", i); | ||
136 : | TrieSetString(t, key, key); | ||
137 : | } | ||
138 : | for (new i = 0; i < 1000; i++) | ||
139 : | { | ||
140 : | formatex(key, charsmax(key), "!%d!", i); | ||
141 : | |||
142 : | if (!TrieKeyExists(t, key)) | ||
143 : | { | ||
144 : | ok = false; | ||
145 : | server_print("Key '%s' does not exist", key); | ||
146 : | } | ||
147 : | else | ||
148 : | { | ||
149 : | if (!TrieDeleteKey(t, key)) | ||
150 : | { | ||
151 : | server_print("Key '%s' could not be deleted", key); | ||
152 : | ok = false; | ||
153 : | } | ||
154 : | } | ||
155 : | } | ||
156 : | if (ok) | ||
157 : | pass("Exists/Delete"); | ||
158 : | |||
159 : | else | ||
160 : | fail("Exists/Delete"); | ||
161 : | |||
162 : | check_frees(); | ||
163 : | TrieClear(t); | ||
164 : | TrieDestroy(t); | ||
165 : | check_frees(); | ||
166 : | done(); | ||
167 : | |||
168 : | } | ||
169 : |
Contact | ViewVC Help |
Powered by ViewVC 1.0.4 |