QueueTest.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. //
  2. // System.Collections.QueueTest
  3. // Test suite for System.Collections.Queue
  4. //
  5. // Author:
  6. // Ricardo Fernández Pascual
  7. //
  8. // (C) 2001 Ricardo Fernández Pascual
  9. //
  10. using System;
  11. using System.Collections;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Collections {
  14. public class QueueTest : TestCase {
  15. protected Queue q1;
  16. protected Queue q2;
  17. protected Queue emptyQueue;
  18. protected override void SetUp () {
  19. q1 = new Queue (10);
  20. for (int i = 0; i < 100; i++)
  21. q1.Enqueue (i);
  22. q2 = new Queue (50, 1.5f);
  23. for (int i = 50; i < 100; i++)
  24. q2.Enqueue (i);
  25. emptyQueue = new Queue ();
  26. }
  27. public void TestConstructors () {
  28. Assert (q1.Count == 100);
  29. Assert (q2.Count == 50);
  30. Assert (emptyQueue.Count == 0);
  31. // TODO: Test Queue (ICollection)
  32. }
  33. // TODO: should test all methods from ICollection,
  34. // but it should be done in ICollectionTest.cs... ??
  35. public void TestCopyTo () {
  36. int[] a1 = new int[100];
  37. int[] a2 = new int[60];
  38. string progress_marker = "";
  39. try {
  40. progress_marker = "before first CopyTo";
  41. q1.CopyTo (a1, 0);
  42. for (int i = 0; i < 100; i++)
  43. AssertEquals (i, a1[i]);
  44. // Remove some items from q2 and add other
  45. // items, to avoid having an "easy" just created
  46. // Queue
  47. for (int i = 50; i < 60; i++)
  48. Assert (i == (int) q2.Dequeue ());
  49. for (int i = 100; i < 110; i++)
  50. q2.Enqueue (i);
  51. progress_marker = "before second CopyTo";
  52. q2.CopyTo (a2, 10);
  53. for (int i = 60; i < 110; i++)
  54. Assert (i == a2[i - 60 + 10]);
  55. // Copying an empty Queue should not modify the array
  56. progress_marker = "before third CopyTo";
  57. emptyQueue.CopyTo (a2, 10);
  58. for (int i = 60; i < 110; i++)
  59. Assert (i == a2[i - 60 + 10]);
  60. } catch (Exception e) {
  61. Fail ("Unexpected exception at marker <" + progress_marker + ">: e = " + e);
  62. }
  63. }
  64. public void TestEnumerator () {
  65. int i;
  66. IEnumerator e;
  67. e = q1.GetEnumerator ();
  68. i = 0;
  69. while (e.MoveNext ()) {
  70. AssertEquals ("q1 at i=" + i, i, ((int) e.Current));
  71. i++;
  72. }
  73. e = q2.GetEnumerator ();
  74. i = 50;
  75. while (e.MoveNext ()) {
  76. AssertEquals (i, ((int) e.Current));
  77. i++;
  78. }
  79. e = emptyQueue.GetEnumerator ();
  80. if (e.MoveNext ())
  81. Fail ("Empty Queue enumerator returning elements!");
  82. e = q1.GetEnumerator ();
  83. try {
  84. e.MoveNext ();
  85. q1.Enqueue (0);
  86. e.MoveNext ();
  87. Fail ("#1 Should have thrown InvalidOperationException");
  88. } catch (InvalidOperationException) { }
  89. e = q1.GetEnumerator ();
  90. }
  91. public void TestClone () {
  92. Queue q3 = (Queue) q2.Clone ();
  93. Assert (q3.Count == q2.Count);
  94. for (int i = 0; i < 50; i++)
  95. Assert (q2.Dequeue ().Equals (q3.Dequeue ()));
  96. Assert (q3.Count == 0);
  97. Assert (q2.Count == 0);
  98. }
  99. public void ClearTest () {
  100. q1.Clear ();
  101. Assert (q1.Count == 0);
  102. q2.Clear ();
  103. Assert (q2.Count == 0);
  104. emptyQueue.Clear ();
  105. Assert (emptyQueue.Count == 0);
  106. }
  107. public void ContainsTest () {
  108. for (int i = 0; i < 100; i++) {
  109. Assert (q1.Contains (i));
  110. Assert (!emptyQueue.Contains (i));
  111. if (i < 50)
  112. Assert (!q2.Contains (i));
  113. else
  114. Assert (q2.Contains (i));
  115. }
  116. }
  117. public void EnqueueDequeuePeekTest () {
  118. int q1size = q1.Count;
  119. int q2size = q2.Count;
  120. q2.Enqueue (null);
  121. Assert (q2.Count == ++q2size);
  122. for (int i = 0; i < 50; i++) {
  123. int k = (int) q1.Peek ();
  124. Assert (q1.Count == q1size);
  125. int j = (int) q1.Dequeue ();
  126. Assert (q1.Count == --q1size);
  127. Assert (i == j);
  128. Assert (j == k);
  129. q2.Enqueue (j);
  130. Assert (q2.Count == ++q2size);
  131. }
  132. for (int i = 50; i < 100; i++) {
  133. Assert (((int) q2.Dequeue ()) == i);
  134. Assert (q2.Count == --q2size);
  135. }
  136. Assert (q2.Peek () == null);
  137. Assert (q2.Dequeue () == null);
  138. Assert (q2.Count == --q2size);
  139. for (int i = 0; i < 50; i++) {
  140. Assert (((int) q2.Dequeue ()) == i);
  141. Assert (q2.Count == --q2size);
  142. }
  143. }
  144. public void DequeueTest() {
  145. Queue queue = new Queue();
  146. string[] tmp = new string[50];
  147. int i;
  148. for (i=0;i<50;i++) {
  149. tmp[i] = "Data #" + i;
  150. queue.Enqueue(tmp[i]);
  151. }
  152. while(queue.Count>0){
  153. i--;
  154. string z = (string) queue.Dequeue();
  155. AssertEquals (tmp[i], tmp[i], z);
  156. }
  157. }
  158. // TODO: test Syncronized operation
  159. }
  160. }