示例#1
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();
    }
示例#2
0
 public void UnsafeHashSet_SameElement()
 {
     using (var container = new UnsafeHashSet <int>(0, Allocator.Persistent))
     {
         Assert.IsTrue(container.Add(0));
         Assert.IsFalse(container.Add(0));
     }
 }
示例#3
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);
        }
示例#4
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}");
            }
        }
    }
示例#5
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);
        }
示例#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_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();
    }
示例#8
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]);
                }
            }
示例#9
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);
        }
示例#10
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);
        }
示例#11
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);
        }
示例#12
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);
        }
示例#13
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);
        }
示例#14
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);
        }
示例#15
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);
        }
示例#16
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);
        }
示例#17
0
        public void ContainsTest()
        {
            var set = UnsafeHashSet.Allocate <int>(10);

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

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

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

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

            Assert.IsFalse(UnsafeHashSet.Contains <int>(set, 14));

            UnsafeHashSet.Free(set);
        }
示例#18
0
    public void UnsafeHashSet_Full_Throws()
    {
        var container = new UnsafeHashSet <int>(16, Allocator.Temp);

        ExpectedCount(ref container, 0);

        for (int i = 0, capacity = container.Capacity; i < capacity; ++i)
        {
            Assert.DoesNotThrow(() => { container.Add(i); });
        }
        ExpectedCount(ref container, container.Capacity);

        // Make sure overallocating throws and exception if using the Concurrent version - normal hash map would grow
        var writer = container.AsParallelWriter();

        Assert.Throws <System.InvalidOperationException>(() => { writer.Add(100); });
        ExpectedCount(ref container, container.Capacity);

        container.Clear();
        ExpectedCount(ref container, 0);

        container.Dispose();
    }
示例#19
0
        public void ExpandTest()
        {
            var initialCapacity = 7;
            var set             = UnsafeHashSet.Allocate <int>(initialCapacity, fixedSize: false);

            // 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));

            UnsafeHashSet.Add(set, 42);
            UnsafeHashSet.Add(set, 18);

            var nextCapacity = UnsafeHashCollection.GetNextPrime(initialCapacity + 1);

            Assert.AreEqual(9, UnsafeHashSet.GetCount(set));
            Assert.AreEqual(nextCapacity, UnsafeHashSet.GetCapacity(set));

            UnsafeHashSet.Free(set);
        }
示例#20
0
 public void Add(T item)
 {
     UnsafeHashSet.Add <T>(m_inner, item);
 }