1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
//
// StackTest.cs
//
// Author:
// Chris Hynes <chrish@assistedsolutions.com>
//
// (C) 2001 Chris Hynes
//
using System;
using System.Collections;
using NUnit.Framework;
namespace MonoTests.System.Collections
{
[TestFixture]
public class StackTest
{
private Stack stack1;
private Stack stackInt;
[Test]
public void TestConstructor()
{
Assert.AreEqual (stack1 == null, false);
}
[Test]
public void TestICollectionConstructor1()
{
Stack stackTest = new Stack(new int[] {0, 1, 2, 3, 4});
for (int i = 4; i >= 0; i--)
Assert.AreEqual (stackTest.Pop(), i);
Assert.AreEqual (stackTest.Count, 0);
}
[Test]
public void TestICollectionConstructor2()
{
bool exceptionThrown = false;
try {
Stack stackTest = new Stack(null);
} catch (ArgumentNullException e) {
exceptionThrown = true;
Assert.AreEqual ("col", e.ParamName, "ParamName must be \"col\"");
}
Assert.IsTrue (exceptionThrown, "null argument must throw ArgumentNullException");
}
[Test]
public void TestIntConstructor1()
{
Stack stackTest = new Stack(50);
Assert.IsTrue (stackTest != null);
}
[Test]
public void TestIntConstructor2()
{
bool exceptionThrown = false;
try {
Stack stackTest = new Stack(-1);
}
catch (ArgumentOutOfRangeException e)
{
exceptionThrown = true;
Assert.AreEqual ("initialCapacity", e.ParamName, "ParamName must be \"initialCapacity\"");
}
Assert.IsTrue (exceptionThrown, "negative argument must throw ArgumentOutOfRangeException");
}
[Test]
public void TestCount()
{
Stack stackTest = new Stack();
stackTest.Push(50);
stackTest.Push(5);
stackTest.Push(0);
stackTest.Push(50);
Assert.AreEqual (stackTest.Count, 4);
}
[Test]
public void TestIsSyncronized()
{
Assert.AreEqual (stack1.IsSynchronized, false);
Assert.AreEqual (Stack.Synchronized(stack1).IsSynchronized, true);
}
[Test]
public void TestSyncRoot()
{
Assert.AreEqual (stack1.SyncRoot == null, false);
}
[Test]
public void TestGetEnumerator1()
{
stackInt.Pop();
int j = 3;
foreach (int i in stackInt)
{
Assert.AreEqual (i, j--);
}
stackInt.Clear();
IEnumerator e = stackInt.GetEnumerator();
Assert.AreEqual (e.MoveNext(), false);
}
[Test]
public void TestGetEnumerator2()
{
IEnumerator e = stackInt.GetEnumerator();
try
{
// Tests InvalidOperationException if enumerator is uninitialized
Object o = e.Current;
Assert.Fail ("InvalidOperationException should be thrown");
} catch (InvalidOperationException) {}
}
[Test]
public void TestGetEnumerator3()
{
IEnumerator e = stack1.GetEnumerator();
e.MoveNext();
try
{
// Tests InvalidOperationException if enumeration has ended
Object o = e.Current;
Assert.Fail ("InvalidOperationException should be thrown");
} catch (InvalidOperationException) {}
}
[Test]
public void TestEnumeratorReset1()
{
IEnumerator e = stackInt.GetEnumerator();
e.MoveNext();
Assert.AreEqual (4, e.Current, "current value");
e.MoveNext();
e.Reset();
e.MoveNext();
Assert.AreEqual (4, e.Current, "current value after reset");
}
[Test]
public void TestEnumeratorReset2()
{
IEnumerator e = stackInt.GetEnumerator();
e.MoveNext();
Assert.AreEqual (4, e.Current, "current value");
// modifies underlying the stack. Reset must throw InvalidOperationException
stackInt.Push(5);
try
{
e.Reset();
Assert.Fail ("InvalidOperationException should be thrown");
}
catch (InvalidOperationException) {}
}
[Test]
public void TestEnumeratorMoveNextException()
{
IEnumerator e = stackInt.GetEnumerator();
// modifies underlying the stack. MoveNext must throw InvalidOperationException
stackInt.Push(5);
try
{
e.MoveNext();
Assert.Fail ("InvalidOperationException should be thrown");
}
catch (InvalidOperationException) {}
}
[Test]
public void TestClear()
{
stackInt.Clear();
Assert.AreEqual (stackInt.Count, 0);
}
[Test]
public void TestClone()
{
Stack clone = (Stack)stackInt.Clone();
while (stackInt.Count > 0)
{
Assert.AreEqual (clone.Pop(), stackInt.Pop());
}
}
[Test]
public void TestContains()
{
string toLocate = "test";
stackInt.Push(toLocate);
stackInt.Push("chaff");
stackInt.Push(null);
Assert.IsTrue (stackInt.Contains(toLocate));
Assert.IsTrue (stackInt.Contains(null), "must contain null");
stackInt.Pop();
stackInt.Pop();
Assert.IsTrue (stackInt.Contains(toLocate));
stackInt.Pop();
Assert.IsTrue (!stackInt.Contains(toLocate));
stackInt.Push(null);
Assert.IsTrue (stackInt.Contains(null));
stackInt.Pop();
Assert.IsTrue (!stackInt.Contains(null));
}
[Test]
public void TestCopyTo()
{
int[] arr = new int[stackInt.Count - 1];
int[,] arrMulti;
try
{
stackInt.CopyTo(null, 0);
Assert.Fail ("Should throw an ArgumentNullException");
} catch (ArgumentNullException e) {
Assert.AreEqual ("array", e.ParamName, "ParamName must be \"array\"");
}
try
{
stackInt.CopyTo(arr, -1);
Assert.Fail ("Should throw an ArgumentOutOfRangeException");
}
catch (ArgumentOutOfRangeException e)
{
Assert.AreEqual ("index", e.ParamName, "ParamName must be \"index\"");
}
try
{
stackInt.CopyTo(arrMulti = new int[1, 1], 1);
Assert.Fail ("Should throw an ArgumentException");
}
catch (ArgumentException) {}
try
{
stackInt.CopyTo(arr = new int[2], 3);
Assert.Fail ("Should throw an ArgumentException");
}
catch (ArgumentException) {}
try
{
stackInt.CopyTo(arr = new int[3], 2);
Assert.Fail ("Should throw an ArgumentException");
}
catch (ArgumentException) {}
try
{
stackInt.CopyTo(arr = new int[2], 3);
Assert.Fail ("Should throw an ArgumentException");
}
catch (ArgumentException) {}
arr = new int[stackInt.Count];
stackInt.CopyTo(arr, 0);
int j = 4;
for (int i = 0; i < 4; i++)
{
Assert.AreEqual (arr[i], j--);
}
}
[Test]
public void TestSyncronized()
{
Stack syncStack = Stack.Synchronized(stackInt);
syncStack.Push(5);
for (int i = 5; i >= 0; i--)
Assert.AreEqual (syncStack.Pop(), i);
}
[Test]
public void TestPushPeekPop()
{
stackInt.Pop();
int topVal = (int)stackInt.Peek();
Assert.AreEqual (topVal, 3);
Assert.AreEqual (stackInt.Count, 4);
Assert.AreEqual (stackInt.Pop(), topVal);
Assert.AreEqual (stackInt.Pop(), 2);
Stack test = new Stack();
test.Push(null);
Assert.AreEqual (test.Pop(), null);
}
[Test]
public void TestPop()
{
for (int i = 4; i >= 0; i--)
{
Assert.AreEqual (stackInt.Pop(), i);
}
try {
stackInt.Pop();
Assert.Fail ("must throw InvalidOperationException");
} catch (InvalidOperationException){
}
}
[Test]
public void TestToArray()
{
object[] arr = stackInt.ToArray();
Assert.AreEqual (arr.Length, stackInt.Count);
for (int i = 0; i < 5; i++)
Assert.AreEqual (stackInt.Pop(), arr[i]);
}
[Test]
public void TestResize()
{
Stack myStack = new Stack(20);
for (int i = 0; i < 500; i++)
{
myStack.Push(i);
Assert.AreEqual (i+1, myStack.Count, "push count test");
}
for (int i = 499; i >= 0; i--)
{
Assert.AreEqual (myStack.Pop(), i);
Assert.AreEqual (i, myStack.Count, "pop count test");
}
}
[Test]
public void TestEmptyCopyTo ()
{
Stack stack = new Stack ();
string [] arr = new string [0];
stack.CopyTo (arr, 0);
}
[Test]
public void TestICollectionCtorUsesEnum ()
{
BitArray x = new BitArray (10, true);
Stack s = new Stack (x);
}
[SetUp]
protected void SetUp()
{
stack1 = new Stack();
stackInt = new Stack();
for (int i = 0; i < 5; i++)
stackInt.Push(i);
}
}
}
|