📄 collectiontests.cs
字号:
((IList)col).Add(s1); ((IList)col).Add(s2); ((IList)col).Add(s3); ((IList)col).Add(s1); ((IList)col).Add(s2); ((IList)col).Add(s3); ((IList)col).Insert(6,s1+s1); Assert.AreEqual ("abc3", col [6], "#01"); Assert.AreEqual ("abc2", col [5], "#02"); Assert.AreEqual ("abc1abc1", col [7], "#03"); } [Test] public void IList_Item() { Collection col = new Collection(); string s1 = "abc1"; string s2 = "abc2"; string s3 = "abc3"; ((IList)col).Add(s1); ((IList)col).Add(s2); ((IList)col).Add(s3); ((IList)col).Add(s1); ((IList)col).Add(s2); ((IList)col).Add(s3); Assert.AreEqual(s1,((IList)col)[0]); Assert.AreEqual(s2,((IList)col)[1]); Assert.AreEqual(s3,((IList)col)[2]); Assert.AreEqual(s1,((IList)col)[3]); Assert.AreEqual(s2,((IList)col)[4]); Assert.AreEqual(s3,((IList)col)[5]); } [Test] [Category("NotWorking")] // Unstable behaviour in .Net public void IList_Remove() { /* Collection col = new Collection(); string s1 = "abc1"; string s2 = "abc2"; string s3 = "abc3"; ((IList)col).Add(s1); ((IList)col).Add(s2); ((IList)col).Add(s3); ((IList)col).Add(s3); ((IList)col).Add(s2); ((IList)col).Add(s1); ((IList)col).Remove(s2); Assert.AreEqual(5,col.Count); Assert.AreEqual(s1,col[1]); Assert.AreEqual(s3,col[2]); Assert.AreEqual(s3,col[3]); Assert.AreEqual(s2,col[4]); Assert.AreEqual(s1,col[5]); ((IList)col).Remove(s1); Assert.AreEqual(4,col.Count); Assert.AreEqual(s3,col[1]); Assert.AreEqual(s3,col[2]); Assert.AreEqual(s2,col[3]); Assert.AreEqual(s1,col[4]); ((IList)col).Remove(s1); Assert.AreEqual(3,col.Count); Assert.AreEqual(s3,col[1]); Assert.AreEqual(s3,col[2]); Assert.AreEqual(s2,col[3]); */ } #endregion [Test] [ExpectedException(typeof(ArgumentException))] public void Dirty_Read() { Collection col = new Collection(); string s = "abc"; col.Add(s,null,null,null); col.Add(s,null,null,null); col.Add(s,null,null,null); col.Add(s,null,null,null); object o = col["abc"]; } // Test all the Exceptions we're supposed to throw [Test] public void Exception () { Collection c = new Collection (); try { // nothing in Collection yet object o = c[0]; Assert.Fail ("#E02"); } catch (IndexOutOfRangeException) { } c.Add ("Baseball", "Base", null, null); c.Add ("Football", "Foot", null, null); c.Add ("Basketball", "Basket", null, null); c.Add ("Volleyball", "Volley", null, null); try { // only 4 elements object o = c[5]; Assert.Fail ("#E04"); } catch (IndexOutOfRangeException) { } try { // Collection class is 1-based object o = c[0]; Assert.Fail ("#E06"); } catch (IndexOutOfRangeException) { } try { // no member with Key == "Kick" object o = c["Kick"]; Assert.Fail ("#E08"); } catch (ArgumentException) { // FIXME // VB Language Reference says IndexOutOfRangeException // here, but MS throws ArgumentException } try { // Even though Indexer is an object, really it's a string object o = c[typeof (int)]; Assert.Fail ("#E10"); } catch (ArgumentException) { } try { // can't specify both Before and After c.Add ("Kickball", "Kick", "Volley", "Foot"); Assert.Fail ("#E12"); } catch (ArgumentException) { } try { // Key "Foot" already exists c.Add ("Kickball", "Foot", null, null); Assert.Fail ("#E14"); } catch (ArgumentException) { } try { // Even though Before is object, it's really a string c.Add ("Dodgeball", "Dodge", typeof (int), null); Assert.Fail ("#E16"); } catch (InvalidCastException) { } try { // Even though After is object, it's really a string c.Add ("Wallyball", "Wally", null, typeof (int)); Assert.Fail ("#E18"); } catch (InvalidCastException) { } try { // have to pass a legitimate value to remove c.Remove (null); Assert.Fail ("#E20"); } catch (ArgumentNullException) { } try { // no Key "Golf" exists c.Remove ("Golf"); Assert.Fail ("#E22"); } catch (ArgumentException) { } try { // no Index 10 exists c.Remove (10); Assert.Fail ("#E24"); } catch (IndexOutOfRangeException) { } try { IEnumerator e = c.GetEnumerator (); // Must MoveNext before Current object item = e.Current;#if NET_2_0 Assert.IsNull (item, "#E25");#else Assert.Fail ("#E26");#endif } catch (IndexOutOfRangeException) {#if NET_2_0 Assert.Fail ("#E27");#endif } try { IEnumerator e = c.GetEnumerator (); e.MoveNext (); c.Add ("Paintball", "Paint", null, null); // Can't MoveNext if Collection has been modified e.MoveNext (); // FIXME // On-line help says this should throw an error. MS doesn't. } catch (Exception) { Assert.Fail ("#E28"); } try { IEnumerator e = c.GetEnumerator (); e.MoveNext (); c.Add ("Racketball", "Racket", null, null); // Can't Reset if Collection has been modified e.Reset (); // FIXME // On-line help says this should throw an error. MS doesn't. } catch (InvalidOperationException) { Assert.Fail ("#E30"); } } [Test] public void IList_Remove_2 () { Collection c = new Collection (); IList list = (IList) c; list.Remove (null); c.Add ("Baseball", "Base", null, null); c.Add ("Paintball", "Paint", null, null); Assert.AreEqual (2, c.Count, "#1"); try { list.Contains (null); Assert.Fail ("#2"); } catch (NullReferenceException) { } Assert.AreEqual (2, c.Count, "#3"); list.Remove (c.GetType ()); Assert.AreEqual (2, c.Count, "#4"); list.Remove (1); Assert.AreEqual (2, c.Count, "#5"); list.Remove ("Something"); Assert.AreEqual (2, c.Count, "#6"); list.Remove ("Baseball"); Assert.AreEqual (1, c.Count, "#7"); Assert.AreEqual ("Paintball", c[1], "#8"); } [Test] public void IList_RemoveAt () { Collection c = new Collection (); IList list = (IList) c; try { list.RemoveAt (0); Assert.Fail ("#1"); } catch (ArgumentOutOfRangeException) { } try { list.RemoveAt (-1); Assert.Fail ("#2"); } catch (ArgumentOutOfRangeException) { } c.Add ("Baseball", "Base", null, null); c.Add ("Paintball", "Paint", null, null); Assert.AreEqual (2, c.Count, "#3"); Assert.AreEqual ("Baseball", list[0], "#4"); list.RemoveAt (0); Assert.AreEqual (1, c.Count, "#5"); Assert.AreEqual ("Paintball", list[0], "#6"); c.Add ("Baseball", "Base", null, null); c.Add ("Basketball", "Basket", null, null); Assert.AreEqual ("Paintball", list[0], "#7"); Assert.AreEqual ("Baseball", list[1], "#8"); Assert.AreEqual ("Basketball", list[2], "#9"); try { list.RemoveAt (3); Assert.Fail ("#10"); } catch (ArgumentOutOfRangeException) { } list.RemoveAt (-1); Assert.AreEqual (2, c.Count, "#11"); Assert.AreEqual ("Baseball", list[0], "#12"); Assert.AreEqual ("Basketball", list[1], "#13"); } [Test] public void IList_IndexOf_2 () { Collection c = new Collection (); IList list = (IList) c; Assert.AreEqual (-1, list.IndexOf (null), "#1"); c.Add ("Baseball", "Base", null, null); c.Add ("Paintball", "Paint", null, null); c.Add (5, "6", null, null); try { list.IndexOf (null); Assert.Fail ("#2"); } catch (NullReferenceException) { } Assert.AreEqual (0, list.IndexOf ("Baseball"), "#3"); Assert.AreEqual (-1, list.IndexOf ("Base"), "#4"); Assert.AreEqual (1, list.IndexOf ("Paintball"), "#5"); Assert.AreEqual (-1, list.IndexOf ("Pain"), "#6"); Assert.AreEqual (2, list.IndexOf (5), "#7"); Assert.AreEqual (-1, list.IndexOf (6), "#8"); Assert.AreEqual (-1, list.IndexOf ("Something"), "#9"); } [Test] public void IList_Contains_2 () { Collection c = new Collection (); IList list = (IList) c; Assert.IsFalse (list.Contains (null), "#1"); c.Add ("Baseball", "Base", null, null); c.Add ("Paintball", "Paint", null, null); c.Add (5, "6", null, null); try { list.Contains (null); Assert.Fail ("#2"); } catch (NullReferenceException) { } Assert.AreEqual (true, list.Contains ("Baseball"), "#3"); Assert.AreEqual (false, list.Contains ("Base"), "#4"); Assert.AreEqual (true, list.Contains ("Paintball"), "#5"); Assert.AreEqual (false, list.Contains ("Paint"), "#6"); Assert.AreEqual (true, list.Contains (5), "#7"); Assert.AreEqual (false, list.Contains (6), "#8"); Assert.AreEqual (false, list.Contains ("Something"), "#9"); } [Test] public void IList_Indexer_Get () { Collection c = new Collection (); IList list = (IList) c; try { object value = list[0]; Assert.Fail ("#1"); } catch (ArgumentOutOfRangeException) { } try { object value = list[-1]; Assert.Fail ("#2"); } catch (ArgumentOutOfRangeException) { } c.Add ("Baseball", "Base", null, null); c.Add ("Paintball", "Paint", null, null); c.Add (5, "6", null, null); Assert.AreEqual ("Baseball", list[0], "#3"); Assert.AreEqual ("Paintball", list[1], "#4"); Assert.AreEqual (5, list[2], "#5"); try { object value = list[3]; Assert.Fail ("#6"); } catch (ArgumentOutOfRangeException) { } object val = list[-1]; Assert.AreEqual ("Baseball", val, "#6"); Assert.AreEqual ("Baseball", list [-2], "#7"); Assert.AreEqual ("Baseball", list [int.MinValue], "#8"); } [Test]#if !NET_2_0 [Category ("NotDotNet")] // setter is badly broken in MS.NET 1.x#endif public void IList_Indexer_Set () { Collection c = new Collection (); IList list = (IList) c; try { list[0] = "Baseball"; Assert.Fail ("#1"); } catch (ArgumentOutOfRangeException) { } try { list[-1] = "Baseball"; Assert.Fail ("#2"); } catch (ArgumentOutOfRangeException) { } c.Add ("Baseball", "Base", null, null); c.Add ("Paintball", "Paint", null, null); c.Add (5, "6", null, null); Assert.AreEqual (3, c.Count, "#3"); list[0] = "Basketball"; list[2] = "Six"; Assert.AreEqual (3, c.Count, "#4"); Assert.AreEqual ("Basketball", list[0], "#5"); Assert.AreEqual ("Paintball", list[1], "#6"); Assert.AreEqual ("Six", list[2], "#7"); try { list[3] = "Baseball"; Assert.Fail ("#8"); } catch (ArgumentOutOfRangeException) { } list[-1] = "Whatever"; Assert.AreEqual (3, c.Count, "#8"); Assert.AreEqual ("Whatever", list[0], "#9"); Assert.AreEqual ("Paintball", list[1], "#10"); Assert.AreEqual ("Six", list[2], "#11"); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -