CollectionBaseTest.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // System.Collections.CollectionBase
  3. // Test suite for System.Collections.CollectionBase
  4. //
  5. // Author:
  6. // Nick D. Drochak II
  7. //
  8. // (C) 2001 Nick D. Drochak II
  9. //
  10. using System;
  11. using System.Collections;
  12. using NUnit.Framework;
  13. namespace MonoTests.System.Collections
  14. {
  15. public class CollectionBaseTest : TestCase
  16. {
  17. // We need a concrete class to test the abstract base class
  18. public class ConcreteCollection : CollectionBase
  19. {
  20. // These fields are used as markers to test the On* hooks.
  21. public bool onClearFired;
  22. public bool onClearCompleteFired;
  23. public bool onInsertFired;
  24. public int onInsertIndex;
  25. public bool onInsertCompleteFired;
  26. public int onInsertCompleteIndex;
  27. public bool onRemoveFired;
  28. public int onRemoveIndex;
  29. public bool onRemoveCompleteFired;
  30. public int onRemoveCompleteIndex;
  31. public bool onSetFired;
  32. public int onSetOldValue;
  33. public int onSetNewValue;
  34. public bool onSetCompleteFired;
  35. public int onSetCompleteOldValue;
  36. public int onSetCompleteNewValue;
  37. // This constructor is used to test OnValid()
  38. public ConcreteCollection()
  39. {
  40. IList listObj;
  41. listObj = this;
  42. listObj.Add(null);
  43. }
  44. // This constructor puts consecutive integers into the list
  45. public ConcreteCollection(int i) {
  46. IList listObj;
  47. listObj = this;
  48. int j;
  49. for (j = 0; j< i; j++) {
  50. listObj.Add(j);
  51. }
  52. }
  53. // A helper method to look at a value in the list at a specific index
  54. public int PeekAt(int index)
  55. {
  56. IList listObj;
  57. listObj = this;
  58. return (int) listObj[index];
  59. }
  60. // Mark the flag if this hook is fired
  61. protected override void OnClear() {
  62. this.onClearFired = true;
  63. }
  64. // Mark the flag if this hook is fired
  65. protected override void OnClearComplete()
  66. {
  67. this.onClearCompleteFired = true;
  68. }
  69. // Mark the flag, and save the paramter if this hook is fired
  70. protected override void OnInsert(int index, object value)
  71. {
  72. this.onInsertFired = true;
  73. this.onInsertIndex = index;
  74. }
  75. // Mark the flag, and save the paramter if this hook is fired
  76. protected override void OnInsertComplete(int index, object value)
  77. {
  78. this.onInsertCompleteFired = true;
  79. this.onInsertCompleteIndex = index;
  80. }
  81. // Mark the flag, and save the paramter if this hook is fired
  82. protected override void OnRemove(int index, object value)
  83. {
  84. this.onRemoveFired = true;
  85. this.onRemoveIndex = index;
  86. }
  87. // Mark the flag, and save the paramter if this hook is fired
  88. protected override void OnRemoveComplete(int index, object value)
  89. {
  90. this.onRemoveCompleteFired = true;
  91. this.onRemoveCompleteIndex = index;
  92. }
  93. // Mark the flag, and save the paramters if this hook is fired
  94. protected override void OnSet(int index, object oldValue, object newValue)
  95. {
  96. this.onSetFired = true;
  97. this.onSetOldValue = (int) oldValue;
  98. this.onSetNewValue = (int) newValue;
  99. }
  100. // Mark the flag, and save the paramters if this hook is fired
  101. protected override void OnSetComplete(int index, object oldValue, object newValue)
  102. {
  103. this.onSetCompleteFired = true;
  104. this.onSetCompleteOldValue = (int) oldValue;
  105. this.onSetCompleteNewValue = (int) newValue;
  106. }
  107. } // public class ConcreteCollection
  108. // Check the count property
  109. public void TestCount() {
  110. ConcreteCollection myCollection;
  111. myCollection = new ConcreteCollection(4);
  112. Assert(4 == myCollection.Count);
  113. }
  114. // Make sure GetEnumerator returns an object
  115. public void TestGetEnumerator() {
  116. ConcreteCollection myCollection;
  117. myCollection = new ConcreteCollection(4);
  118. Assert(null != myCollection.GetEnumerator());
  119. }
  120. // OnValid disallows nulls
  121. public void TestOnValid() {
  122. ConcreteCollection myCollection;
  123. try {
  124. myCollection = new ConcreteCollection();
  125. }
  126. catch (ArgumentNullException) {
  127. }
  128. }
  129. // Test various Insert paths
  130. public void TestInsert() {
  131. ConcreteCollection myCollection;
  132. int numberOfItems;
  133. numberOfItems = 3;
  134. // The constructor inserts
  135. myCollection = new ConcreteCollection(numberOfItems);
  136. Assert(myCollection.onInsertFired);
  137. Assert(myCollection.onInsertCompleteFired);
  138. // Using the IList interface, check inserts in the middle
  139. IList listObj = myCollection;
  140. listObj.Insert(1, 9);
  141. Assert(myCollection.onInsertIndex == 1);
  142. Assert(myCollection.onInsertCompleteIndex == 1);
  143. Assert(myCollection.PeekAt(1) == 9);
  144. }
  145. // Test Clear and it's hooks
  146. public void TestClear()
  147. {
  148. ConcreteCollection myCollection;
  149. int numberOfItems;
  150. numberOfItems = 1;
  151. myCollection = new ConcreteCollection(numberOfItems);
  152. myCollection.Clear();
  153. Assert(myCollection.Count == 0);
  154. Assert(myCollection.onClearFired);
  155. Assert(myCollection.onClearCompleteFired);
  156. }
  157. // Test RemoveAt, other removes and the hooks
  158. public void TestRemove()
  159. {
  160. ConcreteCollection myCollection;
  161. int numberOfItems;
  162. numberOfItems = 3;
  163. // Set up a test collection
  164. myCollection = new ConcreteCollection(numberOfItems);
  165. // The list is 0-based. So if we remove the second one
  166. myCollection.RemoveAt(1);
  167. // We should see the original third one in it's place
  168. Assert(myCollection.PeekAt(1) == 2);
  169. Assert(myCollection.onRemoveFired);
  170. Assert(myCollection.onRemoveIndex == 1);
  171. Assert(myCollection.onRemoveCompleteFired);
  172. Assert(myCollection.onRemoveCompleteIndex == 1);
  173. IList listObj = myCollection;
  174. listObj.Remove(0);
  175. // Confirm parameters are being passed to the hooks
  176. Assert(myCollection.onRemoveIndex == 0);
  177. Assert(myCollection.onRemoveCompleteIndex == 0);
  178. }
  179. // Test the random access feature
  180. public void TestSet()
  181. {
  182. ConcreteCollection myCollection;
  183. int numberOfItems;
  184. numberOfItems = 3;
  185. myCollection = new ConcreteCollection(numberOfItems);
  186. IList listObj = myCollection;
  187. listObj[0] = 99;
  188. Assert((int) listObj[0] == 99);
  189. Assert(myCollection.onSetFired);
  190. Assert(myCollection.onSetCompleteFired);
  191. Assert(myCollection.onSetOldValue == 0);
  192. Assert(myCollection.onSetCompleteOldValue == 0);
  193. Assert(myCollection.onSetNewValue == 99);
  194. Assert(myCollection.onSetCompleteNewValue == 99);
  195. }
  196. }
  197. }