123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682 |
- using System;
- using NUnit.Framework;
- using Unity.Burst;
- using Unity.Collections;
- using Unity.Collections.LowLevel.Unsafe;
- using Unity.Collections.NotBurstCompatible;
- using Unity.Collections.Tests;
- using Unity.Jobs;
- using UnityEngine;
- using UnityEngine.TestTools;
- using System.Text.RegularExpressions;
-
- internal class NativeParallelMultiHashMapTests : CollectionsTestFixture
- {
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeParallelMultiHashMap_UseAfterFree_UsesCustomOwnerTypeName()
- {
- var container = new NativeParallelMultiHashMap<int, int>(10, CommonRwdAllocator.Handle);
- container.Add(0, 123);
- container.Dispose();
- Assert.That(() => container.ContainsKey(0),
- Throws.Exception.TypeOf<ObjectDisposedException>()
- .With.Message.Contains($"The {container.GetType()} has been deallocated"));
- }
-
- [BurstCompile(CompileSynchronously = true)]
- struct NativeParallelMultiHashMap_CreateAndUseAfterFreeBurst : IJob
- {
- public void Execute()
- {
- var container = new NativeParallelMultiHashMap<int, int>(10, Allocator.Temp);
- container.Add(0, 17);
- container.Dispose();
- container.Add(1, 42);
- }
- }
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeParallelMultiHashMap_CreateAndUseAfterFreeInBurstJob_UsesCustomOwnerTypeName()
- {
- // Make sure this isn't the first container of this type ever created, so that valid static safety data exists
- var container = new NativeParallelMultiHashMap<int, int>(10, CommonRwdAllocator.Handle);
- container.Dispose();
-
- var job = new NativeParallelMultiHashMap_CreateAndUseAfterFreeBurst
- {
- };
-
- // Two things:
- // 1. This exception is logged, not thrown; thus, we use LogAssert to detect it.
- // 2. Calling write operation after container.Dispose() emits an unintuitive error message. For now, all this test cares about is whether it contains the
- // expected type name.
- job.Run();
- LogAssert.Expect(LogType.Exception,
- new Regex($"InvalidOperationException: The {Regex.Escape(container.GetType().ToString())} has been declared as \\[ReadOnly\\] in the job, but you are writing to it"));
- }
-
- [Test]
- public void NativeParallelMultiHashMap_IsEmpty()
- {
- var container = new NativeParallelMultiHashMap<int, int>(0, Allocator.Persistent);
- Assert.IsTrue(container.IsEmpty);
-
- container.Add(0, 0);
- Assert.IsFalse(container.IsEmpty);
- Assert.AreEqual(1, container.Capacity);
- ExpectedCount(ref container, 1);
-
- container.Remove(0, 0);
- Assert.IsTrue(container.IsEmpty);
-
- container.Add(0, 0);
- container.Clear();
- Assert.IsTrue(container.IsEmpty);
-
- container.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_CountValuesForKey()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- hashMap.Add(5, 7);
- hashMap.Add(6, 9);
- hashMap.Add(6, 10);
-
- Assert.AreEqual(1, hashMap.CountValuesForKey(5));
- Assert.AreEqual(2, hashMap.CountValuesForKey(6));
- Assert.AreEqual(0, hashMap.CountValuesForKey(7));
-
- hashMap.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_RemoveKeyAndValue()
- {
- var hashMap = new NativeParallelMultiHashMap<int, long>(1, Allocator.Temp);
- hashMap.Add(10, 0);
- hashMap.Add(10, 1);
- hashMap.Add(10, 2);
-
- hashMap.Add(20, 2);
- hashMap.Add(20, 2);
- hashMap.Add(20, 1);
- hashMap.Add(20, 2);
- hashMap.Add(20, 1);
-
- hashMap.Remove(10, 1L);
- ExpectValues(hashMap, 10, new[] { 0L, 2L });
- ExpectValues(hashMap, 20, new[] { 1L, 1L, 2L, 2L, 2L });
-
- hashMap.Remove(20, 2L);
- ExpectValues(hashMap, 10, new[] { 0L, 2L });
- ExpectValues(hashMap, 20, new[] { 1L, 1L });
-
- hashMap.Remove(20, 1L);
- ExpectValues(hashMap, 10, new[] { 0L, 2L });
- ExpectValues(hashMap, 20, new long[0]);
-
- hashMap.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_ValueIterator()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- hashMap.Add(5, 0);
- hashMap.Add(5, 1);
- hashMap.Add(5, 2);
-
- var list = new NativeList<int>(CommonRwdAllocator.Handle);
-
- GCAllocRecorder.ValidateNoGCAllocs(() =>
- {
- list.Clear();
- foreach (var value in hashMap.GetValuesForKey(5))
- list.Add(value);
- });
-
- list.Sort();
- Assert.AreEqual(list.ToArrayNBC(), new int[] { 0, 1, 2 });
-
- foreach (var value in hashMap.GetValuesForKey(6))
- Assert.Fail();
-
- list.Dispose();
- hashMap.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_RemoveKeyValueDoesntDeallocate()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp) { { 5, 1 } };
-
- hashMap.Remove(5, 5);
- GCAllocRecorder.ValidateNoGCAllocs(() =>
- {
- hashMap.Remove(5, 1);
- });
- Assert.IsTrue(hashMap.IsEmpty);
-
- hashMap.Dispose();
- }
-
- static void ExpectedCount<TKey, TValue>(ref NativeParallelMultiHashMap<TKey, TValue> container, int expected)
- where TKey : unmanaged, IEquatable<TKey>
- where TValue : unmanaged
- {
- Assert.AreEqual(expected == 0, container.IsEmpty);
- Assert.AreEqual(expected, container.Count());
- }
-
- [Test]
- public void NativeParallelMultiHashMap_RemoveOnEmptyMap_DoesNotThrow()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(0, Allocator.Temp);
-
- Assert.DoesNotThrow(() => hashMap.Remove(0));
- Assert.DoesNotThrow(() => hashMap.Remove(-425196));
- Assert.DoesNotThrow(() => hashMap.Remove(0, 0));
- Assert.DoesNotThrow(() => hashMap.Remove(-425196, 0));
-
- hashMap.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_RemoveFromMultiHashMap()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(16, Allocator.Temp);
- int iSquared;
- // Make sure inserting values work
- for (int i = 0; i < 8; ++i)
- hashMap.Add(i, i * i);
- for (int i = 0; i < 8; ++i)
- hashMap.Add(i, i);
- Assert.AreEqual(16, hashMap.Capacity, "HashMap grew larger than expected");
- // Make sure reading the inserted values work
- for (int i = 0; i < 8; ++i)
- {
- NativeParallelMultiHashMapIterator<int> it;
- Assert.IsTrue(hashMap.TryGetFirstValue(i, out iSquared, out it), "Failed get value from hash table");
- Assert.AreEqual(iSquared, i, "Got the wrong value from the hash table");
- Assert.IsTrue(hashMap.TryGetNextValue(out iSquared, ref it), "Failed get value from hash table");
- Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
- }
- for (int rm = 0; rm < 8; ++rm)
- {
- Assert.AreEqual(2, hashMap.Remove(rm));
- NativeParallelMultiHashMapIterator<int> it;
- Assert.IsFalse(hashMap.TryGetFirstValue(rm, out iSquared, out it), "Failed to remove value from hash table");
- for (int i = rm + 1; i < 8; ++i)
- {
- Assert.IsTrue(hashMap.TryGetFirstValue(i, out iSquared, out it), "Failed get value from hash table");
- Assert.AreEqual(iSquared, i, "Got the wrong value from the hash table");
- Assert.IsTrue(hashMap.TryGetNextValue(out iSquared, ref it), "Failed get value from hash table");
- Assert.AreEqual(iSquared, i * i, "Got the wrong value from the hash table");
- }
- }
- // Make sure entries were freed
- for (int i = 0; i < 8; ++i)
- hashMap.Add(i, i * i);
- for (int i = 0; i < 8; ++i)
- hashMap.Add(i, i);
- Assert.AreEqual(16, hashMap.Capacity, "HashMap grew larger than expected");
- hashMap.Dispose();
- }
-
- void ExpectValues(NativeParallelMultiHashMap<int, long> hashMap, int key, long[] expectedValues)
- {
- var list = new NativeList<long>(CommonRwdAllocator.Handle);
- foreach (var value in hashMap.GetValuesForKey(key))
- list.Add(value);
-
- list.Sort();
- Assert.AreEqual(list.ToArrayNBC(), expectedValues);
- list.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_GetKeys()
- {
- var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- for (int i = 0; i < 30; ++i)
- {
- container.Add(i, 2 * i);
- container.Add(i, 3 * i);
- }
- var keys = container.GetKeyArray(Allocator.Temp);
- var (unique, uniqueLength) = container.GetUniqueKeyArray(Allocator.Temp);
- Assert.AreEqual(30, uniqueLength);
-
- Assert.AreEqual(60, keys.Length);
- keys.Sort();
- for (int i = 0; i < 30; ++i)
- {
- Assert.AreEqual(i, keys[i * 2 + 0]);
- Assert.AreEqual(i, keys[i * 2 + 1]);
- Assert.AreEqual(i, unique[i]);
- }
- }
-
- [Test]
- public void NativeParallelMultiHashMap_GetUniqueKeysEmpty()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- var keys = hashMap.GetUniqueKeyArray(Allocator.Temp);
-
- Assert.AreEqual(0, keys.Item1.Length);
- Assert.AreEqual(0, keys.Item2);
- }
-
- [Test]
- public void NativeParallelMultiHashMap_GetUniqueKeys()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- for (int i = 0; i < 30; ++i)
- {
- hashMap.Add(i, 2 * i);
- hashMap.Add(i, 3 * i);
- }
- var keys = hashMap.GetUniqueKeyArray(Allocator.Temp);
- hashMap.Dispose();
- Assert.AreEqual(30, keys.Item2);
- for (int i = 0; i < 30; ++i)
- {
- Assert.AreEqual(i, keys.Item1[i]);
- }
- keys.Item1.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_GetValues()
- {
- var hashMap = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- for (int i = 0; i < 30; ++i)
- {
- hashMap.Add(i, 30 + i);
- hashMap.Add(i, 60 + i);
- }
- var values = hashMap.GetValueArray(Allocator.Temp);
- hashMap.Dispose();
-
- Assert.AreEqual(60, values.Length);
- values.Sort();
- for (int i = 0; i < 60; ++i)
- {
- Assert.AreEqual(30 + i, values[i]);
- }
- values.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_ForEach_FixedStringInHashMap()
- {
- using (var stringList = new NativeList<FixedString32Bytes>(10, Allocator.Persistent) { "Hello", ",", "World", "!" })
- {
- var container = new NativeParallelMultiHashMap<FixedString128Bytes, float>(50, Allocator.Temp);
- var seen = new NativeArray<int>(stringList.Length, Allocator.Temp);
- foreach (var str in stringList)
- {
- container.Add(str, 0);
- }
-
- foreach (var pair in container)
- {
- int index = stringList.IndexOf(pair.Key);
- Assert.AreEqual(stringList[index], pair.Key.ToString());
- seen[index] = seen[index] + 1;
- }
-
- for (int i = 0; i < stringList.Length; i++)
- {
- Assert.AreEqual(1, seen[i], $"Incorrect value count {stringList[i]}");
- }
- }
- }
-
- [Test]
- public void NativeParallelMultiHashMap_ForEach([Values(10, 1000)]int n)
- {
- var seenKeys = new NativeArray<int>(n, Allocator.Temp);
- var seenValues = new NativeArray<int>(n * 2, Allocator.Temp);
- using (var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp))
- {
- for (int i = 0; i < n; ++i)
- {
- container.Add(i, i);
- container.Add(i, i + n);
- }
-
- var count = 0;
- foreach (var kv in container)
- {
- if (kv.Value < n)
- {
- Assert.AreEqual(kv.Key, kv.Value);
- }
- else
- {
- Assert.AreEqual(kv.Key + n, kv.Value);
- }
-
- seenKeys[kv.Key] = seenKeys[kv.Key] + 1;
- seenValues[kv.Value] = seenValues[kv.Value] + 1;
-
- ++count;
- }
-
- Assert.AreEqual(container.Count(), count);
- for (int i = 0; i < n; i++)
- {
- Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}");
- Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}");
- Assert.AreEqual(1, seenValues[i + n], $"Incorrect value count {i + n}");
- }
- }
- }
-
- struct NativeParallelMultiHashMap_ForEach_Job : IJob
- {
- [ReadOnly]
- public NativeParallelMultiHashMap<int, int> Input;
-
- [ReadOnly]
- public int Num;
-
- public void Execute()
- {
- var seenKeys = new NativeArray<int>(Num, Allocator.Temp);
- var seenValues = new NativeArray<int>(Num * 2, Allocator.Temp);
-
- var count = 0;
- foreach (var kv in Input)
- {
- if (kv.Value < Num)
- {
- Assert.AreEqual(kv.Key, kv.Value);
- }
- else
- {
- Assert.AreEqual(kv.Key + Num, kv.Value);
- }
-
- seenKeys[kv.Key] = seenKeys[kv.Key] + 1;
- seenValues[kv.Value] = seenValues[kv.Value] + 1;
-
- ++count;
- }
-
- Assert.AreEqual(Input.Count(), count);
- for (int i = 0; i < Num; i++)
- {
- Assert.AreEqual(2, seenKeys[i], $"Incorrect key count {i}");
- Assert.AreEqual(1, seenValues[i], $"Incorrect value count {i}");
- Assert.AreEqual(1, seenValues[i + Num], $"Incorrect value count {i + Num}");
- }
-
- seenKeys.Dispose();
- seenValues.Dispose();
- }
- }
-
- [Test]
- public void NativeParallelMultiHashMap_ForEach_From_Job([Values(10, 1000)] int n)
- {
- using (var container = new NativeParallelMultiHashMap<int, int>(1, CommonRwdAllocator.Handle))
- {
- for (int i = 0; i < n; ++i)
- {
- container.Add(i, i);
- container.Add(i, i + n);
- }
-
- new NativeParallelMultiHashMap_ForEach_Job
- {
- Input = container,
- Num = n,
-
- }.Run();
- }
- }
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeParallelMultiHashMap_ForEach_Throws_When_Modified()
- {
- using (var container = new NativeParallelMultiHashMap<int, int>(32, CommonRwdAllocator.Handle))
- {
- for (int i = 0; i < 30; ++i)
- {
- container.Add(i, 30 + i);
- container.Add(i, 60 + i);
- }
-
- Assert.Throws<ObjectDisposedException>(() =>
- {
- foreach (var kv in container)
- {
- container.Add(10, 10);
- }
- });
-
- Assert.Throws<ObjectDisposedException>(() =>
- {
- foreach (var kv in container)
- {
- container.Remove(1);
- }
- });
- }
- }
-
- struct NativeParallelMultiHashMap_ForEachIterator : IJob
- {
- [ReadOnly]
- public NativeParallelMultiHashMap<int, int>.KeyValueEnumerator Iter;
-
- public void Execute()
- {
- while (Iter.MoveNext())
- {
- }
- }
- }
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeParallelMultiHashMap_ForEach_Throws_Job_Iterator()
- {
- using (var container = new NativeParallelMultiHashMap<int, int>(32, CommonRwdAllocator.Handle))
- {
- var jobHandle = new NativeParallelMultiHashMap_ForEachIterator
- {
- Iter = container.GetEnumerator()
-
- }.Schedule();
-
- Assert.Throws<InvalidOperationException>(() => { container.Add(1, 1); });
-
- jobHandle.Complete();
- }
- }
-
- struct ParallelWriteToMultiHashMapJob : IJobParallelFor
- {
- [WriteOnly]
- public NativeParallelMultiHashMap<int, int>.ParallelWriter Writer;
-
- public void Execute(int index)
- {
- Writer.Add(index, 0);
- }
- }
-
- [Test]
- [TestRequiresCollectionChecks]
- public void NativeParallelMultiHashMap_ForEach_Throws_When_Modified_From_Job()
- {
- using (var container = new NativeParallelMultiHashMap<int, int>(32, CommonRwdAllocator.Handle))
- {
- var iter = container.GetEnumerator();
-
- var jobHandle = new ParallelWriteToMultiHashMapJob
- {
- Writer = container.AsParallelWriter()
-
- }.Schedule(1, 2);
-
- Assert.Throws<ObjectDisposedException>(() =>
- {
- while (iter.MoveNext())
- {
- }
- });
-
- jobHandle.Complete();
- }
- }
-
- [Test]
- public void NativeParallelMultiHashMap_GetKeysAndValues()
- {
- var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- for (int i = 0; i < 30; ++i)
- {
- container.Add(i, 30 + i);
- container.Add(i, 60 + i);
- }
- var keysValues = container.GetKeyValueArrays(Allocator.Temp);
- container.Dispose();
-
- Assert.AreEqual(60, keysValues.Keys.Length);
- Assert.AreEqual(60, keysValues.Values.Length);
-
- // ensure keys and matching values are aligned (though unordered)
- for (int i = 0; i < 30; ++i)
- {
- var k0 = keysValues.Keys[i * 2 + 0];
- var k1 = keysValues.Keys[i * 2 + 1];
- var v0 = keysValues.Values[i * 2 + 0];
- var v1 = keysValues.Values[i * 2 + 1];
-
- if (v0 > v1)
- (v0, v1) = (v1, v0);
-
- Assert.AreEqual(k0, k1);
- Assert.AreEqual(30 + k0, v0);
- Assert.AreEqual(60 + k0, v1);
- }
-
- keysValues.Keys.Sort();
- for (int i = 0; i < 30; ++i)
- {
- Assert.AreEqual(i, keysValues.Keys[i * 2 + 0]);
- Assert.AreEqual(i, keysValues.Keys[i * 2 + 1]);
- }
-
- keysValues.Values.Sort();
- for (int i = 0; i < 60; ++i)
- {
- Assert.AreEqual(30 + i, keysValues.Values[i]);
- }
-
- keysValues.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_ContainsKeyMultiHashMap()
- {
- var container = new NativeParallelMultiHashMap<int, int>(1, Allocator.Temp);
- container.Add(5, 7);
-
- container.Add(6, 9);
- container.Add(6, 10);
-
- Assert.IsTrue(container.ContainsKey(5));
- Assert.IsTrue(container.ContainsKey(6));
- Assert.IsFalse(container.ContainsKey(4));
-
- container.Dispose();
- }
-
- [Test]
- public void NativeParallelMultiHashMap_CustomAllocatorTest()
- {
- AllocatorManager.Initialize();
- var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- using (var container = new NativeParallelMultiHashMap<int, int>(1, allocator.Handle))
- {
- }
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- allocatorHelper.Dispose();
- AllocatorManager.Shutdown();
- }
-
- [BurstCompile]
- struct BurstedCustomAllocatorJob : IJob
- {
- [NativeDisableUnsafePtrRestriction]
- public unsafe CustomAllocatorTests.CountingAllocator* Allocator;
-
- public void Execute()
- {
- unsafe
- {
- using (var container = new NativeParallelMultiHashMap<int, int>(1, Allocator->Handle))
- {
- }
- }
- }
- }
-
- [Test]
- public unsafe void NativeParallelMultiHashMap_BurstedCustomAllocatorTest()
- {
- AllocatorManager.Initialize();
- var allocatorHelper = new AllocatorHelper<CustomAllocatorTests.CountingAllocator>(AllocatorManager.Persistent);
- ref var allocator = ref allocatorHelper.Allocator;
- allocator.Initialize();
-
- var allocatorPtr = (CustomAllocatorTests.CountingAllocator*)UnsafeUtility.AddressOf<CustomAllocatorTests.CountingAllocator>(ref allocator);
- unsafe
- {
- var handle = new BurstedCustomAllocatorJob {Allocator = allocatorPtr}.Schedule();
- handle.Complete();
- }
-
- Assert.IsTrue(allocator.WasUsed);
- allocator.Dispose();
- allocatorHelper.Dispose();
- AllocatorManager.Shutdown();
- }
-
- public struct NestedHashMap
- {
- public NativeParallelMultiHashMap<int, int> map;
- }
-
- [Test]
- public void NativeParallelMultiHashMap_Nested()
- {
- var mapInner = new NativeParallelMultiHashMap<int, int>(16, CommonRwdAllocator.Handle);
- NestedHashMap mapStruct = new NestedHashMap { map = mapInner };
-
- var mapNestedStruct = new NativeParallelMultiHashMap<int, NestedHashMap>(16, CommonRwdAllocator.Handle);
- var mapNested = new NativeParallelMultiHashMap<int, NativeParallelMultiHashMap<int, int>>(16, CommonRwdAllocator.Handle);
-
- mapNested.Add(14, mapInner);
- mapNestedStruct.Add(17, mapStruct);
-
- mapNested.Dispose();
- mapNestedStruct.Dispose();
- mapInner.Dispose();
- }
- }
|