示例#1
0
        public void TryGetValueTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, int>(10);

            Assert.IsFalse(UnsafeSortedDictionary.TryGetValue(set, 1, out int value));

            UnsafeSortedDictionary.Add(set, 1, 1);
            UnsafeSortedDictionary.Add(set, 7, 2);
            UnsafeSortedDictionary.Add(set, 51, 3);
            UnsafeSortedDictionary.Add(set, 13, 4);

            Assert.IsFalse(UnsafeSortedDictionary.TryGetValue(set, 3, out value));

            Assert.IsTrue(UnsafeSortedDictionary.TryGetValue(set, 1, out value));
            Assert.AreEqual(1, value);
            Assert.IsTrue(UnsafeSortedDictionary.TryGetValue(set, 7, out value));
            Assert.AreEqual(2, value);
            Assert.IsTrue(UnsafeSortedDictionary.TryGetValue(set, 13, out value));
            Assert.AreEqual(4, value);
            Assert.IsTrue(UnsafeSortedDictionary.TryGetValue(set, 51, out value));
            Assert.AreEqual(3, value);

            Assert.IsFalse(UnsafeSortedDictionary.TryGetValue(set, 14, out value));

            UnsafeSortedDictionary.Free(set);
        }
示例#2
0
        public void IteratorTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, decimal>(10);

            // Fill set
            for (int i = 10; i >= 0; i--)
            {
                // Add in reverse order
                UnsafeSortedDictionary.Add <int, decimal>(set, i, i * i);
            }

            var enumerator = UnsafeSortedDictionary.GetEnumerator <int, decimal>(set);

            for (int i = 0; i < 10; i++)
            {
                enumerator.MoveNext();
                Assert.AreEqual(i, enumerator.CurrentKey);
                Assert.AreEqual(i * i, enumerator.CurrentValue);

                Assert.AreEqual(i, enumerator.Current.Key);
                Assert.AreEqual(i * i, enumerator.Current.Value);
            }

            UnsafeSortedDictionary.Free(set);
        }
示例#3
0
        public void CopyToTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, bool>(10);

            // Fill set
            for (int i = 10; i >= 0; i--)
            {
                UnsafeSortedDictionary.Add(set, i, i % 2 == 0);
            }

            var count = UnsafeSortedDictionary.GetCount(set);
            var arr   = new KeyValuePair <int, bool> [count];

            UnsafeSortedDictionary.CopyTo(set, arr, 0);

            // Check
            int num = 0;

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

            UnsafeSortedDictionary.Free(set);
        }
示例#4
0
        public void AddRandomTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, double>(10);

            Random r = new Random();

            for (int i = 0; i < 10; i++)
            {
                UnsafeSortedDictionary.Add(set, r.Next(), r.NextDouble());
            }

            var arr = new KeyValuePair <int, double> [10];

            UnsafeSortedDictionary.CopyTo(set, arr, 0);

            // Validate values are from low to high
            var last = arr[0];

            for (int i = 1; i < 10; i++)
            {
                Assert.IsTrue(last.Key <= arr[i].Key);
                last = arr[i++];
            }

            UnsafeSortedDictionary.Free(set);
        }
示例#5
0
        public void ConstructorTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, bool>(10);

            Assert.AreEqual(0, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(10, UnsafeSortedDictionary.GetCapacity(set));

            UnsafeSortedDictionary.Free(set);
        }
示例#6
0
        public void ClearTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, float>(16, fixedSize: false);

            // Add some random data
            Random r = new Random();

            for (int i = 0; i < 10; i++)
            {
                UnsafeSortedDictionary.Add <int, float>(set, r.Next(), (float)r.NextDouble());
            }

            // Verify data has been added
            Assert.AreEqual(10, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(16, UnsafeSortedDictionary.GetCapacity(set));

            // Clear set and verify it's cleared
            UnsafeSortedDictionary.Clear(set);

            Assert.AreEqual(0, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(16, UnsafeSortedDictionary.GetCapacity(set));


            // Validate we can still add data and have it be valid
            // Add data to cleared set
            for (int i = 10; i >= 0; i--)
            {
                UnsafeSortedDictionary.Add(set, i, 41f);
            }

            var count = UnsafeSortedDictionary.GetCount(set);
            var arr   = new KeyValuePair <int, float> [count];

            UnsafeSortedDictionary.CopyTo(set, arr, 0);

            // Validate data has been written
            int num = 0;

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

            Assert.AreEqual(count, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(16, UnsafeSortedDictionary.GetCapacity(set));

            UnsafeSortedDictionary.Free(set);
        }
示例#7
0
        public void ContainsValueTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, int>(10);

            Assert.IsFalse(UnsafeSortedDictionary.ContainsKey <int>(set, 1));

            UnsafeSortedDictionary.Add(set, 1, 5);
            UnsafeSortedDictionary.Add(set, 2, 2);
            UnsafeSortedDictionary.Add(set, 3, 38);

            Assert.IsTrue(UnsafeSortedDictionary.ContainsValue <int>(set, 2));
            Assert.IsTrue(UnsafeSortedDictionary.ContainsValue <int>(set, 38));
            Assert.IsFalse(UnsafeSortedDictionary.ContainsValue <int>(set, 1));

            UnsafeSortedDictionary.Free(set);
        }
示例#8
0
        public void InvalidTypeTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, bool>(10);

            // Test key
            Assert.Catch <AssertException>(() =>
            {
                UnsafeSortedDictionary.Add <float, bool>(set, 4, true);
            });

            // Test value
            Assert.Catch <AssertException>(() =>
            {
                UnsafeSortedDictionary.Add <int, float>(set, 4, 4.1f);
            });

            UnsafeSortedDictionary.Free(set);
        }
示例#9
0
        public void ExpandTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, short>(8, fixedSize: false);

            // Valid adds
            for (int i = 0; i < 8; i++)
            {
                UnsafeSortedDictionary.Add <int, short>(set, i + 1, 2);
            }

            Assert.AreEqual(8, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(8, UnsafeSortedDictionary.GetCapacity(set));

            UnsafeSortedDictionary.Add <int, short>(set, 42, 12);

            Assert.AreEqual(9, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(16, UnsafeSortedDictionary.GetCapacity(set));

            UnsafeSortedDictionary.Free(set);
        }
示例#10
0
        public void ExpandFailedTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, float>(8, true);

            // Valid adds
            for (int i = 0; i < 8; i++)
            {
                UnsafeSortedDictionary.Add <int, float>(set, i + 1, i * i);
            }

            Assert.AreEqual(8, UnsafeSortedDictionary.GetCount(set));
            Assert.AreEqual(8, UnsafeSortedDictionary.GetCapacity(set));

            Assert.Throws <InvalidOperationException>(() =>
            {
                UnsafeSortedDictionary.Add <int, float>(set, 42, 13.3f);
            });

            UnsafeSortedDictionary.Free(set);
        }
示例#11
0
        public void RemoveTest()
        {
            var set = UnsafeSortedDictionary.Allocate <int, int>(10);

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

            UnsafeSortedDictionary.Add(set, 1, 3);
            UnsafeSortedDictionary.Add(set, 7, 3);
            UnsafeSortedDictionary.Add(set, 51, 3);
            UnsafeSortedDictionary.Add(set, 13, 3);

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

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

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

            UnsafeSortedDictionary.Free(set);
        }