⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ordereddictionarytests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 4 页
字号:
			dict1[4.67] =  222;
			dict1[double.MaxValue] =  444;

			b = dict1.Remove(double.NaN);
			Assert.IsTrue(b);
			b = dict1.Remove(double.NaN);
			Assert.IsFalse(b);
			b = dict1.Remove(double.MinValue);
			Assert.IsFalse(b);
			b = dict1.Remove(4.67);
			Assert.IsTrue(b);
			b = dict1.Remove(4.67);
			Assert.IsFalse(b);
			
			InterfaceTests.TestReadonlyCollectionGeneric(dict1.Keys, new double[] { double.NegativeInfinity, double.MaxValue, double.PositiveInfinity }, true, "KeysCollection");
			InterfaceTests.TestReadonlyCollectionGeneric(dict1.Values, new int[] { 421, 444, 0 }, true, "ValuesCollection");
		}

        [Test]
        public void TryGetValue()
        {
            OrderedDictionary<double, int> dict1 = new OrderedDictionary<double, int>();
            bool b;
            int val;

            b = dict1.TryGetValue(4.67, out val);
            Assert.IsFalse(b);
            dict1[4.67] =  12;
            b = dict1.TryGetValue(4.67, out val);
            Assert.IsTrue(b); Assert.AreEqual(val, 12);
            dict1[double.NaN] =  -17;
            dict1[double.PositiveInfinity] =  0;
            b = dict1.TryGetValue(12.3, out val);
            Assert.IsFalse(b);
            dict1[4.67] =  187;
            dict1[double.NegativeInfinity] =  188921;
            b = dict1.TryGetValue(double.NegativeInfinity, out val);
            Assert.IsTrue(b); Assert.AreEqual(val, 188921);
            dict1[double.NegativeInfinity] =  421;
            dict1[4.67] =  222;
            b = dict1.TryGetValue(double.MaxValue, out val);
            Assert.IsFalse(b);
            dict1[double.MaxValue] =  444;
            b = dict1.TryGetValue(double.NaN, out val);
            Assert.IsTrue(b); Assert.AreEqual(val, -17);
            b = dict1.TryGetValue(4.67, out val);
            Assert.IsTrue(b); Assert.AreEqual(val, 222);
            b = dict1.TryGetValue(double.NegativeInfinity, out val);
            Assert.IsTrue(b); Assert.AreEqual(val, 421);
        }

        [Test]
        public void GetValueElseAdd()
        {
            OrderedDictionary<double, int> dict1 = new OrderedDictionary<double, int>();
            bool b; 
            int val;

            val = 12;
            b = dict1.GetValueElseAdd(4.67, ref val);
            Assert.IsFalse(b); Assert.AreEqual(12, val);

            val = -17;
            b = dict1.GetValueElseAdd(double.NaN, ref val);
            Assert.IsFalse(b); Assert.AreEqual(-17, val);

            val = 0;
            b = dict1.GetValueElseAdd(double.PositiveInfinity, ref val);
            Assert.IsFalse(b); Assert.AreEqual(0, val);

            val = 187;
            b = dict1.GetValueElseAdd(4.67, ref val);
            Assert.IsTrue(b); Assert.AreEqual(12, val);

            val = 188921;
            b = dict1.GetValueElseAdd(double.NegativeInfinity, ref val);
            Assert.IsFalse(b); Assert.AreEqual(188921, val);

            Assert.AreEqual(12, dict1[4.67]);
            dict1.Replace(4.67, 999);

            val = 121;
            b = dict1.GetValueElseAdd(4.67, ref val);
            Assert.IsTrue(b); Assert.AreEqual(999, val);

            val = 421;
            b = dict1.GetValueElseAdd(double.NegativeInfinity, ref val);
            Assert.IsTrue(b); Assert.AreEqual(188921, val);

            Assert.AreEqual(188921, dict1[double.NegativeInfinity]);
            Assert.AreEqual(999, dict1[4.67]);
            Assert.AreEqual(0, dict1[double.PositiveInfinity]);
            Assert.AreEqual(-17, dict1[double.NaN]);
        }

        /// <summary>
		/// Test clearing the dictionary.
		/// </summary>
		[Test]
		public void Clear()
		{
			OrderedDictionary<string,double> dict1 = new OrderedDictionary<string,double>();

			Assert.AreEqual(0, dict1.Count);

			dict1["hello"] = 4.5;
			dict1["hi"] = 1.22;
			dict1[""] = 7.6;

			Assert.AreEqual(3, dict1.Count);

			dict1.Clear();

			Assert.AreEqual(0, dict1.Count);
			InterfaceTests.TestReadonlyCollectionGeneric(dict1.Keys, new string[0], true, "KeysCollection");
			InterfaceTests.TestReadonlyCollectionGeneric(dict1.Values, new double[0], true, "ValuesCollection");

			Assert.IsFalse(dict1.ContainsKey("hello"));
			Assert.IsFalse(dict1.ContainsKey("hi"));
			Assert.IsFalse(dict1.ContainsKey("banana"));
			Assert.IsFalse(dict1.ContainsKey(""));
		}

		/// <summary>
		/// Test ContainsKey. Make sure it returns the right valuesl. 
		/// </summary>
		[Test]
		public void ContainsKey()
		{
			OrderedDictionary<string,string> dict1 = new OrderedDictionary<string,string>();

			dict1["b"] =  "foo";
			dict1["r"] =  "bar";
			dict1[""] =  "golde";
			dict1["n"] =  "baz";
			dict1["B"] =  "hello";
			dict1[""] =  "peter";
			dict1["n"] =  "horton";
			dict1["x"] =  "hears";
			dict1.Remove("n");
			dict1["c"] =  "a who";
			dict1["q"] =  null;

			Assert.IsTrue(dict1.ContainsKey("b"));
			Assert.IsTrue(dict1.ContainsKey("r"));
			Assert.IsTrue(dict1.ContainsKey(""));
			Assert.IsFalse(dict1.ContainsKey("n"));
			Assert.IsTrue(dict1.ContainsKey("B"));
			Assert.IsTrue(dict1.ContainsKey("x"));
			Assert.IsTrue(dict1.ContainsKey("q"));
			Assert.IsFalse(dict1.ContainsKey("a"));

			dict1.Remove("");

			Assert.IsFalse(dict1.ContainsKey(""));
		}

		class ComparableClass1: IComparable<ComparableClass1>
		{
			public int Value = 0;
			int IComparable<ComparableClass1>.CompareTo(ComparableClass1 other)
			{
				if (Value > other.Value)
					return 1;
				else if (Value < other.Value)
					return -1;
				else
					return 0;
			}
        }

		class ComparableClass2: IComparable
		{
			public int Value = 0;
			int IComparable.CompareTo(object other)
			{
				if (other is ComparableClass2)
				{
					ComparableClass2 o = (ComparableClass2)other;

					if (Value > o.Value)
						return 1;
					else if (Value < o.Value)
						return -1;
					else
						return 0;
				}
				else 
					throw new ArgumentException("Argument of wrong type.", "other");
			}
		}

		// Not comparable, because the type parameter on ComparableClass is incorrect.
		public class UncomparableClass1: IComparable<ComparableClass1>
		{
			public int Value = 0;
			int IComparable<ComparableClass1>.CompareTo(ComparableClass1 other)
			{
				if (Value > other.Value)
					return 1;
				else if (Value < other.Value)
					return -1;
				else
					return 0;
			}
        }

        public class UncomparableClass2
		{
			public int Value = 0;
		}

		/// <summary>
		/// Make sure that the parameterless constructor on SimpleDictionary can be called with
		/// a comparable struct and class.
		/// </summary>
		[Test]
		public void SimpleConstruction()
		{
			OrderedDictionary<int,string> dict1 = new OrderedDictionary<int,string>();
			OrderedDictionary<string,string> dict2 = new OrderedDictionary<string,string>();
			OrderedDictionary<ComparableClass2,string> dict3 = new OrderedDictionary<ComparableClass2,string>();
			OrderedDictionary<ComparableClass1,string> dict4 = new OrderedDictionary<ComparableClass1,string>();
		}

		/// <summary>
		/// Check that a OrderedDictionary can't be instantiated on an incomparable type.
		/// </summary>
		[Test, ExpectedException(typeof(InvalidOperationException))]
		public void NotComparable1()
		{
			OrderedDictionary<UncomparableClass1,string> dict1 = new OrderedDictionary<UncomparableClass1,string>();
		}

		/// <summary>
		/// Check that a OrderedDictionary can't be instantiated on an incomparable type.
		/// </summary>
		[Test, ExpectedException(typeof(InvalidOperationException))]
		public void NotComparable2()
		{
			OrderedDictionary<UncomparableClass2,string> dict2 = new OrderedDictionary<UncomparableClass2,string>();
		}

		/// <summary>
		/// Check that IsReadOnly always returns false.
		/// </summary>
		[Test]
		public void IsReadOnly()
		{
			OrderedDictionary<int,string> dict1 = new OrderedDictionary<int,string>();

			Assert.IsFalse(((IDictionary)dict1).IsReadOnly, "IsReadOnly should be false");
			Assert.IsFalse(((IDictionary<int,string>)dict1).IsReadOnly, "IsReadOnly should be false");
		}

		/// <summary>
		/// Check that IsFixedSize always returns false.
		/// </summary>
		[Test]
		public void IsFixedSize()
		{
			OrderedDictionary<int,string> dict1 = new OrderedDictionary<int,string>();

			Assert.IsFalse(((IDictionary)dict1).IsFixedSize, "IsFixedSize should be false");
		}

		/// <summary>
		/// Check the Keys and Values collections.
		/// </summary>
		[Test]
		public void KeysValuesCollections()
		{
			OrderedDictionary<string,int> dict1 = new OrderedDictionary<string,int>();

			dict1.Add("q", 17);
			dict1.Add("a", 143);
			dict1.Add("r", -5);
			dict1.Add("z", 0);
			dict1.Add("x", 12);
			dict1.Add("m", 17);

			ICollection keysCollection = ((IDictionary)dict1).Keys;
			ICollection<string> keysGenCollection = dict1.Keys;
			ICollection valuesCollection = ((IDictionary)dict1).Values;
			ICollection<int> valuesGenCollection = dict1.Values;

			InterfaceTests.TestCollection<string>(keysCollection, new string[] { "a", "m", "q", "r", "x", "z" }, true);
            InterfaceTests.TestReadonlyCollectionGeneric<string>(keysGenCollection, new string[] { "a", "m", "q", "r", "x", "z" }, true, "KeysCollection");
            InterfaceTests.TestCollection<int>(valuesCollection, new int[] { 143, 17, 17, -5, 12, 0 }, true);
            InterfaceTests.TestReadonlyCollectionGeneric<int>(valuesGenCollection, new int[] { 143, 17, 17, -5, 12, 0 }, true, "ValuesCollection");
        }

		/// <summary>
		/// Check that null keys and values work correctly.
		/// </summary>
		[Test]
		public void NullKeysValues()
		{
			OrderedDictionary<string,string> dict1 = new OrderedDictionary<string,string>();

			Assert.IsFalse(dict1.ContainsKey(null));

			dict1.Add("q",null);
			dict1.Add("a", "hello");
			dict1.Add(null, "goodbye");

			ICollection keysCollection = ((IDictionary)dict1).Keys;
			ICollection<string> keysGenCollection = dict1.Keys;
			ICollection valuesCollection = ((IDictionary)dict1).Values;
			ICollection<string> valuesGenCollection = dict1.Values;

			InterfaceTests.TestCollection<string>(keysCollection, new string[] { null, "a", "q" }, true);
            InterfaceTests.TestReadonlyCollectionGeneric(keysGenCollection, new string[] { null, "a", "q" }, true, "KeysCollection");
            InterfaceTests.TestCollection<string>(valuesCollection, new string[] { "goodbye", "hello", null }, true);
            InterfaceTests.TestReadonlyCollectionGeneric<string>(valuesGenCollection, new string[] { "goodbye", "hello", null }, true, "ValuesCollection");

            Assert.IsNull(dict1["q"]);
			Assert.AreEqual("goodbye", dict1[null]);
			Assert.IsTrue(dict1.ContainsKey(null));

			// Enumerate key/values directly. The pair with a null key will be enumerated.
			int i = 0;
			foreach (KeyValuePair<string,string> pair in dict1)
			{
                if (i == 0) {
                    Assert.AreEqual(null, pair.Key);
                    Assert.AreEqual("goodbye", pair.Value);
                }
                else if (i == 1) {
					Assert.AreEqual("a", pair.Key);
					Assert.AreEqual("hello", pair.Value);
				}
				else if (i == 2)
				{
					Assert.AreEqual("q", pair.Key);
					Assert.AreEqual(null, pair.Value);
				}
				else
				{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -