Advertisement
yahorrr

Untitled

Nov 28th, 2022
794
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.83 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using GenericStackTask;
  4. using NUnit.Framework;
  5.  
  6. namespace GeneticStackTask.Tests
  7. {
  8.     [TestFixture(new[] { 12, 3, 4, int.MaxValue, int.MinValue, -12, 45, 12 }, 67, 8, TypeArgs = new Type[] { typeof(int) })]
  9.     [TestFixture(new[] { 0.362, 3.0987, -198.4, -1008245.78, 9012.0001 }, 0.8911, 5, TypeArgs = new Type[] { typeof(double) })]
  10.     [TestFixture(new[] { "12", "Zero", "Test", "Hello" }, "Hello, world!", 4, TypeArgs = new Type[] { typeof(string) })]
  11.     [TestFixture(new[] { "12", "Zero", null, "Test", "Hello", null }, null, 6, TypeArgs = new Type[] { typeof(string) })]
  12.     [TestFixture(new[] { 'a', '\n', '4', '5' }, '\t', 4, TypeArgs = new Type[] { typeof(char) })]
  13.     [TestFixture(new[] { true, false, true, true }, false, 4, TypeArgs = new Type[] { typeof(bool) })]
  14.     public class StackGenericTests<T>
  15.     {
  16.         private const int someValue = 3;
  17.         private readonly Stack<T> stack;
  18.         private readonly T[] array;
  19.         private readonly T value;
  20.         private readonly int initCount;
  21.  
  22.         public StackGenericTests(T[] source, T value, int count)
  23.         {
  24.             this.stack = new Stack<T>(source);
  25.             this.value = value;
  26.             this.initCount = count;
  27.             this.array = source;
  28.         }
  29.  
  30.         [Test]
  31.         [Order(0)]
  32.         public void Ctor_BasedOnEnumerableSource()
  33.         {
  34.             Assert.That(this.stack.Count == this.initCount);
  35.         }
  36.  
  37.         [Test]
  38.         [Order(1)]
  39.         public void Iterator_Test()
  40.         {
  41.             int i = this.initCount;
  42.             foreach (var item in this.stack)
  43.             {
  44.                 Assert.AreEqual(item, this.array[--i]);
  45.             }
  46.         }
  47.  
  48.         [Test]
  49.         [Order(2)]
  50.         public void ToArray_Test()
  51.         {
  52.             T[] copy = this.stack.ToArray();
  53.             Assert.Multiple(() =>
  54.             {
  55.                 CollectionAssert.AreEqual(this.array.Reverse(), copy);
  56.                 Assert.AreNotSame(this.array, copy);
  57.             });
  58.         }
  59.  
  60.         [Test]
  61.         [Order(3)]
  62.         public void Push_OneElement()
  63.         {
  64.             int count = this.stack.Count;
  65.             this.stack.Push(this.value);
  66.             Assert.Multiple(() =>
  67.             {
  68.                 CollectionAssert.Contains(this.stack, this.value);
  69.                 Assert.That(this.stack.Count == count + 1);
  70.             });
  71.         }
  72.  
  73.         [Test]
  74.         [Order(4)]
  75.         public void Contains_Test()
  76.         {
  77.             Assert.IsTrue(this.stack.Contains(this.value));
  78.         }
  79.  
  80.         [Test]
  81.         [Order(4)]
  82.         public void Peek_Test()
  83.         {
  84.             int count = this.stack.Count;
  85.             T actual = this.stack.Peek();
  86.             Assert.Multiple(() =>
  87.             {
  88.                 Assert.That(this.stack.Count == count);
  89.                 Assert.AreEqual(this.value, actual);
  90.             });
  91.         }
  92.  
  93.         [Test]
  94.         [Order(5)]
  95.         public void Push_SomeElement()
  96.         {
  97.             int count = this.stack.Count;
  98.             for (int i = 1; i <= someValue; i++)
  99.             {
  100.                 this.stack.Push(this.value);
  101.             }
  102.  
  103.             Assert.That(this.stack.Count == count + someValue);
  104.         }
  105.  
  106.         [Test]
  107.         [Order(6)]
  108.         public void Pop_SomeElement()
  109.         {
  110.             int count = this.stack.Count;
  111.             for (int i = 1; i <= someValue; i++)
  112.             {
  113.                 T item = this.stack.Pop();
  114.             }
  115.  
  116.             Assert.That(this.stack.Count == count - someValue);
  117.         }
  118.  
  119.         [Test]
  120.         [Order(7)]
  121.         public void Iterator_PopFromStack_Throw_InvalidOperationException()
  122.         {
  123.             Assert.Throws<InvalidOperationException>(
  124.                 () =>
  125.             {
  126.                 foreach (var item in this.stack)
  127.                 {
  128.                     T t = this.stack.Pop();
  129.                 }
  130.             }, "Stack cannot be changed during iteration.");
  131.         }
  132.  
  133.         [Test]
  134.         [Order(8)]
  135.         public void Iterator_PushIntoStack_Throw_InvalidOperationException()
  136.         {
  137.             Assert.Throws<InvalidOperationException>(
  138.                 () =>
  139.             {
  140.                 foreach (var item in this.stack)
  141.                 {
  142.                     this.stack.Push(this.value);
  143.                 }
  144.             }, "Stack cannot be changed during iteration.");
  145.         }
  146.  
  147.         [Test]
  148.         [Order(9)]
  149.         public void Clear_Test()
  150.         {
  151.             this.stack.Clear();
  152.             Assert.That(this.stack.Count == 0);
  153.         }
  154.  
  155.         [Test]
  156.         [Order(10)]
  157.         public void Pop_StackIsEmpty_ThrowInvalidOperationException()
  158.         {
  159.             Assert.Throws<InvalidOperationException>(() => this.stack.Pop(), "Invalid operation pop, stack is empty.");
  160.         }
  161.     }
  162. }
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement