StackTest.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // StackTest.cs
  3. //
  4. // Author:
  5. // Ben Maurer ([email protected])
  6. //
  7. #if NET_2_0
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using NUnit.Framework;
  12. namespace MonoTests.System.Collections.Generic {
  13. [TestFixture]
  14. public class StackTest: Assertion
  15. {
  16. [Test]
  17. public void TestCtor ()
  18. {
  19. Stack <int> a = new Stack <int> ();
  20. Stack <int> b = new Stack <int> (1);
  21. Stack <object> c = new Stack <object> ();
  22. Stack <object> d = new Stack <object> (1);
  23. Stack <object> e = new Stack <object> (0);
  24. }
  25. [Test]
  26. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  27. public void TestCtorEx ()
  28. {
  29. Stack <int> a = new Stack <int> (-1);
  30. }
  31. [Test]
  32. public void TestCtorEnum ()
  33. {
  34. List <int> l = new List <int> ();
  35. l.Add (1);
  36. l.Add (2);
  37. l.Add (3);
  38. Stack <int> s = new Stack <int> (l);
  39. // Things get pop'd in reverse
  40. AssertPop (s, 3);
  41. AssertPop (s, 2);
  42. AssertPop (s, 1);
  43. }
  44. [Test]
  45. [ExpectedException (typeof (ArgumentNullException))]
  46. public void TestCtorEnumNull ()
  47. {
  48. Stack <int> s = new Stack <int> (null);
  49. }
  50. [Test]
  51. public void TestClear()
  52. {
  53. Stack <int> s = new Stack <int> ();
  54. s.Clear ();
  55. AssertEquals (s.Count, 0);
  56. s.Push (1);
  57. s.Push (2);
  58. AssertEquals (s.Count, 2);
  59. s.Clear ();
  60. AssertEquals (s.Count, 0);
  61. }
  62. [Test]
  63. public void TestContains ()
  64. {
  65. Stack <int> s = new Stack <int> ();
  66. AssertEquals (s.Contains (1), false);
  67. s.Push (1);
  68. AssertEquals (s.Contains (1), true);
  69. AssertEquals (s.Contains (0), false);
  70. }
  71. [Test]
  72. public void TestCopyTo ()
  73. {
  74. int [] x = new int [3];
  75. Stack <int> z = new Stack <int> ();
  76. z.Push (1);
  77. z.Push (2);
  78. x [0] = 10;
  79. z.CopyTo (x, 1);
  80. AssertEquals (x [0], 10);
  81. AssertEquals (x [1], 2);
  82. AssertEquals (x [2], 1);
  83. }
  84. [Test]
  85. public void TestPeek ()
  86. {
  87. Stack <int> s = new Stack <int> ();
  88. s.Push (1);
  89. AssertEquals (s.Peek (), 1);
  90. AssertEquals (s.Count, 1);
  91. }
  92. [Test]
  93. [ExpectedException (typeof (InvalidOperationException))]
  94. public void TestPeekEx ()
  95. {
  96. Stack <int> s = new Stack <int> ();
  97. s.Peek ();
  98. }
  99. [Test]
  100. [ExpectedException (typeof (InvalidOperationException))]
  101. public void TestPeekEx2 ()
  102. {
  103. Stack <int> s = new Stack <int> ();
  104. s.Push (1);
  105. s.Pop ();
  106. s.Peek ();
  107. }
  108. [Test]
  109. public void TestPop ()
  110. {
  111. Stack <int> s = new Stack <int> ();
  112. s.Push (1);
  113. AssertEquals (s.Pop (), 1);
  114. AssertEquals (s.Count, 0);
  115. }
  116. [Test]
  117. [ExpectedException (typeof (InvalidOperationException))]
  118. public void TestPopEx ()
  119. {
  120. Stack <int> s = new Stack <int> ();
  121. s.Pop ();
  122. }
  123. [Test]
  124. [ExpectedException (typeof (InvalidOperationException))]
  125. public void TestPopEx2 ()
  126. {
  127. Stack <int> s = new Stack <int> ();
  128. s.Push (1);
  129. s.Pop ();
  130. s.Pop ();
  131. }
  132. [Test]
  133. public void TestPush ()
  134. {
  135. Stack <int> s = new Stack <int> ();
  136. s.Push (1);
  137. AssertEquals (s.Count, 1);
  138. s.Push (2);
  139. AssertEquals (s.Count, 2);
  140. for (int i = 0; i < 100; i ++)
  141. s.Push (i);
  142. AssertEquals (s.Count, 102);
  143. }
  144. [Test]
  145. public void TestToArray ()
  146. {
  147. Stack <int> s = new Stack <int> ();
  148. int [] x = s.ToArray ();
  149. AssertEquals (x.Length, 0);
  150. s.Push (1);
  151. x = s.ToArray ();
  152. AssertEquals (x.Length, 1);
  153. AssertEquals (x [0], 1);
  154. }
  155. [Test]
  156. public void TestEnumerator ()
  157. {
  158. Stack <int> s = new Stack <int> ();
  159. foreach (int x in s)
  160. Fail ();
  161. s.Push (1);
  162. int i = 0;
  163. foreach (int x in s) {
  164. AssertEquals (i, 0);
  165. AssertEquals (x, 1);
  166. i ++;
  167. }
  168. i = 0;
  169. s.Push (2);
  170. s.Push (3);
  171. foreach (int x in s) {
  172. AssertEquals (x, 3 - i);
  173. Assert (i < 3);
  174. i ++;
  175. }
  176. }
  177. void AssertPop <T> (Stack <T> s, T t)
  178. {
  179. AssertEquals (s.Pop (), t);
  180. }
  181. }
  182. }
  183. #endif