No Description
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.

UnsafeListTests.cs 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. using NUnit.Framework;
  2. using System;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.Tests;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using Unity.Jobs;
  8. internal class UnsafeListTests : CollectionsTestCommonBase
  9. {
  10. [Test]
  11. public void UnsafeListT_Init()
  12. {
  13. var container = new UnsafeList<int>(0, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  14. Assert.True(container.IsCreated);
  15. Assert.True(container.IsEmpty);
  16. Assert.DoesNotThrow(() => container.Dispose());
  17. }
  18. [Test]
  19. public unsafe void UnsafeListT_Init_ClearMemory()
  20. {
  21. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  22. for (var i = 0; i < list.Length; ++i)
  23. {
  24. Assert.AreEqual(0, UnsafeUtility.ReadArrayElement<int>(list.Ptr, i));
  25. }
  26. list.Dispose();
  27. }
  28. [Test]
  29. public unsafe void UnsafeListT_Allocate_Deallocate_Read_Write()
  30. {
  31. var list = new UnsafeList<int>(0, Allocator.Persistent);
  32. Assert.True(list.IsCreated);
  33. Assert.True(list.IsEmpty);
  34. list.Add(1);
  35. list.Add(2);
  36. Assert.AreEqual(2, list.Length);
  37. Assert.AreEqual(1, UnsafeUtility.ReadArrayElement<int>(list.Ptr, 0));
  38. Assert.AreEqual(2, UnsafeUtility.ReadArrayElement<int>(list.Ptr, 1));
  39. list.Dispose();
  40. }
  41. [Test]
  42. public unsafe void UnsafeListT_Resize_ClearMemory()
  43. {
  44. var list = new UnsafeList<int>(5, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  45. list.SetCapacity(32);
  46. var capacity = list.Capacity;
  47. list.Resize(5, NativeArrayOptions.UninitializedMemory);
  48. Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize
  49. for (var i = 0; i < 5; ++i)
  50. {
  51. UnsafeUtility.WriteArrayElement(list.Ptr, i, i);
  52. }
  53. list.Resize(10, NativeArrayOptions.ClearMemory);
  54. Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize
  55. for (var i = 0; i < 5; ++i)
  56. {
  57. Assert.AreEqual(i, UnsafeUtility.ReadArrayElement<int>(list.Ptr, i));
  58. }
  59. for (var i = 5; i < list.Length; ++i)
  60. {
  61. Assert.AreEqual(0, UnsafeUtility.ReadArrayElement<int>(list.Ptr, i));
  62. }
  63. list.Dispose();
  64. }
  65. [Test]
  66. public unsafe void UnsafeListT_Resize_Zero()
  67. {
  68. var list = new UnsafeList<int>(5, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  69. var capacity = list.Capacity;
  70. list.Add(1);
  71. list.Resize(0);
  72. Assert.AreEqual(0, list.Length);
  73. Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize
  74. list.Add(2);
  75. list.Clear();
  76. Assert.AreEqual(0, list.Length);
  77. Assert.AreEqual(capacity, list.Capacity); // list capacity should not change on resize
  78. list.Dispose();
  79. }
  80. [Test]
  81. public unsafe void UnsafeListT_SetCapacity()
  82. {
  83. using (var list = new UnsafeList<int>(1, Allocator.Persistent, NativeArrayOptions.ClearMemory))
  84. {
  85. list.Add(1);
  86. Assert.DoesNotThrow(() => list.SetCapacity(128));
  87. list.Add(1);
  88. Assert.AreEqual(2, list.Length);
  89. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  90. Assert.Throws<ArgumentOutOfRangeException>(() => list.SetCapacity(1));
  91. #endif
  92. list.RemoveAtSwapBack(0);
  93. Assert.AreEqual(1, list.Length);
  94. Assert.DoesNotThrow(() => list.SetCapacity(1));
  95. list.TrimExcess();
  96. Assert.AreEqual(1, list.Capacity);
  97. }
  98. }
  99. [Test]
  100. public unsafe void UnsafeListT_TrimExcess()
  101. {
  102. using (var list = new UnsafeList<int>(32, Allocator.Persistent, NativeArrayOptions.ClearMemory))
  103. {
  104. list.Add(1);
  105. list.TrimExcess();
  106. Assert.AreEqual(1, list.Length);
  107. Assert.AreEqual(1, list.Capacity);
  108. list.RemoveAtSwapBack(0);
  109. Assert.AreEqual(list.Length, 0);
  110. list.TrimExcess();
  111. Assert.AreEqual(list.Capacity, 0);
  112. list.Add(1);
  113. Assert.AreEqual(list.Length, 1);
  114. Assert.AreNotEqual(list.Capacity, 0);
  115. list.Clear();
  116. }
  117. }
  118. [Test]
  119. public unsafe void UnsafeListT_DisposeJob()
  120. {
  121. var list = new UnsafeList<int>(5, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  122. var disposeJob = list.Dispose(default);
  123. Assert.IsTrue(list.Ptr == null);
  124. disposeJob.Complete();
  125. }
  126. unsafe void Expected(ref UnsafeList<int> list, int expectedLength, int[] expected)
  127. {
  128. Assert.AreEqual(0 == expectedLength, list.IsEmpty);
  129. Assert.AreEqual(list.Length, expectedLength);
  130. for (var i = 0; i < list.Length; ++i)
  131. {
  132. var value = UnsafeUtility.ReadArrayElement<int>(list.Ptr, i);
  133. Assert.AreEqual(expected[i], value);
  134. }
  135. }
  136. [Test]
  137. public void UnsafeListT_AddReplicate()
  138. {
  139. using (var list = new UnsafeList<int>(32, Allocator.Persistent))
  140. {
  141. list.AddReplicate(value: 42, count: 10);
  142. Assert.AreEqual(10, list.Length);
  143. foreach (var item in list)
  144. Assert.AreEqual(42, item);
  145. list.AddReplicate(value: 42, count: 100);
  146. Assert.AreEqual(110, list.Length);
  147. foreach (var item in list)
  148. Assert.AreEqual(42, item);
  149. }
  150. }
  151. [Test]
  152. public unsafe void UnsafeListT_AddNoResize()
  153. {
  154. var container = new UnsafeList<int>(1, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  155. // List's capacity is always cache-line aligned, number of items fills up whole cache-line.
  156. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 };
  157. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  158. Assert.Throws<InvalidOperationException>(() => { fixed (int* r = range) container.AddRangeNoResize(r, 17); });
  159. #endif
  160. container.SetCapacity(17);
  161. Assert.DoesNotThrow(() => { fixed (int* r = range) container.AddRangeNoResize(r, 17); });
  162. container.Length = 16;
  163. container.TrimExcess();
  164. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  165. Assert.Throws<InvalidOperationException>(() => { container.AddNoResize(16); });
  166. #endif
  167. container.Dispose();
  168. }
  169. [Test]
  170. public unsafe void UnsafeListT_AddNoResize_Read()
  171. {
  172. var container = new UnsafeList<int>(4, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  173. container.AddNoResize(4);
  174. container.AddNoResize(6);
  175. container.AddNoResize(4);
  176. container.AddNoResize(9);
  177. Expected(ref container, 4, new int[] { 4, 6, 4, 9 });
  178. container.Dispose();
  179. }
  180. [Test]
  181. public unsafe void UnsafeListT_RemoveAtSwapBack()
  182. {
  183. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  184. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  185. // test removing from the end
  186. fixed (int* r = range) list.AddRange(r, 10);
  187. list.RemoveAtSwapBack(list.Length - 1);
  188. Expected(ref list, 9, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
  189. list.Clear();
  190. // test removing from the end
  191. fixed (int* r = range) list.AddRange(r, 10);
  192. list.RemoveAtSwapBack(5);
  193. Expected(ref list, 9, new int[] { 0, 1, 2, 3, 4, 9, 6, 7, 8 });
  194. list.Clear();
  195. list.Dispose();
  196. }
  197. [Test]
  198. public unsafe void UnsafeListT_RemoveRangeSwapBackBE()
  199. {
  200. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  201. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  202. // test removing from the end
  203. fixed (int* r = range) list.AddRange(r, 10);
  204. list.RemoveRangeSwapBack(6, 3);
  205. Expected(ref list, 7, new int[] { 0, 1, 2, 3, 4, 5, 9 });
  206. list.Clear();
  207. // test removing all but one
  208. fixed (int* r = range) list.AddRange(r, 10);
  209. list.RemoveRangeSwapBack(0, 9);
  210. Expected(ref list, 1, new int[] { 9 });
  211. list.Clear();
  212. // test removing from the front
  213. fixed (int* r = range) list.AddRange(r, 10);
  214. list.RemoveRangeSwapBack(0, 3);
  215. Expected(ref list, 7, new int[] { 7, 8, 9, 3, 4, 5, 6 });
  216. list.Clear();
  217. // test removing from the middle
  218. fixed (int* r = range) list.AddRange(r, 10);
  219. list.RemoveRangeSwapBack(0, 3);
  220. Expected(ref list, 7, new int[] { 7, 8, 9, 3, 4, 5, 6 });
  221. list.Clear();
  222. // test removing whole range
  223. fixed (int* r = range) list.AddRange(r, 10);
  224. list.RemoveRangeSwapBack(0, 10);
  225. Expected(ref list, 0, new int[] { 0 });
  226. list.Clear();
  227. list.Dispose();
  228. }
  229. [Test]
  230. public unsafe void UnsafeListT_RemoveAt()
  231. {
  232. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  233. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  234. // test removing from the end
  235. fixed (int* r = range) list.AddRange(r, 10);
  236. list.RemoveAt(list.Length - 1);
  237. Expected(ref list, 9, new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 });
  238. list.Clear();
  239. // test removing from the end
  240. fixed (int* r = range) list.AddRange(r, 10);
  241. list.RemoveAt(5);
  242. Expected(ref list, 9, new int[] { 0, 1, 2, 3, 4, 6, 7, 8, 9 });
  243. list.Clear();
  244. list.Dispose();
  245. }
  246. [Test]
  247. public unsafe void UnsafeListT_RemoveRange()
  248. {
  249. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  250. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  251. // test removing from the end
  252. fixed (int* r = range) list.AddRange(r, 10);
  253. list.RemoveRange(6, 3);
  254. Expected(ref list, 7, new int[] { 0, 1, 2, 3, 4, 5, 9 });
  255. list.Clear();
  256. // test removing all but one
  257. fixed (int* r = range) list.AddRange(r, 10);
  258. list.RemoveRange(0, 9);
  259. Expected(ref list, 1, new int[] { 9 });
  260. list.Clear();
  261. // test removing from the front
  262. fixed (int* r = range) list.AddRange(r, 10);
  263. list.RemoveRange(0, 3);
  264. Expected(ref list, 7, new int[] { 3, 4, 5, 6, 7, 8, 9 });
  265. list.Clear();
  266. // test removing from the middle
  267. fixed (int* r = range) list.AddRange(r, 10);
  268. list.RemoveRange(0, 3);
  269. Expected(ref list, 7, new int[] { 3, 4, 5, 6, 7, 8, 9 });
  270. list.Clear();
  271. // test removing whole range
  272. fixed (int* r = range) list.AddRange(r, 10);
  273. list.RemoveRange(0, 10);
  274. Expected(ref list, 0, new int[] { 0 });
  275. list.Clear();
  276. // Test removing at the end of the list with a zero length.
  277. // This simply must not throw.
  278. list.RemoveRange(list.Length, 0);
  279. list.Dispose();
  280. }
  281. [Test]
  282. [TestRequiresDotsDebugOrCollectionChecks]
  283. public unsafe void UnsafeListT_Remove_Throws()
  284. {
  285. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  286. Assert.Throws<IndexOutOfRangeException>(() => { list.RemoveAt(0); });
  287. Assert.AreEqual(0, list.Length);
  288. Assert.Throws<IndexOutOfRangeException>(() => { list.RemoveAtSwapBack(0); });
  289. Assert.AreEqual(0, list.Length);
  290. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  291. fixed (int* r = range) list.AddRange(r, 10);
  292. Assert.Throws<IndexOutOfRangeException>(() => { list.RemoveAt(100); });
  293. Assert.AreEqual(10, list.Length);
  294. Assert.Throws<IndexOutOfRangeException>(() => { list.RemoveAtSwapBack(100); });
  295. Assert.AreEqual(10, list.Length);
  296. Assert.Throws<ArgumentOutOfRangeException>(() => { list.RemoveRange(0, 100); });
  297. Assert.AreEqual(10, list.Length);
  298. Assert.Throws<ArgumentOutOfRangeException>(() => { list.RemoveRangeSwapBack(0, 100); });
  299. Assert.AreEqual(10, list.Length);
  300. Assert.Throws<ArgumentOutOfRangeException>(() => { list.RemoveRange(100, -1); });
  301. Assert.AreEqual(10, list.Length);
  302. Assert.Throws<ArgumentOutOfRangeException>(() => { list.RemoveRangeSwapBack(100, -1); });
  303. Assert.AreEqual(10, list.Length);
  304. list.Dispose();
  305. }
  306. [Test]
  307. public unsafe void UnsafeListT_PtrLength()
  308. {
  309. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  310. int[] range = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
  311. fixed (int* r = range) list.AddRange(r, 10);
  312. var listView = new UnsafeList<int>(list.Ptr + 4, 2);
  313. Expected(ref listView, 2, new int[] { 4, 5 });
  314. listView.Dispose();
  315. list.Dispose();
  316. }
  317. // Burst error BC1071: Unsupported assert type
  318. // [BurstCompile(CompileSynchronously = true)]
  319. struct UnsafeListAsReadOnly : IJob
  320. {
  321. public UnsafeList<int>.ReadOnly list;
  322. public void Execute()
  323. {
  324. Assert.True(list.Contains(123));
  325. }
  326. }
  327. [Test]
  328. public void UnsafeListT_AsReadOnly()
  329. {
  330. var list = new UnsafeList<int>(10, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  331. list.Add(123);
  332. var job = new UnsafeListAsReadOnly
  333. {
  334. list = list.AsReadOnly(),
  335. };
  336. list.Dispose(job.Schedule()).Complete();
  337. }
  338. [BurstCompile(CompileSynchronously = true)]
  339. struct UnsafeListParallelWriter : IJobParallelFor
  340. {
  341. public UnsafeList<int>.ParallelWriter list;
  342. public void Execute(int index)
  343. {
  344. list.AddNoResize(index);
  345. }
  346. }
  347. [Test]
  348. public void UnsafeListT_ParallelWriter()
  349. {
  350. var list = new UnsafeList<int>(256, Allocator.Persistent, NativeArrayOptions.ClearMemory);
  351. var job = new UnsafeListParallelWriter
  352. {
  353. list = list.AsParallelWriter(),
  354. };
  355. job.Schedule(list.Capacity, 1).Complete();
  356. Assert.AreEqual(list.Length, list.Capacity);
  357. list.Sort<int>();
  358. for (int i = 0; i < list.Length; i++)
  359. {
  360. unsafe
  361. {
  362. var value = UnsafeUtility.ReadArrayElement<int>(list.Ptr, i);
  363. Assert.AreEqual(i, value);
  364. }
  365. }
  366. list.Dispose();
  367. }
  368. [BurstCompile(CompileSynchronously = true)]
  369. struct UnsafeListTestParallelWriter : IJob
  370. {
  371. [WriteOnly]
  372. public UnsafeList<int>.ParallelWriter writer;
  373. public unsafe void Execute()
  374. {
  375. var range = stackalloc int[2] { 7, 3 };
  376. writer.AddNoResize(range[0]);
  377. writer.AddRangeNoResize(range, 1);
  378. }
  379. }
  380. [Test]
  381. public void UnsafeListT_ParallelWriter_NoPtrCaching()
  382. {
  383. UnsafeList<int> list;
  384. {
  385. list = new UnsafeList<int>(2, Allocator.Persistent);
  386. var writer = list.AsParallelWriter();
  387. list.SetCapacity(100);
  388. var writerJob = new UnsafeListTestParallelWriter { writer = writer }.Schedule();
  389. writerJob.Complete();
  390. }
  391. Assert.AreEqual(2, list.Length);
  392. Assert.AreEqual(7, list[0]);
  393. Assert.AreEqual(7, list[1]);
  394. list.Dispose();
  395. }
  396. [Test]
  397. public unsafe void UnsafeListT_IndexOf()
  398. {
  399. using (var list = new UnsafeList<int>(10, Allocator.Persistent) { 123, 789 })
  400. {
  401. bool r0 = false, r1 = false, r2 = false;
  402. GCAllocRecorder.ValidateNoGCAllocs(() =>
  403. {
  404. r0 = -1 != list.IndexOf(456);
  405. r1 = list.Contains(123);
  406. r2 = list.Contains(789);
  407. });
  408. Assert.False(r0);
  409. Assert.True(r1);
  410. Assert.True(r2);
  411. }
  412. }
  413. [Test]
  414. public void UnsafeListT_InsertRangeWithBeginEnd()
  415. {
  416. var list = new UnsafeList<byte>(3, Allocator.Persistent);
  417. list.Add(0);
  418. list.Add(3);
  419. list.Add(4);
  420. Assert.AreEqual(3, list.Length);
  421. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  422. Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRangeWithBeginEnd(-1, 8));
  423. Assert.Throws<ArgumentException>(() => list.InsertRangeWithBeginEnd(3, 1));
  424. #endif
  425. Assert.DoesNotThrow(() => list.InsertRangeWithBeginEnd(1, 3));
  426. Assert.AreEqual(5, list.Length);
  427. list[1] = 1;
  428. list[2] = 2;
  429. for (var i = 0; i < list.Length; ++i)
  430. {
  431. Assert.AreEqual(i, list[i]);
  432. }
  433. Assert.DoesNotThrow(() => list.InsertRangeWithBeginEnd(5, 8));
  434. Assert.AreEqual(8, list.Length);
  435. list[5] = 5;
  436. list[6] = 6;
  437. list[7] = 7;
  438. for (var i = 0; i < list.Length; ++i)
  439. {
  440. Assert.AreEqual(i, list[i]);
  441. }
  442. list.Dispose();
  443. }
  444. [Test]
  445. public void UnsafeListT_InsertRange()
  446. {
  447. var list = new UnsafeList<byte>(3, Allocator.Persistent);
  448. list.Add(0);
  449. list.Add(3);
  450. list.Add(4);
  451. Assert.AreEqual(3, list.Length);
  452. #if ENABLE_UNITY_COLLECTIONS_CHECKS || UNITY_DOTS_DEBUG
  453. Assert.Throws<ArgumentOutOfRangeException>(() => list.InsertRange(-1, 8));
  454. Assert.Throws<ArgumentException>(() => list.InsertRange(3, -1));
  455. #endif
  456. Assert.DoesNotThrow(() => list.InsertRange(1, 0));
  457. Assert.AreEqual(3, list.Length);
  458. Assert.DoesNotThrow(() => list.InsertRange(1, 2));
  459. Assert.AreEqual(5, list.Length);
  460. list[1] = 1;
  461. list[2] = 2;
  462. for (var i = 0; i < list.Length; ++i)
  463. {
  464. Assert.AreEqual(i, list[i]);
  465. }
  466. Assert.DoesNotThrow(() => list.InsertRange(5, 3));
  467. Assert.AreEqual(8, list.Length);
  468. list[5] = 5;
  469. list[6] = 6;
  470. list[7] = 7;
  471. for (var i = 0; i < list.Length; ++i)
  472. {
  473. Assert.AreEqual(i, list[i]);
  474. }
  475. list.Dispose();
  476. }
  477. [Test]
  478. public void UnsafeListT_ForEach([Values(10, 1000)] int n)
  479. {
  480. var seen = new NativeArray<int>(n, Allocator.Temp);
  481. using (var container = new UnsafeList<int>(32, CommonRwdAllocator.Handle))
  482. {
  483. for (int i = 0; i < n; i++)
  484. {
  485. container.Add(i);
  486. }
  487. var count = 0;
  488. unsafe
  489. {
  490. UnsafeList<int>* test = &container;
  491. foreach (var item in *test)
  492. {
  493. Assert.True(test->Contains(item));
  494. seen[item] = seen[item] + 1;
  495. ++count;
  496. }
  497. }
  498. Assert.AreEqual(container.Length, count);
  499. for (int i = 0; i < n; i++)
  500. {
  501. Assert.AreEqual(1, seen[i], $"Incorrect item count {i}");
  502. }
  503. }
  504. }
  505. [Test]
  506. public void UnsafeListT_CustomAllocatorTest()
  507. {
  508. AllocatorManager.Initialize();
  509. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  510. ref var allocator = ref allocatorHelper.Allocator;
  511. allocator.Initialize();
  512. using (var container = new UnsafeList<byte>(1, allocator.Handle))
  513. {
  514. }
  515. Assert.IsTrue(allocator.WasUsed);
  516. allocator.Dispose();
  517. allocatorHelper.Dispose();
  518. AllocatorManager.Shutdown();
  519. }
  520. [BurstCompile]
  521. struct BurstedCustomAllocatorJob : IJob
  522. {
  523. [NativeDisableUnsafePtrRestriction]
  524. public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
  525. public void Execute()
  526. {
  527. unsafe
  528. {
  529. using (var container = new UnsafeList<byte>(1, Allocator->Handle))
  530. {
  531. }
  532. }
  533. }
  534. }
  535. [Test]
  536. public unsafe void UnsafeListT_BurstedCustomAllocatorTest()
  537. {
  538. AllocatorManager.Initialize();
  539. var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
  540. ref var allocator = ref allocatorHelper.Allocator;
  541. allocator.Initialize();
  542. var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf(ref allocator);
  543. unsafe
  544. {
  545. var handle = new BurstedCustomAllocatorJob { Allocator = allocatorPtr }.Schedule();
  546. handle.Complete();
  547. }
  548. Assert.IsTrue(allocator.WasUsed);
  549. allocator.Dispose();
  550. allocatorHelper.Dispose();
  551. AllocatorManager.Shutdown();
  552. }
  553. void IIndexableTest<T>(T container)
  554. where T : unmanaged, IIndexable<int>
  555. {
  556. var length = container.Length;
  557. Assert.Throws<IndexOutOfRangeException>(() => container.ElementAt(-1));
  558. Assert.Throws<IndexOutOfRangeException>(() => container.ElementAt(container.Length));
  559. Assert.DoesNotThrow(() => { for (int i = 0, len = container.Length; i < len; ++i) { container.ElementAt(i) = 4; } });
  560. for (int i = 0, len = container.Length; i < len; ++i)
  561. {
  562. Assert.AreEqual(4, container.ElementAt(i));
  563. }
  564. }
  565. void INativeListTest<T>(T container)
  566. where T : unmanaged, INativeList<int>
  567. {
  568. var length = container.Length;
  569. Assert.Throws<IndexOutOfRangeException>(() => container[-1] = 1);
  570. Assert.Throws<IndexOutOfRangeException>(() => container[container.Length] = 1);
  571. Assert.DoesNotThrow(() => { for (int i = 0, len = container.Length; i < len; ++i) { container[i] = 4; } });
  572. Assert.Throws<ArgumentOutOfRangeException>(() => container.Capacity = container.Length - 1);
  573. Assert.DoesNotThrow(() => container.Capacity = container.Length);
  574. Assert.DoesNotThrow(() => container.Capacity = container.Length + 1);
  575. for (int i = 0, len = container.Length; i < len; ++i)
  576. {
  577. Assert.AreEqual(4, container[i]);
  578. }
  579. }
  580. private unsafe void TestInterfaces<T>(T container)
  581. where T : unmanaged, IIndexable<int>, INativeList<int>
  582. {
  583. container.Length = 4;
  584. Assert.DoesNotThrow(() => { for (int i = 0, len = container.Length; i < len; ++i) { container.ElementAt(i) = i; } });
  585. IIndexableTest(container);
  586. INativeListTest(container);
  587. }
  588. private unsafe void TestInterfacesDispose<T>(T container)
  589. where T : unmanaged, IIndexable<int>, INativeList<int>, IDisposable
  590. {
  591. TestInterfaces(container);
  592. container.Dispose();
  593. }
  594. [Test]
  595. [TestRequiresDotsDebugOrCollectionChecks]
  596. public unsafe void UnsafeListT_TestInterfaces() => TestInterfacesDispose(new UnsafeList<int>(1, CommonRwdAllocator.Handle));
  597. [Test]
  598. [TestRequiresDotsDebugOrCollectionChecks]
  599. public unsafe void NativeList_TestInterfaces() => TestInterfacesDispose(new NativeList<int>(1, CommonRwdAllocator.Handle));
  600. [Test]
  601. [TestRequiresDotsDebugOrCollectionChecks]
  602. public unsafe void FixedList32Bytes_TestInterfaces() => TestInterfaces(new FixedList32Bytes<int>());
  603. [Test]
  604. [TestRequiresDotsDebugOrCollectionChecks]
  605. public unsafe void FixedList64Bytes_TestInterfaces() => TestInterfaces(new FixedList64Bytes<int>());
  606. [Test]
  607. [TestRequiresDotsDebugOrCollectionChecks]
  608. public unsafe void FixedList128Bytes_TestInterfaces() => TestInterfaces(new FixedList128Bytes<int>());
  609. [Test]
  610. [TestRequiresDotsDebugOrCollectionChecks]
  611. public unsafe void FixedList512Bytes_TestInterfaces() => TestInterfaces(new FixedList512Bytes<int>());
  612. [Test]
  613. [TestRequiresDotsDebugOrCollectionChecks]
  614. public unsafe void FixedList4096Bytes_TestInterfaces() => TestInterfaces(new FixedList4096Bytes<int>());
  615. }