tests.inc 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /**--------------------------------------------------------------------------**\
  2. ========================================
  3. y_stringhash - Fast string comparisons
  4. ========================================
  5. Description:
  6. Allows you to hash strings at compile time to use them in a switch.
  7. Legal:
  8. Version: MPL 1.1
  9. The contents of this file are subject to the Mozilla Public License Version
  10. 1.1 (the "License"); you may not use this file except in compliance with
  11. the License. You may obtain a copy of the License at
  12. http://www.mozilla.org/MPL/
  13. Software distributed under the License is distributed on an "AS IS" basis,
  14. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. for the specific language governing rights and limitations under the
  16. License.
  17. The Original Code is the YSI stringhash include.
  18. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  19. Portions created by the Initial Developer are Copyright (C) 2011
  20. the Initial Developer. All Rights Reserved.
  21. Contributors:
  22. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  23. Thanks:
  24. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  25. ZeeX - Very productive conversations.
  26. koolk - IsPlayerinAreaEx code.
  27. TheAlpha - Danish translation.
  28. breadfish - German translation.
  29. Fireburn - Dutch translation.
  30. yom - French translation.
  31. 50p - Polish translation.
  32. Zamaroht - Spanish translation.
  33. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  34. for me to strive to better.
  35. Pixels^ - Running XScripters where the idea was born.
  36. Matite - Pestering me to release it and using it.
  37. Very special thanks to:
  38. Thiadmer - PAWN, whose limits continue to amaze me!
  39. Kye/Kalcor - SA:MP.
  40. SA:MP Team past, present and future - SA:MP.
  41. Version:
  42. 2.0
  43. Changelog:
  44. 06/03/11:
  45. Changed the order of some letters to better support numbers in _I.
  46. 01/03/11:
  47. Rewrote compile-time hashes to not require commas.
  48. 25/10/10:
  49. Updated comments.
  50. Added to YSI 1.0.
  51. Added FNV1 and FNV1a hashes to avoid collisions.
  52. 19/08/10:
  53. First version.
  54. Functions:
  55. stock:
  56. YHash - Generate a string hash at run time.
  57. Definitions:
  58. _H - Generate a string hash at compile time.
  59. _I - Generate a case insensitive string hash at compile time.
  60. \**--------------------------------------------------------------------------**/
  61. Test:y_stringhash__H1()
  62. {
  63. new
  64. h0 = _H<hello>,
  65. h1 = _H<there>;
  66. ASSERT(h0 != h1);
  67. }
  68. Test:y_stringhash__I1()
  69. {
  70. new
  71. h0 = _I<hello>,
  72. h1 = _I<HelLo>;
  73. ASSERT(h0 == h1);
  74. }
  75. Test:y_stringhash__H2()
  76. {
  77. new
  78. h0 = _H(h,e,l,l,o),
  79. h1 = _H(t,h,e,r,e);
  80. ASSERT(h0 != h1);
  81. }
  82. Test:y_stringhash__I2()
  83. {
  84. new
  85. h0 = _I(h,e,l,l,o),
  86. h1 = _I(H,E,L,L,O);
  87. ASSERT(h0 == h1);
  88. }
  89. Test:y_stringhash__H3()
  90. {
  91. new
  92. h0 = _H@b<hello>,
  93. h1 = _H@b<there>;
  94. ASSERT(h0 != h1);
  95. }
  96. Test:y_stringhash__I3()
  97. {
  98. new
  99. h0 = _I@b<hello>,
  100. h1 = _I@b<HelLo>;
  101. ASSERT(h0 == h1);
  102. }
  103. Test:y_stringhash__H4()
  104. {
  105. new
  106. h0 = _H@b(h,e,l,l,o),
  107. h1 = _H@b(t,h,e,r,e);
  108. ASSERT(h0 != h1);
  109. }
  110. Test:y_stringhash__I4()
  111. {
  112. new
  113. h0 = _I@b(h,e,l,l,o),
  114. h1 = _I@b(H,E,L,L,O);
  115. ASSERT(h0 == h1);
  116. }
  117. Test:y_stringhash__H5()
  118. {
  119. new
  120. h0 = _H@f<hello>,
  121. h1 = _H@f<there>;
  122. ASSERT(h0 != h1);
  123. }
  124. Test:y_stringhash__I5()
  125. {
  126. new
  127. h0 = _I@f<hello>,
  128. h1 = _I@f<HelLo>;
  129. ASSERT(h0 == h1);
  130. }
  131. Test:y_stringhash__H6()
  132. {
  133. new
  134. h0 = _H@f(h,e,l,l,o),
  135. h1 = _H@f(t,h,e,r,e);
  136. ASSERT(h0 != h1);
  137. }
  138. Test:y_stringhash__I6()
  139. {
  140. new
  141. h0 = _I@f(h,e,l,l,o),
  142. h1 = _I@f(H,E,L,L,O);
  143. ASSERT(h0 == h1);
  144. }
  145. Test:y_stringhash__H7()
  146. {
  147. new
  148. h0 = _H@a<hello>,
  149. h1 = _H@a<there>;
  150. ASSERT(h0 != h1);
  151. }
  152. Test:y_stringhash__I7()
  153. {
  154. new
  155. h0 = _I@a<hello>,
  156. h1 = _I@a<HelLo>;
  157. ASSERT(h0 == h1);
  158. }
  159. Test:y_stringhash__H8()
  160. {
  161. new
  162. h0 = _H@a(h,e,l,l,o),
  163. h1 = _H@a(t,h,e,r,e);
  164. ASSERT(h0 != h1);
  165. }
  166. Test:y_stringhash__I8()
  167. {
  168. new
  169. h0 = _I@a(h,e,l,l,o),
  170. h1 = _I@a(H,E,L,L,O);
  171. ASSERT(h0 == h1);
  172. }
  173. Test:y_stringhash__H3e()
  174. {
  175. new
  176. h0 = _H@b<hello>,
  177. h1 = YHash("hello", .type = hash_bernstein);
  178. ASSERT(h0 == h1);
  179. }
  180. Test:y_stringhash__I3e()
  181. {
  182. new
  183. h0 = _I@b<hello>,
  184. h1 = YHash("hello", .type = hash_bernstein, .sensitive = false);
  185. //printf("%04x%04x %04x%04x", h0 >>> 16, h0 & 0xFFFF, h1 >>> 16, h1 & 0xFFFF);
  186. ASSERT(h0 == h1);
  187. }
  188. Test:y_stringhash__H4e()
  189. {
  190. new
  191. h0 = _H@b(h,e,l,l,o),
  192. h1 = YHash("hello", .type = hash_bernstein);
  193. ASSERT(h0 == h1);
  194. }
  195. Test:y_stringhash__I4e()
  196. {
  197. new
  198. h0 = _I@b(h,e,l,l,o),
  199. h1 = YHash("hello", .type = hash_bernstein, .sensitive = false);
  200. ASSERT(h0 == h1);
  201. }
  202. Test:y_stringhash__H5e()
  203. {
  204. new
  205. h0 = _H@f<hello>,
  206. h1 = YHash("hello", .type = hash_fnv1);
  207. ASSERT(h0 == h1);
  208. }
  209. Test:y_stringhash__I5e()
  210. {
  211. new
  212. h0 = _I@f<hello>,
  213. h1 = YHash("hello", .type = hash_fnv1, .sensitive = false);
  214. ASSERT(h0 == h1);
  215. }
  216. Test:y_stringhash__H6e()
  217. {
  218. new
  219. h0 = _H@f(h,e,l,l,o),
  220. h1 = YHash("hello", .type = hash_fnv1);
  221. ASSERT(h0 == h1);
  222. }
  223. Test:y_stringhash__I6e()
  224. {
  225. new
  226. h0 = _I@f(h,e,l,l,o),
  227. h1 = YHash("hello", .type = hash_fnv1, .sensitive = false);
  228. ASSERT(h0 == h1);
  229. }
  230. Test:y_stringhash__H7e()
  231. {
  232. new
  233. h0 = _H@a<hello>,
  234. h1 = YHash("hello", .type = hash_fnv1a);
  235. ASSERT(h0 == h1);
  236. }
  237. Test:y_stringhash__I7e()
  238. {
  239. new
  240. h0 = _I@a<hello>,
  241. h1 = YHash("hello", .type = hash_fnv1a, .sensitive = false);
  242. ASSERT(h0 == h1);
  243. }
  244. Test:y_stringhash__H8e()
  245. {
  246. new
  247. h0 = _H@a(h,e,l,l,o),
  248. h1 = YHash("hello", .type = hash_fnv1a);
  249. ASSERT(h0 == h1);
  250. }
  251. Test:y_stringhash__I8e()
  252. {
  253. new
  254. h0 = _I@a(h,e,l,l,o),
  255. h1 = YHash("hello", .type = hash_fnv1a, .sensitive = false);
  256. ASSERT(h0 == h1);
  257. }
  258. Test:y_stringhash__H3d()
  259. {
  260. new
  261. h0 = HASH:bernstein<hello>,
  262. h1 = YHash("hello", .type = hash_bernstein);
  263. ASSERT(h0 == h1);
  264. }
  265. Test:y_stringhash__I3d()
  266. {
  267. new
  268. h0 = HASHi:bernstein<hello>,
  269. h1 = YHash("hello", .type = hash_bernstein, .sensitive = false);
  270. ASSERT(h0 == h1);
  271. }
  272. Test:y_stringhash__H4d()
  273. {
  274. new
  275. h0 = HASH(bernstein,h,e,l,l,o),
  276. h1 = YHash("hello", .type = hash_bernstein);
  277. ASSERT(h0 == h1);
  278. }
  279. Test:y_stringhash__I4d()
  280. {
  281. new
  282. h0 = HASHi(bernstein,h,e,l,l,o),
  283. h1 = YHash("hello", .type = hash_bernstein, .sensitive = false);
  284. ASSERT(h0 == h1);
  285. }
  286. Test:y_stringhash__H5d()
  287. {
  288. new
  289. h0 = HASH:fnv1<hello>,
  290. h1 = YHash("hello", .type = hash_fnv1);
  291. ASSERT(h0 == h1);
  292. }
  293. Test:y_stringhash__I5d()
  294. {
  295. new
  296. h0 = HASHi:fnv1<hello>,
  297. h1 = YHash("hello", .type = hash_fnv1, .sensitive = false);
  298. ASSERT(h0 == h1);
  299. }
  300. Test:y_stringhash__H6d()
  301. {
  302. new
  303. h0 = HASH(fnv1,h,e,l,l,o),
  304. h1 = YHash("hello", .type = hash_fnv1);
  305. ASSERT(h0 == h1);
  306. }
  307. Test:y_stringhash__I6d()
  308. {
  309. new
  310. h0 = HASHi(fnv1,h,e,l,l,o),
  311. h1 = YHash("hello", .type = hash_fnv1, .sensitive = false);
  312. ASSERT(h0 == h1);
  313. }
  314. Test:y_stringhash__H7d()
  315. {
  316. new
  317. h0 = HASH:fnv1a<hello>,
  318. h1 = YHash("hello", .type = hash_fnv1a);
  319. ASSERT(h0 == h1);
  320. }
  321. Test:y_stringhash__I7d()
  322. {
  323. new
  324. h0 = HASHi:fnv1a<hello>,
  325. h1 = YHash("hello", .type = hash_fnv1a, .sensitive = false);
  326. ASSERT(h0 == h1);
  327. }
  328. Test:y_stringhash__H8d()
  329. {
  330. new
  331. h0 = HASH(fnv1a,h,e,l,l,o),
  332. h1 = YHash("hello", .type = hash_fnv1a);
  333. ASSERT(h0 == h1);
  334. }
  335. Test:y_stringhash__I8d()
  336. {
  337. new
  338. h0 = HASHi(fnv1a,h,e,l,l,o),
  339. h1 = YHash("hello", .type = hash_fnv1a, .sensitive = false);
  340. ASSERT(h0 == h1);
  341. }
  342. Test:y_stringhash__H3c()
  343. {
  344. new
  345. h0 = HASH:bernstein<hello>,
  346. h1 = _H(h,e,l,l,o);
  347. ASSERT(h0 == h1);
  348. }
  349. Test:y_stringhash__I3c()
  350. {
  351. new
  352. h0 = HASHi:bernstein<hello>,
  353. h1 = _I(h,e,L,L,o);
  354. ASSERT(h0 == h1);
  355. }
  356. Test:y_stringhash__H4c()
  357. {
  358. new
  359. h0 = HASH(bernstein,h,e,l,l,o),
  360. h1 = _H<hello>;
  361. ASSERT(h0 == h1);
  362. }
  363. Test:y_stringhash__I4c()
  364. {
  365. new
  366. h0 = HASHi(bernstein,h,e,l,l,o),
  367. h1 = _I<hellO>;
  368. ASSERT(h0 == h1);
  369. }
  370. Test:y_stringhash__H5c()
  371. {
  372. new
  373. h0 = HASH:fnv1<hello>,
  374. h1 = _H@f(h,e,l,l,o);
  375. ASSERT(h0 == h1);
  376. }
  377. Test:y_stringhash__I5c()
  378. {
  379. new
  380. h0 = HASHi:fnv1<hello>,
  381. h1 = _I@f(h,E,l,l,o);
  382. ASSERT(h0 == h1);
  383. }
  384. Test:y_stringhash__H6c()
  385. {
  386. new
  387. h0 = HASH(fnv1,h,e,l,l,o),
  388. h1 = _H@f<hello>;
  389. ASSERT(h0 == h1);
  390. }
  391. Test:y_stringhash__I6c()
  392. {
  393. new
  394. h0 = HASHi(fnv1,h,e,l,l,o),
  395. h1 = _I@f<hElLO>;
  396. ASSERT(h0 == h1);
  397. }
  398. Test:y_stringhash__H7c()
  399. {
  400. new
  401. h0 = HASH:fnv1a<hello>,
  402. h1 = _H@a(h,e,l,l,o);
  403. ASSERT(h0 == h1);
  404. }
  405. Test:y_stringhash__I7c()
  406. {
  407. new
  408. h0 = HASHi:fnv1a<hello>,
  409. h1 = _I@a(H,e,l,l,o);
  410. ASSERT(h0 == h1);
  411. }
  412. Test:y_stringhash__H8c()
  413. {
  414. new
  415. h0 = HASH(fnv1a,h,e,l,l,o),
  416. h1 = _H@a<hello>;
  417. ASSERT(h0 == h1);
  418. }
  419. Test:y_stringhash__I8c()
  420. {
  421. new
  422. h0 = HASHi(fnv1a,h,e,l,l,o),
  423. h1 = _I@a<HELLO>;
  424. ASSERT(h0 == h1);
  425. }
  426. Test:y_stringhash__H3b()
  427. {
  428. new
  429. h0 = HASH:bernstein<hello>,
  430. h1 = HASH:bernstein<there>;
  431. ASSERT(h0 != h1);
  432. }
  433. Test:y_stringhash__I3b()
  434. {
  435. new
  436. h0 = HASHi:bernstein<hello>,
  437. h1 = HASHi:bernstein<HelLo>;
  438. ASSERT(h0 == h1);
  439. }
  440. Test:y_stringhash__H4b()
  441. {
  442. new
  443. h0 = HASH(bernstein,h,e,l,l,o),
  444. h1 = HASH(bernstein,t,h,e,r,e);
  445. ASSERT(h0 != h1);
  446. }
  447. Test:y_stringhash__I4b()
  448. {
  449. new
  450. h0 = HASHi(bernstein,h,e,l,l,o),
  451. h1 = HASHi(bernstein,H,E,L,L,O);
  452. ASSERT(h0 == h1);
  453. }
  454. Test:y_stringhash__H5b()
  455. {
  456. new
  457. h0 = HASH:fnv1<hello>,
  458. h1 = HASH:fnv1<there>;
  459. ASSERT(h0 != h1);
  460. }
  461. Test:y_stringhash__I5b()
  462. {
  463. new
  464. h0 = HASHi:fnv1<hello>,
  465. h1 = HASHi:fnv1<HelLo>;
  466. ASSERT(h0 == h1);
  467. }
  468. Test:y_stringhash__H6b()
  469. {
  470. new
  471. h0 = HASH(fnv1,h,e,l,l,o),
  472. h1 = HASH(fnv1,t,h,e,r,e);
  473. ASSERT(h0 != h1);
  474. }
  475. Test:y_stringhash__I6b()
  476. {
  477. new
  478. h0 = HASHi(fnv1,h,e,l,l,o),
  479. h1 = HASHi(fnv1,H,E,L,L,O);
  480. ASSERT(h0 == h1);
  481. }
  482. Test:y_stringhash__H7b()
  483. {
  484. new
  485. h0 = HASH:fnv1a<hello>,
  486. h1 = HASH:fnv1a<there>;
  487. ASSERT(h0 != h1);
  488. }
  489. Test:y_stringhash__I7b()
  490. {
  491. new
  492. h0 = HASHi:fnv1a<hello>,
  493. h1 = HASHi:fnv1a<HelLo>;
  494. ASSERT(h0 == h1);
  495. }
  496. Test:y_stringhash__H8b()
  497. {
  498. new
  499. h0 = HASH(fnv1a,h,e,l,l,o),
  500. h1 = HASH(fnv1a,t,h,e,r,e);
  501. ASSERT(h0 != h1);
  502. }
  503. Test:y_stringhash__I8b()
  504. {
  505. new
  506. h0 = HASHi(fnv1a,h,e,l,l,o),
  507. h1 = HASHi(fnv1a,H,E,L,L,O);
  508. ASSERT(h0 == h1);
  509. }