Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnsafeBitArrayTests.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. using NUnit.Framework;
  2. using System;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Jobs;
  7. using Unity.Mathematics;
  8. using Assert = FastAssert;
  9. internal class UnsafeBitArrayTests
  10. {
  11. [Test]
  12. public void UnsafeBitArray_Get_Set_Long()
  13. {
  14. var numBits = 256;
  15. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  16. Assert.False(test.IsSet(123));
  17. test.Set(123, true);
  18. Assert.True(test.IsSet(123));
  19. Assert.False(test.TestAll(0, numBits));
  20. Assert.False(test.TestNone(0, numBits));
  21. Assert.True(test.TestAny(0, numBits));
  22. Assert.AreEqual(1, test.CountBits(0, numBits));
  23. Assert.False(test.TestAll(0, 122));
  24. Assert.True(test.TestNone(0, 122));
  25. Assert.False(test.TestAny(0, 122));
  26. test.Clear();
  27. Assert.False(test.IsSet(123));
  28. Assert.AreEqual(0, test.CountBits(0, numBits));
  29. test.SetBits(40, true, 4);
  30. Assert.AreEqual(4, test.CountBits(0, numBits));
  31. test.SetBits(0, true, numBits);
  32. Assert.False(test.TestNone(0, numBits));
  33. Assert.True(test.TestAll(0, numBits));
  34. test.SetBits(0, false, numBits);
  35. Assert.True(test.TestNone(0, numBits));
  36. Assert.False(test.TestAll(0, numBits));
  37. test.SetBits(123, true, 7);
  38. Assert.True(test.TestAll(123, 7));
  39. test.Clear();
  40. test.SetBits(64, true, 64);
  41. Assert.AreEqual(false, test.IsSet(63));
  42. Assert.AreEqual(true, test.TestAll(64, 64));
  43. Assert.AreEqual(false, test.IsSet(128));
  44. Assert.AreEqual(64, test.CountBits(64, 64));
  45. Assert.AreEqual(64, test.CountBits(0, numBits));
  46. test.Clear();
  47. test.SetBits(65, true, 62);
  48. Assert.AreEqual(false, test.IsSet(64));
  49. Assert.AreEqual(true, test.TestAll(65, 62));
  50. Assert.AreEqual(false, test.IsSet(127));
  51. Assert.AreEqual(62, test.CountBits(64, 64));
  52. Assert.AreEqual(62, test.CountBits(0, numBits));
  53. test.Clear();
  54. test.SetBits(66, true, 64);
  55. Assert.AreEqual(false, test.IsSet(65));
  56. Assert.AreEqual(true, test.TestAll(66, 64));
  57. Assert.AreEqual(false, test.IsSet(130));
  58. Assert.AreEqual(64, test.CountBits(66, 64));
  59. Assert.AreEqual(64, test.CountBits(0, numBits));
  60. test.Dispose();
  61. }
  62. [Test]
  63. public void UnsafeBitArray_Get_Set_Short()
  64. {
  65. var numBits = 31;
  66. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  67. Assert.False(test.IsSet(13));
  68. test.Set(13, true);
  69. Assert.True(test.IsSet(13));
  70. Assert.False(test.TestAll(0, numBits));
  71. Assert.False(test.TestNone(0, numBits));
  72. Assert.True(test.TestAny(0, numBits));
  73. Assert.AreEqual(1, test.CountBits(0, numBits));
  74. Assert.False(test.TestAll(0, 12));
  75. Assert.True(test.TestNone(0, 12));
  76. Assert.False(test.TestAny(0, 12));
  77. test.Clear();
  78. Assert.False(test.IsSet(13));
  79. Assert.AreEqual(0, test.CountBits(0, numBits));
  80. test.SetBits(4, true, 4);
  81. Assert.AreEqual(4, test.CountBits(0, numBits));
  82. test.SetBits(0, true, numBits);
  83. Assert.False(test.TestNone(0, numBits));
  84. Assert.True(test.TestAll(0, numBits));
  85. test.SetBits(0, false, numBits);
  86. Assert.True(test.TestNone(0, numBits));
  87. Assert.False(test.TestAll(0, numBits));
  88. test.SetBits(13, true, 7);
  89. Assert.True(test.TestAll(13, 7));
  90. test.Clear();
  91. test.SetBits(4, true, 4);
  92. Assert.AreEqual(false, test.IsSet(3));
  93. Assert.AreEqual(true, test.TestAll(4, 4));
  94. Assert.AreEqual(false, test.IsSet(18));
  95. Assert.AreEqual(4, test.CountBits(4, 4));
  96. Assert.AreEqual(4, test.CountBits(0, numBits));
  97. test.Clear();
  98. test.SetBits(5, true, 2);
  99. Assert.AreEqual(false, test.IsSet(4));
  100. Assert.AreEqual(true, test.TestAll(5, 2));
  101. Assert.AreEqual(false, test.IsSet(17));
  102. Assert.AreEqual(2, test.CountBits(4, 4));
  103. Assert.AreEqual(2, test.CountBits(0, numBits));
  104. test.Clear();
  105. test.SetBits(6, true, 4);
  106. Assert.AreEqual(false, test.IsSet(5));
  107. Assert.AreEqual(true, test.TestAll(6, 4));
  108. Assert.AreEqual(false, test.IsSet(10));
  109. Assert.AreEqual(4, test.CountBits(6, 4));
  110. Assert.AreEqual(4, test.CountBits(0, numBits));
  111. test.Dispose();
  112. }
  113. [Test]
  114. public void UnsafeBitArray_Get_Set_Tiny()
  115. {
  116. var numBits = 7;
  117. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  118. Assert.False(test.IsSet(3));
  119. test.Set(3, true);
  120. Assert.True(test.IsSet(3));
  121. Assert.False(test.TestAll(0, numBits));
  122. Assert.False(test.TestNone(0, numBits));
  123. Assert.True(test.TestAny(0, numBits));
  124. Assert.AreEqual(1, test.CountBits(0, numBits));
  125. Assert.False(test.TestAll(0, 2));
  126. Assert.True(test.TestNone(0, 2));
  127. Assert.False(test.TestAny(0, 2));
  128. test.Clear();
  129. Assert.False(test.IsSet(3));
  130. Assert.AreEqual(0, test.CountBits(0, numBits));
  131. test.SetBits(3, true, 4);
  132. Assert.AreEqual(4, test.CountBits(0, numBits));
  133. test.SetBits(0, true, numBits);
  134. Assert.False(test.TestNone(0, numBits));
  135. Assert.True(test.TestAll(0, numBits));
  136. test.SetBits(0, false, numBits);
  137. Assert.True(test.TestNone(0, numBits));
  138. Assert.False(test.TestAll(0, numBits));
  139. test.Dispose();
  140. }
  141. [Test]
  142. public unsafe void UnsafeBitArray_Throws()
  143. {
  144. var numBits = 256;
  145. using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory))
  146. {
  147. Assert.DoesNotThrow(() => { test.TestAll(0, numBits); });
  148. Assert.DoesNotThrow(() => { test.TestAny(numBits - 1, numBits); });
  149. Assert.Throws<ArgumentException>(() => { test.IsSet(-1); });
  150. Assert.Throws<ArgumentException>(() => { test.IsSet(numBits); });
  151. Assert.Throws<ArgumentException>(() => { test.TestAny(0, 0); });
  152. Assert.Throws<ArgumentException>(() => { test.TestAny(numBits, 1); });
  153. Assert.Throws<ArgumentException>(() => { test.TestAny(numBits - 1, 0); });
  154. // GetBits numBits must be 1-64.
  155. Assert.Throws<ArgumentException>(() => { test.GetBits(0, 0); });
  156. Assert.Throws<ArgumentException>(() => { test.GetBits(0, 65); });
  157. Assert.DoesNotThrow(() => { test.GetBits(63, 2); });
  158. Assert.Throws<ArgumentException>(() => { new UnsafeBitArray(null, 7); /* check sizeInBytes must be multiple of 8-bytes. */ });
  159. }
  160. }
  161. static void GetBitsTest(ref UnsafeBitArray test, int pos, int numBits)
  162. {
  163. test.SetBits(pos, true, numBits);
  164. Assert.AreEqual(numBits, test.CountBits(0, test.Length));
  165. Assert.AreEqual(0xfffffffffffffffful >> (64 - numBits), test.GetBits(pos, numBits));
  166. test.Clear();
  167. }
  168. [Test]
  169. public void UnsafeBitArray_GetBits()
  170. {
  171. var numBits = 256;
  172. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  173. GetBitsTest(ref test, 0, 5);
  174. GetBitsTest(ref test, 1, 3);
  175. GetBitsTest(ref test, 0, 63);
  176. GetBitsTest(ref test, 0, 64);
  177. GetBitsTest(ref test, 1, 63);
  178. GetBitsTest(ref test, 1, 64);
  179. GetBitsTest(ref test, 62, 5);
  180. GetBitsTest(ref test, 127, 3);
  181. GetBitsTest(ref test, 250, 6);
  182. GetBitsTest(ref test, 254, 2);
  183. test.Dispose();
  184. }
  185. static void SetBitsTest(ref UnsafeBitArray test, int pos, ulong value, int numBits)
  186. {
  187. test.SetBits(pos, value, numBits);
  188. Assert.AreEqual(value, test.GetBits(pos, numBits));
  189. test.Clear();
  190. }
  191. [Test]
  192. public void UnsafeBitArray_SetBits()
  193. {
  194. var numBits = 256;
  195. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  196. SetBitsTest(ref test, 0, 16, 5);
  197. SetBitsTest(ref test, 1, 7, 3);
  198. SetBitsTest(ref test, 1, 32, 64);
  199. SetBitsTest(ref test, 62, 6, 5);
  200. SetBitsTest(ref test, 127, 1, 3);
  201. SetBitsTest(ref test, 60, 0xaa, 8);
  202. test.Dispose();
  203. }
  204. static void CopyBitsTest(ref UnsafeBitArray dstBitArray, int dstPos, ref UnsafeBitArray srcBitArray, int srcPos, int numBits)
  205. {
  206. for (int pos = 0; pos < dstBitArray.Length; pos += 64)
  207. {
  208. dstBitArray.SetBits(pos, 0xaaaaaaaaaaaaaaaaul, 64);
  209. }
  210. srcBitArray.SetBits(srcPos, true, numBits);
  211. dstBitArray.Copy(dstPos, ref srcBitArray, srcPos, numBits);
  212. Assert.AreEqual(true, dstBitArray.TestAll(dstPos, numBits));
  213. for (int pos = 0; pos < dstBitArray.Length; ++pos)
  214. {
  215. if ((pos >= dstPos && pos < dstPos + numBits) ||
  216. (pos >= srcPos && pos < srcPos + numBits))
  217. {
  218. Assert.AreEqual(true, dstBitArray.IsSet(pos));
  219. }
  220. else
  221. {
  222. Assert.AreEqual((0 != (pos & 1)), dstBitArray.IsSet(pos));
  223. }
  224. }
  225. dstBitArray.Clear();
  226. }
  227. static void CopyBitsTest(ref UnsafeBitArray test, int dstPos, int srcPos, int numBits)
  228. {
  229. CopyBitsTest(ref test, dstPos, ref test, srcPos, numBits);
  230. }
  231. [Test]
  232. public void UnsafeBitArray_Copy()
  233. {
  234. var numBits = 512;
  235. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  236. CopyBitsTest(ref test, 1, 16, 12); // short up to 64-bits copy
  237. CopyBitsTest(ref test, 1, 80, 63); // short up to 64-bits copy
  238. CopyBitsTest(ref test, 1, 11, 12); // short up to 64-bits copy overlapped
  239. CopyBitsTest(ref test, 11, 1, 12); // short up to 64-bits copy overlapped
  240. CopyBitsTest(ref test, 1, 16, 76); // short up to 128-bits copy
  241. CopyBitsTest(ref test, 1, 80, 127); // short up to 128-bits copy
  242. CopyBitsTest(ref test, 1, 11, 76); // short up to 128-bits copy overlapped
  243. CopyBitsTest(ref test, 11, 1, 76); // short up to 128-bits copy overlapped
  244. CopyBitsTest(ref test, 1, 81, 255); // long copy aligned
  245. CopyBitsTest(ref test, 8, 0, 255); // long copy overlapped aligned
  246. CopyBitsTest(ref test, 1, 80, 255); // long copy unaligned
  247. CopyBitsTest(ref test, 80, 1, 255); // long copy overlapped unaligned
  248. test.Dispose();
  249. }
  250. [Test]
  251. public void UnsafeBitArray_CopyBetweenBitArrays()
  252. {
  253. var numBits = 512;
  254. var test0 = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  255. var test1 = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  256. var test2 = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  257. for (int pos = 0; pos < test0.Length; pos += 64)
  258. {
  259. test0.SetBits(pos, 0xaaaaaaaaaaaaaaaaul, 64);
  260. test1.SetBits(pos, 0x5555555555555555ul, 64);
  261. }
  262. var numCopyBits = 255;
  263. test0.SetBits(13, true, numCopyBits);
  264. test1.Copy(1, ref test0, 13, numCopyBits);
  265. Assert.AreEqual(true, test1.TestAll(1, numCopyBits));
  266. test2.Copy(43, ref test1, 1, numCopyBits);
  267. Assert.AreEqual(true, test2.TestAll(43, numCopyBits));
  268. test0.Dispose();
  269. test1.Dispose();
  270. test2.Dispose();
  271. }
  272. [Test]
  273. public unsafe void UnsafeBitArray_Copy_Throws()
  274. {
  275. var numBits = 512;
  276. var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  277. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, 0, numBits - 1, 16); }); // short up to 64-bits copy out of bounds
  278. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, numBits - 1, 0, 16); }); // short up to 64-bits copy out of bounds
  279. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, 0, numBits - 1, 80); }); // short up to 128-bits copy out of bounds
  280. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, numBits - 1, 0, 80); }); // short up to 128-bits copy out of bounds
  281. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, 1, numBits - 7, 127); }); // long copy aligned
  282. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, numBits - 7, 1, 127); }); // long copy aligned
  283. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, 2, numBits - 1, 127); }); // long copy unaligned
  284. Assert.Throws<ArgumentException>(() => { CopyBitsTest(ref test, numBits - 1, 2, 127); }); // long copy unaligned
  285. test.Dispose();
  286. }
  287. [Test]
  288. public unsafe void UnsafeBitArray_Find()
  289. {
  290. var numBits = 512;
  291. using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory))
  292. {
  293. test.SetBits(0, true, 11);
  294. for (var i = 0; i < 256; ++i)
  295. {
  296. Assert.AreEqual(11, test.Find(0, i + 1));
  297. }
  298. for (var j = 0; j < 64; ++j)
  299. {
  300. for (var i = 0; i < 256; ++i)
  301. {
  302. var numBitsToFind = 7 + i;
  303. var pos = 37 + j;
  304. test.SetBits(0, true, test.Length);
  305. test.SetBits(pos, false, numBitsToFind);
  306. Assert.AreEqual(pos, test.Find(0, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}");
  307. Assert.AreEqual(pos, test.Find(pos, numBitsToFind), $"{j}/{i}:pos {pos}, numBitsToFind {numBitsToFind}");
  308. Assert.AreEqual(pos, test.Find(0, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}");
  309. Assert.AreEqual(pos, test.Find(pos, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}");
  310. Assert.IsTrue(test.TestNone(test.Find(0, numBitsToFind), numBitsToFind));
  311. Assert.AreEqual(int.MaxValue, test.Find(pos + 1, numBitsToFind), $"{j}/{i}: pos {pos}, numBitsToFind {numBitsToFind}");
  312. }
  313. }
  314. }
  315. }
  316. [Test]
  317. public unsafe void UnsafeBitArray_Find_With_Begin_End()
  318. {
  319. var numBits = 512;
  320. using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory))
  321. {
  322. Assert.AreEqual(0, test.Find(0, 2, 1));
  323. Assert.AreEqual(1, test.Find(1, 2, 1));
  324. test.SetBits(0, true, 6);
  325. Assert.AreEqual(int.MaxValue, test.Find(0, 2, 1));
  326. for (var j = 0; j < 64; ++j)
  327. {
  328. for (var i = 0; i < 256; ++i)
  329. {
  330. var numBitsToFind = 7 + i;
  331. var padding = 11;
  332. var begin = 37 + j;
  333. var end = begin + padding + numBitsToFind;
  334. var count = end - begin;
  335. test.Clear();
  336. test.SetBits(begin, true, count);
  337. test.SetBits(begin + padding + 1, false, numBitsToFind - 1);
  338. Assert.AreEqual(begin + padding + 1, test.Find(begin, count, numBitsToFind - 1)); //, $"{j}/{i}: begin {begin}, end {end}, count {count}, numBitsToFind {numBitsToFind}");
  339. Assert.AreEqual(int.MaxValue, test.Find(begin, count, numBitsToFind)); //, $"{j}/{i}: begin {begin}, end {end}, count {count}, numBitsToFind {numBitsToFind}");
  340. }
  341. }
  342. }
  343. }
  344. [Test]
  345. public unsafe void UnsafeBitArray_Find_Throws()
  346. {
  347. var numBits = 512;
  348. using (var test = new UnsafeBitArray(numBits, Allocator.Persistent, NativeArrayOptions.ClearMemory))
  349. {
  350. Assert.Throws<ArgumentException>(() => { test.Find(0, 0, 1); }); // empty range
  351. Assert.Throws<ArgumentException>(() => { test.Find(0, 1, 0); }); // zero bits
  352. Assert.Throws<ArgumentException>(() => { test.Find(0, 1, 2); }); // numBits is larger than range
  353. Assert.Throws<ArgumentException>(() => { test.Find(10, 0, 0); }); // empty range, numBits is less than 1
  354. Assert.Throws<ArgumentException>(() => { test.Find(1, 10, -2); }); // numBits can't be negative
  355. }
  356. }
  357. void findWithPattern(ref UnsafeBitArray test, byte pattern, int numBits)
  358. {
  359. for (int pos = 0; pos < test.Length; pos += 8)
  360. {
  361. test.SetBits(pos, pattern, 8);
  362. }
  363. var bitCount = math.countbits((int)pattern);
  364. var numEmptyBits = test.Length - (test.Length / 8 * bitCount);
  365. for (int i = 0; i < numEmptyBits; i += numBits)
  366. {
  367. var pos = test.Find(0, numBits);
  368. Assert.AreNotEqual(int.MaxValue, pos, $"{i}");
  369. test.SetBits(pos, true, numBits);
  370. }
  371. Assert.True(test.TestAll(0, test.Length));
  372. }
  373. [Test]
  374. public void UnsafeBitArray_FindWithPattern()
  375. {
  376. var test = new UnsafeBitArray(512, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  377. // Separated test for some more interesting patterns
  378. findWithPattern(ref test, 0x81, 1);
  379. findWithPattern(ref test, 0x81, 2);
  380. findWithPattern(ref test, 0x81, 3);
  381. findWithPattern(ref test, 0x81, 6);
  382. findWithPattern(ref test, 0x88, 3);
  383. findWithPattern(ref test, 0x99, 2);
  384. findWithPattern(ref test, 0xaa, 1);
  385. findWithPattern(ref test, 0xc3, 1);
  386. findWithPattern(ref test, 0xc3, 2);
  387. findWithPattern(ref test, 0xc3, 4);
  388. findWithPattern(ref test, 0xe7, 1);
  389. findWithPattern(ref test, 0xe7, 2);
  390. // Test all patterns
  391. for (int i = 0; i < 256; i++)
  392. {
  393. findWithPattern(ref test, (byte)i, 1);
  394. }
  395. test.Dispose();
  396. }
  397. [Test]
  398. public void UnsafeBitArray_FindInTinyBitArray()
  399. {
  400. var test = new UnsafeBitArray(3, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  401. Assert.AreEqual(3, test.Length);
  402. test.SetBits(0, 0x55, test.Length);
  403. Assert.AreEqual(1, test.Find(0, 1));
  404. Assert.AreEqual(1, test.Find(0, test.Length, 1));
  405. test.SetBits(1, true, 1);
  406. Assert.True(test.TestAll(0, test.Length));
  407. Assert.AreEqual(int.MaxValue, test.Find(0, test.Length, 1));
  408. test.Dispose();
  409. }
  410. [Test]
  411. public void UnsafeBitArray_FindLastUnsetBit([NUnit.Framework.Range(1, 64)] int numBits)
  412. {
  413. using (var bits = new UnsafeBitArray(numBits, Allocator.Persistent))
  414. {
  415. // Set all bits to one then unset a single bit to find.
  416. for (int i = 0; i < numBits; ++i)
  417. {
  418. bits.SetBits(0, true, numBits);
  419. bits.Set(i, false);
  420. Assert.AreEqual(i, bits.Find(0, 1));
  421. }
  422. }
  423. }
  424. [Test]
  425. public void UnsafeBitArray_CustomAllocatorTest()
  426. {
  427. AllocatorManager.Initialize();
  428. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  429. ref var allocator = ref allocatorHelper.Allocator;
  430. allocator.Initialize();
  431. using (var container = new UnsafeBitArray(1, allocator.Handle))
  432. {
  433. }
  434. Assert.IsTrue(allocator.WasUsed);
  435. allocator.Dispose();
  436. allocatorHelper.Dispose();
  437. AllocatorManager.Shutdown();
  438. }
  439. [BurstCompile]
  440. struct BurstedCustomAllocatorJob : IJob
  441. {
  442. [NativeDisableUnsafePtrRestriction]
  443. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  444. public void Execute()
  445. {
  446. unsafe
  447. {
  448. using (var container = new UnsafeBitArray(1, Allocator->Handle))
  449. {
  450. }
  451. }
  452. }
  453. }
  454. [Test]
  455. public unsafe void UnsafeBitArray_BurstedCustomAllocatorTest()
  456. {
  457. AllocatorManager.Initialize();
  458. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  459. ref var allocator = ref allocatorHelper.Allocator;
  460. allocator.Initialize();
  461. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
  462. unsafe
  463. {
  464. var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
  465. handle.Complete();
  466. }
  467. Assert.IsTrue(allocator.WasUsed);
  468. allocator.Dispose();
  469. allocatorHelper.Dispose();
  470. AllocatorManager.Shutdown();
  471. }
  472. }