CollectionBaseTest.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. //
  2. // System.Collections.CollectionBase
  3. // Test suite for System.Collections.CollectionBase
  4. //
  5. // Authors:
  6. // Nick D. Drochak II
  7. // Gonzalo Paniagua Javier ([email protected])
  8. //
  9. // (C) 2001 Nick D. Drochak II
  10. // (c) 2003 Ximian, Inc. (http://www.ximian.com)
  11. //
  12. using System;
  13. using System.Collections;
  14. using NUnit.Framework;
  15. namespace MonoTests.System.Collections
  16. {
  17. [TestFixture]
  18. public class CollectionBaseTest : Assertion
  19. {
  20. // We need a concrete class to test the abstract base class
  21. public class ConcreteCollection : CollectionBase
  22. {
  23. // These fields are used as markers to test the On* hooks.
  24. public bool onClearFired;
  25. public bool onClearCompleteFired;
  26. public bool onInsertFired;
  27. public int onInsertIndex;
  28. public bool onInsertCompleteFired;
  29. public int onInsertCompleteIndex;
  30. public bool onRemoveFired;
  31. public int onRemoveIndex;
  32. public bool onRemoveCompleteFired;
  33. public int onRemoveCompleteIndex;
  34. public bool onSetFired;
  35. public int onSetOldValue;
  36. public int onSetNewValue;
  37. public bool onSetCompleteFired;
  38. public int onSetCompleteOldValue;
  39. public int onSetCompleteNewValue;
  40. public int mustThrowException;
  41. public bool onValidateFired;
  42. // This constructor is used to test OnValid()
  43. public ConcreteCollection()
  44. {
  45. IList listObj;
  46. listObj = this;
  47. listObj.Add(null);
  48. }
  49. // This constructor puts consecutive integers into the list
  50. public ConcreteCollection(int i) {
  51. IList listObj;
  52. listObj = this;
  53. int j;
  54. for (j = 0; j< i; j++) {
  55. listObj.Add(j);
  56. }
  57. }
  58. void CheckIfThrow ()
  59. {
  60. if (mustThrowException > 0) {
  61. mustThrowException--;
  62. if (mustThrowException == 0)
  63. throw new Exception ();
  64. }
  65. }
  66. // A helper method to look at a value in the list at a specific index
  67. public int PeekAt(int index)
  68. {
  69. IList listObj;
  70. listObj = this;
  71. return (int) listObj[index];
  72. }
  73. protected override void OnValidate (object value) {
  74. this.onValidateFired = true;
  75. CheckIfThrow ();
  76. base.OnValidate (value);
  77. }
  78. // Mark the flag if this hook is fired
  79. protected override void OnClear() {
  80. this.onClearFired = true;
  81. CheckIfThrow ();
  82. }
  83. // Mark the flag if this hook is fired
  84. protected override void OnClearComplete()
  85. {
  86. this.onClearCompleteFired = true;
  87. CheckIfThrow ();
  88. }
  89. // Mark the flag, and save the paramter if this hook is fired
  90. protected override void OnInsert(int index, object value)
  91. {
  92. this.onInsertFired = true;
  93. this.onInsertIndex = index;
  94. CheckIfThrow ();
  95. }
  96. // Mark the flag, and save the paramter if this hook is fired
  97. protected override void OnInsertComplete(int index, object value)
  98. {
  99. this.onInsertCompleteFired = true;
  100. this.onInsertCompleteIndex = index;
  101. CheckIfThrow ();
  102. }
  103. // Mark the flag, and save the paramter if this hook is fired
  104. protected override void OnRemove(int index, object value)
  105. {
  106. this.onRemoveFired = true;
  107. this.onRemoveIndex = index;
  108. CheckIfThrow ();
  109. }
  110. // Mark the flag, and save the paramter if this hook is fired
  111. protected override void OnRemoveComplete(int index, object value)
  112. {
  113. this.onRemoveCompleteFired = true;
  114. this.onRemoveCompleteIndex = index;
  115. CheckIfThrow ();
  116. }
  117. // Mark the flag, and save the paramters if this hook is fired
  118. protected override void OnSet(int index, object oldValue, object newValue)
  119. {
  120. this.onSetFired = true;
  121. this.onSetOldValue = (int) oldValue;
  122. this.onSetNewValue = (int) newValue;
  123. CheckIfThrow ();
  124. }
  125. // Mark the flag, and save the paramters if this hook is fired
  126. protected override void OnSetComplete(int index, object oldValue, object newValue)
  127. {
  128. this.onSetCompleteFired = true;
  129. this.onSetCompleteOldValue = (int) oldValue;
  130. this.onSetCompleteNewValue = (int) newValue;
  131. CheckIfThrow ();
  132. }
  133. public IList BaseList {
  134. get { return base.List; }
  135. }
  136. } // public class ConcreteCollection
  137. // Check the count property
  138. [Test]
  139. public void Count() {
  140. ConcreteCollection myCollection;
  141. myCollection = new ConcreteCollection(4);
  142. Assert(4 == myCollection.Count);
  143. }
  144. // Make sure GetEnumerator returns an object
  145. [Test]
  146. public void GetEnumerator() {
  147. ConcreteCollection myCollection;
  148. myCollection = new ConcreteCollection(4);
  149. Assert(null != myCollection.GetEnumerator());
  150. }
  151. // OnValid disallows nulls
  152. [Test]
  153. [ExpectedException(typeof(ArgumentNullException))]
  154. public void OnValid() {
  155. ConcreteCollection myCollection;
  156. myCollection = new ConcreteCollection();
  157. }
  158. // Test various Insert paths
  159. [Test]
  160. public void Insert() {
  161. ConcreteCollection myCollection;
  162. int numberOfItems;
  163. numberOfItems = 3;
  164. // The constructor inserts
  165. myCollection = new ConcreteCollection(numberOfItems);
  166. Assert(myCollection.onInsertFired);
  167. Assert(myCollection.onInsertCompleteFired);
  168. // Using the IList interface, check inserts in the middle
  169. IList listObj = myCollection;
  170. listObj.Insert(1, 9);
  171. Assert(myCollection.onInsertIndex == 1);
  172. Assert(myCollection.onInsertCompleteIndex == 1);
  173. Assert(myCollection.PeekAt(1) == 9);
  174. }
  175. // Test Clear and it's hooks
  176. [Test]
  177. public void Clear()
  178. {
  179. ConcreteCollection myCollection;
  180. int numberOfItems;
  181. numberOfItems = 1;
  182. myCollection = new ConcreteCollection(numberOfItems);
  183. myCollection.Clear();
  184. Assert(myCollection.Count == 0);
  185. Assert(myCollection.onClearFired);
  186. Assert(myCollection.onClearCompleteFired);
  187. }
  188. // Test RemoveAt, other removes and the hooks
  189. [Test]
  190. public void Remove()
  191. {
  192. ConcreteCollection myCollection;
  193. int numberOfItems;
  194. numberOfItems = 3;
  195. // Set up a test collection
  196. myCollection = new ConcreteCollection(numberOfItems);
  197. // The list is 0-based. So if we remove the second one
  198. myCollection.RemoveAt(1);
  199. // We should see the original third one in it's place
  200. Assert(myCollection.PeekAt(1) == 2);
  201. Assert(myCollection.onRemoveFired);
  202. Assert(myCollection.onRemoveIndex == 1);
  203. Assert(myCollection.onRemoveCompleteFired);
  204. Assert(myCollection.onRemoveCompleteIndex == 1);
  205. IList listObj = myCollection;
  206. listObj.Remove(0);
  207. // Confirm parameters are being passed to the hooks
  208. Assert(myCollection.onRemoveIndex == 0);
  209. Assert(myCollection.onRemoveCompleteIndex == 0);
  210. }
  211. // Test the random access feature
  212. [Test]
  213. public void Set()
  214. {
  215. ConcreteCollection myCollection;
  216. int numberOfItems;
  217. numberOfItems = 3;
  218. myCollection = new ConcreteCollection(numberOfItems);
  219. IList listObj = myCollection;
  220. listObj[0] = 99;
  221. Assert((int) listObj[0] == 99);
  222. Assert(myCollection.onSetFired);
  223. Assert(myCollection.onSetCompleteFired);
  224. Assert(myCollection.onSetOldValue == 0);
  225. Assert(myCollection.onSetCompleteOldValue == 0);
  226. Assert(myCollection.onSetNewValue == 99);
  227. Assert(myCollection.onSetCompleteNewValue == 99);
  228. }
  229. [Test]
  230. public void InsertComplete_Add ()
  231. {
  232. ConcreteCollection coll = new ConcreteCollection (0);
  233. coll.mustThrowException = 1;
  234. try {
  235. coll.BaseList.Add (0);
  236. } catch {
  237. }
  238. AssertEquals (0, coll.Count);
  239. }
  240. [Test]
  241. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  242. public void ValidateCalled ()
  243. {
  244. ConcreteCollection coll = new ConcreteCollection (0);
  245. coll.mustThrowException = 1;
  246. try {
  247. coll.BaseList [5] = 8888;
  248. } catch (ArgumentOutOfRangeException) {
  249. throw;
  250. } finally {
  251. AssertEquals (false, coll.onValidateFired);
  252. }
  253. }
  254. [Test]
  255. public void SetCompleteCalled ()
  256. {
  257. ConcreteCollection coll = new ConcreteCollection (0);
  258. coll.BaseList.Add (88);
  259. coll.mustThrowException = 1;
  260. try {
  261. coll.BaseList [0] = 11;
  262. } catch {
  263. } finally {
  264. AssertEquals (false, coll.onSetCompleteFired);
  265. }
  266. }
  267. [Test]
  268. public void SetCompleteUndo ()
  269. {
  270. ConcreteCollection coll = new ConcreteCollection (0);
  271. bool throwsException = true;
  272. coll.BaseList.Add (88);
  273. coll.onValidateFired = false;
  274. coll.onInsertFired = false;
  275. coll.onSetCompleteFired = false;
  276. coll.mustThrowException = 3;
  277. try {
  278. coll.BaseList [0] = 11;
  279. throwsException = false;
  280. } catch {
  281. } finally {
  282. Assert (throwsException);
  283. Assert (coll.onValidateFired);
  284. Assert (coll.onSetFired);
  285. Assert (coll.onSetCompleteFired);
  286. AssertEquals (88, coll.BaseList [0]);
  287. }
  288. }
  289. [Test]
  290. [ExpectedException (typeof (ArgumentException))]
  291. public void InvalidRemove ()
  292. {
  293. ConcreteCollection coll = new ConcreteCollection (0);
  294. coll.BaseList.Remove (10);
  295. }
  296. }
  297. }