Пример #1
0
        public void CopyToTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            // Fill set
            for (int i = 0; i < 10; i++)
            {
                UnsafeHashSet.Add(set, i);
            }

            var  count = UnsafeHashSet.GetCount(set);
            int *arr   = stackalloc int[count];

            UnsafeHashSet.CopyTo <int>(set, arr, 0);

            // Check
            int num = 0;

            for (int i = 0; i < count; i++)
            {
                Assert.AreEqual(i, arr[num++]);
            }

            UnsafeHashSet.Free(set);
        }
Пример #2
0
    public void UnsafeHashSet_ForEach([Values(10, 1000)] int n)
    {
        var seen = new NativeArray <int>(n, Allocator.Temp);

        using (var container = new UnsafeHashSet <int>(32, Allocator.TempJob))
        {
            for (int i = 0; i < n; i++)
            {
                container.Add(i);
            }

            var count = 0;
            foreach (var item in container)
            {
                Assert.True(container.Contains(item));
                seen[item] = seen[item] + 1;
                ++count;
            }

            Assert.AreEqual(container.Count(), count);
            for (int i = 0; i < n; i++)
            {
                Assert.AreEqual(1, seen[i], $"Incorrect item count {i}");
            }
        }
    }
Пример #3
0
        public void ClearHashSet()
        {
            var set = UnsafeHashSet.Allocate <int>(3);

            UnsafeHashSet.Add(set, 1);
            UnsafeHashSet.Add(set, 2);
            UnsafeHashSet.Add(set, 3);

            Assert.IsTrue(UnsafeHashSet.Contains(set, 2));
            Assert.AreEqual(3, UnsafeHashSet.GetCount(set));

            UnsafeHashSet.Add(set, 4);
            Assert.AreEqual(4, UnsafeHashSet.GetCount(set));

            UnsafeHashSet.Clear(set);
            Assert.AreEqual(0, UnsafeHashSet.GetCount(set));
            Assert.IsFalse(UnsafeHashSet.Contains(set, 2));

            UnsafeHashSet.Add(set, 4);
            Assert.AreEqual(1, UnsafeHashSet.GetCount(set));
            Assert.IsTrue(UnsafeHashSet.Contains(set, 4));

            UnsafeHashSet.Clear(set);
            Assert.AreEqual(0, UnsafeHashSet.GetCount(set));

            UnsafeHashSet.Free(set);
        }
Пример #4
0
    public void UnsafeHashSet_EIU_UnionWith()
    {
        var setA = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
            0, 1, 2, 3, 4, 5
        };
        var setB = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
            3, 4, 5, 6, 7, 8
        };

        setA.UnionWith(setB);

        ExpectedCount(ref setA, 9);
        Assert.True(setA.Contains(0));
        Assert.True(setA.Contains(1));
        Assert.True(setA.Contains(2));
        Assert.True(setA.Contains(3));
        Assert.True(setA.Contains(4));
        Assert.True(setA.Contains(5));
        Assert.True(setA.Contains(6));
        Assert.True(setA.Contains(7));
        Assert.True(setA.Contains(8));

        setA.Dispose();
        setB.Dispose();
    }
Пример #5
0
    public void UnsafeHashSet_Collisions()
    {
        var container = new UnsafeHashSet <int>(16, Allocator.Temp);

        Assert.IsFalse(container.Contains(0), "Contains on empty hash map did not fail");
        ExpectedCount(ref container, 0);

        // Make sure inserting values work
        for (int i = 0; i < 8; ++i)
        {
            Assert.IsTrue(container.Add(i), "Failed to add value");
        }
        ExpectedCount(ref container, 8);

        // The bucket size is capacity * 2, adding that number should result in hash collisions
        for (int i = 0; i < 8; ++i)
        {
            Assert.IsTrue(container.Add(i + 32), "Failed to add value with potential hash collision");
        }

        // Make sure reading the inserted values work
        for (int i = 0; i < 8; ++i)
        {
            Assert.IsTrue(container.Contains(i), "Failed get value from hash set");
        }

        for (int i = 0; i < 8; ++i)
        {
            Assert.IsTrue(container.Contains(i + 32), "Failed get value from hash set");
        }

        container.Dispose();
    }
