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_PushMovesAlreadyPresentItemToTopOfStack() { var stack = new StackSet(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(2); var expected = 2; var actual = stack.First(); Assert.AreEqual(expected, actual); Assert.AreEqual(3, stack.Count); } [Test] public void Test_PeekReturnsFirstAddedItemAndDoesntRemoveIt() { 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_PopReturnsFirstAddedItemAndRemovesIt() { 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() { StackSet stack = new StackSet(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); } } }