From 0306a8de6c430e3670f0ecf57d3b6c930d8665aa Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Fri, 26 Feb 2016 22:41:14 +0000 Subject: [PATCH] Add tests and make minor fixes to PriorityQueue. --- OpenRA.Game/Primitives/PriorityQueue.cs | 14 +++--- OpenRA.Test/OpenRA.Game/PriorityQueueTest.cs | 48 ++++++++++++++++++++ OpenRA.Test/OpenRA.Test.csproj | 1 + 3 files changed, 57 insertions(+), 6 deletions(-) create mode 100644 OpenRA.Test/OpenRA.Game/PriorityQueueTest.cs diff --git a/OpenRA.Game/Primitives/PriorityQueue.cs b/OpenRA.Game/Primitives/PriorityQueue.cs index 7a3b6d83de..54c313c696 100644 --- a/OpenRA.Game/Primitives/PriorityQueue.cs +++ b/OpenRA.Game/Primitives/PriorityQueue.cs @@ -76,17 +76,19 @@ namespace OpenRA.Primitives return At(lastLevel, lastIndex); } - public T Peek() { return At(0, 0); } + public T Peek() + { + if (level <= 0 && index <= 0) + throw new InvalidOperationException("PriorityQueue empty."); + return At(0, 0); + } + public T Pop() { - if (level == 0 && index == 0) - throw new InvalidOperationException("Attempting to pop empty PriorityQueue"); - - var ret = At(0, 0); + var ret = Peek(); BubbleInto(0, 0, Last()); if (--index < 0) index = (1 << --level) - 1; - return ret; } diff --git a/OpenRA.Test/OpenRA.Game/PriorityQueueTest.cs b/OpenRA.Test/OpenRA.Game/PriorityQueueTest.cs new file mode 100644 index 0000000000..dc0c461382 --- /dev/null +++ b/OpenRA.Test/OpenRA.Game/PriorityQueueTest.cs @@ -0,0 +1,48 @@ +#region Copyright & License Information +/* + * Copyright 2007-2016 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. For more + * information, see COPYING. + */ +#endregion + +using System; +using NUnit.Framework; +using OpenRA.Primitives; + +namespace OpenRA.Test +{ + [TestFixture] + class PriorityQueueTest + { + [TestCase(TestName = "PriorityQueue maintains invariants when adding and removing items.")] + public void PriorityQueueGeneralTest() + { + var queue = new PriorityQueue(); + + Assert.IsTrue(queue.Empty, "New queue should start out empty."); + Assert.Throws(() => queue.Peek(), "Peeking at an empty queue should throw."); + Assert.Throws(() => queue.Pop(), "Popping an empty queue should throw."); + + foreach (var value in new[] { 4, 3, 5, 1, 2 }) + { + queue.Add(value); + Assert.IsFalse(queue.Empty, "Queue should not be empty - items have been added."); + } + + foreach (var value in new[] { 1, 2, 3, 4, 5 }) + { + Assert.AreEqual(value, queue.Peek(), "Peek returned the wrong item - should be in order."); + Assert.IsFalse(queue.Empty, "Queue should not be empty yet."); + Assert.AreEqual(value, queue.Pop(), "Pop returned the wrong item - should be in order."); + } + + Assert.IsTrue(queue.Empty, "Queue should now be empty."); + Assert.Throws(() => queue.Peek(), "Peeking at an empty queue should throw."); + Assert.Throws(() => queue.Pop(), "Popping an empty queue should throw."); + } + } +} \ No newline at end of file diff --git a/OpenRA.Test/OpenRA.Test.csproj b/OpenRA.Test/OpenRA.Test.csproj index 0150735602..3d43054cc6 100644 --- a/OpenRA.Test/OpenRA.Test.csproj +++ b/OpenRA.Test/OpenRA.Test.csproj @@ -50,6 +50,7 @@ +