From f741a048f0e550627b0adcb9d411eeb0716e1eb5 Mon Sep 17 00:00:00 2001 From: Raja R Harinath Date: Fri, 31 Jul 2009 10:25:04 +0000 Subject: [PATCH] IList implementation: convert NullReferenceException to ArgumentException * List.cs (IList.this.set): Convert NullReference and InvalidCast exceptions into ArgumentExceptions. (IList.Add, IList.Contains, IList.IndexOf, IList.Insert, IList.Remove): Convert NullReferenceException to ArgumentException. * ListTest.cs: Add a few testcases for IList interface, using null values with a List. svn path=/trunk/mcs/; revision=139159 --- .../System.Collections.Generic/ChangeLog | 7 ++++ .../corlib/System.Collections.Generic/List.cs | 38 +++++++++++++------ .../Test/System.Collections.Generic/ChangeLog | 5 +++ .../System.Collections.Generic/ListTest.cs | 25 +++++++++++- 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/mcs/class/corlib/System.Collections.Generic/ChangeLog b/mcs/class/corlib/System.Collections.Generic/ChangeLog index 60c27bf585f..58c80b1a56c 100644 --- a/mcs/class/corlib/System.Collections.Generic/ChangeLog +++ b/mcs/class/corlib/System.Collections.Generic/ChangeLog @@ -1,3 +1,10 @@ +2009-07-31 Raja R Harinath + + * List.cs (IList.this.set): Convert NullReference and InvalidCast + exceptions into ArgumentExceptions. + (IList.Add, IList.Contains, IList.IndexOf, IList.Insert, IList.Remove): + Convert NullReferenceException to ArgumentException. + 2009-07-31 Raja R Harinath * List.cs (Enumerator.next): Rename from Enumerator.idx and change diff --git a/mcs/class/corlib/System.Collections.Generic/List.cs b/mcs/class/corlib/System.Collections.Generic/List.cs index 71eb3ecebc8..0c9ce31a562 100644 --- a/mcs/class/corlib/System.Collections.Generic/List.cs +++ b/mcs/class/corlib/System.Collections.Generic/List.cs @@ -658,28 +658,31 @@ namespace System.Collections.Generic { { try { Add ((T) item); + return _size - 1; + } catch (NullReferenceException) { } catch (InvalidCastException) { - throw new ArgumentException("item"); } - return _size - 1; + throw new ArgumentException ("item"); } bool IList.Contains (object item) { try { return Contains ((T) item); + } catch (NullReferenceException) { } catch (InvalidCastException) { - return false; } + return false; } int IList.IndexOf (object item) { try { - return IndexOf((T) item); + return IndexOf ((T) item); + } catch (NullReferenceException) { } catch (InvalidCastException) { - return -1; } + return -1; } void IList.Insert (int index, object item) @@ -691,22 +694,24 @@ namespace System.Collections.Generic { CheckIndex (index); try { Insert (index, (T) item); + return; + } catch (NullReferenceException) { } catch (InvalidCastException) { - throw new ArgumentException("item"); } + throw new ArgumentException ("item"); } void IList.Remove (object item) { try { Remove ((T) item); + return; + } catch (NullReferenceException) { } catch (InvalidCastException) { - // Swallow the exception--if we - // can't cast to the correct type - // then we've already "succeeded" - // in removing the item from the - // List. } + // Swallow the exception--if we can't cast to the + // correct type then we've already "succeeded" in + // removing the item from the List. } bool ICollection .IsReadOnly { @@ -729,7 +734,16 @@ namespace System.Collections.Generic { object IList.this [int index] { get { return this [index]; } - set { this [index] = (T) value; } + set { + try { + this [index] = (T) value; + return; + } catch (NullReferenceException) { + // can happen when 'value' is null and T is a valuetype + } catch (InvalidCastException) { + } + throw new ArgumentException ("value"); + } } #endregion diff --git a/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog b/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog index 2567ac08c72..3b26d8414f3 100644 --- a/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog +++ b/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog @@ -1,3 +1,8 @@ +2009-07-31 Raja R Harinath + + * ListTest.cs: Add a few testcases for IList interface, using null + values with a List. + 2009-07-31 Raja R Harinath * DictionaryTest.cs (KeyEnumerator_Current): New test. Test the diff --git a/mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs b/mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs index d4b1a45d2ae..b132b319a97 100644 --- a/mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs +++ b/mcs/class/corlib/Test/System.Collections.Generic/ListTest.cs @@ -188,18 +188,32 @@ namespace MonoTests.System.Collections.Generic { } [Test, ExpectedException(typeof (ArgumentException))] - public void IList_InsertInvalidType () + public void IList_InsertInvalidType1 () { IList list = _list1 as IList; list.Insert(0, new object()); } + + [Test, ExpectedException(typeof (ArgumentException))] + public void IList_InsertInvalidType2 () + { + IList list = _list1 as IList; + list.Insert(0, null); + } [Test, ExpectedException(typeof (ArgumentException))] - public void IList_AddInvalidType() + public void IList_AddInvalidType1() { IList list = _list1 as IList; list.Add(new object()); } + + [Test, ExpectedException(typeof (ArgumentException))] + public void IList_AddInvalidType2() + { + IList list = _list1 as IList; + list.Add(null); + } [Test] public void IList_RemoveInvalidType() @@ -208,6 +222,9 @@ namespace MonoTests.System.Collections.Generic { int nCount = list.Count; list.Remove(new object()); Assert.AreEqual(nCount, list.Count); + + list.Remove(null); + Assert.AreEqual(nCount, list.Count); } [Test, ExpectedException (typeof (ArgumentOutOfRangeException))] @@ -1128,6 +1145,8 @@ namespace MonoTests.System.Collections.Generic { List list = new List(); list.Add("foo"); Assert.IsFalse (((IList)list).Contains(new object())); + + Assert.IsFalse (((IList)_list1).Contains(null)); } [Test] @@ -1136,6 +1155,8 @@ namespace MonoTests.System.Collections.Generic { List list = new List(); list.Add("foo"); Assert.AreEqual (-1, ((IList)list).IndexOf(new object())); + + Assert.AreEqual (-1, ((IList)_list1).IndexOf(null)); } // for bug #77277 test case -- 2.25.1