Пример #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 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);
        }
Пример #3
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);
        }
Пример #4
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);
        }
Пример #5
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);
        }
Пример #6
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);
        }
Пример #7
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);
        }
Пример #8
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);
        }
Пример #9
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);
        }