コード例 #1
0
        public void RemoveMinimum(int[] keys, double errorRate)
        {
            QuikGraphAssert.TrueForAll(keys, k => k < int.MaxValue);
            Assert.IsTrue(keys.Length > 0);

            var heap = new SoftHeap <int, string>(errorRate, int.MaxValue);

            foreach (int key in keys)
            {
                heap.Add(key, key.ToString());
            }
            Assert.AreEqual(keys.Length, heap.Count);

            int lastMinimum = int.MaxValue;
            int nbError     = 0;

            while (heap.Count > 0)
            {
                KeyValuePair <int, string> pair = heap.RemoveMinimum();
                // Removed pair can be not the minimal
                // Since it's a "soft" heap that not guarantee 100% result
                // But a faster management
                if (lastMinimum < pair.Key)
                {
                    ++nbError;
                }
                lastMinimum = pair.Key;
                Assert.AreEqual(pair.Key.ToString(), pair.Value);
            }

            Assert.IsTrue(nbError / (double)keys.Length <= errorRate);
        }
コード例 #2
0
        private void Add([NotNull] int[] keys)
        {
            QuikGraphAssert.TrueForAll(keys, k => k < int.MaxValue);
            Assert.IsTrue(keys.Length > 0);

            var target = new SoftHeap <int, int>(1 / 4.0, int.MaxValue);

            foreach (int key in keys)
            {
                int count = target.Count;
                target.Add(key, key + 1);
                Assert.AreEqual(count + 1, target.Count);
            }

            int lastMin = int.MaxValue;
            int error   = 0;

            while (target.Count > 0)
            {
                var kv = target.DeleteMin();
                if (lastMin < kv.Key)
                {
                    ++error;
                }
                lastMin = kv.Key;
                Assert.AreEqual(kv.Key + 1, kv.Value);
            }

            Assert.IsTrue(error / (double)keys.Length <= target.ErrorRate);
        }
コード例 #3
0
        public void Add([PexAssumeNotNull]int[] keys)
        {
            PexAssume.TrueForAll(keys, k => k < int.MaxValue);
            PexAssume.IsTrue(keys.Length > 0);

            var target = new SoftHeap<int, int>(1/4.0, int.MaxValue);
            Console.WriteLine("expected error rate: {0}", target.ErrorRate);
            foreach (var key in keys)
            {
                var count = target.Count;
                target.Add(key, key + 1);
                Assert.AreEqual(count + 1, target.Count);
            }

            int lastMin = int.MaxValue;
            int error = 0;
            while (target.Count > 0)
            {
                var kv = target.DeleteMin();
                if (lastMin < kv.Key)
                    error++;
                lastMin = kv.Key;
                Assert.AreEqual(kv.Key + 1, kv.Value);
            }

            Console.WriteLine("error rate: {0}", error / (double)keys.Length);
            Assert.IsTrue(error / (double)keys.Length <= target.ErrorRate);
        }
コード例 #4
0
        public void EnumerateHeap()
        {
            var heap = new SoftHeap <double, int>(ErrorRate, 10.0);

            CollectionAssert.IsEmpty(heap);

            heap.Add(1.0, 1);
            // Enumerator does nothing!
            CollectionAssert.IsEmpty(heap);

            using (IEnumerator <KeyValuePair <double, int> > enumerator = heap.GetEnumerator())
            {
                Assert.AreEqual(default(KeyValuePair <double, int>), enumerator.Current);
                Assert.Throws <NotSupportedException>(() => enumerator.Reset());
            }
        }
コード例 #5
0
        public void Add_Throws()
        {
            AddThrowsTest(1);
            AddThrowsTest(new TestVertex("1"));
            AddThrowsTest(new EquatableTestVertex("1"));

            #region Local function

            void AddThrowsTest <TValue>(TValue value)
            {
                AddInternalTest();
                AddInternalTest2();

                #region Local functions

                void AddInternalTest()
                {
                    var heap = new SoftHeap <int, TValue>(
                        ErrorRate,
                        10);

                    Assert.Throws <ArgumentException>(() => heap.Add(150, value));
                }

                void AddInternalTest2()
                {
                    var heap = new SoftHeap <TestPriority, TValue>(
                        ErrorRate,
                        new TestPriority(10));

                    Assert.Throws <ArgumentException>(() => heap.Add(new TestPriority(150), value));
                    // ReSharper disable once AssignNullToNotNullAttribute
                    Assert.Throws <ArgumentNullException>(() => heap.Add(null, value));
                }

                #endregion
            }

            #endregion
        }
コード例 #6
0
 private static void AssertHeapSize <TPriority, TValue>(
     SoftHeap <TPriority, TValue> heap,
     int expectedCount)
 {
     Assert.AreEqual(expectedCount, heap.Count);
 }
コード例 #7
0
        public void Add()
        {
            AddTest(1, 2, 3);
            AddTest(
                new TestVertex("1"),
                new TestVertex("2"),
                new TestVertex("3"));
            AddTest(
                new EquatableTestVertex("1"),
                new EquatableTestVertex("2"),
                new EquatableTestVertex("3"));

            #region Local function

            void AddTest <TValue>(
                TValue value1,
                TValue value2,
                TValue value3)
            {
                AddInternalTest();
                AddInternalTest2();
                AddSamePriorityInternalTest();

                #region Local functions

                void AddInternalTest()
                {
                    var heap = new SoftHeap <int, TValue>(ErrorRate, 25);

                    AssertHeapSize(heap, 0);

                    heap.Add(1, value1);
                    AssertHeapSize(heap, 1);

                    heap.Add(1, value2);
                    AssertHeapSize(heap, 2);

                    heap.Add(1, value3);
                    AssertHeapSize(heap, 3);

                    heap.Add(int.MinValue, value1);
                    AssertHeapSize(heap, 4);

                    heap.Add(24, value3);
                    AssertHeapSize(heap, 5);
                }

                void AddInternalTest2()
                {
                    var heap = new SoftHeap <TestPriority, TValue>(ErrorRate, new TestPriority(25));

                    AssertHeapSize(heap, 0);

                    heap.Add(new TestPriority(1), value1);
                    AssertHeapSize(heap, 1);

                    heap.Add(new TestPriority(1), value2);
                    AssertHeapSize(heap, 2);

                    heap.Add(new TestPriority(1), value3);
                    AssertHeapSize(heap, 3);

                    heap.Add(new TestPriority(int.MinValue), value1);
                    AssertHeapSize(heap, 4);

                    heap.Add(new TestPriority(24), value3);
                    AssertHeapSize(heap, 5);
                }

                void AddSamePriorityInternalTest()
                {
                    var heap = new SoftHeap <int, TValue>(ErrorRate, 25);

                    AssertHeapSize(heap, 0);

                    heap.Add(1, value1);
                    AssertHeapSize(heap, 1);

                    heap.Add(1, value2);
                    AssertHeapSize(heap, 2);

                    heap.Add(1, value3);
                    AssertHeapSize(heap, 3);

                    heap.Add(1, value1);
                    AssertHeapSize(heap, 4);

                    heap.Add(1, value2);
                    AssertHeapSize(heap, 5);

                    heap.Add(1, value3);
                    AssertHeapSize(heap, 6);
                }

                #endregion
            }

            #endregion
        }