tests.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /**--------------------------------------------------------------------------**\
  2. ====================================
  3. y_hashmap - Link strings to values
  4. ====================================
  5. Description:
  6. Maps string indexes to integer indexes. Uses a fast hash to get an array
  7. slot, then a linked list to resolve collisions.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI hashmap include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 2.0
  44. Changelog:
  45. 23/06/13:
  46. First version.
  47. Functions:
  48. stock:
  49. HashMap_Init - Associate a hash map with an array.
  50. HashMap_Add - Add a value under a given string.
  51. HashMap_Get - Get a value from a string.
  52. HashMap_RemoveKey - Remove a string and its value from a hash map.
  53. HashMap_Set - Change the value associated with a key.
  54. Definitions:
  55. HASH_MAP_DATA - What should be added to enums to be hash map referenced.
  56. HashMap - Declare a new hash map.
  57. \**--------------------------------------------------------------------------**/
  58. enum E_HASH_MAP_TEST
  59. {
  60. //HASH_MAP_DATA<32>,
  61. E_HASH_MAP_TEST_NAME[32],
  62. E_HASH_MAP_TEST_DATA[HASH_MAP_DATA]
  63. }
  64. Test:y_hashmap_Init()
  65. {
  66. new HashMap:m<>;
  67. new data[100][E_HASH_MAP_TEST];
  68. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  69. ASSERT(data[88][E_HASH_MAP_TEST_DATA][0] == 0);
  70. ASSERT(data[88][E_HASH_MAP_TEST_DATA][1] == 0);
  71. ASSERT(data[88][E_HASH_MAP_TEST_DATA][2] == 0);
  72. }
  73. Test:rawMemcpy()
  74. {
  75. new src[100] = "Hello there", dst[100];
  76. rawMemcpy(ref(dst), ref(src), 100 * 4);
  77. ASSERT(bool:dst[0]);
  78. ASSERT(!strcmp(dst, "Hello there"));
  79. }
  80. Test:y_hashmap_Add()
  81. {
  82. new HashMap:m<>;
  83. new data[100][E_HASH_MAP_TEST];
  84. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  85. HashMap_Add(m, "Hello", 42);
  86. ASSERT(bool:data[42][E_HASH_MAP_TEST_NAME][0]);
  87. ASSERT(!strcmp(data[42][E_HASH_MAP_TEST_NAME], "Hello"));
  88. ASSERT(data[42][E_HASH_MAP_TEST_DATA][0] == bernstein("Hello"));
  89. ASSERT(data[42][E_HASH_MAP_TEST_DATA][1] == 0);
  90. ASSERT(data[42][E_HASH_MAP_TEST_DATA][2] == 0);
  91. }
  92. Test:y_hashmap_Get1()
  93. {
  94. new HashMap:m<>;
  95. new data[100][E_HASH_MAP_TEST];
  96. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  97. HashMap_Add(m, "Hello", 42);
  98. /*printf("%s %d %d %d %d"
  99. , data[42][_E_HASH_MAP_NAME]
  100. , data[42][_E_HASH_MAP_NEXT]
  101. , data[42][_E_HASH_MAP_HASH]
  102. , data[42][E_HASH_MAP_TEST_1]
  103. , data[42][E_HASH_MAP_TEST_2]);*/
  104. ASSERT(HashMap_Get(m, "Hello") == 42);
  105. }
  106. Test:y_hashmap_GetVarious()
  107. {
  108. new HashMap:m<>;
  109. new data[100][E_HASH_MAP_TEST];
  110. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  111. HashMap_Add(m, "01234567890", 10);
  112. HashMap_Add(m, "012345678901", 11);
  113. HashMap_Add(m, "0123456789012", 12);
  114. HashMap_Add(m, "01234567890123", 13);
  115. HashMap_Add(m, "012345678901234", 14);
  116. HashMap_Add(m, "0123456789012345", 15);
  117. HashMap_Add(m, "01234567890123456", 16);
  118. HashMap_Add(m, "012345678901234567", 17);
  119. HashMap_Add(m, "0123456789012345678", 18);
  120. HashMap_Add(m, "01234567890123456789", 19);
  121. HashMap_Add(m, "0", 0);
  122. HashMap_Add(m, "01", 1);
  123. HashMap_Add(m, "012", 2);
  124. HashMap_Add(m, "0123", 3);
  125. HashMap_Add(m, "01234", 4);
  126. HashMap_Add(m, "012345", 5);
  127. HashMap_Add(m, "0123456", 6);
  128. HashMap_Add(m, "01234567", 7);
  129. HashMap_Add(m, "012345678", 8);
  130. HashMap_Add(m, "0123456789", 9);
  131. HashMap_Add(m, "012345678901234567890", 20);
  132. HashMap_Add(m, "0123456789012345678901", 21);
  133. HashMap_Add(m, "01234567890123456789012", 22);
  134. HashMap_Add(m, "012345678901234567890123", 23);
  135. HashMap_Add(m, "0123456789012345678901234", 24);
  136. HashMap_Add(m, "01234567890123456789012345", 25);
  137. HashMap_Add(m, "012345678901234567890123456", 26);
  138. HashMap_Add(m, "0123456789012345678901234567", 27);
  139. HashMap_Add(m, "01234567890123456789012345678", 28);
  140. HashMap_Add(m, "012345678901234567890123456789", 29);
  141. ASSERT(HashMap_Get(m, "012345678901234567890") == 20);
  142. ASSERT(HashMap_Get(m, "0123456789012345678901") == 21);
  143. ASSERT(HashMap_Get(m, "01234567890123456789012") == 22);
  144. ASSERT(HashMap_Get(m, "012345678901234567890123") == 23);
  145. ASSERT(HashMap_Get(m, "0123456789012345678901234") == 24);
  146. ASSERT(HashMap_Get(m, "01234567890123456789012345") == 25);
  147. ASSERT(HashMap_Get(m, "012345678901234567890123456") == 26);
  148. ASSERT(HashMap_Get(m, "0123456789012345678901234567") == 27);
  149. ASSERT(HashMap_Get(m, "01234567890123456789012345678") == 28);
  150. ASSERT(HashMap_Get(m, "012345678901234567890123456789") == 29);
  151. ASSERT(HashMap_Get(m, "01234567890") == 10);
  152. ASSERT(HashMap_Get(m, "012345678901") == 11);
  153. ASSERT(HashMap_Get(m, "0123456789012") == 12);
  154. ASSERT(HashMap_Get(m, "01234567890123") == 13);
  155. ASSERT(HashMap_Get(m, "012345678901234") == 14);
  156. ASSERT(HashMap_Get(m, "0123456789012345") == 15);
  157. ASSERT(HashMap_Get(m, "01234567890123456") == 16);
  158. ASSERT(HashMap_Get(m, "012345678901234567") == 17);
  159. ASSERT(HashMap_Get(m, "0123456789012345678") == 18);
  160. ASSERT(HashMap_Get(m, "01234567890123456789") == 19);
  161. ASSERT(HashMap_Get(m, "0") == 0);
  162. ASSERT(HashMap_Get(m, "01") == 1);
  163. ASSERT(HashMap_Get(m, "012") == 2);
  164. ASSERT(HashMap_Get(m, "0123") == 3);
  165. ASSERT(HashMap_Get(m, "01234") == 4);
  166. ASSERT(HashMap_Get(m, "012345") == 5);
  167. ASSERT(HashMap_Get(m, "0123456") == 6);
  168. ASSERT(HashMap_Get(m, "01234567") == 7);
  169. ASSERT(HashMap_Get(m, "012345678") == 8);
  170. ASSERT(HashMap_Get(m, "0123456789") == 9);
  171. }
  172. Test:y_hashmap_RemoveKey1()
  173. {
  174. new HashMap:m<>;
  175. new data[100][E_HASH_MAP_TEST];
  176. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  177. HashMap_Add(m, "Hello", 42);
  178. ASSERT(HashMap_Get(m, "Hello") == 42);
  179. HashMap_RemoveKey(m, "Hello");
  180. ASSERT(HashMap_Get(m, "Hello") == -1);
  181. }
  182. Test:y_hashmap_Get100()
  183. {
  184. new HashMap:m<>;
  185. new data[100][E_HASH_MAP_TEST];
  186. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  187. for (new i = 0, str[6]; i != 100; ++i)
  188. {
  189. format(str, sizeof (str), "hi%d", i);
  190. HashMap_Add(m, str, i);
  191. }
  192. for (new i = 0, str[6]; i != 100; ++i)
  193. {
  194. format(str, sizeof (str), "hi%d", i);
  195. ASSERT(HashMap_Get(m, str) == i);
  196. }
  197. }
  198. Test:y_hashmap_Get100Hash()
  199. {
  200. new HashMap:m<>;
  201. new data[100][E_HASH_MAP_TEST];
  202. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  203. for (new i = 0, str[6]; i != 100; ++i)
  204. {
  205. format(str, sizeof (str), "hi%d", i);
  206. HashMap_Add(m, str, i);
  207. }
  208. for (new i = 0, str[6]; i != 100; ++i)
  209. {
  210. format(str, sizeof (str), "hi%d", i);
  211. ASSERT(HashMap_Get(m, str) == i);
  212. }
  213. }
  214. Test:y_hashmap_Remove50()
  215. {
  216. new HashMap:m<>;
  217. new data[100][E_HASH_MAP_TEST];
  218. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  219. // Add.
  220. for (new i = 0, str[6]; i != 100; ++i)
  221. {
  222. format(str, sizeof (str), "hi%d", i);
  223. HashMap_Add(m, str, i);
  224. }
  225. // ASSERT.
  226. for (new i = 0, str[6]; i != 100; ++i)
  227. {
  228. format(str, sizeof (str), "hi%d", i);
  229. ASSERT(HashMap_Get(m, str) == i);
  230. }
  231. // Remove half.
  232. for (new i = 1, str[6]; i != 101; i += 2)
  233. {
  234. format(str, sizeof (str), "hi%d", i);
  235. HashMap_RemoveKey(m, str);
  236. }
  237. // ASSERT.
  238. for (new i = 0, str[6]; i != 100; ++i)
  239. {
  240. format(str, sizeof (str), "hi%d", i);
  241. if (i & 1) ASSERT(HashMap_Get(m, str) == -1);
  242. else ASSERT(HashMap_Get(m, str) == i);
  243. }
  244. }
  245. Test:y_hashmap_RemoveValues()
  246. {
  247. new HashMap:m<>;
  248. new data[100][E_HASH_MAP_TEST];
  249. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  250. // Add.
  251. for (new i = 0, str[6]; i != 100; ++i)
  252. {
  253. format(str, sizeof (str), "hi%d", i);
  254. HashMap_Add(m, str, i);
  255. }
  256. // ASSERT.
  257. for (new i = 0, str[6]; i != 100; ++i)
  258. {
  259. format(str, sizeof (str), "hi%d", i);
  260. ASSERT(HashMap_Get(m, str) == i);
  261. }
  262. // Remove half.
  263. for (new i = 0; i != 100; ++i)
  264. {
  265. HashMap_RemoveValue(m, i);
  266. }
  267. // ASSERT.
  268. for (new i = 0, str[6]; i != 100; ++i)
  269. {
  270. format(str, sizeof (str), "hi%d", i);
  271. ASSERT(HashMap_Get(m, str) == -1);
  272. }
  273. }
  274. Test:y_hashmap_Get1000()
  275. {
  276. new HashMap:m<>;
  277. new data[1000][E_HASH_MAP_TEST];
  278. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  279. for (new i = 0, str[6]; i != 1000; ++i)
  280. {
  281. format(str, sizeof (str), "hi%d", i);
  282. HashMap_Add(m, str, i);
  283. }
  284. for (new i = 0, str[6]; i != 1000; ++i)
  285. {
  286. format(str, sizeof (str), "hi%d", i);
  287. ASSERT(HashMap_Get(m, str) == i);
  288. }
  289. }
  290. Test:y_hashmap_Remove500()
  291. {
  292. new HashMap:m<>;
  293. new data[1000][E_HASH_MAP_TEST];
  294. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  295. // Add.
  296. for (new i = 0, str[6]; i != 1000; ++i)
  297. {
  298. format(str, sizeof (str), "hi%d", i);
  299. HashMap_Add(m, str, i);
  300. }
  301. // ASSERT.
  302. for (new i = 0, str[6]; i != 1000; ++i)
  303. {
  304. format(str, sizeof (str), "hi%d", i);
  305. ASSERT(HashMap_Get(m, str) == i);
  306. }
  307. // Remove half.
  308. for (new i = 1, str[6]; i != 1001; i += 2)
  309. {
  310. format(str, sizeof (str), "hi%d", i);
  311. HashMap_RemoveKey(m, str);
  312. }
  313. // ASSERT.
  314. for (new i = 0, str[6]; i != 1000; ++i)
  315. {
  316. format(str, sizeof (str), "hi%d", i);
  317. if (i & 1) ASSERT(HashMap_Get(m, str) == -1);
  318. else ASSERT(HashMap_Get(m, str) == i);
  319. }
  320. }
  321. Test:y_hashmap_Set500()
  322. {
  323. new HashMap:m<>;
  324. new data[1000][E_HASH_MAP_TEST];
  325. HashMap_Init(m, data, E_HASH_MAP_TEST_DATA);
  326. // Add.
  327. for (new i = 0, str[6]; i != 500; ++i)
  328. {
  329. format(str, sizeof (str), "hi%d", i);
  330. HashMap_Add(m, str, i);
  331. }
  332. // ASSERT.
  333. for (new i = 0, str[6]; i != 500; ++i)
  334. {
  335. format(str, sizeof (str), "hi%d", i);
  336. ASSERT(HashMap_Get(m, str) == i);
  337. }
  338. // Remove half.
  339. for (new i = 1, str[6]; i != 501; i += 2)
  340. {
  341. format(str, sizeof (str), "hi%d", i);
  342. HashMap_Set(m, str, i + 500);
  343. }
  344. // ASSERT.
  345. for (new i = 0, str[6]; i != 500; ++i)
  346. {
  347. format(str, sizeof (str), "hi%d", i);
  348. if (i & 1) ASSERT(HashMap_Get(m, str) == i + 500);
  349. else ASSERT(HashMap_Get(m, str) == i);
  350. }
  351. for (new i = 500, str[6]; i != 1000; ++i)
  352. {
  353. format(str, sizeof (str), "hi%d", i);
  354. ASSERT(HashMap_Get(m, str) == -1);
  355. }
  356. }