Skip to content

Commit 25bcaad

Browse files
authored
✅ Add priority queue enumeration test (#226)
1 parent 9196603 commit 25bcaad

1 file changed

Lines changed: 34 additions & 1 deletion

File tree

Bearded.Utilities.Tests/Collections/PriorityQueueTests.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using Bearded.Utilities.Collections;
5+
using Bearded.Utilities.Linq;
6+
using FluentAssertions;
7+
using FsCheck.Xunit;
58
using Xunit;
69

710
namespace Bearded.Utilities.Tests.Collections
@@ -74,5 +77,35 @@ public void TestIncreasePriority()
7477
q.Enqueue(2, "item");
7578
Assert.Throws<InvalidOperationException>(() => q.DecreasePriority("item", 4));
7679
}
80+
81+
[Property]
82+
public void TestEnumerationContainsCorrectItems(int seed, byte itemsToEnumerate, byte otherItems)
83+
{
84+
var random = new System.Random(seed);
85+
var totalItems = itemsToEnumerate + otherItems;
86+
var q = new PriorityQueue<double, int>();
87+
var items = Enumerable
88+
.Range(0, totalItems)
89+
.Select(i => KeyValuePair.Create(random.NextDouble(), i))
90+
.Shuffled();
91+
foreach (var (priority, value) in items)
92+
{
93+
q.Enqueue(priority, value);
94+
}
95+
for (var i = 0; i < otherItems; i++)
96+
{
97+
q.Dequeue();
98+
}
99+
100+
// ReSharper disable once RedundantCast to make sure we're not using a ToList() that we may add to the queue
101+
var enumerated = ((IEnumerable<KeyValuePair<double, int>>)q).ToList();
102+
103+
enumerated.Should().HaveCount(itemsToEnumerate).And.BeSubsetOf(items);
104+
while (q.Count > 0)
105+
{
106+
var dequeued = q.Dequeue();
107+
enumerated.Should().Contain(dequeued);
108+
}
109+
}
77110
}
78-
}
111+
}

0 commit comments

Comments
 (0)