Пример #6
0
        public void InvalidTypeTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            Assert.Catch <AssertException>(() => { UnsafeHashSet.Add <float>(set, 4); });

            UnsafeHashSet.Free(set);
        }
Пример #7
0
 public void UnsafeHashSet_SameElement()
 {
     using (var container = new UnsafeHashSet <int>(0, Allocator.Persistent))
     {
         Assert.IsTrue(container.Add(0));
         Assert.IsFalse(container.Add(0));
     }
 }
Пример #8
0
    public void UnsafeHashSet_RemoveOnEmptyMap_DoesNotThrow()
    {
        var container = new UnsafeHashSet <int>(0, Allocator.Temp);

        Assert.DoesNotThrow(() => container.Remove(0));
        Assert.DoesNotThrow(() => container.Remove(-425196));
        container.Dispose();
    }
        /// <summary>
        /// Returns managed array populated with elements from the container.
        /// </summary>
        /// <typeparam name="T">Source type of elements</typeparam>
        /// <param name="container">The container to perform conversion to array.</param>
        /// <returns>Array of elements of the container.</returns>
        public static T[] ToArray <T>(this UnsafeHashSet <T> container)
            where T : unmanaged, IEquatable <T>
        {
            var array   = container.ToNativeArray(Allocator.TempJob);
            var managed = array.ToArray();

            array.Dispose();
            return(managed);
        }
Пример #10
0
        public void ConstructorTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            Assert.AreEqual(0, UnsafeHashSet.GetCount(set));
            // Next expected prime is 17
            Assert.AreEqual(17, UnsafeHashSet.GetCapacity(set));

            UnsafeHashSet.Free(set);
        }
Пример #11
0
            public void Execute()
            {
                var indices = new UnsafeHashSet <int>(128, Allocator.Temp);

                for (int i = 0; i < Chunks.Length; i++)
                {
                    HandleChunk(Chunks[i].m_Chunk, ref indices);
                }

                SharedComponentIndices.AddRange(indices.ToNativeArray(Allocator.Temp));
            }
Пример #12
0
            void HandleChunk(Chunk *srcChunk, ref UnsafeHashSet <int> indices)
            {
                var srcArchetype  = srcChunk->Archetype;
                var n             = srcArchetype->NumSharedComponents;
                var sharedIndices = stackalloc int[srcArchetype->NumSharedComponents];

                srcChunk->SharedComponentValues.CopyTo(sharedIndices, 0, srcArchetype->NumSharedComponents);
                for (int i = 0; i < n; i++)
                {
                    indices.Add(sharedIndices[i]);
                }
            }
Пример #13
0
    public void UnsafeHashSet_Capacity()
    {
        var container = new UnsafeHashSet <int>(0, Allocator.Persistent);

        Assert.IsTrue(container.IsEmpty);
        Assert.AreEqual(0, container.Capacity);

        container.Capacity = 10;
        Assert.AreEqual(10, container.Capacity);

        container.Dispose();
    }
Пример #14
0
        public void UnionWithTest()
        {
            var setEven = GetOddEvenSet(false);
            var setOdd  = GetOddEvenSet(true);

            UnsafeHashSet.UnionWith <int>(setEven, setOdd);

            // Resulting collection should contain both sets
            Assert.AreEqual(10, UnsafeHashSet.GetCount(setEven));

            UnsafeHashSet.Free(setEven);
            UnsafeHashSet.Free(setOdd);
        }
Пример #15
0
        public void IntersectsWithTest()
        {
            var setEven = GetOddEvenSet(false);
            var setOdd  = GetOddEvenSet(true);

            UnsafeHashSet.IntersectsWith <int>(setEven, setOdd);

            // Resulting collection should be empty.
            Assert.AreEqual(0, UnsafeHashSet.GetCount(setEven));

            UnsafeHashSet.Free(setEven);
            UnsafeHashSet.Free(setOdd);
        }
