Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. using System;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using StackDataStructure;
  4.  
  5. namespace StackTesting
  6. {
  7. [TestClass]
  8. public class Tests
  9. {
  10. [TestMethod]
  11. public void TestEmptyStackSize()
  12. {
  13. var stack = new Stack();
  14. Assert.IsTrue(stack.IsEmpty());
  15. }
  16.  
  17. [TestMethod]
  18. [ExpectedException(typeof(InvalidOperationException))]
  19. public void EmptyStackPopException()
  20. {
  21. var stack = new Stack();
  22. stack.Pop();
  23. }
  24.  
  25. [TestMethod]
  26. public void PushingAndPoppintItemsToStack()
  27. {
  28. var stack = new Stack();
  29. stack.Push(1);
  30. Assert.AreEqual(1, stack.Size);
  31. stack.Push(2);
  32. Assert.AreEqual(2, stack.Size);
  33. Assert.AreEqual(2, stack.Pop());
  34. Assert.AreEqual(1, stack.Size);
  35. Assert.AreEqual(1, stack.Pop());
  36. Assert.IsTrue(stack.IsEmpty());
  37. Assert.IsTrue(stack.Size == 0);
  38. stack.Push(10);
  39. Assert.IsTrue(stack.Size == 1);
  40. Assert.IsTrue(10 == stack.Pop());
  41. Assert.IsTrue(stack.IsEmpty());
  42. }
  43. }
  44. }
  45.  
  46. using System;
  47.  
  48. namespace StackDataStructure
  49. {
  50. internal class Node
  51. {
  52. internal int value;
  53. internal Node underTop;
  54. }
  55.  
  56. public class Stack
  57. {
  58. private int size;
  59. private Node top;
  60.  
  61. public int Size { get { return size; } }
  62.  
  63. public bool IsEmpty()
  64. {
  65. return top == null;
  66. }
  67.  
  68. public int Pop()
  69. {
  70. if (IsEmpty())
  71. throw new InvalidOperationException("Stack is empty");
  72.  
  73. int value = top.value;
  74. top = top.underTop;
  75. size--;
  76. return value;
  77. }
  78.  
  79. public void Push(int v)
  80. {
  81. top = new Node{
  82. value = v,
  83. underTop = top
  84. };
  85. size++;
  86. }
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement