소스 검색

2007-03-05 David Mitchell <[email protected]>

	* List.cs: Fix InsertRange bug (80930).


svn path=/trunk/mcs/; revision=73762
Miguel de Icaza 19 년 전
부모
커밋
9ea62353d8

+ 4 - 0
mcs/class/corlib/System.Collections.Generic/ChangeLog

@@ -1,3 +1,7 @@
+2007-03-05  David Mitchell <[email protected]>
+
+	* List.cs: Fix InsertRange bug (80930).
+
 2006-09-15  Gert Driesen  <[email protected]>
 
 	* List.cs: Fixed binary serialization compatibility with MS.NET.

+ 13 - 5
mcs/class/corlib/System.Collections.Generic/List.cs

@@ -372,11 +372,19 @@ namespace System.Collections.Generic {
 		{
 			CheckCollection (collection);
 			CheckIndex (index);
-			ICollection <T> c = collection as ICollection <T>;
-			if (c != null)
-				InsertCollection (index, c);
-			else
-				InsertEnumeration (index, collection);
+			if (collection == this) {
+				T[] buffer = new T[_size];
+				CopyTo (buffer, 0);
+				GrowIfNeeded (_size);
+				Shift (index, buffer.Length);
+				Array.Copy (buffer, 0, _items, index, buffer.Length);
+			} else {
+				ICollection <T> c = collection as ICollection <T>;
+				if (c != null)
+					InsertCollection (index, c);
+				else
+					InsertEnumeration (index, collection);
+			}
 			_version++;
 		}
 

+ 21 - 0
mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs

@@ -141,6 +141,27 @@ namespace MonoTests.System.Collections.Generic {
 			newRange.InsertRange (newRange.Count, li);
 			Assert.AreEqual (2, newRange.Count);
 		}
+		
+		[Test]
+		public void InsertSelfTest()
+		{
+			List <int> range = new List <int> (5);
+			for (int i = 0; i < 5; ++ i)
+				range.Add (i);
+			
+			range.InsertRange(2, range);
+			Assert.AreEqual (10, range.Count);
+			Assert.AreEqual (0, range [0]);
+			Assert.AreEqual (1, range [1]);
+			Assert.AreEqual (0, range [2]);
+			Assert.AreEqual (1, range [3]);
+			Assert.AreEqual (2, range [4]);
+			Assert.AreEqual (3, range [5]);
+			Assert.AreEqual (4, range [6]);
+			Assert.AreEqual (2, range [7]);
+			Assert.AreEqual (3, range [8]);
+			Assert.AreEqual (4, range [9]);
+		}
 
 		[Test, ExpectedException (typeof (ArgumentNullException))]
 		public void InsertRangeNullTest ()