File: CollectionBaseTest.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (340 lines) | stat: -rw-r--r-- 8,576 bytes parent folder | download | duplicates (12)
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
//
// System.Collections.CollectionBase
// Test suite for System.Collections.CollectionBase
//
// Authors:
//    Nick D. Drochak II
//    Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2001 Nick D. Drochak II
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
//


using System;
using System.Collections;
using NUnit.Framework;

namespace MonoTests.System.Collections
{

[TestFixture]
public class CollectionBaseTest
{
	// We need a concrete class to test the abstract base class
	public class ConcreteCollection : CollectionBase 
	{
		// These fields are used as markers to test the On* hooks.
		public bool onClearFired;
		public bool onClearCompleteFired;

		public bool onInsertFired;
		public int onInsertIndex;
		public bool onInsertCompleteFired;
		public int onInsertCompleteIndex;

		public bool onRemoveFired;
		public int onRemoveIndex;
		public bool onRemoveCompleteFired;
		public int onRemoveCompleteIndex;

		public bool onSetFired;
		public int onSetOldValue;
		public int onSetNewValue;
		public bool onSetCompleteFired;
		public int onSetCompleteOldValue;
		public int onSetCompleteNewValue;
		public int mustThrowException;
		public bool onValidateFired;

		// This constructor is used to test OnValid()
		public ConcreteCollection()	
		{
			IList listObj;
			listObj = this;
			listObj.Add(null);
		}

		// This constructor puts consecutive integers into the list
		public ConcreteCollection(int i) {
			IList listObj;
			listObj = this;

			int j;
			for (j = 0; j< i; j++) {
				listObj.Add(j);
			}
		}

		void CheckIfThrow ()
		{
			if (mustThrowException > 0) {
				mustThrowException--;
				if (mustThrowException == 0)
					throw new Exception ();
			}
		}
		
		// A helper method to look at a value in the list at a specific index
		public int PeekAt(int index)
		{
			IList listObj;
			listObj = this;
			return (int) listObj[index];
		}

		protected override void OnValidate (object value) {
			this.onValidateFired = true;
			CheckIfThrow ();
			base.OnValidate (value);
		}

		// Mark the flag if this hook is fired
		protected override void OnClear() {
			this.onClearFired = true;
			CheckIfThrow ();
		}

		// Mark the flag if this hook is fired
		protected override void OnClearComplete() 
		{
			this.onClearCompleteFired = true;
			CheckIfThrow ();
		}

		// Mark the flag, and save the paramter if this hook is fired
		protected override void OnInsert(int index, object value) 
		{
			this.onInsertFired = true;
			this.onInsertIndex = index;
			CheckIfThrow ();
		}

		// Mark the flag, and save the paramter if this hook is fired
		protected override void OnInsertComplete(int index, object value) 
		{
			this.onInsertCompleteFired = true;
			this.onInsertCompleteIndex = index;
			CheckIfThrow ();
		}
		
		// Mark the flag, and save the paramter if this hook is fired
		protected override void OnRemove(int index, object value) 
		{
			this.onRemoveFired = true;
			this.onRemoveIndex = index;
			CheckIfThrow ();
		}
		
		// Mark the flag, and save the paramter if this hook is fired
		protected override void OnRemoveComplete(int index, object value) 
		{
			this.onRemoveCompleteFired = true;
			this.onRemoveCompleteIndex = index;
			CheckIfThrow ();
		}
		
		// Mark the flag, and save the paramters if this hook is fired
		protected override void OnSet(int index, object oldValue, object newValue) 
		{
			this.onSetFired = true;
			this.onSetOldValue = (int) oldValue;
			this.onSetNewValue = (int) newValue;
			CheckIfThrow ();
		}
		
		// Mark the flag, and save the paramters if this hook is fired
		protected override void OnSetComplete(int index, object oldValue, object newValue) 
		{
			this.onSetCompleteFired = true;
			this.onSetCompleteOldValue = (int) oldValue;
			this.onSetCompleteNewValue = (int) newValue;
			CheckIfThrow ();
		}

		public IList BaseList {
			get { return base.List; }
		}
	}  // public class ConcreteCollection

	// Check the count property
	[Test]
	public void Count() {
		ConcreteCollection myCollection;
		myCollection = new ConcreteCollection(4);
		Assert.IsTrue (4 == myCollection.Count);
	}

	// Make sure GetEnumerator returns an object
	[Test]
	public void GetEnumerator() {
		ConcreteCollection myCollection;
		myCollection = new ConcreteCollection(4);
		Assert.IsTrue (null != myCollection.GetEnumerator());
	}

	// OnValid disallows nulls
	[Test]
	[ExpectedException(typeof(ArgumentNullException))]
	public void OnValid() {
		ConcreteCollection myCollection;
		myCollection = new ConcreteCollection();
	}

