Exemple #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();
    }
Exemple #2
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();
    }
Exemple #3
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}");
            }
        }
    }
Exemple #4
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);
        }
Exemple #5
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();
    }
Exemple #6
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();
    }
Exemple #7
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);
        }
Exemple #8
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);
        }
Exemple #9
0
 public bool Contains(T item)
 {
     return(UnsafeHashSet.Contains <T>(m_inner, item));
 }