Sin descripción
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.

NativeMultiHashMapTests_InJobs.cs 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. using NUnit.Framework;
  2. using System.Collections.Generic;
  3. using Unity.Jobs;
  4. using Unity.Burst;
  5. using Unity.Collections;
  6. using Assert = FastAssert;
  7. internal class NativeMultiHashMapTests_InJobs : NativeMultiHashMapTestsFixture
  8. {
  9. [Test]
  10. public void NativeMultiHashMap_Read_And_Write()
  11. {
  12. var hashMap = new NativeMultiHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
  13. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  14. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  15. var writeData = new MultiHashMapWriteParallelForJob()
  16. {
  17. hashMap = hashMap.AsParallelWriter(),
  18. status = writeStatus,
  19. keyMod = hashMapSize,
  20. };
  21. var readData = new MultiHashMapReadParallelForJob()
  22. {
  23. hashMap = hashMap,
  24. values = readValues,
  25. keyMod = writeData.keyMod,
  26. };
  27. var writeJob = writeData.Schedule(hashMapSize, 1);
  28. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  29. readJob.Complete();
  30. for (int i = 0; i < hashMapSize; ++i)
  31. {
  32. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  33. Assert.AreEqual(1, readValues[i], "Job failed to read from hash map");
  34. }
  35. hashMap.Dispose();
  36. writeStatus.Dispose();
  37. readValues.Dispose();
  38. }
  39. [Test]
  40. [IgnoreInPortableTests("Hash map throws when full.")]
  41. public void NativeMultiHashMap_Read_And_Write_Full()
  42. {
  43. var hashMap = new NativeMultiHashMap<int, int>(hashMapSize / 2, CommonRwdAllocator.Handle);
  44. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  45. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  46. var writeData = new MultiHashMapWriteParallelForJob()
  47. {
  48. hashMap = hashMap.AsParallelWriter(),
  49. status = writeStatus,
  50. keyMod = hashMapSize,
  51. };
  52. var readData = new MultiHashMapReadParallelForJob()
  53. {
  54. hashMap = hashMap,
  55. values = readValues,
  56. keyMod = writeData.keyMod,
  57. };
  58. var writeJob = writeData.Schedule(hashMapSize, 1);
  59. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  60. readJob.Complete();
  61. var missing = new Dictionary<int, bool>();
  62. for (int i = 0; i < hashMapSize; ++i)
  63. {
  64. if (writeStatus[i] == -2)
  65. {
  66. missing[i] = true;
  67. Assert.AreEqual(-1, readValues[i], "Job read a value form hash map which should not be there");
  68. }
  69. else
  70. {
  71. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  72. Assert.AreEqual(1, readValues[i], "Job failed to read from hash map");
  73. }
  74. }
  75. Assert.AreEqual(hashMapSize - hashMapSize / 2, missing.Count, "Wrong indices written to hash map");
  76. hashMap.Dispose();
  77. writeStatus.Dispose();
  78. readValues.Dispose();
  79. }
  80. [Test]
  81. public void NativeMultiHashMap_Key_Collisions()
  82. {
  83. var hashMap = new NativeMultiHashMap<int, int>(hashMapSize, CommonRwdAllocator.Handle);
  84. var writeStatus = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  85. var readValues = CollectionHelper.CreateNativeArray<int>(hashMapSize, CommonRwdAllocator.Handle);
  86. var writeData = new MultiHashMapWriteParallelForJob()
  87. {
  88. hashMap = hashMap.AsParallelWriter(),
  89. status = writeStatus,
  90. keyMod = 16,
  91. };
  92. var readData = new MultiHashMapReadParallelForJob()
  93. {
  94. hashMap = hashMap,
  95. values = readValues,
  96. keyMod = writeData.keyMod,
  97. };
  98. var writeJob = writeData.Schedule(hashMapSize, 1);
  99. var readJob = readData.Schedule(hashMapSize, 1, writeJob);
  100. readJob.Complete();
  101. for (int i = 0; i < hashMapSize; ++i)
  102. {
  103. Assert.AreEqual(0, writeStatus[i], "Job failed to write value to hash map");
  104. Assert.AreEqual(hashMapSize / readData.keyMod, readValues[i], "Job failed to read from hash map");
  105. }
  106. hashMap.Dispose();
  107. writeStatus.Dispose();
  108. readValues.Dispose();
  109. }
  110. [BurstCompile(CompileSynchronously = true)]
  111. struct AddMultiIndex : IJobParallelFor
  112. {
  113. public NativeMultiHashMap<int, int>.ParallelWriter hashMap;
  114. public void Execute(int index)
  115. {
  116. hashMap.Add(index, index);
  117. }
  118. }
  119. [Test]
  120. public void NativeMultiHashMap_TryMultiAddScalabilityConcurrent()
  121. {
  122. for (int count = 0; count < 1024; count++)
  123. {
  124. var hashMap = new NativeMultiHashMap<int, int>(count, CommonRwdAllocator.Handle);
  125. var addIndexJob = new AddMultiIndex
  126. {
  127. hashMap = hashMap.AsParallelWriter()
  128. };
  129. var addIndexJobHandle = addIndexJob.Schedule(count, 64);
  130. addIndexJobHandle.Complete();
  131. hashMap.Dispose();
  132. }
  133. }
  134. }