	// Test various Insert paths
	[Test]
	public void Insert() {
		ConcreteCollection myCollection;
		int numberOfItems;
		numberOfItems = 3;
		// The constructor inserts
		myCollection = new ConcreteCollection(numberOfItems);
		Assert.IsTrue (myCollection.onInsertFired);
		Assert.IsTrue (myCollection.onInsertCompleteFired);

		// Using the IList interface, check inserts in the middle
		IList listObj = myCollection;
		listObj.Insert(1, 9);
		Assert.IsTrue (myCollection.onInsertIndex == 1);
		Assert.IsTrue (myCollection.onInsertCompleteIndex == 1);
		Assert.IsTrue (myCollection.PeekAt(1) == 9);
	}

	// Test Clear and it's hooks
	[Test]
	public void Clear() 
	{
		ConcreteCollection myCollection;
		int numberOfItems;
		numberOfItems = 1;
		myCollection = new ConcreteCollection(numberOfItems);
		myCollection.Clear();
		Assert.IsTrue (myCollection.Count == 0);
		Assert.IsTrue (myCollection.onClearFired);
		Assert.IsTrue (myCollection.onClearCompleteFired);
	}

	// Test RemoveAt, other removes and the hooks
	[Test]
	public void Remove() 
	{
		ConcreteCollection myCollection;
		int numberOfItems;
		numberOfItems = 3;
		// Set up a test collection
		myCollection = new ConcreteCollection(numberOfItems);

		// The list is 0-based.  So if we remove the second one
		myCollection.RemoveAt(1);

		// We should see the original third one in it's place
		Assert.IsTrue (myCollection.PeekAt(1) == 2);
		Assert.IsTrue (myCollection.onRemoveFired);
		Assert.IsTrue (myCollection.onRemoveIndex == 1);
		Assert.IsTrue (myCollection.onRemoveCompleteFired);
		Assert.IsTrue (myCollection.onRemoveCompleteIndex == 1);
		IList listObj = myCollection;
		listObj.Remove(0);
		// Confirm parameters are being passed to the hooks
		Assert.IsTrue (myCollection.onRemoveIndex == 0);
		Assert.IsTrue (myCollection.onRemoveCompleteIndex == 0);
	}

	// Test the random access feature
	[Test]
	public void Set() 
	{
		ConcreteCollection myCollection;
		int numberOfItems;
		numberOfItems = 3;
		myCollection = new ConcreteCollection(numberOfItems);
		IList listObj = myCollection;
		listObj[0] = 99;
		Assert.IsTrue ((int) listObj[0] == 99);
		Assert.IsTrue (myCollection.onSetFired);
		Assert.IsTrue (myCollection.onSetCompleteFired);
		Assert.IsTrue (myCollection.onSetOldValue == 0);
		Assert.IsTrue (myCollection.onSetCompleteOldValue == 0);
		Assert.IsTrue (myCollection.onSetNewValue == 99);
		Assert.IsTrue (myCollection.onSetCompleteNewValue == 99);
	}

	[Test]
	public void InsertComplete_Add ()
	{
		ConcreteCollection coll = new ConcreteCollection (0);
		coll.mustThrowException = 1;

		try {
			coll.BaseList.Add (0);
		} catch {
		}
		Assert.AreEqual (0, coll.Count);
	}

	[Test]
	[ExpectedException (typeof (ArgumentOutOfRangeException))]
	public void ValidateCalled ()
	{
		ConcreteCollection coll = new ConcreteCollection (0);
		coll.mustThrowException = 1;

		try {
			coll.BaseList [5] = 8888;
		} catch (ArgumentOutOfRangeException) {
			throw;
		} finally {
			Assert.AreEqual (false, coll.onValidateFired);
		}
	}

	[Test]
	public void SetCompleteCalled ()
	{
		ConcreteCollection coll = new ConcreteCollection (0);

		coll.BaseList.Add (88);
		coll.mustThrowException = 1;
		try {
			coll.BaseList [0] = 11;
		} catch {
		} finally {
			Assert.AreEqual (false, coll.onSetCompleteFired);
		}
	}

	[Test]
	public void SetCompleteUndo ()
	{
		ConcreteCollection coll = new ConcreteCollection (0);

		bool throwsException = true;

		coll.BaseList.Add (88);
		coll.onValidateFired = false;
		coll.onInsertFired = false;
		coll.onSetCompleteFired = false;
		coll.mustThrowException = 3;
		try {
			coll.BaseList [0] = 11;
			throwsException = false;
		} catch {
		} finally {
			Assert.IsTrue (throwsException);
			Assert.IsTrue (coll.onValidateFired);
			Assert.IsTrue (coll.onSetFired);
			Assert.IsTrue (coll.onSetCompleteFired);
			Assert.AreEqual (88, coll.BaseList [0]);
		}
	}

	[Test]
	[ExpectedException (typeof (ArgumentException))]
	public void InvalidRemove ()
	{
		ConcreteCollection coll = new ConcreteCollection (0);
		coll.BaseList.Remove (10);
	}
}

}