PageRenderTime 61ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs

https://bitbucket.org/danipen/mono
C# | 1468 lines | 1173 code | 232 blank | 63 comment | 9 complexity | c2076a1f99a22bca732c46718625e298 MD5 | raw file
Possible License(s): Unlicense, Apache-2.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //
  2. // MonoTests.System.Collections.Generic.Test.ListTest
  3. //
  4. // Authors:
  5. // David Waite (mass@akuma.org)
  6. // Andres G. Aragoneses (andres.aragoneses@7digital.com)
  7. //
  8. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  9. // Copyright (C) 2005 David Waite (mass@akuma.org)
  10. // Copyright 2011 Xamarin Inc (http://www.xamarin.com).
  11. // Copyright 2012 7digital Ltd (http://www.7digital.com).
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. #if NET_2_0
  33. using System;
  34. using System.Collections;
  35. using System.Collections.Generic;
  36. using System.Collections.ObjectModel;
  37. using System.IO;
  38. using System.Runtime.Serialization.Formatters.Binary;
  39. using System.Text;
  40. using NUnit.Framework;
  41. namespace MonoTests.System.Collections.Generic {
  42. class GenericComparer<T> : IComparer<T> {
  43. private bool called = false;
  44. public bool Called {
  45. get {
  46. bool result = called;
  47. called = false;
  48. return called;
  49. }
  50. }
  51. public int Compare (T x, T y)
  52. {
  53. called = true;
  54. return 0;
  55. }
  56. }
  57. [TestFixture]
  58. public class ListTest
  59. {
  60. static byte [] _serializedList = new byte [] {
  61. 0x00, 0x01, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
  62. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x01, 0x00, 0x00, 0x00,
  63. 0x7e, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x43, 0x6f, 0x6c,
  64. 0x6c, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x2e, 0x47, 0x65,
  65. 0x6e, 0x65, 0x72, 0x69, 0x63, 0x2e, 0x4c, 0x69, 0x73, 0x74, 0x60,
  66. 0x31, 0x5b, 0x5b, 0x53, 0x79, 0x73, 0x74, 0x65, 0x6d, 0x2e, 0x49,
  67. 0x6e, 0x74, 0x33, 0x32, 0x2c, 0x20, 0x6d, 0x73, 0x63, 0x6f, 0x72,
  68. 0x6c, 0x69, 0x62, 0x2c, 0x20, 0x56, 0x65, 0x72, 0x73, 0x69, 0x6f,
  69. 0x6e, 0x3d, 0x32, 0x2e, 0x30, 0x2e, 0x30, 0x2e, 0x30, 0x2c, 0x20,
  70. 0x43, 0x75, 0x6c, 0x74, 0x75, 0x72, 0x65, 0x3d, 0x6e, 0x65, 0x75,
  71. 0x74, 0x72, 0x61, 0x6c, 0x2c, 0x20, 0x50, 0x75, 0x62, 0x6c, 0x69,
  72. 0x63, 0x4b, 0x65, 0x79, 0x54, 0x6f, 0x6b, 0x65, 0x6e, 0x3d, 0x62,
  73. 0x37, 0x37, 0x61, 0x35, 0x63, 0x35, 0x36, 0x31, 0x39, 0x33, 0x34,
  74. 0x65, 0x30, 0x38, 0x39, 0x5d, 0x5d, 0x03, 0x00, 0x00, 0x00, 0x06,
  75. 0x5f, 0x69, 0x74, 0x65, 0x6d, 0x73, 0x05, 0x5f, 0x73, 0x69, 0x7a,
  76. 0x65, 0x08, 0x5f, 0x76, 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x07,
  77. 0x00, 0x00, 0x08, 0x08, 0x08, 0x09, 0x02, 0x00, 0x00, 0x00, 0x03,
  78. 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0f, 0x02, 0x00, 0x00,
  79. 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00,
  80. 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  81. 0x0b };
  82. int [] _list1_contents;
  83. List <int> _list1;
  84. [SetUp]
  85. public void SetUp ()
  86. {
  87. // FIXME arrays currently do not support generic collection
  88. // interfaces
  89. _list1_contents = new int [] { 55, 50, 22, 80, 56, 52, 40, 63 };
  90. // _list1 = new List <int> (_list1_contents);
  91. _list1 = new List <int> (8);
  92. foreach (int i in _list1_contents)
  93. _list1.Add (i);
  94. }
  95. [Test] // This was for bug #74980
  96. public void InsertTest ()
  97. {
  98. List <string> test = new List <string> ();
  99. test.Insert (0, "a");
  100. test.Insert (0, "b");
  101. test.Insert (1, "c");
  102. Assert.AreEqual (3, test.Count);
  103. Assert.AreEqual ("b", test [0]);
  104. Assert.AreEqual ("c", test [1]);
  105. Assert.AreEqual ("a", test [2]);
  106. }
  107. [Test]
  108. public void InsertRangeTest ()
  109. {
  110. int count = _list1.Count;
  111. // FIXME arrays currently do not support generic collection
  112. // interfaces
  113. int [] items = {1, 2, 3};
  114. // List <int> newRange = new List <int> (items);
  115. List <int> newRange = new List <int> (3);
  116. foreach (int i in items)
  117. newRange.Add (i);
  118. _list1.InsertRange (1, newRange);
  119. Assert.AreEqual (count + 3, _list1.Count);
  120. Assert.AreEqual (55, _list1 [0]);
  121. Assert.AreEqual (1, _list1 [1]);
  122. Assert.AreEqual (2, _list1 [2]);
  123. Assert.AreEqual (3, _list1 [3]);
  124. Assert.AreEqual (50, _list1 [4]);
  125. newRange = new List <int> ();
  126. List <int> li = new List <int> ();
  127. li.Add (1);
  128. newRange.InsertRange (0, li);
  129. newRange.InsertRange (newRange.Count, li);
  130. Assert.AreEqual (2, newRange.Count);
  131. }
  132. [Test]
  133. public void InsertSelfTest()
  134. {
  135. List <int> range = new List <int> (5);
  136. for (int i = 0; i < 5; ++ i)
  137. range.Add (i);
  138. range.InsertRange(2, range);
  139. Assert.AreEqual (10, range.Count);
  140. Assert.AreEqual (0, range [0]);
  141. Assert.AreEqual (1, range [1]);
  142. Assert.AreEqual (0, range [2]);
  143. Assert.AreEqual (1, range [3]);
  144. Assert.AreEqual (2, range [4]);
  145. Assert.AreEqual (3, range [5]);
  146. Assert.AreEqual (4, range [6]);
  147. Assert.AreEqual (2, range [7]);
  148. Assert.AreEqual (3, range [8]);
  149. Assert.AreEqual (4, range [9]);
  150. }
  151. [Test, ExpectedException (typeof (ArgumentNullException))]
  152. public void InsertRangeNullTest ()
  153. {
  154. IEnumerable <int> n = null;
  155. _list1.InsertRange (0, n);
  156. }
  157. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  158. public void InsertRangeNegativeIndexTest ()
  159. {
  160. _list1.InsertRange (-1, _list1);
  161. }
  162. [Test]
  163. public void IndexOfTest ()
  164. {
  165. List <int> l = new List <int> ();
  166. l.Add (100);
  167. l.Add (200);
  168. Assert.AreEqual (1, l.IndexOf (200), "Could not find value");
  169. }
  170. [Test, ExpectedException(typeof (ArgumentException))]
  171. public void IList_InsertInvalidType1 ()
  172. {
  173. IList list = _list1 as IList;
  174. list.Insert(0, new object());
  175. }
  176. [Test, ExpectedException(typeof (ArgumentException))]
  177. public void IList_InsertInvalidType2 ()
  178. {
  179. IList list = _list1 as IList;
  180. list.Insert(0, null);
  181. }
  182. [Test, ExpectedException(typeof (ArgumentException))]
  183. public void IList_AddInvalidType1()
  184. {
  185. IList list = _list1 as IList;
  186. list.Add(new object());
  187. }
  188. [Test, ExpectedException(typeof (ArgumentException))]
  189. public void IList_AddInvalidType2()
  190. {
  191. IList list = _list1 as IList;
  192. list.Add(null);
  193. }
  194. [Test]
  195. public void IList_RemoveInvalidType()
  196. {
  197. IList list = _list1 as IList;
  198. int nCount = list.Count;
  199. list.Remove(new object());
  200. Assert.AreEqual(nCount, list.Count);
  201. list.Remove(null);
  202. Assert.AreEqual(nCount, list.Count);
  203. }
  204. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  205. public void IndexOfOutOfRangeTest ()
  206. {
  207. List <int> l = new List <int> (4);
  208. l.IndexOf (0, 0, 4);
  209. }
  210. [Test]
  211. public void GetRangeTest ()
  212. {
  213. List <int> r = _list1.GetRange (2, 4);
  214. Assert.AreEqual (4, r.Count);
  215. Assert.AreEqual (22, r [0]);
  216. Assert.AreEqual (80, r [1]);
  217. Assert.AreEqual (56, r [2]);
  218. Assert.AreEqual (52, r [3]);
  219. }
  220. [Test]
  221. public void EnumeratorTest ()
  222. {
  223. List <int>.Enumerator e = _list1.GetEnumerator ();
  224. for (int i = 0; i < _list1_contents.Length; i++)
  225. {
  226. Assert.IsTrue (e.MoveNext ());
  227. Assert.AreEqual (_list1_contents [i], e.Current);
  228. }
  229. Assert.IsFalse (e.MoveNext ());
  230. }
  231. [Test]
  232. public void ConstructWithSizeTest ()
  233. {
  234. List <object> l_1 = new List <object> (1);
  235. List <object> l_2 = new List <object> (50);
  236. List <object> l_3 = new List <object> (0);
  237. Assert.AreEqual (1, l_1.Capacity);
  238. Assert.AreEqual (50, l_2.Capacity);
  239. Assert.AreEqual (0, l_3.Capacity);
  240. }
  241. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  242. public void ConstructWithInvalidSizeTest ()
  243. {
  244. List <int> l = new List <int> (-1);
  245. }
  246. [Test]
  247. public void ConstructWithCollectionTest ()
  248. {
  249. List <int> l1 = new List <int> (_list1);
  250. Assert.AreEqual (_list1.Count, l1.Count);
  251. Assert.AreEqual (l1.Count, l1.Capacity);
  252. for (int i = 0; i < l1.Count; i++)
  253. Assert.AreEqual (_list1 [i], l1 [i]);
  254. }
  255. [Test, ExpectedException (typeof (ArgumentNullException))]
  256. public void ConstructWithInvalidCollectionTest ()
  257. {
  258. List <int> n = null;
  259. List <int> l1 = new List <int> (n);
  260. }
  261. [Test]
  262. public void AddTest ()
  263. {
  264. int count = _list1.Count;
  265. _list1.Add (-1);
  266. Assert.AreEqual (count + 1, _list1.Count);
  267. Assert.AreEqual (-1, _list1 [_list1.Count - 1]);
  268. }
  269. [Test]
  270. public void AddRangeTest ()
  271. {
  272. int count = _list1.Count;
  273. // FIXME arrays currently do not support generic collection
  274. // interfaces
  275. int [] range = { -1, -2, -3 };
  276. List <int> tmp = new List <int> (3);
  277. foreach (int i in range)
  278. tmp.Add (i);
  279. // _list1.AddRange (range);
  280. _list1.AddRange (tmp);
  281. Assert.AreEqual (count + 3, _list1.Count);
  282. Assert.AreEqual (-1, _list1 [_list1.Count - 3]);
  283. Assert.AreEqual (-2, _list1 [_list1.Count - 2]);
  284. Assert.AreEqual (-3, _list1 [_list1.Count - 1]);
  285. }
  286. [Test, ExpectedException (typeof (ArgumentNullException))]
  287. public void AddNullRangeTest ()
  288. {
  289. int [] n = null;
  290. _list1.AddRange (n);
  291. }
  292. [Test]
  293. public void BinarySearchTest ()
  294. {
  295. List <int> l = new List <int> (_list1);
  296. l.Sort ();
  297. Assert.AreEqual (0, l.BinarySearch (22));
  298. Assert.AreEqual (-2, l.BinarySearch (23));
  299. Assert.AreEqual (- (l.Count + 1), l.BinarySearch (int.MaxValue));
  300. }
  301. #if !NET_4_0 // FIXME: the blob contains the 2.0 mscorlib version
  302. [Test]
  303. [Category ("TargetJvmNotWorking")]
  304. public void SerializeTest ()
  305. {
  306. List <int> list = new List <int> ();
  307. list.Add (5);
  308. list.Add (0);
  309. list.Add (7);
  310. #if TARGET_JVM
  311. BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
  312. #else
  313. BinaryFormatter bf = new BinaryFormatter ();
  314. #endif // TARGET_JVM
  315. MemoryStream ms = new MemoryStream ();
  316. bf.Serialize (ms, list);
  317. byte [] buffer = new byte [ms.Length];
  318. ms.Position = 0;
  319. ms.Read (buffer, 0, buffer.Length);
  320. Assert.AreEqual (_serializedList, buffer);
  321. }
  322. #endif
  323. [Test]
  324. [Category ("TargetJvmNotWorking")]
  325. public void DeserializeTest ()
  326. {
  327. MemoryStream ms = new MemoryStream ();
  328. ms.Write (_serializedList, 0, _serializedList.Length);
  329. ms.Position = 0;
  330. #if TARGET_JVM
  331. BinaryFormatter bf = (BinaryFormatter)vmw.@internal.remoting.BinaryFormatterUtils.CreateBinaryFormatter (false);
  332. #else
  333. BinaryFormatter bf = new BinaryFormatter ();
  334. #endif // TARGET_JVM
  335. List<int> list = (List<int>) bf.Deserialize (ms);
  336. Assert.AreEqual (3, list.Count, "#1");
  337. Assert.AreEqual (5, list [0], "#2");
  338. Assert.AreEqual (0, list [1], "#3");
  339. Assert.AreEqual (7, list [2], "#4");
  340. }
  341. [Test]
  342. public void SortTest ()
  343. {
  344. List <int> l = new List <int> (_list1);
  345. l.Sort ();
  346. Assert.AreEqual (_list1.Count, l.Count);
  347. Assert.AreEqual (22, l [0]);
  348. int minimum = 22;
  349. foreach (int i in l)
  350. {
  351. Assert.IsTrue (minimum <= i);
  352. minimum = i;
  353. }
  354. }
  355. [Test]
  356. public void ClearTest ()
  357. {
  358. int capacity = _list1.Capacity;
  359. _list1.Clear ();
  360. Assert.AreEqual (0, _list1.Count);
  361. Assert.AreEqual (capacity, _list1.Capacity);
  362. }
  363. [Test]
  364. public void ContainsTest ()
  365. {
  366. Assert.IsTrue (_list1.Contains (22));
  367. Assert.IsFalse (_list1.Contains (23));
  368. }
  369. private string StringConvert (int i)
  370. {
  371. return i.ToString ();
  372. }
  373. [Test]
  374. public void ConvertAllTest ()
  375. {
  376. List <string> s = _list1.ConvertAll ( (Converter <int, string>)StringConvert);
  377. Assert.AreEqual (_list1.Count, s.Count);
  378. Assert.AreEqual ("55", s [0]);
  379. }
  380. [Test]
  381. public void CopyToTest ()
  382. {
  383. int [] a = new int [2];
  384. _list1.CopyTo (1, a, 0, 2);
  385. Assert.AreEqual (50, a [0]);
  386. Assert.AreEqual (22, a [1]);
  387. int [] b = new int [_list1.Count + 1];
  388. b [_list1.Count] = 555;
  389. _list1.CopyTo (b);
  390. Assert.AreEqual (55, b [0]);
  391. Assert.AreEqual (555, b [_list1.Count]);
  392. b [0] = 888;
  393. _list1.CopyTo (b, 1);
  394. Assert.AreEqual (888, b [0]);
  395. Assert.AreEqual (55, b [1]);
  396. }
  397. [Test, ExpectedException (typeof (ArgumentNullException))]
  398. public void CopyToNullTest ()
  399. {
  400. int [] a = null;
  401. _list1.CopyTo (0, a, 0, 0);
  402. }
  403. static bool FindMultipleOfThree (int i)
  404. {
  405. return (i % 3) == 0;
  406. }
  407. static bool FindMultipleOfFour (int i)
  408. {
  409. return (i % 4) == 0;
  410. }
  411. static bool FindMultipleOfTwelve (int i)
  412. {
  413. return (i % 12) == 0;
  414. }
  415. [Test]
  416. public void FindTest ()
  417. {
  418. int i = _list1.Find (FindMultipleOfThree);
  419. Assert.AreEqual (63, i);
  420. i = _list1.Find (FindMultipleOfTwelve);
  421. Assert.AreEqual (default (int), i);
  422. }
  423. [Test, ExpectedException (typeof (ArgumentNullException))]
  424. public void FindNullTest ()
  425. {
  426. int i = _list1.Find (null);
  427. }
  428. [Test]
  429. public void FindAllSmallTest ()
  430. {
  431. List <int> findings = _list1.FindAll (FindMultipleOfFour);
  432. Assert.AreEqual (4, findings.Count);
  433. Assert.AreEqual (80, findings [0]);
  434. Assert.AreEqual (56, findings [1]);
  435. Assert.AreEqual (52, findings [2]);
  436. Assert.AreEqual (40, findings [3]);
  437. findings = _list1.FindAll (FindMultipleOfTwelve);
  438. Assert.IsNotNull (findings);
  439. Assert.AreEqual (0, findings.Count);
  440. }
  441. [Test]
  442. public void FindAllMediumTest ()
  443. {
  444. List <int> integers = new List <int> (10000);
  445. for (int i = 1; i <= 10000; i++)
  446. integers.Add (i);
  447. List <int> results = integers.FindAll (FindMultipleOfFour);
  448. Assert.IsNotNull (results);
  449. Assert.AreEqual (2500, results.Count);
  450. results = integers.FindAll (FindMultipleOfTwelve);
  451. Assert.IsNotNull (results);
  452. Assert.AreEqual (833, results.Count);
  453. }
  454. [Test]
  455. public void FindAllLargeTest ()
  456. {
  457. List <int> integers = new List <int> (70000);
  458. for (int i = 1; i <= 80000; i++)
  459. integers.Add (i);
  460. List <int> results = integers.FindAll (FindMultipleOfFour);
  461. Assert.IsNotNull (results);
  462. Assert.AreEqual (20000, results.Count);
  463. results = integers.FindAll (FindMultipleOfTwelve);
  464. Assert.IsNotNull (results);
  465. Assert.AreEqual (6666, results.Count);
  466. }
  467. [Test, ExpectedException (typeof (ArgumentNullException))]
  468. public void FindAllNullTest ()
  469. {
  470. List <int> findings = _list1.FindAll (null);
  471. }
  472. [Test]
  473. public void FindIndexTest ()
  474. {
  475. int i = _list1.FindIndex (FindMultipleOfThree);
  476. Assert.AreEqual (7, i);
  477. i = _list1.FindIndex (FindMultipleOfTwelve);
  478. Assert.AreEqual (-1, i);
  479. }
  480. [Test, ExpectedException (typeof (ArgumentNullException))]
  481. public void FindIndexNullTest ()
  482. {
  483. int i = _list1.FindIndex (null);
  484. }
  485. [Test]
  486. public void FindLastTest ()
  487. {
  488. int i = _list1.FindLast (FindMultipleOfFour);
  489. Assert.AreEqual (40, i);
  490. i = _list1.FindLast (FindMultipleOfTwelve);
  491. Assert.AreEqual (default (int), i);
  492. }
  493. [Test, ExpectedException (typeof (ArgumentNullException))]
  494. public void FindLastNullTest ()
  495. {
  496. int i = _list1.FindLast (null);
  497. }
  498. // FIXME currently generates Invalid IL Code error
  499. /*
  500. [Test]
  501. public void ForEachTest ()
  502. {
  503. int i = 0;
  504. _list1.ForEach (delegate (int j) { i += j; });
  505. Assert.AreEqual (418, i);
  506. }
  507. */
  508. [Test]
  509. public void FindLastIndexTest ()
  510. {
  511. int i = _list1.FindLastIndex (FindMultipleOfFour);
  512. Assert.AreEqual (6, i);
  513. i = _list1.FindLastIndex (5, FindMultipleOfFour);
  514. Assert.AreEqual (5, i);
  515. i = _list1.FindIndex (FindMultipleOfTwelve);
  516. Assert.AreEqual (-1, i);
  517. }
  518. [Test, ExpectedException (typeof (ArgumentNullException))]
  519. public void FindLastIndexNullTest ()
  520. {
  521. int i = _list1.FindLastIndex (null);
  522. }
  523. [Test]
  524. public void RemoveTest ()
  525. {
  526. int count = _list1.Count;
  527. bool result = _list1.Remove (22);
  528. Assert.IsTrue (result);
  529. Assert.AreEqual (count - 1, _list1.Count);
  530. Assert.AreEqual (-1, _list1.IndexOf (22));
  531. result = _list1.Remove (0);
  532. Assert.IsFalse (result);
  533. }
  534. [Test]
  535. public void RemoveAllTest ()
  536. {
  537. int count = _list1.Count;
  538. int removedCount = _list1.RemoveAll (FindMultipleOfFour);
  539. Assert.AreEqual (4, removedCount);
  540. Assert.AreEqual (count - 4, _list1.Count);
  541. removedCount = _list1.RemoveAll (FindMultipleOfTwelve);
  542. Assert.AreEqual (0, removedCount);
  543. Assert.AreEqual (count - 4, _list1.Count);
  544. }
  545. [Test]
  546. public void RemoveAtTest ()
  547. {
  548. int count = _list1.Count;
  549. _list1.RemoveAt (0);
  550. Assert.AreEqual (count - 1, _list1.Count);
  551. Assert.AreEqual (50, _list1 [0]);
  552. }
  553. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  554. public void RemoveOutOfRangeTest ()
  555. {
  556. _list1.RemoveAt (_list1.Count);
  557. }
  558. [Test]
  559. public void RemoveRangeTest ()
  560. {
  561. int count = _list1.Count;
  562. _list1.RemoveRange (1, 2);
  563. Assert.AreEqual (count - 2, _list1.Count);
  564. Assert.AreEqual (55, _list1 [0]);
  565. Assert.AreEqual (80, _list1 [1]);
  566. _list1.RemoveRange (0, 0);
  567. Assert.AreEqual (count - 2, _list1.Count);
  568. }
  569. [Test]
  570. public void RemoveRangeFromEmptyListTest ()
  571. {
  572. List<int> l = new List<int> ();
  573. l.RemoveRange (0, 0);
  574. }
  575. [Test, ExpectedException (typeof (ArgumentException))]
  576. public void RemoveRangeOutOfRangeTest ()
  577. {
  578. _list1.RemoveRange (1, _list1.Count);
  579. }
  580. [Test]
  581. public void ReverseTest ()
  582. {
  583. int count = _list1.Count;
  584. _list1.Reverse ();
  585. Assert.AreEqual (count, _list1.Count);
  586. Assert.AreEqual (63, _list1 [0]);
  587. Assert.AreEqual (55, _list1 [count - 1]);
  588. _list1.Reverse (0, 2);
  589. Assert.AreEqual (40, _list1 [0]);
  590. Assert.AreEqual (63, _list1 [1]);
  591. }
  592. [Test, ExpectedException (typeof (ArgumentException))]
  593. public void ReverseOutOfRangeTest ()
  594. {
  595. _list1.Reverse (1, _list1.Count);
  596. }
  597. [Test]
  598. public void ToArrayTest ()
  599. {
  600. int [] copiedContents = _list1.ToArray ();
  601. Assert.IsFalse (ReferenceEquals (copiedContents, _list1_contents));
  602. Assert.AreEqual (_list1.Count, copiedContents.Length);
  603. Assert.AreEqual (_list1 [0], copiedContents [0]);
  604. }
  605. [Test]
  606. public void TrimExcessTest ()
  607. {
  608. List <string> l = new List <string> ();
  609. l.Add ("foo");
  610. Assert.IsTrue (l.Count < l.Capacity);
  611. l.TrimExcess ();
  612. Assert.AreEqual (l.Count, l.Capacity);
  613. }
  614. bool IsPositive (int i)
  615. {
  616. return i >= 0;
  617. }
  618. [Test]
  619. public void TrueForAllTest ()
  620. {
  621. Assert.IsFalse (_list1.TrueForAll (FindMultipleOfFour));
  622. Assert.IsTrue (_list1.TrueForAll (IsPositive));
  623. }
  624. [Test, ExpectedException (typeof (ArgumentNullException))]
  625. public void TrueForAllNullTest ()
  626. {
  627. _list1.TrueForAll (null);
  628. }
  629. [Test, ExpectedException (typeof (ArgumentOutOfRangeException))]
  630. public void CapacityOutOfRangeTest ()
  631. {
  632. _list1.Capacity = _list1.Count - 1;
  633. }
  634. [Test]
  635. public void BinarySearch_EmptyList ()
  636. {
  637. GenericComparer<int> comparer = new GenericComparer<int> ();
  638. List<int> l = new List<int> ();
  639. Assert.AreEqual (-1, l.BinarySearch (0, comparer), "BinarySearch");
  640. // bug 77030 - the comparer isn't called for an empty array/list
  641. Assert.IsFalse (comparer.Called, "Called");
  642. }
  643. [Test]
  644. public void BinarySearch2_EmptyList ()
  645. {
  646. GenericComparer<int> comparer = new GenericComparer<int> ();
  647. List<int> l = new List<int> ();
  648. Assert.AreEqual (-1, l.BinarySearch (0, 0, 0, comparer), "BinarySearch");
  649. // bug 77030 - the comparer isn't called for an empty array/list
  650. Assert.IsFalse (comparer.Called, "Called");
  651. }
  652. [Test]
  653. public void AddRange_Bug77019 ()
  654. {
  655. List<int> l = new List<int> ();
  656. Dictionary<string, int> d = new Dictionary<string, int> ();
  657. l.AddRange (d.Values);
  658. Assert.AreEqual (0, l.Count, "Count");
  659. }
  660. [Test]
  661. public void VersionCheck_Add ()
  662. {
  663. List<int> list = new List<int> ();
  664. IEnumerator enumerator = list.GetEnumerator ();
  665. list.Add (5);
  666. try {
  667. enumerator.MoveNext ();
  668. Assert.Fail ("#1");
  669. } catch (InvalidOperationException) {
  670. }
  671. try {
  672. enumerator.Reset ();
  673. Assert.Fail ("#2");
  674. } catch (InvalidOperationException) {
  675. }
  676. enumerator = list.GetEnumerator ();
  677. enumerator.MoveNext ();
  678. }
  679. [Test]
  680. public void VersionCheck_AddRange ()
  681. {
  682. List<int> list = new List<int> ();
  683. IEnumerator enumerator = list.GetEnumerator ();
  684. list.AddRange (new int [] { 5, 7 });
  685. try {
  686. enumerator.MoveNext ();
  687. Assert.Fail ("#1");
  688. } catch (InvalidOperationException) {
  689. }
  690. try {
  691. enumerator.Reset ();
  692. Assert.Fail ("#2");
  693. } catch (InvalidOperationException) {
  694. }
  695. enumerator = list.GetEnumerator ();
  696. enumerator.MoveNext ();
  697. }
  698. [Test]
  699. public void VersionCheck_Clear ()
  700. {
  701. List<int> list = new List<int> ();
  702. IEnumerator enumerator = list.GetEnumerator ();
  703. list.Clear ();
  704. try {
  705. enumerator.MoveNext ();
  706. Assert.Fail ("#1");
  707. } catch (InvalidOperationException) {
  708. }
  709. try {
  710. enumerator.Reset ();
  711. Assert.Fail ("#2");
  712. } catch (InvalidOperationException) {
  713. }
  714. enumerator = list.GetEnumerator ();
  715. enumerator.MoveNext ();
  716. }
  717. [Test]
  718. public void VersionCheck_Insert ()
  719. {
  720. List<int> list = new List<int> ();
  721. IEnumerator enumerator = list.GetEnumerator ();
  722. list.Insert (0, 7);
  723. try {
  724. enumerator.MoveNext ();
  725. Assert.Fail ("#1");
  726. } catch (InvalidOperationException) {
  727. }
  728. try {
  729. enumerator.Reset ();
  730. Assert.Fail ("#2");
  731. } catch (InvalidOperationException) {
  732. }
  733. enumerator = list.GetEnumerator ();
  734. enumerator.MoveNext ();
  735. }
  736. [Test]
  737. public void VersionCheck_InsertRange ()
  738. {
  739. List<int> list = new List<int> ();
  740. IEnumerator enumerator = list.GetEnumerator ();
  741. list.InsertRange (0, new int [] { 5, 7 });
  742. try {
  743. enumerator.MoveNext ();
  744. Assert.Fail ("#1");
  745. } catch (InvalidOperationException) {
  746. }
  747. try {
  748. enumerator.Reset ();
  749. Assert.Fail ("#2");
  750. } catch (InvalidOperationException) {
  751. }
  752. enumerator = list.GetEnumerator ();
  753. enumerator.MoveNext ();
  754. }
  755. [Test]
  756. public void VersionCheck_Remove ()
  757. {
  758. List<int> list = new List<int> ();
  759. list.Add (5);
  760. IEnumerator enumerator = list.GetEnumerator ();
  761. // version number is not incremented if item does not exist in list
  762. list.Remove (7);
  763. enumerator.MoveNext ();
  764. list.Remove (5);
  765. try {
  766. enumerator.MoveNext ();
  767. Assert.Fail ("#1");
  768. } catch (InvalidOperationException) {
  769. }
  770. try {
  771. enumerator.Reset ();
  772. Assert.Fail ("#2");
  773. } catch (InvalidOperationException) {
  774. }
  775. enumerator = list.GetEnumerator ();
  776. enumerator.MoveNext ();
  777. }
  778. [Test]
  779. public void VersionCheck_RemoveAll ()
  780. {
  781. List<int> list = new List<int> ();
  782. list.Add (5);
  783. IEnumerator enumerator = list.GetEnumerator ();
  784. // version is not incremented if there are no items to remove
  785. list.RemoveAll (FindMultipleOfFour);
  786. enumerator.MoveNext ();
  787. list.Add (4);
  788. enumerator = list.GetEnumerator ();
  789. list.RemoveAll (FindMultipleOfFour);
  790. try {
  791. enumerator.MoveNext ();
  792. Assert.Fail ("#1");
  793. } catch (InvalidOperationException) {
  794. }
  795. try {
  796. enumerator.Reset ();
  797. Assert.Fail ("#2");
  798. } catch (InvalidOperationException) {
  799. }
  800. enumerator = list.GetEnumerator ();
  801. enumerator.MoveNext ();
  802. }
  803. [Test]
  804. public void VersionCheck_RemoveAt ()
  805. {
  806. List<int> list = new List<int> ();
  807. list.Add (5);
  808. IEnumerator enumerator = list.GetEnumerator ();
  809. list.RemoveAt (0);
  810. try {
  811. enumerator.MoveNext ();
  812. Assert.Fail ("#1");
  813. } catch (InvalidOperationException) {
  814. }
  815. try {
  816. enumerator.Reset ();
  817. Assert.Fail ("#2");
  818. } catch (InvalidOperationException) {
  819. }
  820. enumerator = list.GetEnumerator ();
  821. enumerator.MoveNext ();
  822. }
  823. [Test]
  824. public void VersionCheck_RemoveRange ()
  825. {
  826. List<int> list = new List<int> ();
  827. list.Add (5);
  828. IEnumerator enumerator = list.GetEnumerator ();
  829. // version is not incremented if count is zero
  830. list.RemoveRange (0, 0);
  831. enumerator.MoveNext ();
  832. enumerator.Reset ();
  833. list.RemoveRange (0, 1);
  834. try {
  835. enumerator.MoveNext ();
  836. Assert.Fail ("#1");
  837. } catch (InvalidOperationException) {
  838. }
  839. try {
  840. enumerator.Reset ();
  841. Assert.Fail ("#2");
  842. } catch (InvalidOperationException) {
  843. }
  844. enumerator = list.GetEnumerator ();
  845. enumerator.MoveNext ();
  846. }
  847. [Test, ExpectedException (typeof (InvalidOperationException))] // #699182
  848. public void VersionCheck_Indexer ()
  849. {
  850. var list = new List<int> () { 0, 2, 3 };
  851. var enumerator = list.GetEnumerator ();
  852. list [0] = 1;
  853. enumerator.MoveNext ();
  854. }
  855. [Test]
  856. public void VersionCheck_Reverse ()
  857. {
  858. List<int> list = new List<int> ();
  859. IEnumerator enumerator = list.GetEnumerator ();
  860. list.Reverse ();
  861. try {
  862. enumerator.MoveNext ();
  863. Assert.Fail ("#A1");
  864. } catch (InvalidOperationException) {
  865. }
  866. try {
  867. enumerator.Reset ();
  868. Assert.Fail ("#A2");
  869. } catch (InvalidOperationException) {
  870. }
  871. enumerator = list.GetEnumerator ();
  872. list.Reverse (0, 0);
  873. try {
  874. enumerator.MoveNext ();
  875. Assert.Fail ("#B1");
  876. } catch (InvalidOperationException) {
  877. }
  878. try {
  879. enumerator.Reset ();
  880. Assert.Fail ("#B2");
  881. } catch (InvalidOperationException) {
  882. }
  883. enumerator = list.GetEnumerator ();
  884. enumerator.MoveNext ();
  885. }
  886. class SortTestComparer: IComparer<string> {
  887. public int Compare (string s1, string s2)
  888. {
  889. return String.Compare (s1, s2);
  890. }
  891. }
  892. [Test]
  893. public void Sort_Bug76361 ()
  894. {
  895. SortTestComparer comparer = new SortTestComparer ();
  896. List<string> l = new List<string> ();
  897. l.Add ("foo");
  898. l.Add ("bar");
  899. l.Sort (comparer);
  900. Assert.AreEqual ("bar", l[0], "0");
  901. Assert.AreEqual ("foo", l[1], "1");
  902. Assert.AreEqual (2, l.Count, "Count");
  903. }
  904. // for bug #77039 test case
  905. class GenericIComparable: IComparable<GenericIComparable> {
  906. private int _NumberToSortOn;
  907. public int NumberToSortOn {
  908. get { return _NumberToSortOn; }
  909. set { _NumberToSortOn = value; }
  910. }
  911. public GenericIComparable (int val)
  912. {
  913. _NumberToSortOn = val;
  914. }
  915. public int CompareTo (GenericIComparable other)
  916. {
  917. return NumberToSortOn.CompareTo (other.NumberToSortOn);
  918. }
  919. }
  920. [Test]
  921. public void Sort_GenericIComparable_Bug77039 ()
  922. {
  923. List<GenericIComparable> l = new List<GenericIComparable> ();
  924. l.Add (new GenericIComparable (2));
  925. l.Add (new GenericIComparable (1));
  926. l.Add (new GenericIComparable (3));
  927. l.Sort ();
  928. Assert.AreEqual (1, l[0].NumberToSortOn, "0");
  929. Assert.AreEqual (2, l[1].NumberToSortOn, "1");
  930. Assert.AreEqual (3, l[2].NumberToSortOn, "2");
  931. }
  932. class NonGenericIComparable: IComparable {
  933. private int _NumberToSortOn;
  934. public int NumberToSortOn {
  935. get { return _NumberToSortOn; }
  936. set { _NumberToSortOn = value; }
  937. }
  938. public NonGenericIComparable (int val)
  939. {
  940. _NumberToSortOn = val;
  941. }
  942. public int CompareTo (object obj)
  943. {
  944. return NumberToSortOn.CompareTo ((obj as NonGenericIComparable).NumberToSortOn);
  945. }
  946. }
  947. [Test]
  948. public void Sort_NonGenericIComparable ()
  949. {
  950. List<NonGenericIComparable> l = new List<NonGenericIComparable> ();
  951. l.Add (new NonGenericIComparable (2));
  952. l.Add (new NonGenericIComparable (1));
  953. l.Add (new NonGenericIComparable (3));
  954. l.Sort ();
  955. Assert.AreEqual (1, l[0].NumberToSortOn, "0");
  956. Assert.AreEqual (2, l[1].NumberToSortOn, "1");
  957. Assert.AreEqual (3, l[2].NumberToSortOn, "2");
  958. }
  959. class NonComparable {
  960. }
  961. [Test]
  962. public void Sort_GenericNonIComparable ()
  963. {
  964. List<NonComparable> l = new List<NonComparable> ();
  965. l.Sort ();
  966. // no element -> no sort -> no exception
  967. l.Add (new NonComparable ());
  968. l.Sort ();
  969. // one element -> no sort -> no exception
  970. }
  971. [Test]
  972. [ExpectedException (typeof (InvalidOperationException))]
  973. public void Sort_GenericNonIComparable_2 ()
  974. {
  975. List<NonComparable> l = new List<NonComparable> ();
  976. l.Add (new NonComparable ());
  977. l.Add (new NonComparable ());
  978. l.Sort ();
  979. // two element -> sort -> exception!
  980. }
  981. [Test]
  982. public void IList_Contains_InvalidType()
  983. {
  984. List<string> list = new List<string>();
  985. list.Add("foo");
  986. Assert.IsFalse (((IList)list).Contains(new object()));
  987. Assert.IsFalse (((IList)_list1).Contains(null));
  988. }
  989. [Test]
  990. public void IList_IndexOf_InvalidType()
  991. {
  992. List<string> list = new List<string>();
  993. list.Add("foo");
  994. Assert.AreEqual (-1, ((IList)list).IndexOf(new object()));
  995. Assert.AreEqual (-1, ((IList)_list1).IndexOf(null));
  996. }
  997. // for bug #77277 test case
  998. [Test]
  999. public void Test_ContainsAndIndexOf_EquatableItem ()
  1000. {
  1001. List<EquatableClass> list = new List<EquatableClass> ();
  1002. EquatableClass item0 = new EquatableClass (0);
  1003. EquatableClass item1 = new EquatableClass (1);
  1004. list.Add (item0);
  1005. list.Add (item1);
  1006. list.Add (item0);
  1007. Assert.AreEqual (true, list.Contains (item0), "#0");
  1008. Assert.AreEqual (true, list.Contains (new EquatableClass (0)), "#1");
  1009. Assert.AreEqual (0, list.IndexOf (item0), "#2");
  1010. Assert.AreEqual (0, list.IndexOf (new EquatableClass (0)), "#3");
  1011. Assert.AreEqual (2, list.LastIndexOf (item0), "#4");
  1012. Assert.AreEqual (2, list.LastIndexOf (new EquatableClass (0)), "#5");
  1013. }
  1014. // for bug #81387 test case
  1015. [Test]
  1016. public void Test_Contains_After_Remove ()
  1017. {
  1018. List<int> list = new List<int> ();
  1019. list.Add (2);
  1020. list.Remove (2);
  1021. Assert.AreEqual (false, list.Contains (2), "#0");
  1022. }
  1023. [Test]
  1024. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1025. public void SetItem_OutOfRange()
  1026. {
  1027. List<string> list = new List<string>();
  1028. list[0] = "foo";
  1029. }
  1030. [Test]
  1031. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1032. public void SetItem_IList_OutOfRange()
  1033. {
  1034. IList<string> list = new List<string>();
  1035. list[0] = "foo";
  1036. }
  1037. public class EquatableClass : IEquatable<EquatableClass>
  1038. {
  1039. int _x;
  1040. public EquatableClass (int x)
  1041. {
  1042. _x = x;
  1043. }
  1044. public bool Equals (EquatableClass other)
  1045. {
  1046. return this._x == other._x;
  1047. }
  1048. }
  1049. delegate void D ();
  1050. bool Throws (D d)
  1051. {
  1052. try {
  1053. d ();
  1054. return false;
  1055. } catch {
  1056. return true;
  1057. }
  1058. }
  1059. [Test]
  1060. // based on #491858, #517415
  1061. public void Enumerator_Current ()
  1062. {
  1063. var e1 = new List<int>.Enumerator ();
  1064. Assert.IsFalse (Throws (delegate { var x = e1.Current; }));
  1065. var d = new List<int> ();
  1066. var e2 = d.GetEnumerator ();
  1067. Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
  1068. e2.MoveNext ();
  1069. Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
  1070. e2.Dispose ();
  1071. Assert.IsFalse (Throws (delegate { var x = e2.Current; }));
  1072. var e3 = ((IEnumerable<int>) d).GetEnumerator ();
  1073. Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
  1074. e3.MoveNext ();
  1075. Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
  1076. e3.Dispose ();
  1077. Assert.IsFalse (Throws (delegate { var x = e3.Current; }));
  1078. var e4 = ((IEnumerable) d).GetEnumerator ();
  1079. Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
  1080. e4.MoveNext ();
  1081. Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
  1082. ((IDisposable) e4).Dispose ();
  1083. Assert.IsTrue (Throws (delegate { var x = e4.Current; }));
  1084. }
  1085. [Test] //bug #672907
  1086. public void ICollectionCopyToExceptions ()
  1087. {
  1088. var l = new List <int> ();
  1089. ICollection x = l;
  1090. try {
  1091. x.CopyTo (null, 0);
  1092. Assert.Fail ("#1");
  1093. } catch (Exception e) {
  1094. Assert.IsTrue (e is ArgumentNullException, "#2");
  1095. }
  1096. try {
  1097. x.CopyTo (new int [10], -1);
  1098. Assert.Fail ("#3");
  1099. } catch (Exception e) {
  1100. Assert.IsTrue (e is ArgumentOutOfRangeException, "#4");
  1101. }
  1102. try {
  1103. x.CopyTo (new int [10, 1], 0);
  1104. Assert.Fail ("#5");
  1105. } catch (Exception e) {
  1106. Assert.IsTrue (e is ArgumentException, "#6");
  1107. }
  1108. try {
  1109. x.CopyTo (Array.CreateInstance (typeof (int), new int [] { 10 }, new int[] { 1 }), 0);
  1110. Assert.Fail ("#7");
  1111. } catch (Exception e) {
  1112. Assert.IsTrue (e is ArgumentException, "#8");
  1113. }
  1114. l.Add (10); l.Add (20);
  1115. try {
  1116. x.CopyTo (new int [1], 0);
  1117. Assert.Fail ("#9");
  1118. } catch (Exception e) {
  1119. Assert.IsTrue (e is ArgumentException, "#10");
  1120. }
  1121. }
  1122. [Test]
  1123. public void LastIndexOfEmpty_2558 () {
  1124. var l = new List<int> ();
  1125. Assert.AreEqual (-1, l.IndexOf (-1));
  1126. }
  1127. #region Enumerator mutability
  1128. class Bar
  1129. {
  1130. }
  1131. class Foo : IEnumerable<Bar>
  1132. {
  1133. Baz enumerator;
  1134. public Foo ()
  1135. {
  1136. enumerator = new Baz ();
  1137. }
  1138. public IEnumerator<Bar> GetEnumerator ()
  1139. {
  1140. return enumerator;
  1141. }
  1142. IEnumerator IEnumerable.GetEnumerator ()
  1143. {
  1144. return enumerator;
  1145. }
  1146. }
  1147. class Baz : IEnumerator<Bar>
  1148. {
  1149. public bool DisposeWasCalled = false;
  1150. public void Dispose ()
  1151. {
  1152. DisposeWasCalled = true;
  1153. }
  1154. public bool MoveNext ()
  1155. {
  1156. return false; //assume empty collection
  1157. }
  1158. public void Reset ()
  1159. {
  1160. }
  1161. public Bar Current
  1162. {
  1163. get { return null; }
  1164. }
  1165. object IEnumerator.Current
  1166. {
  1167. get { return Current; }
  1168. }
  1169. }
  1170. [Test]
  1171. public void PremiseAboutDisposeBeingCalledWhenLooping ()
  1172. {
  1173. Foo enumerable = new Foo ();
  1174. Baz enumerator = enumerable.GetEnumerator () as Baz;
  1175. Assert.IsNotNull (enumerator);
  1176. Assert.AreEqual (false, enumerator.DisposeWasCalled);
  1177. foreach (var element in enumerable) ; //sic
  1178. Assert.AreEqual (true, enumerator.DisposeWasCalled);
  1179. }
  1180. [Test]
  1181. public void TwoEnumeratorsOfTwoDifferentListsAreDifferent ()
  1182. {
  1183. var twoThree = new List<int> { 2, 3 };
  1184. var oneTwo = new List<int> { 2, 4 };
  1185. Assert.IsFalse (oneTwo.GetEnumerator ().Equals (twoThree.GetEnumerator ()));
  1186. }
  1187. [Test]
  1188. public void TwoEnumeratorsOfTwoDifferentListsWithSameElementsAreDifferent ()
  1189. {
  1190. var twoThree = new List<int> { 2, 3 };
  1191. var anotherTwoThree = new List<int> { 2, 3 };
  1192. Assert.IsFalse(twoThree.GetEnumerator ().Equals (anotherTwoThree.GetEnumerator ()));
  1193. }
  1194. [Test]
  1195. public void EnumeratorIsSameInSameListAfterSubsequentCalls ()
  1196. {
  1197. var enumerable = new List<Bar> ();
  1198. var enumerator = enumerable.GetEnumerator ();
  1199. var enumerator2 = enumerable.GetEnumerator ();
  1200. Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
  1201. Assert.IsTrue (enumerator2.Equals (enumerator));
  1202. }
  1203. [Test] // was bug in Mono 2.10.9
  1204. public void EnumeratorIsStillSameInSubsequentCallsEvenHavingADisposalInBetween ()
  1205. {
  1206. var enumerable = new List<Bar> ();
  1207. var enumerator = enumerable.GetEnumerator ();
  1208. enumerator.Dispose ();
  1209. var enumerator2 = enumerable.GetEnumerator ();
  1210. Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
  1211. Assert.IsTrue (enumerator2.Equals (enumerator));
  1212. }
  1213. [Test]
  1214. public void EnumeratorIsObviouslyDifferentAfterListChanges ()
  1215. {
  1216. var enumerable = new List<Bar> ();
  1217. var enumerator = enumerable.GetEnumerator ();
  1218. enumerable.Add (new Bar ());
  1219. var enumerator2 = enumerable.GetEnumerator ();
  1220. Assert.IsFalse (ReferenceEquals (enumerator2, enumerator)); //because they are value-types
  1221. Assert.IsFalse (enumerator2.Equals (enumerator));
  1222. }
  1223. [Test] // was bug in Mono 2.10.9
  1224. public void DotNetDoesntThrowObjectDisposedExceptionAfterSubsequentDisposes()
  1225. {
  1226. var enumerable = new List<Bar> ();
  1227. var enumerator = enumerable.GetEnumerator ();
  1228. Assert.AreEqual (false, enumerator.MoveNext ());
  1229. enumerator.Dispose();
  1230. Assert.AreEqual (false, enumerator.MoveNext ());
  1231. }
  1232. #endregion
  1233. }
  1234. }
  1235. #endif