暫無描述
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.

ConcurrentMaskTests.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using NUnit.Framework;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Mathematics;
  6. using Unity.Collections.LowLevel.Unsafe;
  7. using Unity.Jobs;
  8. internal class ConcurrentMaskTests
  9. {
  10. internal struct Test
  11. {
  12. internal long value;
  13. internal int bits;
  14. internal long expectedModified;
  15. internal int offset;
  16. internal bool expectedWorked;
  17. internal Test(ulong uValue, ulong uExpectedModified, bool w)
  18. {
  19. this = default;
  20. value = (long)uValue;
  21. expectedModified = (long)uExpectedModified;
  22. expectedWorked = w;
  23. }
  24. internal Test(ulong uValue, int bits, ulong uExpectedModified, bool w)
  25. {
  26. this = default;
  27. value = (long)uValue;
  28. this.bits = bits;
  29. expectedModified = (long)uExpectedModified;
  30. expectedWorked = w;
  31. }
  32. internal Test(ulong uValue, ulong uExpectedModified, int offset, bool w)
  33. {
  34. this = default;
  35. value = (long)uValue;
  36. expectedModified = (long)uExpectedModified;
  37. this.offset = offset;
  38. expectedWorked = w;
  39. }
  40. internal Test(ulong uValue, int bits, ulong uExpectedModified, int offset, bool w)
  41. {
  42. value = (long)uValue;
  43. this.bits = bits;
  44. expectedModified = (long)uExpectedModified;
  45. this.offset = offset;
  46. expectedWorked = w;
  47. }
  48. }
  49. [Test]
  50. public void AllocatesOneBitFromLong()
  51. {
  52. foreach(var test in new Test[] {
  53. new Test(0x0000000000000000UL,0x0000000000000001UL, true),
  54. new Test(0x0000000000000001UL,0x0000000000000003UL, true),
  55. new Test(0x00000000000000FFUL,0x00000000000001FFUL, true),
  56. new Test(0x0000000000000100UL,0x0000000000000101UL, true),
  57. new Test(0x7FFFFFFFFFFFFFFFUL,0xFFFFFFFFFFFFFFFFUL, true),
  58. new Test(0x8000000000000000UL,0x8000000000000001UL, true),
  59. }) {
  60. long value = test.value;
  61. long expectedModified = test.expectedModified;
  62. long modified = value;
  63. var worked = ConcurrentMask.TryAllocate(ref modified, out int _, 1);
  64. Assert.AreEqual(expectedModified, modified);
  65. Assert.AreEqual(test.expectedWorked, ConcurrentMask.Succeeded(worked));
  66. }
  67. }
  68. [Test]
  69. public void FailsToAllocateOneBitFromLong()
  70. {
  71. foreach(var test in new Test[] {
  72. new Test(0xFFFFFFFFFFFFFFFFUL,0xFFFFFFFFFFFFFFFFUL, false)
  73. }) {
  74. long value = test.value;
  75. long expectedModified = test.expectedModified;
  76. long modified = value;
  77. var worked = ConcurrentMask.TryAllocate(ref modified, out int _, 1);
  78. Assert.AreEqual(expectedModified, modified);
  79. Assert.AreEqual(test.expectedWorked, ConcurrentMask.Succeeded(worked));
  80. }
  81. }
  82. [Test]
  83. public void AllocatesMultipleBitsFromLong()
  84. {
  85. foreach(var test in new Test[] {
  86. new Test(0x0000000000000000UL, 64,0xFFFFFFFFFFFFFFFFUL, true),
  87. new Test(0x0000000000000000UL, 2,0x0000000000000003UL, true),
  88. new Test(0x0000000000000001UL,63,0xFFFFFFFFFFFFFFFFUL, true),
  89. new Test(0x00000000000000FFUL,8, 0x000000000000FFFFUL, true),
  90. new Test(0x00000000000FF0FFUL,8, 0x000000000FFFF0FFUL, true),
  91. }) {
  92. long value = test.value;
  93. long expectedModified = test.expectedModified;
  94. long modified = value;
  95. var worked = ConcurrentMask.TryAllocate(ref modified, out int _, test.bits);
  96. Assert.AreEqual(expectedModified, modified);
  97. Assert.AreEqual(test.expectedWorked, ConcurrentMask.Succeeded(worked));
  98. }
  99. }
  100. [Test]
  101. public void FailsToAllocateMultipleBitsFromLong()
  102. {
  103. foreach(var test in new Test[] {
  104. new Test(0x0000000000000000UL, 65,0x0000000000000000UL, false),
  105. new Test(0x0000000000000001UL,64,0x0000000000000001UL, false),
  106. new Test(0xFF000000000000FFUL,49, 0xFF000000000000FFUL, false),
  107. }) {
  108. long value = test.value;
  109. long expectedModified = test.expectedModified;
  110. long modified = value;
  111. var worked = ConcurrentMask.TryAllocate(ref modified, out int _, test.bits);
  112. Assert.AreEqual(expectedModified, modified);
  113. Assert.AreEqual(test.expectedWorked, ConcurrentMask.Succeeded(worked));
  114. }
  115. }
  116. [Test]
  117. public void FreesOneBitFromLong()
  118. {
  119. foreach(var test in new Test[] {
  120. new Test(0x0000000000000000UL,0x0000000000000001UL, 0, true),
  121. new Test(0x0000000000000001UL,0x0000000000000003UL, 1, true),
  122. new Test(0x00000000000000FFUL,0x00000000000001FFUL, 8, true),
  123. new Test(0x0000000000000100UL,0x0000000000000101UL, 0, true),
  124. new Test(0x7FFFFFFFFFFFFFFFUL,0xFFFFFFFFFFFFFFFFUL, 63, true),
  125. new Test(0x8000000000000000UL,0x8000000000000001UL, 0, true),
  126. }) {
  127. long expectedModified = (long)test.value;
  128. long modified = (long)test.expectedModified;
  129. var worked = ConcurrentMask.TryFree(ref modified, test.offset, 1);
  130. Assert.AreEqual(expectedModified, modified);
  131. Assert.AreEqual(test.expectedWorked, ConcurrentMask.Succeeded(worked));
  132. }
  133. }
  134. [Test]
  135. public void FreesMultipleBitsFromLong()
  136. {
  137. foreach(var test in new Test[] {
  138. new Test(0x0000000000000000UL, 64,0xFFFFFFFFFFFFFFFFUL, 0, true),
  139. new Test(0x0000000000000000UL, 2,0x0000000000000003UL, 0, true),
  140. new Test(0x0000000000000001UL,63,0xFFFFFFFFFFFFFFFFUL, 1, true),
  141. new Test(0x00000000000000FFUL,8, 0x000000000000FFFFUL, 8, true),
  142. new Test(0x00000000000FF0FFUL,8, 0x000000000FFFF0FFUL, 20, true),
  143. }) {
  144. long expectedModified = test.value;
  145. long modified = test.expectedModified;
  146. var worked = ConcurrentMask.TryFree(ref modified, test.offset, test.bits);
  147. Assert.AreEqual(expectedModified, modified);
  148. Assert.AreEqual(test.expectedWorked, ConcurrentMask.Succeeded(worked));
  149. }
  150. }
  151. [Test]
  152. public void AllocatesOneBitFromArray()
  153. {
  154. var storage = new NativeList<long>(3, Allocator.Persistent);
  155. storage.Length = 3;
  156. for(var i = 0; i < 64; ++i)
  157. {
  158. var worked = ConcurrentMask.TryAllocate(ref storage, out int offset, 1);
  159. Assert.AreEqual(true, ConcurrentMask.Succeeded(worked));
  160. Assert.AreEqual(i, offset);
  161. }
  162. Assert.AreEqual(-1L, storage[0]);
  163. Assert.AreEqual(0L, storage[1]);
  164. Assert.AreEqual(0L, storage[2]);
  165. for(var i = 0; i < 64; ++i)
  166. {
  167. var worked = ConcurrentMask.TryAllocate(ref storage, out int offset, 1);
  168. Assert.AreEqual(true, ConcurrentMask.Succeeded(worked));
  169. Assert.AreEqual(64+i, offset);
  170. }
  171. Assert.AreEqual(-1L, storage[0]);
  172. Assert.AreEqual(-1L, storage[1]);
  173. Assert.AreEqual(0L, storage[2]);
  174. storage.Dispose();
  175. }
  176. [Test]
  177. public void AllocatesMultipleBitsFromArray()
  178. {
  179. var storage = new NativeList<long>(3, Allocator.Persistent);
  180. storage.Length = 3;
  181. for(var i = 0; i < 3; ++i)
  182. {
  183. var worked = ConcurrentMask.TryAllocate(ref storage, out int offset, 33);
  184. Assert.AreEqual(true, ConcurrentMask.Succeeded(worked));
  185. Assert.AreEqual(i * 64, offset);
  186. }
  187. {
  188. var worked = ConcurrentMask.TryAllocate(ref storage, out int offset, 33);
  189. Assert.AreEqual(false, ConcurrentMask.Succeeded(worked));
  190. }
  191. storage.Dispose();
  192. }
  193. [Test]
  194. public void FreesOneBitFromArray()
  195. {
  196. var storage = new NativeList<long>(3, Allocator.Persistent);
  197. storage.Length = 3;
  198. ConcurrentMask.TryAllocate(ref storage, out int _, 64);
  199. ConcurrentMask.TryAllocate(ref storage, out int _, 64);
  200. ConcurrentMask.TryAllocate(ref storage, out int _, 64);
  201. for(var i = 0; i < 64 * 3; ++i)
  202. {
  203. var worked = ConcurrentMask.TryFree(ref storage, i, 1);
  204. Assert.AreEqual(true, ConcurrentMask.Succeeded(worked));
  205. }
  206. Assert.AreEqual(0L, storage[0]);
  207. Assert.AreEqual(0L, storage[1]);
  208. Assert.AreEqual(0L, storage[2]);
  209. storage.Dispose();
  210. }
  211. [Test]
  212. public void FreesMultipleBitsFromArray()
  213. {
  214. var storage = new NativeList<long>(3, Allocator.Persistent);
  215. storage.Length = 3;
  216. ConcurrentMask.TryAllocate(ref storage, out int _, 64);
  217. ConcurrentMask.TryAllocate(ref storage, out int _, 64);
  218. ConcurrentMask.TryAllocate(ref storage, out int _, 64);
  219. for(var i = 0; i < 3; ++i)
  220. {
  221. var worked = ConcurrentMask.TryFree(ref storage, i * 64 + 1, 63);
  222. Assert.AreEqual(true, ConcurrentMask.Succeeded(worked));
  223. }
  224. Assert.AreEqual(1L, storage[0]);
  225. Assert.AreEqual(1L, storage[1]);
  226. Assert.AreEqual(1L, storage[2]);
  227. storage.Dispose();
  228. }
  229. [BurstCompile(CompileSynchronously = true)]
  230. struct AllocateJob : IJobParallelFor
  231. {
  232. [NativeDisableContainerSafetyRestriction] public NativeList<long> m_storage;
  233. public void Execute(int index)
  234. {
  235. ConcurrentMask.TryAllocate(ref m_storage, out int _, 1);
  236. }
  237. }
  238. [Test]
  239. public void AllocatesFromJob()
  240. {
  241. const int kLengthInWords = 10;
  242. const int kLengthInBits = kLengthInWords * 64;
  243. var storage = new NativeList<long>(kLengthInWords, Allocator.Persistent);
  244. storage.Length = kLengthInWords;
  245. for (int i = 0; i < kLengthInWords; ++i)
  246. Assert.AreEqual(0L, storage[i]);
  247. var allocateJob = new AllocateJob();
  248. allocateJob.m_storage = storage;
  249. allocateJob.Schedule(kLengthInBits, 1).Complete();
  250. for (int i = 0; i < kLengthInWords; ++i)
  251. Assert.AreEqual(~0L, storage[i]);
  252. storage.Dispose();
  253. }
  254. }