📄 collectiontests.cs
字号:
Assert.AreEqual(s1,col[1]); Assert.AreEqual(s2,col[2]); Assert.AreEqual(s1,col["key1"]); Assert.AreEqual(s2,col["key2"]); } [Test] public void Index_2() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; object key1 = "key1"; object key2 = "key2"; col.Add(s1,(string)key1,null,null); col.Add(s2,(string)key2,null,null); Assert.AreEqual(2,col.Count); Assert.AreEqual(s1,col[1]); Assert.AreEqual(s2,col[2]); Assert.AreEqual(s1,col[key1]); Assert.AreEqual(s2,col[key2]); } [Test] [ExpectedException(typeof(ArgumentException))] public void Index_3() { // Argument 'Index' is not a valid value. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); object o = col["key3"]; } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Index_4() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); object o = col[0]; } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Index_5() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); object o = col[-1]; } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Index_6() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); object o = col[3]; } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Index_7() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); object o = col[null]; } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Index_8() { Collection col = new Collection(); ((System.Collections.IList)col)[1] = "a"; } [Test]#if !NET_2_0 [ExpectedException(typeof(InvalidCastException))]#endif public void Index_9() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); System.Collections.IList il = (System.Collections.IList)col; il[0] = "q"; il[1] = "r"; object o = il[1]; } #endregion #region Remove Tests [Test] public void Remove_1() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,null,null,null); col.Add(s1,null,null,null); col.Add(s3,null,null,null); col.Add(s2 + s1,null,null,null); col.Add(s1 + s1,null,null,null); col.Add(s3 + s1,null,null,null); col.Remove(1); Assert.AreEqual(5,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("c",col[2]); Assert.AreEqual("ba",col[3]); Assert.AreEqual("aa",col[4]); Assert.AreEqual("ca",col[5]); col.Remove(3); Assert.AreEqual(4,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("c",col[2]); Assert.AreEqual("aa",col[3]); Assert.AreEqual("ca",col[4]); col.Remove(4); Assert.AreEqual(3,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("c",col[2]); Assert.AreEqual("aa",col[3]); col.Remove(2); Assert.AreEqual(2,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("aa",col[2]); col.Remove(1); Assert.AreEqual(1,col.Count); Assert.AreEqual("aa",col[1]); col.Remove(1); Assert.AreEqual(0,col.Count); } [Test] public void Remove_2() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"keya",null,null); col.Add(s1,"keyb",null,null); col.Add(s3,"keyc",null,null); col.Add(s2 + s1,"keyd",null,null); col.Add(s1 + s1,"keye",null,null); col.Add(s3 + s1,"keyf",null,null); col.Remove("keya"); Assert.AreEqual(5,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("c",col[2]); Assert.AreEqual("ba",col[3]); Assert.AreEqual("aa",col[4]); Assert.AreEqual("ca",col[5]); col.Remove("keyd"); Assert.AreEqual(4,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("c",col[2]); Assert.AreEqual("aa",col[3]); Assert.AreEqual("ca",col[4]); col.Remove("keyf"); Assert.AreEqual(3,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("c",col[2]); Assert.AreEqual("aa",col[3]); col.Remove("keyc"); Assert.AreEqual(2,col.Count); Assert.AreEqual("a",col[1]); Assert.AreEqual("aa",col[2]); col.Remove("keyb"); Assert.AreEqual(1,col.Count); Assert.AreEqual("aa",col[1]); col.Remove("keye"); Assert.AreEqual(0,col.Count); } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Remove_3() { // Collection index must be in the range 1 to the size of the collection. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"keya",null,null); col.Add(s1,"keyb",null,null); col.Add(s3,"keyc",null,null); col.Remove(-1); } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Remove_4() { // Collection index must be in the range 1 to the size of the collection. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"keya",null,null); col.Add(s1,"keyb",null,null); col.Add(s3,"keyc",null,null); col.Remove(0); } [Test] [ExpectedException(typeof(IndexOutOfRangeException))] public void Remove_5() { // Collection index must be in the range 1 to the size of the collection. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"keya",null,null); col.Add(s1,"keyb",null,null); col.Add(s3,"keyc",null,null); col.Remove(4); } [Test] [ExpectedException(typeof(ArgumentException))] public void Remove_6() { // Argument 'Key' is not a valid value. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"keya",null,null); col.Add(s1,"keyb",null,null); col.Add(s3,"keyc",null,null); col.Remove("keyd"); } [Test] [ExpectedException(typeof(ArgumentNullException))] public void Remove_7() { // Key cannot be null Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"keya",null,null); col.Add(s1,"keyb",null,null); col.Add(s3,"keyc",null,null); col.Remove(null); } // Test Remove method with Index [Test] public void RemoveNoKey () { Collection c; c = new Collection (); c.Add (typeof (int), null, null, null); c.Add (typeof (double), null, null, null); c.Add (typeof (string), null, null, null); c.Add (typeof (object), null, null, null); Assert.AreEqual (4, c.Count, "#RNK01"); c.Remove (3); Assert.AreEqual (3, c.Count, "#RNK02"); // Collection class is 1-based Assert.AreEqual (typeof (object), c[3], "#RNK03"); c.Remove (1); Assert.AreEqual (2, c.Count, "#RNK04"); Assert.AreEqual (typeof (double), c[1], "#RNK05"); Assert.AreEqual (typeof (object), c[2], "#RNK06"); c.Remove (2); Assert.AreEqual (1, c.Count, "#RNK07"); Assert.AreEqual (typeof (double), c[1], "#RNK08"); c.Remove (1); Assert.AreEqual (0, c.Count, "#RNK09"); } // Test Remove method with Key [Test] public void RemoveKey () { Collection c; c = new Collection (); c.Add ("Baseball", "Base", null, null); c.Add ("Football", "Foot", null, null); c.Add ("Basketball", "Basket", null, null); c.Add ("Volleyball", "Volley", null, null); Assert.AreEqual (4, c.Count, "#RK01"); c.Remove ("Foot"); Assert.AreEqual (3, c.Count, "#RK02"); Assert.AreEqual ("Basketball", c["Basket"], "#RK03"); // Collection class is 1-based Assert.AreEqual ("Volleyball", c[3], "#RK04"); c.Remove ("Base"); Assert.AreEqual (2, c.Count, "#RK05"); Assert.AreEqual ("Basketball", c[1], "#RK06"); Assert.AreEqual ("Volleyball", c["Volley"], "#RK07"); c.Remove (2); Assert.AreEqual (1, c.Count, "#RK08"); Assert.AreEqual ("Basketball", c[1], "#RK09"); Assert.AreEqual ("Basketball", c["Basket"], "#RK10"); c.Remove (1); Assert.AreEqual (0, c.Count, "#RK11"); } #endregion #region GetEnumerator Tests [Test] public void GetEnumerator_1() { Collection col = new Collection(); Byte o1 = 1; short o2 = 1; int o3 = 1; long o4 = 1000; Single o5 = 1.1F; Double o6 = 2.2; Decimal o7 = 1000; String o8 = "abc"; Object o9 = null; bool o10 = true; Char o11 = 'c'; DateTime o12 = DateTime.Parse("5/31/1993"); col.Add(o1,null,null,null); col.Add(o2,null,null,null); col.Add(o3,null,null,null); col.Add(o4,null,null,null); col.Add(o5,null,null,null); col.Add(o6,null,null,null); col.Add(o7,null,null,null); col.Add(o8,null,null,null); col.Add(o9,null,null,null); col.Add(o10,null,null,null); col.Add(o11,null,null,null); col.Add(o12,null,null,null); IEnumerator en = col.GetEnumerator(); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o1,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o2,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o3,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o4,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o5,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o6,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o7,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o8,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o9,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o10,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o11,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o12,en.Current); Assert.IsFalse(en.MoveNext()); Assert.AreEqual(null,en.Current); } [Test]#if !NET_2_0 [ExpectedException(typeof(IndexOutOfRangeException))]#endif public void GetEnumerator_2() { // Collection index must be in the range 1 to the size of the collection. Collection col = new Collection(); col.Add("a",null,null,null); IEnumerator en = col.GetEnumerator(); object o = en.Current; } [Test] public void GetEnumerator_3() { Collection col = new Collection(); Byte o1 = 1; short o2 = 1; int o3 = 1; long o4 = 1000; Single o5 = 1.1F; Double o6 = 2.2; Decimal o7 = 1000; String o8 = "abc"; Object o9 = null; bool o10 = true; Char o11 = 'c'; DateTime o12 = DateTime.Parse("5/31/1993"); col.Add(o1,null,null,null); col.Add(o2,null,null,null); col.Add(o3,null,null,null); col.Add(o4,null,null,null); col.Add(o5,null,null,null); col.Add(o6,null,null,null); col.Add(o7,null,null,null); col.Add(o8,null,null,null); col.Add(o9,null,null,null); col.Add(o10,null,null,null); col.Add(o11,null,null,null); col.Add(o12,null,null,null); IEnumerator en = col.GetEnumerator(); while(en.MoveNext()); Assert.AreEqual(null,en.Current); en.Reset(); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o1,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o2,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o3,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o4,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o5,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o6,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o7,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o8,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o9,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o10,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o11,en.Current); Assert.IsTrue(en.MoveNext()); Assert.AreEqual(o12,en.Current); Assert.IsFalse(en.MoveNext()); Assert.AreEqual(null,en.Current); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -