In System:
authorAnkit Jain <radical@corewars.org>
Tue, 14 Feb 2006 12:18:14 +0000 (12:18 -0000)
committerAnkit Jain <radical@corewars.org>
Tue, 14 Feb 2006 12:18:14 +0000 (12:18 -0000)
* ArraySegment.cs (.ctor): Fix bounds check. Rename param 'length' to
'count'.

In Test/System:
* ArraySegmentTest.cs: New.

svn path=/trunk/mcs/; revision=56850

mcs/class/corlib/System/ArraySegment.cs
mcs/class/corlib/System/ChangeLog
mcs/class/corlib/Test/System/ArraySegmentTest.cs [new file with mode: 0644]
mcs/class/corlib/Test/System/ChangeLog

index f1fab2f269616dd534023803d2b65a8e5b090ad0..d0a7603a43ed8337f7d7eb64935aa82cc64751e3 100644 (file)
@@ -30,19 +30,30 @@ namespace System {
        [Serializable]
        public struct ArraySegment <T> {
                T [] array;
-               int offset, length;
+               int offset, count;
                
-               public ArraySegment (T [] array, int offset, int length)
+               public ArraySegment (T [] array, int offset, int count)
                {
                        if (array == null)
                                throw new ArgumentNullException ("array");
                        
-                       if (offset >= array.Length || (offset + length) >= array.Length)
+                       if (offset < 0)
+                               throw new ArgumentOutOfRangeException ("offset", "Non-negative number required.");
+
+                       if (count < 0)
+                               throw new ArgumentOutOfRangeException ("count", "Non-negative number required.");
+
+                       if (offset > array.Length)
                                throw new ArgumentException ("out of bounds");
+
+                       // now offset is valid, or just beyond the end.
+                       // Check count -- do it this way to avoid overflow on 'offset + count'
+                       if (array.Length - offset < count)
+                               throw new ArgumentException ("out of bounds", "offset");
                        
                        this.array = array;
                        this.offset = offset;
-                       this.length = length;
+                       this.count = count;
                }
                
                public ArraySegment (T [] array)
@@ -52,7 +63,7 @@ namespace System {
                        
                        this.array = array;
                        this.offset = 0;
-                       this.length = array.Length;
+                       this.count = array.Length;
                }
                
                public T [] Array {
@@ -64,8 +75,8 @@ namespace System {
                }
                
                public int Count {
-                       get { return length; }
+                       get { return count; }
                }
        }
 }
-#endif
\ No newline at end of file
+#endif
index e5c1f25c41729e08430bc57c72921c06e5bd210a..d86ad55228545f2a239d123e75f9b6c8f7038fe7 100644 (file)
@@ -1,3 +1,9 @@
+2006-02-14  Ankit Jain  <jankit@novell.com>
+           Raja R Harinath  <rharinath@novell.com>
+       * ArraySegment.cs (.ctor): Fix bounds check. Rename param 'length' to
+       'count'.
+
 2006-02-11  Zoltan Varga  <vargaz@gmail.com>
 
        * TermInfoDriver.cs (CreateKeyInfoFromInt): Fix handling of tab and its
diff --git a/mcs/class/corlib/Test/System/ArraySegmentTest.cs b/mcs/class/corlib/Test/System/ArraySegmentTest.cs
new file mode 100644 (file)
index 0000000..5da7394
--- /dev/null
@@ -0,0 +1,144 @@
+// ArraySegmentTest.cs - NUnit Test Cases for the System.ArraySegment class
+//
+// Ankit Jain  <jankit@novell.com>
+// Raja R Harinath  <rharinath@novell.com>
+// 
+// Copyright (C) 2006 Novell, Inc (http://www.novell.com)
+// 
+
+#if NET_2_0
+using NUnit.Framework;
+using System;
+using System.Collections.Generic;
+
+namespace MonoTests.System
+{
+
+[TestFixture]
+public class ArraySegmentTest
+{
+       public ArraySegmentTest() {}
+
+       [Test]
+       public void CtorTest1 () 
+       {
+               byte [] b_arr = new byte [4096];
+               Array arr;
+
+               ArraySegment<byte> seg = new ArraySegment<byte> (b_arr, 0, b_arr.Length);
+               Assert.AreEqual (seg.Count, b_arr.Length, "#1");
+               Assert.AreEqual (seg.Offset, 0, "#2");
+               
+               arr = seg.Array;
+               Assert.AreEqual (arr.Length, 4096, "#5");
+               
+               seg = new ArraySegment<byte> (b_arr, 100, b_arr.Length - 100);
+               Assert.AreEqual (seg.Count, b_arr.Length - 100, "#3");
+               Assert.AreEqual (seg.Offset, 100, "#4");
+               
+               arr = seg.Array;
+               Assert.AreEqual (arr.Length, 4096, "#5");
+       }
+
+       [Test]
+       public void CtorTest2 ()
+       {
+               byte [] b_arr = new byte [4096];
+               ArraySegment<byte> seg = new ArraySegment<byte> (b_arr);
+               Assert.AreEqual (seg.Count, b_arr.Length, "#6");
+               Assert.AreEqual (seg.Offset, 0, "#7");
+               
+               Array arr = seg.Array;
+               Assert.AreEqual (arr.Length, 4096, "#8");
+       }
+
+       [Test]
+       public void CtorTest3 ()
+       {
+               EmptyArraySegTest (0);
+               EmptyArraySegTest (10);
+       }
+
+       private void EmptyArraySegTest (int len)
+       {
+               byte [] b_arr = new byte [len];
+
+               ArraySegment<byte> seg = new ArraySegment<byte> (b_arr, 0, b_arr.Length);
+
+               Assert.AreEqual (seg.Count, b_arr.Length, "#1 [array len {0}] ", len);
+               Assert.AreEqual (seg.Offset, 0, "#2 [array len {0}] ", len);
+               Array arr = seg.Array;
+               Assert.AreEqual (arr.Length, len, "#3 [array len {0}] ", len);
+               
+               seg = new ArraySegment<byte> (b_arr, b_arr.Length, 0);
+               Assert.AreEqual (seg.Count, 0, "#4 [array len {0}] ", len);
+               Assert.AreEqual (seg.Offset, b_arr.Length, "#5 [array len {0}] ", len);
+               arr = seg.Array;
+               Assert.AreEqual (arr.Length, len, "#6 [array len {0}] ", len);
+
+               seg = new ArraySegment<byte> (b_arr);
+               Assert.AreEqual (seg.Count, b_arr.Length, "#7 [array len {0}] ", len);
+               Assert.AreEqual (seg.Offset, 0, "#8 [array len {0}] ", len);
+               arr = seg.Array;
+               Assert.AreEqual (arr.Length, len, "#9 [array len {0}] ", len);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void CtorErrorTest ()
+       {
+               byte [] arr = new byte [4096];  
+               ArraySegment<byte> seg = new ArraySegment<byte> (arr, 1, arr.Length);
+       }
+       
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void CtorErrorTest2 ()
+       {
+               byte [] arr = new byte [4096];  
+               ArraySegment<byte> seg = new ArraySegment<byte> (arr, 0, arr.Length + 2);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentOutOfRangeException))]
+       public void CtorErrorTest3 ()
+       {
+               byte [] arr = new byte [4096];  
+               ArraySegment<byte> seg = new ArraySegment<byte> (arr, -1, arr.Length);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentOutOfRangeException))]
+       public void CtorErrorTest4 ()
+       {
+               byte [] arr = new byte [4096];  
+               ArraySegment<byte> seg = new ArraySegment<byte> (arr, 2, -1);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentException))]
+       public void CtorErrorTest5 ()
+       {
+               byte [] arr = new byte [4096];
+               ArraySegment<byte> seg = new ArraySegment<byte> (arr, 0, arr.Length + 2);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentNullException))]
+       public void CtorNullTest1 ()
+       {
+               ArraySegment<byte> seg = new ArraySegment<byte> (null, 0 , 1);
+       }
+
+       [Test]
+       [ExpectedException (typeof (ArgumentNullException))]
+       public void CtorNullTest2 ()
+       {
+               ArraySegment<byte> seg = new ArraySegment<byte> (null);
+       }
+
+
+#endif
+}
+
+}
index 1fb7925aa04319e0382a589a585adc6a81a3c1c6..0ffd50473d5320a2d4e9de071ab315c18475468a 100644 (file)
@@ -1,3 +1,8 @@
+2006-02-14  Ankit Jain  <jankit@novell.com>
+           Raja R Harinath  <rharinath@novell.com>
+
+       * ArraySegmentTest.cs: New.
+
 2006-02-02  Zoltan Varga  <vargaz@gmail.com>
 
        * TypeTest.cs: Add test for getting custom attributes of generic