using System; using System.Linq; using Developpez.Dotnet.Collections; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class StackSetTests { [Test] public void Test_EnumerationOrderIsLIFO() { var stack = new StackSet(); stack.Push(4); stack.Push(8); stack.Push(15); stack.Push(16); stack.Push(23); stack.Push(42); var expected = new[] { 42, 23, 16, 15, 8, 4 }; var actual = stack.ToArray(); CollectionAssert.AreEqual(expected, actual); } [Test] public void Test_PushMovesAlreadyPresentItemToTopOfStackAndReturnsFalse() { var stack = new StackSet(true); Assert.IsTrue(stack.Push(1)); Assert.IsTrue(stack.Push(2)); Assert.IsTrue(stack.Push(3)); Assert.IsFalse(stack.Push(2)); var expected = 2; var actual = stack.First(); Assert.AreEqual(expected, actual); Assert.AreEqual(3, stack.Count); CollectionAssert.AreEquivalent(new[] { 2, 3, 1}, stack); } [Test] public void Test_PushAlreadyPresentItemDoesNothingAndReturnsFalse() { var stack = new StackSet(false); Assert.IsTrue(stack.Push(1)); Assert.IsTrue(stack.Push(2)); Assert.IsTrue(stack.Push(3)); Assert.IsFalse(stack.Push(2)); var expected = 3; var actual = stack.First(); Assert.AreEqual(expected, actual); Assert.AreEqual(3, stack.Count); CollectionAssert.AreEquivalent(new[] { 3, 2, 1 }, stack); } [Test] public void Test_PeekReturnsLastAddedItemAndDoesntRemoveIt() { var stack = new StackSet(); stack.Push(1); stack.Push(2); stack.Push(3); var expected = 3; var actual = stack.Peek(); Assert.AreEqual(expected, actual); Assert.AreEqual(3, stack.Count); } [Test] public void Test_PopReturnsLastAddedItemAndRemovesIt() { var stack = new StackSet(); stack.Push(1); stack.Push(2); stack.Push(3); var expected = 3; var actual = stack.Pop(); Assert.AreEqual(expected, actual); Assert.AreEqual(2, stack.Count); expected = 2; actual = stack.Peek(); Assert.AreEqual(expected, actual); } [Test] public void Test_PeekAndPopThrowIfStackIsEmpty() { var stack = new StackSet(); Assert.Throws(() => stack.Peek()); Assert.Throws(() => stack.Pop()); } [Test] public void Test_SpecificComparerIsUsedAndLatestValueIsKept_MoveExistingToTop() { StackSet stack = new StackSet(true, StringComparer.CurrentCultureIgnoreCase); stack.Push("hello"); stack.Push("world"); stack.Push("foo"); stack.Push("bar"); // Re-push value that is already present stack.Push("HELLO"); string expected = "HELLO"; string actual = stack.Peek(); Assert.AreEqual(expected, actual); expected = "world"; actual = stack.Last(); Assert.AreEqual(expected, actual); stack.Push("FOO"); string[] expected2 = new[] { "FOO", "HELLO", "bar", "world" }; string[] actual2 = stack.ToArray(); CollectionAssert.AreEqual(expected2, actual2); } [Test] public void Test_SpecificComparerIsUsedAndOldValueIsKept_DontMoveExistingToTop() { StackSet stack = new StackSet(false, StringComparer.CurrentCultureIgnoreCase); stack.Push("hello"); stack.Push("world"); stack.Push("foo"); stack.Push("bar"); // Re-push value that is already present stack.Push("HELLO"); string expected = "hello"; string actual = stack.Last(); Assert.AreEqual(expected, actual); stack.Push("FOO"); string[] expected2 = new[] { "bar", "foo", "world", "hello" }; string[] actual2 = stack.ToArray(); CollectionAssert.AreEqual(expected2, actual2); } } }