Skip to content

Commit

Permalink
✅ Add priority queue enumeration test (#226)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcscharf authored Jun 3, 2021
1 parent 9196603 commit 25bcaad
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion Bearded.Utilities.Tests/Collections/PriorityQueueTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using Bearded.Utilities.Collections;
using Bearded.Utilities.Linq;
using FluentAssertions;
using FsCheck.Xunit;
using Xunit;

namespace Bearded.Utilities.Tests.Collections
Expand Down Expand Up @@ -74,5 +77,35 @@ public void TestIncreasePriority()
q.Enqueue(2, "item");
Assert.Throws<InvalidOperationException>(() => q.DecreasePriority("item", 4));
}

[Property]
public void TestEnumerationContainsCorrectItems(int seed, byte itemsToEnumerate, byte otherItems)
{
var random = new System.Random(seed);
var totalItems = itemsToEnumerate + otherItems;
var q = new PriorityQueue<double, int>();
var items = Enumerable
.Range(0, totalItems)
.Select(i => KeyValuePair.Create(random.NextDouble(), i))
.Shuffled();
foreach (var (priority, value) in items)
{
q.Enqueue(priority, value);
}
for (var i = 0; i < otherItems; i++)
{
q.Dequeue();
}

// ReSharper disable once RedundantCast to make sure we're not using a ToList() that we may add to the queue
var enumerated = ((IEnumerable<KeyValuePair<double, int>>)q).ToList();

enumerated.Should().HaveCount(itemsToEnumerate).And.BeSubsetOf(items);
while (q.Count > 0)
{
var dequeued = q.Dequeue();
enumerated.Should().Contain(dequeued);
}
}
}
}
}

0 comments on commit 25bcaad

Please sign in to comment.