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.

NativeSortTests.gen.cs 29KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. using NUnit.Framework;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using Unity.Burst;
  6. using Unity.Collections;
  7. using Unity.Collections.Tests;
  8. using Unity.Collections.LowLevel.Unsafe;
  9. using Unity.Jobs;
  10. using Unity.Mathematics;
  11. //------------------------------------------------------------------------------
  12. // <auto-generated>
  13. // This code was generated by a tool.
  14. //
  15. // TextTransform Packages/com.unity.collections/Unity.Collections.Tests/NativeSortTests.tt
  16. //
  17. // Changes to this file may cause incorrect behavior and will be lost if
  18. // the code is regenerated.
  19. // </auto-generated>
  20. //------------------------------------------------------------------------------
  21. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortExtension.DefaultComparer<int>>))]
  22. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortExtension.DefaultComparer<int>>.SegmentSort))]
  23. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortExtension.DefaultComparer<int>>.SegmentSortMerge))]
  24. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.DescendingComparer<int>>))]
  25. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.DescendingComparer<int>>.SegmentSort))]
  26. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.DescendingComparer<int>>.SegmentSortMerge))]
  27. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer0<int>>))]
  28. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer0<int>>.SegmentSort))]
  29. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer0<int>>.SegmentSortMerge))]
  30. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer1<int>>))]
  31. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer1<int>>.SegmentSort))]
  32. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer1<int>>.SegmentSortMerge))]
  33. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer2<int>>))]
  34. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer2<int>>.SegmentSort))]
  35. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer2<int>>.SegmentSortMerge))]
  36. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer3<int>>))]
  37. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer3<int>>.SegmentSort))]
  38. [assembly: RegisterGenericJobType(typeof(SortJob<int, NativeSortTests.BrokenComparer3<int>>.SegmentSortMerge))]
  39. internal class NativeSortTests : CollectionsTestCommonBase
  40. {
  41. internal struct DescendingComparer<T> : IComparer<T> where T : IComparable<T>
  42. {
  43. public int Compare(T x, T y) => y.CompareTo(x);
  44. }
  45. internal struct BrokenComparer0<T> : IComparer<T> where T : IComparable<T>
  46. {
  47. public int Compare(T x, T y)
  48. {
  49. int result = y.CompareTo(x);
  50. return result < 0 ? -1 : 1;
  51. }
  52. }
  53. internal struct BrokenComparer1<T> : IComparer<T> where T : IComparable<T>
  54. {
  55. public int Compare(T x, T y)
  56. {
  57. int result = y.CompareTo(x);
  58. return result > 0 ? 1 : -1;
  59. }
  60. }
  61. internal struct BrokenComparer2<T> : IComparer<T> where T : IComparable<T>
  62. {
  63. public int Compare(T x, T y)
  64. {
  65. int result = y.CompareTo(x);
  66. return math.max(0, result);
  67. }
  68. }
  69. internal struct BrokenComparer3<T> : IComparer<T> where T : IComparable<T>
  70. {
  71. public int Compare(T x, T y)
  72. {
  73. int result = y.CompareTo(x);
  74. return math.min(0, result);
  75. }
  76. }
  77. [Test]
  78. public void NativeArraySlice_BinarySearch()
  79. {
  80. var init = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 };
  81. var container = new NativeArray<int>(16, Allocator.Persistent);
  82. var slice = new NativeSlice<int>(container, 0, container.Length);
  83. var arrayRo = container.AsReadOnly();
  84. container.CopyFrom(init);
  85. for (int i = 0, num = container.Length; i < num; ++i)
  86. {
  87. Assert.AreEqual(i, container.BinarySearch(container[i]));
  88. Assert.AreEqual(i, slice.BinarySearch(container[i]));
  89. Assert.AreEqual(i, arrayRo.BinarySearch(container[i]));
  90. }
  91. container.Dispose();
  92. }
  93. struct BinarySearch_Job : IJob
  94. {
  95. [ReadOnly]
  96. public NativeArray<int> array;
  97. [ReadOnly]
  98. public NativeSlice<int> slice;
  99. [ReadOnly]
  100. public NativeArray<int>.ReadOnly arrayRo;
  101. [ReadOnly]
  102. public NativeList<int> nativeList;
  103. public void Execute()
  104. {
  105. for (int i = 0, num = array.Length; i < num; ++i)
  106. {
  107. Assert.AreEqual(i, array.BinarySearch(array[i]));
  108. Assert.AreEqual(i, slice.BinarySearch(array[i]));
  109. Assert.AreEqual(i, arrayRo.BinarySearch(array[i]));
  110. Assert.AreEqual(i, nativeList.BinarySearch(array[i]));
  111. }
  112. }
  113. }
  114. [Test]
  115. public void BinarySearch_From_Job()
  116. {
  117. var init = new int[] { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 };
  118. var container = new NativeArray<int>(16, Allocator.Persistent);
  119. var slice = new NativeSlice<int>(container, 0, container.Length);
  120. var arrayRo = container.AsReadOnly();
  121. container.CopyFrom(init);
  122. var nativeList = new NativeList<int>(16, Allocator.Persistent);
  123. nativeList.CopyFrom(container);
  124. new BinarySearch_Job
  125. {
  126. array = container,
  127. slice = slice,
  128. arrayRo = arrayRo,
  129. nativeList = nativeList,
  130. }.Run();
  131. container.Dispose();
  132. nativeList.Dispose();
  133. }
  134. [Test]
  135. public void NativeArraySlice_BinarySearch_NotFound()
  136. {
  137. {
  138. var container = new NativeArray<int>(1, Allocator.Temp);
  139. var slice = new NativeSlice<int>(container, 0, container.Length);
  140. var arrayRo = container.AsReadOnly();
  141. Assert.AreEqual(container.Length, 1);
  142. Assert.AreEqual(-2, container.BinarySearch(1));
  143. Assert.AreEqual(-2, slice.BinarySearch(1));
  144. Assert.AreEqual(-2, arrayRo.BinarySearch(1));
  145. slice[0] = 1;
  146. Assert.AreEqual(0, container.BinarySearch(1));
  147. Assert.AreEqual(0, slice.BinarySearch(1));
  148. Assert.AreEqual(0, arrayRo.BinarySearch(1));
  149. Assert.AreEqual(-1, container.BinarySearch(-2));
  150. Assert.AreEqual(-1, slice.BinarySearch(-2));
  151. Assert.AreEqual(-1, arrayRo.BinarySearch(-2));
  152. Assert.AreEqual(-2, container.BinarySearch(2));
  153. Assert.AreEqual(-2, slice.BinarySearch(2));
  154. Assert.AreEqual(-2, arrayRo.BinarySearch(2));
  155. container.Dispose();
  156. }
  157. {
  158. var init = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  159. var container = new NativeArray<int>(16, Allocator.Temp);
  160. var slice = new NativeSlice<int>(container, 0, container.Length);
  161. var arrayRo = container.AsReadOnly();
  162. container.CopyFrom(init);
  163. for (int i = 0, num = container.Length; i < num; ++i)
  164. {
  165. Assert.AreEqual(~container.Length, container.BinarySearch(i + 16));
  166. Assert.AreEqual(~slice.Length, slice.BinarySearch(i + 16));
  167. Assert.AreEqual(~arrayRo.Length, arrayRo.BinarySearch(i + 16));
  168. }
  169. container.Dispose();
  170. }
  171. {
  172. var init = new int[] { 0, 2, 4, 6, 8, 10, 12, 14 };
  173. var container = new NativeArray<int>(8, Allocator.Temp);
  174. var slice = new NativeSlice<int>(container, 0, container.Length);
  175. var arrayRo = container.AsReadOnly();
  176. container.CopyFrom(init);
  177. for (int i = 0, num = container.Length; i < num; ++i)
  178. {
  179. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, container.BinarySearch(i * 2 + 1));
  180. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, slice.BinarySearch(i * 2 + 1));
  181. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, arrayRo.BinarySearch(i * 2 + 1));
  182. }
  183. container.Dispose();
  184. }
  185. }
  186. [Test]
  187. public void NativeArraySlice_BinarySearch_NotFound_Reference_ArrayList()
  188. {
  189. {
  190. var reference = new ArrayList();
  191. reference.Add(0);
  192. var container = new NativeArray<int>(1, Allocator.Temp);
  193. var slice = new NativeSlice<int>(container, 0, container.Length);
  194. var arrayRo = container.AsReadOnly();
  195. Assert.AreEqual(container.Length, 1);
  196. Assert.AreEqual(-2, reference.BinarySearch(1));
  197. Assert.AreEqual(-2, container.BinarySearch(1));
  198. Assert.AreEqual(-2, slice.BinarySearch(1));
  199. Assert.AreEqual(-2, arrayRo.BinarySearch(1));
  200. reference[0] = 1;
  201. slice[0] = 1;
  202. Assert.AreEqual(0, reference.BinarySearch(1));
  203. Assert.AreEqual(0, container.BinarySearch(1));
  204. Assert.AreEqual(0, slice.BinarySearch(1));
  205. Assert.AreEqual(0, arrayRo.BinarySearch(1));
  206. Assert.AreEqual(-1, reference.BinarySearch(-2));
  207. Assert.AreEqual(-1, container.BinarySearch(-2));
  208. Assert.AreEqual(-1, slice.BinarySearch(-2));
  209. Assert.AreEqual(-1, arrayRo.BinarySearch(-2));
  210. Assert.AreEqual(-2, reference.BinarySearch(2));
  211. Assert.AreEqual(-2, container.BinarySearch(2));
  212. Assert.AreEqual(-2, slice.BinarySearch(2));
  213. Assert.AreEqual(-2, arrayRo.BinarySearch(2));
  214. }
  215. {
  216. var init = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  217. var container = new NativeArray<int>(16, Allocator.Temp);
  218. var slice = new NativeSlice<int>(container, 0, container.Length);
  219. var arrayRo = container.AsReadOnly();
  220. container.CopyFrom(init);
  221. var reference = new ArrayList(init);
  222. for (int i = 0, num = container.Length; i < num; ++i)
  223. {
  224. Assert.AreEqual(~reference.Count, reference.BinarySearch(i + 16));
  225. Assert.AreEqual(~container.Length, container.BinarySearch(i + 16));
  226. Assert.AreEqual(~slice.Length, slice.BinarySearch(i + 16));
  227. Assert.AreEqual(~arrayRo.Length, arrayRo.BinarySearch(i + 16));
  228. }
  229. }
  230. {
  231. var init = new int[] { 0, 2, 4, 6, 8, 10, 12, 14 };
  232. var container = new NativeArray<int>(8, Allocator.Temp);
  233. var slice = new NativeSlice<int>(container, 0, container.Length);
  234. var arrayRo = container.AsReadOnly();
  235. container.CopyFrom(init);
  236. var reference = new ArrayList(init);
  237. for (int i = 0, num = container.Length; i < num; ++i)
  238. {
  239. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, reference.BinarySearch(i * 2 + 1));
  240. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, container.BinarySearch(i * 2 + 1));
  241. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, slice.BinarySearch(i * 2 + 1));
  242. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, arrayRo.BinarySearch(i * 2 + 1));
  243. }
  244. }
  245. }
  246. [Test]
  247. public void NativeList_BinarySearch()
  248. {
  249. using (var container = new NativeList<int>(16, Allocator.Persistent) { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 })
  250. {
  251. for (int i = 0, num = container.Length; i < num; ++i)
  252. {
  253. Assert.AreEqual(i, container.BinarySearch(container[i]));
  254. }
  255. }
  256. }
  257. [Test]
  258. public void NativeList_BinarySearch_NotFound()
  259. {
  260. {
  261. var container = new NativeList<int>(1, Allocator.Temp);
  262. Assert.AreEqual(-1, container.BinarySearch(1));
  263. container.Add(1);
  264. Assert.AreEqual(0, container.BinarySearch(1));
  265. Assert.AreEqual(-1, container.BinarySearch(-2));
  266. Assert.AreEqual(-2, container.BinarySearch(2));
  267. }
  268. using (var container = new NativeList<int>(16, Allocator.Temp) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
  269. {
  270. for (int i = 0, num = container.Length; i < num; ++i)
  271. {
  272. Assert.AreEqual(~container.Length, container.BinarySearch(i + 16));
  273. }
  274. }
  275. using (var container = new NativeList<int>(8, Allocator.Temp) { 0, 2, 4, 6, 8, 10, 12, 14 })
  276. {
  277. for (int i = 0, num = container.Length; i < num; ++i)
  278. {
  279. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, container.BinarySearch(i * 2 + 1));
  280. }
  281. }
  282. }
  283. [Test]
  284. public void NativeList_BinarySearch_NotFound_Reference_ArrayList()
  285. {
  286. {
  287. var reference = new ArrayList();
  288. var container = new NativeList<int>(1, Allocator.Temp);
  289. Assert.AreEqual(-1, reference.BinarySearch(1));
  290. Assert.AreEqual(-1, container.BinarySearch(1));
  291. reference.Add(1);
  292. container.Add(1);
  293. Assert.AreEqual(0, reference.BinarySearch(1));
  294. Assert.AreEqual(0, container.BinarySearch(1));
  295. Assert.AreEqual(-1, reference.BinarySearch(-2));
  296. Assert.AreEqual(-1, container.BinarySearch(-2));
  297. Assert.AreEqual(-2, reference.BinarySearch(2));
  298. Assert.AreEqual(-2, container.BinarySearch(2));
  299. }
  300. using (var container = new NativeList<int>(16, Allocator.Temp) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
  301. {
  302. var reference = new ArrayList() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  303. for (int i = 0, num = container.Length; i < num; ++i)
  304. {
  305. Assert.AreEqual(~reference.Count, reference.BinarySearch(i + 16));
  306. Assert.AreEqual(~container.Length, container.BinarySearch(i + 16));
  307. }
  308. }
  309. using (var container = new NativeList<int>(8, Allocator.Temp) { 0, 2, 4, 6, 8, 10, 12, 14 })
  310. {
  311. var reference = new ArrayList() { 0, 2, 4, 6, 8, 10, 12, 14 };
  312. for (int i = 0, num = container.Length; i < num; ++i)
  313. {
  314. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, reference.BinarySearch(i * 2 + 1));
  315. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, container.BinarySearch(i * 2 + 1));
  316. }
  317. }
  318. }
  319. [Test]
  320. public void NativeList_GenericSortJob_NoBurst()
  321. {
  322. NativeList_GenericSortJob();
  323. }
  324. [BurstCompile]
  325. public static void Bursted_NativeList_GenericSortJob()
  326. {
  327. NativeList_GenericSortJob();
  328. }
  329. [Test]
  330. public void NativeList_GenericSortJob_Burst()
  331. {
  332. Bursted_NativeList_GenericSortJob();
  333. }
  334. public static void NativeList_GenericSortJob()
  335. {
  336. using (var container = new NativeList<int>(5, Allocator.Persistent))
  337. {
  338. for (var i = 0; i < 5; ++i)
  339. {
  340. container.Add(4 - i);
  341. }
  342. container.Sort();
  343. for (var i = 0; i < 5; ++i)
  344. {
  345. Assert.AreEqual(i, container[i]);
  346. }
  347. }
  348. using (var container = new NativeList<int>(5, Allocator.Persistent))
  349. {
  350. for (var i = 0; i < 5; ++i)
  351. {
  352. container.Add(4 - i);
  353. }
  354. container.SortJob().Schedule().Complete();
  355. for (var i = 0; i < 5; ++i)
  356. {
  357. Assert.AreEqual(i, container[i]);
  358. }
  359. }
  360. }
  361. [Test]
  362. [TestRequiresDotsDebugOrCollectionChecks]
  363. public void NativeList_GenericSortJobCustomComparer_NoBurst()
  364. {
  365. NativeList_GenericSortJobCustomComparer();
  366. }
  367. [BurstCompile]
  368. public static void Bursted_NativeList_GenericSortJobCustomComparer()
  369. {
  370. NativeList_GenericSortJobCustomComparer();
  371. }
  372. [Test]
  373. [TestRequiresDotsDebugOrCollectionChecks]
  374. public void NativeList_GenericSortJobCustomComparer_Burst()
  375. {
  376. Bursted_NativeList_GenericSortJobCustomComparer();
  377. }
  378. public static void NativeList_GenericSortJobCustomComparer()
  379. {
  380. var num = 35;
  381. using (var container = new NativeList<int>(num, Allocator.Persistent))
  382. {
  383. for (var i = 0; i < num; ++i)
  384. {
  385. container.Add(i);
  386. }
  387. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer0<int>()));
  388. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer1<int>()));
  389. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer2<int>()));
  390. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer3<int>()));
  391. container.Sort(new DescendingComparer<int>());
  392. for (var i = 0; i < num; ++i)
  393. {
  394. Assert.AreEqual(num - 1 - i, container[i]);
  395. }
  396. }
  397. using (var container = new NativeList<int>(num, Allocator.Persistent))
  398. {
  399. for (var i = 0; i < num; ++i)
  400. {
  401. container.Add(i);
  402. }
  403. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer0<int>()).Schedule().Complete());
  404. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer1<int>()).Schedule().Complete());
  405. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer2<int>()).Schedule().Complete());
  406. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer3<int>()).Schedule().Complete());
  407. container.SortJob(new DescendingComparer<int>()).Schedule().Complete();
  408. for (var i = 0; i < num; ++i)
  409. {
  410. Assert.AreEqual(num - 1 - i, container[i]);
  411. }
  412. }
  413. }
  414. [Test]
  415. public void UnsafeList_BinarySearch()
  416. {
  417. using (var container = new UnsafeList<int>(16, Allocator.Persistent) { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53 })
  418. {
  419. for (int i = 0, num = container.Length; i < num; ++i)
  420. {
  421. Assert.AreEqual(i, container.BinarySearch(container[i]));
  422. }
  423. }
  424. }
  425. [Test]
  426. public void UnsafeList_BinarySearch_NotFound()
  427. {
  428. {
  429. var container = new UnsafeList<int>(1, Allocator.Temp);
  430. Assert.AreEqual(-1, container.BinarySearch(1));
  431. container.Add(1);
  432. Assert.AreEqual(0, container.BinarySearch(1));
  433. Assert.AreEqual(-1, container.BinarySearch(-2));
  434. Assert.AreEqual(-2, container.BinarySearch(2));
  435. }
  436. using (var container = new UnsafeList<int>(16, Allocator.Temp) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
  437. {
  438. for (int i = 0, num = container.Length; i < num; ++i)
  439. {
  440. Assert.AreEqual(~container.Length, container.BinarySearch(i + 16));
  441. }
  442. }
  443. using (var container = new UnsafeList<int>(8, Allocator.Temp) { 0, 2, 4, 6, 8, 10, 12, 14 })
  444. {
  445. for (int i = 0, num = container.Length; i < num; ++i)
  446. {
  447. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, container.BinarySearch(i * 2 + 1));
  448. }
  449. }
  450. }
  451. [Test]
  452. public void UnsafeList_BinarySearch_NotFound_Reference_ArrayList()
  453. {
  454. {
  455. var reference = new ArrayList();
  456. var container = new UnsafeList<int>(1, Allocator.Temp);
  457. Assert.AreEqual(-1, reference.BinarySearch(1));
  458. Assert.AreEqual(-1, container.BinarySearch(1));
  459. reference.Add(1);
  460. container.Add(1);
  461. Assert.AreEqual(0, reference.BinarySearch(1));
  462. Assert.AreEqual(0, container.BinarySearch(1));
  463. Assert.AreEqual(-1, reference.BinarySearch(-2));
  464. Assert.AreEqual(-1, container.BinarySearch(-2));
  465. Assert.AreEqual(-2, reference.BinarySearch(2));
  466. Assert.AreEqual(-2, container.BinarySearch(2));
  467. }
  468. using (var container = new UnsafeList<int>(16, Allocator.Temp) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 })
  469. {
  470. var reference = new ArrayList() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
  471. for (int i = 0, num = container.Length; i < num; ++i)
  472. {
  473. Assert.AreEqual(~reference.Count, reference.BinarySearch(i + 16));
  474. Assert.AreEqual(~container.Length, container.BinarySearch(i + 16));
  475. }
  476. }
  477. using (var container = new UnsafeList<int>(8, Allocator.Temp) { 0, 2, 4, 6, 8, 10, 12, 14 })
  478. {
  479. var reference = new ArrayList() { 0, 2, 4, 6, 8, 10, 12, 14 };
  480. for (int i = 0, num = container.Length; i < num; ++i)
  481. {
  482. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, reference.BinarySearch(i * 2 + 1));
  483. Assert.AreEqual(~(i + 1) /* ~index of first greatest value searched */, container.BinarySearch(i * 2 + 1));
  484. }
  485. }
  486. }
  487. [Test]
  488. public void UnsafeList_GenericSortJob_NoBurst()
  489. {
  490. UnsafeList_GenericSortJob();
  491. }
  492. [BurstCompile]
  493. public static void Bursted_UnsafeList_GenericSortJob()
  494. {
  495. UnsafeList_GenericSortJob();
  496. }
  497. [Test]
  498. public void UnsafeList_GenericSortJob_Burst()
  499. {
  500. Bursted_UnsafeList_GenericSortJob();
  501. }
  502. public static void UnsafeList_GenericSortJob()
  503. {
  504. using (var container = new UnsafeList<int>(5, Allocator.Persistent))
  505. {
  506. for (var i = 0; i < 5; ++i)
  507. {
  508. container.Add(4 - i);
  509. }
  510. container.Sort();
  511. for (var i = 0; i < 5; ++i)
  512. {
  513. Assert.AreEqual(i, container[i]);
  514. }
  515. }
  516. using (var container = new UnsafeList<int>(5, Allocator.Persistent))
  517. {
  518. for (var i = 0; i < 5; ++i)
  519. {
  520. container.Add(4 - i);
  521. }
  522. container.SortJob().Schedule().Complete();
  523. for (var i = 0; i < 5; ++i)
  524. {
  525. Assert.AreEqual(i, container[i]);
  526. }
  527. }
  528. }
  529. [Test]
  530. [TestRequiresDotsDebugOrCollectionChecks]
  531. public void UnsafeList_GenericSortJobCustomComparer_NoBurst()
  532. {
  533. UnsafeList_GenericSortJobCustomComparer();
  534. }
  535. [BurstCompile]
  536. public static void Bursted_UnsafeList_GenericSortJobCustomComparer()
  537. {
  538. UnsafeList_GenericSortJobCustomComparer();
  539. }
  540. [Test]
  541. [TestRequiresDotsDebugOrCollectionChecks]
  542. public void UnsafeList_GenericSortJobCustomComparer_Burst()
  543. {
  544. Bursted_UnsafeList_GenericSortJobCustomComparer();
  545. }
  546. public static void UnsafeList_GenericSortJobCustomComparer()
  547. {
  548. var num = 35;
  549. using (var container = new UnsafeList<int>(num, Allocator.Persistent))
  550. {
  551. for (var i = 0; i < num; ++i)
  552. {
  553. container.Add(i);
  554. }
  555. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer0<int>()));
  556. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer1<int>()));
  557. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer2<int>()));
  558. Assert.Throws<InvalidOperationException>(() => container.Sort(new BrokenComparer3<int>()));
  559. container.Sort(new DescendingComparer<int>());
  560. for (var i = 0; i < num; ++i)
  561. {
  562. Assert.AreEqual(num - 1 - i, container[i]);
  563. }
  564. }
  565. using (var container = new UnsafeList<int>(num, Allocator.Persistent))
  566. {
  567. for (var i = 0; i < num; ++i)
  568. {
  569. container.Add(i);
  570. }
  571. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer0<int>()).Schedule().Complete());
  572. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer1<int>()).Schedule().Complete());
  573. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer2<int>()).Schedule().Complete());
  574. Assert.Throws<InvalidOperationException>(() => container.SortJob(new BrokenComparer3<int>()).Schedule().Complete());
  575. container.SortJob(new DescendingComparer<int>()).Schedule().Complete();
  576. for (var i = 0; i < num; ++i)
  577. {
  578. Assert.AreEqual(num - 1 - i, container[i]);
  579. }
  580. }
  581. }
  582. [Test]
  583. public void FixedList32Bytes_GenericSort()
  584. {
  585. var container = new FixedList32Bytes<int>();
  586. for (var i = 0; i < 5; ++i)
  587. {
  588. container.Add(i);
  589. }
  590. container.Sort(new DescendingComparer<int>());
  591. for (var i = 0; i < 5; ++i)
  592. {
  593. Assert.AreEqual(4 - i, container[i]);
  594. }
  595. }
  596. [Test]
  597. public void FixedList64Bytes_GenericSort()
  598. {
  599. var container = new FixedList64Bytes<int>();
  600. for (var i = 0; i < 5; ++i)
  601. {
  602. container.Add(i);
  603. }
  604. container.Sort(new DescendingComparer<int>());
  605. for (var i = 0; i < 5; ++i)
  606. {
  607. Assert.AreEqual(4 - i, container[i]);
  608. }
  609. }
  610. [Test]
  611. public void FixedList128Bytes_GenericSort()
  612. {
  613. var container = new FixedList128Bytes<int>();
  614. for (var i = 0; i < 5; ++i)
  615. {
  616. container.Add(i);
  617. }
  618. container.Sort(new DescendingComparer<int>());
  619. for (var i = 0; i < 5; ++i)
  620. {
  621. Assert.AreEqual(4 - i, container[i]);
  622. }
  623. }
  624. [Test]
  625. public void FixedList512Bytes_GenericSort()
  626. {
  627. var container = new FixedList512Bytes<int>();
  628. for (var i = 0; i < 5; ++i)
  629. {
  630. container.Add(i);
  631. }
  632. container.Sort(new DescendingComparer<int>());
  633. for (var i = 0; i < 5; ++i)
  634. {
  635. Assert.AreEqual(4 - i, container[i]);
  636. }
  637. }
  638. [Test]
  639. public void FixedList4096Bytes_GenericSort()
  640. {
  641. var container = new FixedList4096Bytes<int>();
  642. for (var i = 0; i < 5; ++i)
  643. {
  644. container.Add(i);
  645. }
  646. container.Sort(new DescendingComparer<int>());
  647. for (var i = 0; i < 5; ++i)
  648. {
  649. Assert.AreEqual(4 - i, container[i]);
  650. }
  651. }
  652. unsafe static void IntroSortNoComparerCheck<T, U>(T* array, int length, U comp)
  653. where T : unmanaged
  654. where U : IComparer<T>
  655. {
  656. NativeSortExtension.IntroSort_R<T, U>(array, 0, length - 1, 2 * CollectionHelper.Log2Floor(length), comp);
  657. }
  658. [Test]
  659. [TestRequiresDotsDebugOrCollectionChecks]
  660. public unsafe void NativeList_BrokenCustomComparerDoesNotCrash()
  661. {
  662. var rng = new Unity.Mathematics.Random(1);
  663. var num = 10000;
  664. using (var container = new NativeList<int>(num, Allocator.Persistent))
  665. {
  666. for (var i = 0; i < num; ++i)
  667. {
  668. container.Add(rng.NextInt());
  669. }
  670. Assert.DoesNotThrow(() => IntroSortNoComparerCheck(container.GetUnsafePtr(), container.Length, new BrokenComparer0<int>()));
  671. }
  672. using (var container = new NativeList<int>(num, Allocator.Persistent))
  673. {
  674. for (var i = 0; i < num; ++i)
  675. {
  676. container.Add(rng.NextInt());
  677. }
  678. Assert.DoesNotThrow(() => IntroSortNoComparerCheck(container.GetUnsafePtr(), container.Length, new BrokenComparer1<int>()));
  679. }
  680. using (var container = new NativeList<int>(num, Allocator.Persistent))
  681. {
  682. for (var i = 0; i < num; ++i)
  683. {
  684. container.Add(rng.NextInt());
  685. }
  686. Assert.DoesNotThrow(() => IntroSortNoComparerCheck(container.GetUnsafePtr(), container.Length, new BrokenComparer2<int>()));
  687. }
  688. using (var container = new NativeList<int>(num, Allocator.Persistent))
  689. {
  690. for (var i = 0; i < num; ++i)
  691. {
  692. container.Add(rng.NextInt());
  693. }
  694. Assert.DoesNotThrow(() => IntroSortNoComparerCheck(container.GetUnsafePtr(), container.Length, new BrokenComparer3<int>()));
  695. }
  696. }
  697. }