Пример #16
0
        public void AddDuplicateTest()
        {
            var set = UnsafeHashSet.Allocate <int>(3);

            Assert.IsTrue(UnsafeHashSet.Add(set, 5));
            Assert.IsTrue(UnsafeHashSet.Add(set, 8));
            Assert.IsTrue(UnsafeHashSet.Add(set, 9));
            Assert.IsFalse(UnsafeHashSet.Add(set, 5));

            Assert.AreEqual(3, UnsafeHashSet.GetCapacity(set));

            UnsafeHashSet.Free(set);
        }
Пример #17
0
        public void ExceptWithTest()
        {
            var setEven = GetOddEvenSet(false);
            var setOdd  = GetOddEvenSet(true);

            UnsafeHashSet.ExceptWith <int>(setEven, setOdd);

            // Resulting collection should only contain Even
            Assert.AreEqual(5, UnsafeHashSet.GetCount(setEven));

            UnsafeHashSet.Free(setEven);
            UnsafeHashSet.Free(setOdd);
        }
Пример #18
0
        public void SymmetricExceptTest()
        {
            var setEven = GetOddEvenSet(false);
            var setOdd  = GetOddEvenSet(true);

            UnsafeHashSet.SymmetricExcept <int>(setEven, setOdd);

            // Resulting collection should contain both (XOr)
            Assert.AreEqual(10, UnsafeHashSet.GetCount(setEven));

            UnsafeHashSet.Free(setEven);
            UnsafeHashSet.Free(setOdd);
        }
Пример #19
0
        private UnsafeHashSet *GetOddEvenSet(bool isOdd = false)
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            int num = isOdd ? 1 : 0;

            for (int i = 0; i < 5; i++)
            {
                UnsafeHashSet.Add(set, num);
                num += 2;
            }

            return(set);
        }
 public void UnsafeHashSet_ToArray()
 {
     using (var set = new UnsafeHashSet <int>(8, Allocator.TempJob)
     {
         0, 1, 2, 3, 4, 5
     })
     {
         var array = set.ToArray();
         Array.Sort(array);
         for (int i = 0, num = set.Count(); i < num; i++)
         {
             Assert.AreEqual(array[i], i);
         }
     }
 }
Пример #21
0
        public void AddHashCollisionTest()
        {
            // Tests linked-list functionality when hash collisions occur.
            var set = UnsafeHashSet.Allocate <DuplicateKey>(3);

            Assert.IsTrue(UnsafeHashSet.Add(set, new DuplicateKey(1)));
            Assert.IsTrue(UnsafeHashSet.Add(set, new DuplicateKey(2)));
            Assert.IsTrue(UnsafeHashSet.Add(set, new DuplicateKey(3)));
            Assert.IsFalse(UnsafeHashSet.Add(set, new DuplicateKey(1)));

            Assert.IsTrue(UnsafeHashSet.Contains(set, new DuplicateKey(2)));

            Assert.AreEqual(3, UnsafeHashSet.GetCapacity(set));

            UnsafeHashSet.Free(set);
        }
Пример #22
0
    public void UnsafeHashSet_EIU_UnionWith_Empty()
    {
        var setA = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
        };
        var setB = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
        };

        setA.UnionWith(setB);

        ExpectedCount(ref setA, 0);

        setA.Dispose();
        setB.Dispose();
    }
Пример #23
0
    public void UnsafeHashSet_IsEmpty()
    {
        var container = new UnsafeHashSet <int>(0, Allocator.Persistent);

        Assert.IsTrue(container.IsEmpty);

        Assert.IsTrue(container.Add(0));
        Assert.IsFalse(container.IsEmpty);
        Assert.AreEqual(1, container.Capacity);
        ExpectedCount(ref container, 1);

        container.Remove(0);
        Assert.IsTrue(container.IsEmpty);

        Assert.IsTrue(container.Add(0));
        container.Clear();
        Assert.IsTrue(container.IsEmpty);

        container.Dispose();
    }
Пример #24
0
        public void IteratorTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            // Fill set
            for (int i = 0; i < 10; i++)
            {
                UnsafeHashSet.Add(set, i);
            }

            var enumerator = UnsafeHashSet.GetEnumerator <int>(set);

            for (int i = 0; i < 10; i++)
            {
                enumerator.MoveNext();
                Assert.AreEqual(i, enumerator.Current);
            }

            UnsafeHashSet.Free(set);
        }
