📄 collectiontests.cs
字号:
// CollectionTests.cs - NUnit Test Cases for Microsoft.VisualBasic.Collection //// Boris Kirzner <borisk@mainsoft.com>//// // Copyright (c) 2002-2006 Mainsoft Corporation.// Copyright (C) 2004 Novell, Inc (http://www.novell.com)//// Permission is hereby granted, free of charge, to any person obtaining// a copy of this software and associated documentation files (the// "Software"), to deal in the Software without restriction, including// without limitation the rights to use, copy, modify, merge, publish,// distribute, sublicense, and/or sell copies of the Software, and to// permit persons to whom the Software is furnished to do so, subject to// the following conditions:// // The above copyright notice and this permission notice shall be// included in all copies or substantial portions of the Software.// // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.//using NUnit.Framework;using System;using System.IO;using System.Collections;using Microsoft.VisualBasic;namespace MonoTests.Microsoft_VisualBasic{ [TestFixture] public class CollectionTests { public CollectionTests() { } [SetUp] public void GetReady() { System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); } [TearDown] public void Clean() { } // Test Constructor [Test] public void New () { Collection c; c = new Collection (); Assert.IsNotNull (c, "#N01"); Assert.AreEqual (0, c.Count, "#N02"); } #region AddTests [Test] public void Add_1() { 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); Assert.AreEqual(3,col.Count); Assert.AreEqual("abc",col[1]); Assert.AreEqual("abc",col[2]); Assert.AreEqual("abc",col[3]); } [Test] public void Add_2() { 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); Assert.AreEqual(12,col.Count); Assert.AreEqual(o1,col[1]); Assert.AreEqual(o2,col[2]); Assert.AreEqual(o3,col[3]); Assert.AreEqual(o4,col[4]); Assert.AreEqual(o5,col[5]); Assert.AreEqual(o6,col[6]); Assert.AreEqual(o7,col[7]); Assert.AreEqual(o8,col[8]); Assert.AreEqual(o9,col[9]); Assert.AreEqual(o10,col[10]); Assert.AreEqual(o11,col[11]); Assert.AreEqual(o12,col[12]); } [Test] public void Add_Key_1() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; col.Add(s2,"key2",null,null); col.Add(s1,"key3",null,null); col.Add(s3,"key1",null,null); Assert.AreEqual(3,col.Count); Assert.AreEqual(s2,col[1]); Assert.AreEqual(s1,col[2]); Assert.AreEqual(s3,col[3]); Assert.AreEqual(s1,col["key3"]); Assert.AreEqual(s2,col["key2"]); Assert.AreEqual(s3,col["key1"]); } [Test] [ExpectedException(typeof(ArgumentException))] public void Add_Key_2() { // Add failed. Duplicate key value supplied. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s2,"key",null,null); col.Add(s1,"key",null,null); } [Test] public void Add_Before_1() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,null,null,null); col.Add(s2,null,1,null); col.Add(s3,null,2,null); col.Add(s4,null,2,null); Assert.AreEqual(4,col.Count); Assert.AreEqual(s2,col[1]); Assert.AreEqual(s4,col[2]); Assert.AreEqual(s3,col[3]); Assert.AreEqual(s1,col[4]); } [Test] public void Add_Before_2() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,"key4",null,null); col.Add(s2,"key3","key4",null); col.Add(s4,null,"key3",null); col.Add(s3,null,"key4",null); Assert.AreEqual(4,col.Count); Assert.AreEqual(s4,col[1]); Assert.AreEqual(s2,col[2]); Assert.AreEqual(s3,col[3]); Assert.AreEqual(s1,col[4]); } [Test] public void Add_Before_3() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,"key4",null,null); col.Add(s2,"key3",1,null); col.Add(s4,null,"key3",null); col.Add(s3,null,1,null); Assert.AreEqual(4,col.Count); Assert.AreEqual(s3,col[1]); Assert.AreEqual(s4,col[2]); Assert.AreEqual(s2,col[3]); Assert.AreEqual(s1,col[4]); } [Test] public void Add_After_1() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,null,null,null); col.Add(s2,null,null,1); col.Add(s3,null,null,2); col.Add(s4,null,null,1); Assert.AreEqual(4,col.Count); Assert.AreEqual(s1,col[1]); Assert.AreEqual(s4,col[2]); Assert.AreEqual(s2,col[3]); Assert.AreEqual(s3,col[4]); } [Test] public void Add_After_2() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,"key4",null,null); col.Add(s2,"key3",null,"key4"); col.Add(s4,null,null,"key4"); col.Add(s3,null,null,"key3"); Assert.AreEqual(4,col.Count); Assert.AreEqual(s1,col[1]); Assert.AreEqual(s4,col[2]); Assert.AreEqual(s2,col[3]); Assert.AreEqual(s3,col[4]); } [Test] public void Add_After_3() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,"key4",null,null); col.Add(s2,"key3",null,1); col.Add(s4,null,null,1); col.Add(s3,null,null,2); Assert.AreEqual(4,col.Count); Assert.AreEqual(s1,col[1]); Assert.AreEqual(s4,col[2]); Assert.AreEqual(s3,col[3]); Assert.AreEqual(s2,col[4]); } [Test] [ExpectedException(typeof(ArgumentException))] public void Add_3() { // 'Before' and 'After' arguments cannot be combined. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; string s3 = "c"; string s4 = "d"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); col.Add(s3,"key3",null,null); col.Add(s4,"key4",3,2); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Add_4() { // Specified argument was out of the range of valid values. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",3,null); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Add_5() { // Specified argument was out of the range of valid values. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",0,null); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Add_6() { // Specified argument was out of the range of valid values. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,3); } [Test] public void Add_After_4() { // Specified argument was out of the range of valid values. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,0); Assert.AreEqual(2,col.Count); Assert.AreEqual(s2,col[1]); Assert.AreEqual(s1,col[2]); } [Test] public void Add_Before_4() { // Specified argument was out of the range of valid values. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",2,null); Assert.AreEqual(2,col.Count); Assert.AreEqual(s1,col[1]); Assert.AreEqual(s2,col[2]); } [Test] [ExpectedException(typeof(ArgumentOutOfRangeException))] public void Add_7() { // Specified argument was out of the range of valid values. Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,-1); } [Test] public void Add_8() { Collection col = new Collection(); Object o1 = null; Object o2 = null; Object o3 = null; Object o4 = null; col.Add(o1,null,null,null); col.Add(o2,null,null,null); col.Add(o3,null,null,null); col.Add(o4,null,null,null); Assert.AreEqual(4,col.Count); Assert.AreEqual(o1,col[1]); Assert.AreEqual(o2,col[2]); Assert.AreEqual(o3,col[3]); Assert.AreEqual(o4,col[4]); } // Test Add method with Key == null [Test] public void AddNoKey () { 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); Assert.AreEqual (3, c.Count, "#ANK01"); // Collection class is 1-based Assert.AreEqual (typeof (string), c[3], "#ANK02"); } // Test Add method with Key specified [Test] public void AddKey () { 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, "#AK01"); // Collection class is 1-based Assert.AreEqual ("Baseball", c[1], "#AK02"); Assert.AreEqual ("Volleyball", c["Volley"], "#AK03"); } // Test Add method with Before specified and Key == null [Test] public void AddBeforeNoKey () { Collection c; c = new Collection (); c.Add (typeof (int), null, null, null); c.Add (typeof (double), null, 1, null); c.Add (typeof (string), null, 2, null); c.Add (typeof (object), null, 2, null); Assert.AreEqual (4, c.Count, "#ABNK01"); // Collection class is 1-based Assert.AreEqual (typeof (int), c[4], "#ABNK02"); Assert.AreEqual (typeof (double), c[1], "#ABNK03"); Assert.AreEqual (typeof (object), c[2], "#ABNK04"); } // Test Add method with Before and Key [Test] public void AddBeforeKey () { Collection c; c = new Collection (); c.Add ("Baseball", "Base", null, null); c.Add ("Football", "Foot", 1, null); c.Add ("Basketball", "Basket", 1, null); c.Add ("Volleyball", "Volley", 3, null); Assert.AreEqual (4, c.Count, "#ABK01"); Assert.AreEqual ("Basketball", c[1], "#ABK02"); Assert.AreEqual ("Baseball", c[4], "#ABK03"); Assert.AreEqual ("Volleyball", c["Volley"], "#ABK04"); Assert.AreEqual ("Football", c["Foot"], "#ABK05"); } // Test Add method with After specified and Key == null [Test] public void AddAfterNoKey () { Collection c; c = new Collection (); c.Add (typeof (int), null, null, 0); c.Add (typeof (double), null, null, 1); c.Add (typeof (string), null, null, 1); c.Add (typeof (object), null, null, 3); Assert.AreEqual (4, c.Count, "#AANK01"); Assert.AreEqual (typeof (object), c[4], "#AANK02"); Assert.AreEqual (typeof (int), c[1], "#AANK03"); Assert.AreEqual (typeof (string), c[2], "#AANK04"); } // Test Add method with After and Key [Test] public void AddAfterKey () { Collection c; c = new Collection (); c.Add ("Baseball", "Base", null, 0); c.Add ("Football", "Foot", null, 1); c.Add ("Basketball", "Basket", null, 1); c.Add ("Volleyball", "Volley", null, 2); Assert.AreEqual (4, c.Count, "#AAK01"); Assert.AreEqual ("Baseball", c[1], "#AAK02"); Assert.AreEqual ("Football", c[4], "#AAK03"); Assert.AreEqual ("Basketball", c["Basket"], "#AAK04"); Assert.AreEqual ("Volleyball", c["Volley"], "#AAK05"); } #endregion #region Item Tests [Test] public void Index_1() { Collection col = new Collection(); string s1 = "a"; string s2 = "b"; col.Add(s1,"key1",null,null); col.Add(s2,"key2",null,null); Assert.AreEqual(2,col.Count);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -