y_malloc_tests.inc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Legal:
  3. Version: MPL 1.1
  4. The contents of this file are subject to the Mozilla Public License Version
  5. 1.1 the "License"; you may not use this file except in compliance with
  6. the License. You may obtain a copy of the License at
  7. http://www.mozilla.org/MPL/
  8. Software distributed under the License is distributed on an "AS IS" basis,
  9. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. for the specific language governing rights and limitations under the
  11. License.
  12. The Original Code is the YSI framework.
  13. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  14. Portions created by the Initial Developer are Copyright C 2011
  15. the Initial Developer. All Rights Reserved.
  16. Contributors:
  17. Y_Less
  18. koolk
  19. JoeBullet/Google63
  20. g_aSlice/Slice
  21. Misiur
  22. samphunter
  23. tianmeta
  24. maddinat0r
  25. spacemud
  26. Crayder
  27. Dayvison
  28. Ahmad45123
  29. Zeex
  30. irinel1996
  31. Yiin-
  32. Chaprnks
  33. Konstantinos
  34. Masterchen09
  35. Southclaws
  36. PatchwerkQWER
  37. m0k1
  38. paulommu
  39. udan111
  40. Thanks:
  41. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  42. ZeeX - Very productive conversations.
  43. koolk - IsPlayerinAreaEx code.
  44. TheAlpha - Danish translation.
  45. breadfish - German translation.
  46. Fireburn - Dutch translation.
  47. yom - French translation.
  48. 50p - Polish translation.
  49. Zamaroht - Spanish translation.
  50. Los - Portuguese translation.
  51. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  52. me to strive to better.
  53. Pixels^ - Running XScripters where the idea was born.
  54. Matite - Pestering me to release it and using it.
  55. Very special thanks to:
  56. Thiadmer - PAWN, whose limits continue to amaze me!
  57. Kye/Kalcor - SA:MP.
  58. SA:MP Team past, present and future - SA:MP.
  59. Optional plugins:
  60. Gamer_Z - GPS.
  61. Incognito - Streamer.
  62. Me - sscanf2, fixes2, Whirlpool.
  63. */
  64. TEST__ y_malloc_100()
  65. {
  66. new
  67. Alloc:a = malloc(1000);
  68. ASSERT(bool:a);
  69. free(a);
  70. }
  71. TEST__ y_malloc_2_100()
  72. {
  73. new
  74. Alloc:a = malloc(100);
  75. ASSERT(bool:a);
  76. free(a);
  77. new
  78. Alloc:b = malloc(100);
  79. ASSERT(bool:b);
  80. free(a);
  81. ASSERT_EQ(a, b);
  82. }
  83. TEST__ y_malloc_2000()
  84. {
  85. new
  86. Alloc:a = malloc(2000);
  87. ASSERT(bool:a);
  88. free(a);
  89. }
  90. TEST__ y_malloc_clear()
  91. {
  92. new Alloc:mem[3];
  93. mem[0] = malloc(2);
  94. mem[1] = malloc(1);
  95. free(mem[0]);
  96. mem[2] = malloc(1);
  97. free(mem[1]);
  98. free(mem[2]);
  99. mem[0] = malloc(30);
  100. ASSERT(bool:mem[0]);
  101. free(mem[0]);
  102. }
  103. TEST__ y_malloc_Loads()
  104. {
  105. // Allocate slightly more memory than we have room for.
  106. new
  107. Alloc:a = malloc(MALLOC_MEMORY + 1);
  108. ASSERT_EQ(a, NO_ALLOC);
  109. ASSERT(!free(a));
  110. }
  111. #define MALLOC_TEST(%0) for (new Alloc:a = malloc(%0), __loop = 1; a && (__loop || !free(a)); --__loop)
  112. TEST__ y_malloc_mset()
  113. {
  114. MALLOC_TEST(10)
  115. {
  116. //ASSERT_ZE(mget(a, 5));
  117. mset(a, 5, 42);
  118. ASSERT_EQ(mget(a, 5), 42);
  119. }
  120. }
  121. TEST__ y_malloc_mget()
  122. {
  123. MALLOC_TEST(10)
  124. {
  125. mset(a, 5, 11);
  126. ASSERT_EQ(mget(a, 5), 11);
  127. mset(a, 5, 42);
  128. //ASSERT_ZE(mget(a, 4));
  129. //ASSERT_ZE(mget(a, 6));
  130. }
  131. }
  132. TEST__ y_malloc_SlotSize()
  133. {
  134. MALLOC_TEST(10)
  135. {
  136. ASSERT_EQ(Malloc_SlotSize(a), 16 - 1);
  137. }
  138. MALLOC_TEST(100)
  139. {
  140. ASSERT_EQ(Malloc_SlotSize(a), 112 - 1);
  141. }
  142. MALLOC_TEST(1000)
  143. {
  144. ASSERT_EQ(Malloc_SlotSize(a), 1008 - 1);
  145. }
  146. MALLOC_TEST(1)
  147. {
  148. ASSERT_EQ(Malloc_SlotSize(a), 16 - 1);
  149. }
  150. MALLOC_TEST(15)
  151. {
  152. ASSERT_EQ(Malloc_SlotSize(a), 16 - 1);
  153. }
  154. MALLOC_TEST(16)
  155. {
  156. ASSERT_EQ(Malloc_SlotSize(a), 32 - 1);
  157. }
  158. for (new size = 1; size != 16; ++size)
  159. {
  160. MALLOC_TEST(size)
  161. {
  162. ASSERT_EQ(Malloc_SlotSize(a), 16 - 1);
  163. }
  164. }
  165. for (new size = 16; size != 32; ++size)
  166. {
  167. MALLOC_TEST(size)
  168. {
  169. ASSERT_EQ(Malloc_SlotSize(a), 32 - 1);
  170. }
  171. }
  172. }
  173. TEST__ y_malloc_msets()
  174. {
  175. static const
  176. str0[] = "1111 ",
  177. str1[] = !"2222 ";
  178. MALLOC_TEST(10)
  179. {
  180. ASSERT_EQ(msets(a, 0, str0, false), 8);
  181. ASSERT_EQ(mget(a, 0), str0[0]);
  182. ASSERT_EQ(msets(a, 0, str1, false), 8);
  183. ASSERT_EQ(mget(a, 0), str1{0});
  184. ASSERT_EQ(msets(a, 0, str0, true), 8 char);
  185. ASSERT_EQ(mget(a, 0), ('1' << 24) | ('1' << 16) | ('1' << 08) | ('1' << 00));
  186. ASSERT_EQ(msets(a, 0, str1, true), 8 char);
  187. ASSERT_EQ(mget(a, 0), str1[0]);
  188. }
  189. }
  190. TEST__ y_malloc_mgets()
  191. {
  192. static const
  193. str0[] = "1111 ";
  194. new
  195. str2[20];
  196. MALLOC_TEST(10)
  197. {
  198. msets(a, 0, str0, false);
  199. mgets(str2, sizeof (str2), a, 0, false);
  200. ASSERT_EQ(str2[0], '1');
  201. msets(a, 0, str0, false);
  202. mgets(str2, sizeof (str2), a, 0, true);
  203. ASSERT_EQ(str2{0}, '1');
  204. msets(a, 0, str0, true);
  205. mgets(str2, sizeof (str2), a, 0, false);
  206. ASSERT_EQ(str2[0], '1');
  207. msets(a, 0, str0, true);
  208. mgets(str2, sizeof (str2), a, 0, true);
  209. ASSERT_EQ(str2{0}, '1');
  210. }
  211. }
  212. /*
  213. TEST__ y_malloc_()
  214. {
  215. MALLOC_TEST(10)
  216. {
  217. }
  218. }
  219. TEST__ y_malloc_()
  220. {
  221. MALLOC_TEST(10)
  222. {
  223. }
  224. }
  225. TEST__ y_malloc_()
  226. {
  227. MALLOC_TEST(10)
  228. {
  229. }
  230. }
  231. TEST__ y_malloc_()
  232. {
  233. MALLOC_TEST(10)
  234. {
  235. }
  236. }
  237. */
  238. TEST__ y_malloc_NewS_0()
  239. {
  240. new
  241. Alloc:a = Malloc_NewS("Hello There", false);
  242. ASSERT(bool:a);
  243. if (a)
  244. {
  245. ASSERT_EQ(mget(a, 0), 'H');
  246. ASSERT_EQ(mget(a, 5), ' ');
  247. ASSERT_EQ(mget(a, 11), '\0');
  248. free(a);
  249. }
  250. }
  251. TEST__ y_malloc_NewS_1()
  252. {
  253. new
  254. Alloc:a = Malloc_NewS("Hello There", true);
  255. ASSERT(bool:a);
  256. if (a)
  257. {
  258. static const
  259. b[12 char] = !"Hello There";
  260. ASSERT_EQ(mget(a, 0), b[0]);
  261. ASSERT_EQ(mget(a, 1), b[1]);
  262. ASSERT_EQ(mget(a, 2), b[2]);
  263. free(a);
  264. }
  265. }