예제 #1
0
        public void Heaps_should_be_merged_correctly(HeapConfiguration conf)
        {
            var iterations = conf.CalculateIterations(5000);

            for (var seed = 0; seed < iterations; ++seed)
            {
                // Arrange
                var random         = new Random(seed);
                var keys1          = conf.GenerateKeys(random);
                var secondHeapSize = random.Next(conf.HeapSize / 2, conf.HeapSize * 2);
                var keys2          = conf.GenerateKeys(random, secondHeapSize);

                var heap1 = CreateHeapInstance();
                var heap2 = CreateHeapInstance();

                foreach (var key in keys1)
                {
                    heap1.Add(key, Guid.NewGuid().ToString());
                }

                foreach (var key in keys2)
                {
                    heap2.Add(key, Guid.NewGuid().ToString());
                }

                var mixedKeys = keys1.Concat(keys2).ToList();

                // Arrange
                heap1.Merge(heap2);

                // Assert
                this.Pop_until_empty(heap1, mixedKeys);
            }
        }
예제 #2
0
        public void Heap_enumeration_should_yield_all_elements(HeapConfiguration conf)
        {
            var iterations = conf.CalculateIterations(1000);

            for (var seed = 0; seed < iterations; ++seed)
            {
                // Arrange
                var random = new Random(seed);
                var keys   = conf.GenerateKeys(random);
                var heap   = CreateHeapInstance();

                var handles = keys
                              .Select(k => heap.Add(k, Guid.NewGuid().ToString()));

                var expected = handles
                               .Select(x => new KeyValuePair <int, string>(x.Key, x.Value))
                               .OrderBy(x => x.Value)
                               .ToArray();

                // Act
                var actual = heap
                             .Select(x => new KeyValuePair <int, string>(x.Key, x.Value))
                             .OrderBy(x => x.Value)
                             .ToArray();

                // Assert
                Assert.True(expected.SequenceEqual(actual));
            }
        }
예제 #3
0
        public void Elements_should_be_updated_correctly(HeapConfiguration conf)
        {
            for (var seed = 0; seed < 15; ++seed)
            {
                // Arrange
                var random = new Random(seed);
                var keys   = conf.GenerateKeys(random);
                var heap   = CreateHeapInstance();

                var handles = keys
                              .Select(k => new HandleKeyPair(heap.Add(k, Guid.NewGuid().ToString()), k))
                              .ToList();

                for (var i = 0; i < 15000; ++i)
                {
                    var handleIndex = random.Next(0, conf.HeapSize);
                    var newKey      = random.Next(conf.KeyLimit);
                    var handle      = handles[handleIndex];

                    // Act
                    heap.Update(handle.Node, newKey);
                    handles[handleIndex].Key = newKey;

                    // Assert
                    Assert.Equal(Peek(handles), heap.Peek().Key);
                }
            }
        }
예제 #4
0
        public void Top_should_be_properly_maintained_after_addition(HeapConfiguration conf)
        {
            var iterations = conf.CalculateIterations(15000);

            for (var seed = 0; seed < iterations; ++seed)
            {
                // Arrange
                var keys  = conf.GenerateKeys(new Random(seed));
                var heap  = CreateHeapInstance();
                var top   = int.MaxValue;
                var count = 0;

                foreach (var key in keys)
                {
                    if (key < top)
                    {
                        top = key;
                    }

                    // Act
                    heap.Add(key, Guid.NewGuid().ToString());
                    ++count;

                    // Assert
                    Assert.Equal(top, heap.Peek().Key);
                    Assert.Equal(count, heap.Count);
                }
            }
        }
예제 #5
0
        public void Elements_should_be_removed_correctly(HeapConfiguration conf)
        {
            var iterations = conf.CalculateIterations(5000);

            for (var seed = 0; seed < iterations; ++seed)
            {
                // Arrange
                var random = new Random(seed);
                var keys   = conf.GenerateKeys(random);

                for (var toRemove = 0; toRemove < conf.HeapSize; ++toRemove)
                {
                    var heap  = CreateHeapInstance();
                    var count = conf.HeapSize;

                    var handles = keys
                                  .Select(k => new HandleKeyPair(heap.Add(k, Guid.NewGuid().ToString()), k))
                                  .ToList();

                    // Act & Assert

                    // Part I - remove one node
                    Assert.Equal(Peek(handles), heap.Peek().Key);
                    Assert.Equal(count--, heap.Count);

                    heap.Remove(handles[toRemove].Node);
                    handles.RemoveAt(toRemove);

                    if (count == 0)
                    {
                        Assert.Throws <InvalidOperationException>(() => heap.Peek());
                    }
                    else
                    {
                        Assert.Equal(Peek(handles), heap.Peek().Key);
                        Assert.Equal(count, heap.Count);
                    }

                    // Part II - pop until empty
                    while (!heap.IsEmpty)
                    {
                        var min = handles.MinBy(x => x.Key);
                        handles.Remove(min);

                        Assert.Equal(min.Key, heap.Pop().Key);
                        Assert.Equal(--count, heap.Count);
                    }

                    Assert.True(heap.IsEmpty);
                    Assert.Equal(0, count);
                }
            }
        }
예제 #6
0
        public void Elements_should_be_popped_correctly(HeapConfiguration conf)
        {
            var iterations = conf.CalculateIterations(5000);

            for (var seed = 0; seed < iterations; ++seed)
            {
                // Arrange
                var keys  = conf.GenerateKeys(new Random(seed));
                var heap  = CreateHeapInstance();
                var count = 0;

                foreach (var key in keys)
                {
                    Assert.Equal(count, heap.Count);
                    heap.Add(key, Guid.NewGuid().ToString());
                    Assert.Equal(++count, heap.Count);
                }

                // Act & Assert
                this.Pop_until_empty(heap, keys);
            }
        }
예제 #7
0
        public void All_elements_should_be_removed_correctly(HeapConfiguration conf)
        {
            var iterations = conf.CalculateIterations(5000);

            for (var seed = 0; seed < iterations; ++seed)
            {
                // Arrange
                var random = new Random(seed);
                var keys   = conf.GenerateKeys(random);
                var heap   = CreateHeapInstance();
                var count  = conf.HeapSize;

                var handles = keys
                              .Select(k => new HandleKeyPair(heap.Add(k, Guid.NewGuid().ToString()), k))
                              .ToList();

                // Act & Assert
                while (!heap.IsEmpty)
                {
                    Assert.Equal(Peek(handles), heap.Peek().Key);
                    Assert.Equal(count, heap.Count);

                    var handle = handles[random.Next(count--)];
                    handles.Remove(handle);
                    heap.Remove(handle.Node);

                    if (count == 0)
                    {
                        Assert.Throws <InvalidOperationException>(() => heap.Peek());
                    }
                    else
                    {
                        Assert.Equal(Peek(handles), heap.Peek().Key);
                        Assert.Equal(count, heap.Count);
                    }
                }
            }
        }