Пример #25
0
        public void CopyHashCollisionTest()
        {
            // Tests linked-list functionality when hash collisions occur.
            var set = UnsafeHashSet.Allocate <DuplicateKey>(3);

            Assert.IsTrue(UnsafeHashSet.Add(set, new DuplicateKey(1)));
            Assert.IsTrue(UnsafeHashSet.Add(set, new DuplicateKey(2)));
            Assert.IsTrue(UnsafeHashSet.Add(set, new DuplicateKey(3)));

            var arr = stackalloc DuplicateKey[3];

            UnsafeHashSet.CopyTo <DuplicateKey>(set, arr, 0);

            for (int i = 0; i < 3; i++)
            {
                Assert.AreEqual(new DuplicateKey(i + 1), arr[i]);
            }

            UnsafeHashSet.Free(set);
        }
Пример #26
0
        public void AddTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            for (int i = 0; i < 10; i++)
            {
                UnsafeHashSet.Add <int>(set, i * i * i);
            }

            int *arr = stackalloc int[10];

            UnsafeHashSet.CopyTo <int>(set, arr, 0);

            for (int i = 0; i < 10; i++)
            {
                Assert.AreEqual(i * i * i, arr[i]);
            }

            UnsafeHashSet.Free(set);
        }
Пример #27
0
        public void ExpandFailedTest()
        {
            var initialCapacity = 7;
            var set             = UnsafeHashSet.Allocate <int>(initialCapacity, true);

            // Valid adds
            for (int i = 0; i < initialCapacity; i++)
            {
                UnsafeHashSet.Add(set, i + 1);
            }

            Assert.AreEqual(initialCapacity, UnsafeHashSet.GetCount(set));
            Assert.AreEqual(initialCapacity, UnsafeHashSet.GetCapacity(set));

            Assert.Throws <InvalidOperationException>(() =>
            {
                UnsafeHashSet.Add(set, 42);
            });

            UnsafeHashSet.Free(set);
        }
Пример #28
0
    public void UnsafeHashSet_EIU_ExceptWith_BxA()
    {
        var setA = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
            0, 1, 2, 3, 4, 5
        };
        var setB = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
            3, 4, 5, 6, 7, 8
        };

        setB.ExceptWith(setA);

        ExpectedCount(ref setB, 3);
        Assert.True(setB.Contains(6));
        Assert.True(setB.Contains(7));
        Assert.True(setB.Contains(8));

        setA.Dispose();
        setB.Dispose();
    }
Пример #29
0
    public void UnsafeHashSet_EIU_IntersectWith()
    {
        var setA = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
            0, 1, 2, 3, 4, 5
        };
        var setB = new UnsafeHashSet <int>(8, Allocator.TempJob)
        {
            3, 4, 5, 6, 7, 8
        };

        setA.IntersectWith(setB);

        ExpectedCount(ref setA, 3);
        Assert.True(setA.Contains(3));
        Assert.True(setA.Contains(4));
        Assert.True(setA.Contains(5));

        setA.Dispose();
        setB.Dispose();
    }
Пример #30
0
        public void RemoveTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

            Assert.IsFalse(UnsafeHashSet.Remove <int>(set, 1));

            UnsafeHashSet.Add(set, 1);
            UnsafeHashSet.Add(set, 7);
            UnsafeHashSet.Add(set, 51);
            UnsafeHashSet.Add(set, 13);

            Assert.IsFalse(UnsafeHashSet.Remove <int>(set, 3));

            Assert.IsTrue(UnsafeHashSet.Remove <int>(set, 1));
            Assert.IsTrue(UnsafeHashSet.Remove <int>(set, 7));
            Assert.IsTrue(UnsafeHashSet.Remove <int>(set, 13));
            Assert.IsTrue(UnsafeHashSet.Remove <int>(set, 51));

            Assert.IsFalse(UnsafeHashSet.Remove <int>(set, 13));

            UnsafeHashSet.Free(set);
        }