StackTest.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. //
  2. // StackTest.cs
  3. //
  4. // Author:
  5. // Chris Hynes <[email protected]>
  6. //
  7. // (C) 2001 Chris Hynes
  8. //
  9. using System;
  10. using System.Collections;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Collections
  13. {
  14. public class StackTest: TestCase
  15. {
  16. private Stack stack1;
  17. private Stack stack2;
  18. private Stack stackInt;
  19. public void TestConstructor()
  20. {
  21. AssertEquals(false, stack1 == null);
  22. }
  23. public void TestICollectionConstructor()
  24. {
  25. Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
  26. for (int i = 4; i >= 0; i--)
  27. AssertEquals(i, stackTest.Pop());
  28. AssertEquals(0, stackTest.Count);
  29. }
  30. public void TestIntConstructor()
  31. {
  32. Stack stackTest = new Stack(50);
  33. AssertEquals(false, stackTest == null);
  34. }
  35. public void TestCount()
  36. {
  37. Stack stackTest = new Stack();
  38. stackTest.Push(50);
  39. stackTest.Push(5);
  40. stackTest.Push(0);
  41. stackTest.Push(50);
  42. AssertEquals(4, stackTest.Count);
  43. }
  44. public void TestIsSyncronized()
  45. {
  46. AssertEquals(false, stack1.IsSynchronized);
  47. AssertEquals(true, Stack.Synchronized(stack1).IsSynchronized);
  48. }
  49. public void TestSyncRoot()
  50. {
  51. AssertEquals(false, stack1.SyncRoot == null);
  52. }
  53. public void TestGetEnumerator()
  54. {
  55. stackInt.Pop();
  56. int j = 3;
  57. foreach (int i in stackInt)
  58. {
  59. AssertEquals(j--, i);
  60. }
  61. stackInt.Clear();
  62. IEnumerator e = stackInt.GetEnumerator();
  63. AssertEquals(false, e.MoveNext());
  64. }
  65. public void TestClear()
  66. {
  67. stackInt.Clear();
  68. AssertEquals(0, stackInt.Count);
  69. }
  70. public void TestClone()
  71. {
  72. Stack clone = (Stack)stackInt.Clone();
  73. while (stackInt.Count > 0)
  74. {
  75. AssertEquals(stackInt.Pop(), clone.Pop());
  76. }
  77. }
  78. public void TestContains()
  79. {
  80. string toLocate = "test";
  81. stackInt.Push(toLocate);
  82. stackInt.Push("chaff");
  83. Assert(stackInt.Contains(toLocate));
  84. stackInt.Pop();
  85. Assert(stackInt.Contains(toLocate));
  86. stackInt.Pop();
  87. Assert(!stackInt.Contains(toLocate));
  88. }
  89. public void TestCopyTo()
  90. {
  91. int[] arr = new int[stackInt.Count - 1];
  92. int[,] arrMulti;
  93. try
  94. {
  95. stackInt.CopyTo(null, 0);
  96. Fail("Should throw an ArgumentNullException");
  97. }
  98. catch (ArgumentNullException) {}
  99. try
  100. {
  101. stackInt.CopyTo(arr, -1);
  102. Fail("Should throw an ArgumentOutOfRangeException");
  103. }
  104. catch (ArgumentOutOfRangeException) {}
  105. try
  106. {
  107. stackInt.CopyTo(arrMulti = new int[1, 1], 1);
  108. Fail("Should throw an ArgumentException");
  109. }
  110. catch (ArgumentException) {}
  111. try
  112. {
  113. stackInt.CopyTo(arr = new int[2], 3);
  114. Fail("Should throw an ArgumentException");
  115. }
  116. catch (ArgumentException) {}
  117. try
  118. {
  119. stackInt.CopyTo(arr = new int[3], 2);
  120. Fail("Should throw an ArgumentException");
  121. }
  122. catch (ArgumentException) {}
  123. try
  124. {
  125. stackInt.CopyTo(arr = new int[2], 3);
  126. Fail("Should throw an ArgumentException");
  127. }
  128. catch (ArgumentException) {}
  129. arr = new int[stackInt.Count];
  130. stackInt.CopyTo(arr, 0);
  131. int j = 4;
  132. for (int i = 0; i < 4; i++)
  133. {
  134. AssertEquals(j--, arr[i]);
  135. }
  136. }
  137. public void TestSyncronized()
  138. {
  139. Stack syncStack = Stack.Synchronized(stackInt);
  140. syncStack.Push(5);
  141. for (int i = 5; i >= 0; i--)
  142. AssertEquals(i, syncStack.Pop());
  143. }
  144. public void TestPushPeekPop()
  145. {
  146. stackInt.Pop();
  147. int topVal = (int)stackInt.Peek();
  148. AssertEquals(3, topVal);
  149. AssertEquals(4, stackInt.Count);
  150. AssertEquals(topVal, stackInt.Pop());
  151. AssertEquals(2, stackInt.Pop());
  152. Stack test = new Stack();
  153. test.Push(null);
  154. AssertEquals(null, test.Pop());
  155. }
  156. public void TestToArray()
  157. {
  158. object[] arr = stackInt.ToArray();
  159. AssertEquals(stackInt.Count, arr.Length);
  160. for (int i = 0; i < 5; i++)
  161. AssertEquals(arr[i], stackInt.Pop());
  162. }
  163. protected override void SetUp()
  164. {
  165. stack1 = new Stack();
  166. stack2 = new Stack();
  167. stackInt = new Stack();
  168. for (int i = 0; i < 5; i++)
  169. stackInt.Push(i);
  170. }
  171. }
  172. }