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

📄 pairtests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 2 页
字号:
        {
            Pair<int, OddEvenComparable> pair1, pair2;

            pair1 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(7, new OddEvenComparable(3));
            Assert.IsTrue(pair1.CompareTo(pair2) < 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(2));
            Assert.IsTrue(pair1.CompareTo(pair2) < 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            Assert.IsTrue(pair1.CompareTo(pair2) == 0);
            Assert.IsTrue(pair1.Equals(pair2));
            Assert.IsTrue(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, OddEvenComparable>(7, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(2));
            Assert.IsTrue(pair1.CompareTo(pair2) > 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, OddEvenComparable>(0, new OddEvenComparable(8));
            pair2 = new Pair<int, OddEvenComparable>(0, new OddEvenComparable(2));
            Assert.IsTrue(pair1.CompareTo(pair2) > 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            Pair<OddEvenComparable, int> pair3, pair4;

            pair3 = new Pair<OddEvenComparable, int>(new OddEvenComparable(7), 4);
            pair4 = new Pair<OddEvenComparable, int>(new OddEvenComparable(3), 7);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<OddEvenComparable, int>(new OddEvenComparable(7), 4);
            pair4 = new Pair<OddEvenComparable, int>(new OddEvenComparable(2), 4);
            Assert.IsTrue(pair3.CompareTo(pair4) < 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<OddEvenComparable, int>(new OddEvenComparable(7), 4);
            pair4 = new Pair<OddEvenComparable, int>(new OddEvenComparable(7), 4);
            Assert.IsTrue(pair3.CompareTo(pair4) == 0);
            Assert.IsTrue(pair3.Equals(pair4));
            Assert.IsTrue(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<OddEvenComparable, int>(new OddEvenComparable(2), 7);
            pair4 = new Pair<OddEvenComparable, int>(new OddEvenComparable(7), 4);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<OddEvenComparable, int>(new OddEvenComparable(8), 0);
            pair4 = new Pair<OddEvenComparable, int>(new OddEvenComparable(2), 0);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<OddEvenComparable, int>(new OddEvenComparable(2), 4);
            pair4 = new Pair<OddEvenComparable, int>(new OddEvenComparable(2), 3);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());
        }

        [Test]
        public void GenericComparable()
        {
            Pair<int, GOddEvenComparable> pair1, pair2;

            pair1 = new Pair<int, GOddEvenComparable>(4, new GOddEvenComparable(7));
            pair2 = new Pair<int, GOddEvenComparable>(7, new GOddEvenComparable(3));
            Assert.IsTrue(pair1.CompareTo(pair2) < 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, GOddEvenComparable>(4, new GOddEvenComparable(7));
            pair2 = new Pair<int, GOddEvenComparable>(4, new GOddEvenComparable(2));
            Assert.IsTrue(pair1.CompareTo(pair2) < 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, GOddEvenComparable>(4, new GOddEvenComparable(7));
            pair2 = new Pair<int, GOddEvenComparable>(4, new GOddEvenComparable(7));
            Assert.IsTrue(pair1.CompareTo(pair2) == 0);
            Assert.IsTrue(pair1.Equals(pair2));
            Assert.IsTrue(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, GOddEvenComparable>(7, new GOddEvenComparable(7));
            pair2 = new Pair<int, GOddEvenComparable>(4, new GOddEvenComparable(2));
            Assert.IsTrue(pair1.CompareTo(pair2) > 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            pair1 = new Pair<int, GOddEvenComparable>(0, new GOddEvenComparable(8));
            pair2 = new Pair<int, GOddEvenComparable>(0, new GOddEvenComparable(2));
            Assert.IsTrue(pair1.CompareTo(pair2) > 0);
            Assert.IsFalse(pair1.Equals(pair2));
            Assert.IsFalse(pair1.GetHashCode() == pair2.GetHashCode());

            Pair<GOddEvenComparable, int> pair3, pair4;

            pair3 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(7), 4);
            pair4 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(3), 7);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(7), 4);
            pair4 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(2), 4);
            Assert.IsTrue(pair3.CompareTo(pair4) < 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(7), 4);
            pair4 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(7), 4);
            Assert.IsTrue(pair3.CompareTo(pair4) == 0);
            Assert.IsTrue(pair3.Equals(pair4));
            Assert.IsTrue(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(2), 7);
            pair4 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(7), 4);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(8), 0);
            pair4 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(2), 0);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());

            pair3 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(2), 4);
            pair4 = new Pair<GOddEvenComparable, int>(new GOddEvenComparable(2), 3);
            Assert.IsTrue(pair3.CompareTo(pair4) > 0);
            Assert.IsFalse(pair3.Equals(pair4));
            Assert.IsFalse(pair3.GetHashCode() == pair4.GetHashCode());
        }

        [Test]
        public void DictionaryKey()
        {
            OrderedDictionary<Pair<string, int>, string> dict1 = new OrderedDictionary<Pair<string, int>, string>();

            dict1[new Pair<string, int>("foo", 12)] = "hello";
            dict1[new Pair<string, int>("zebra", 1)] = "long";
            dict1[new Pair<string, int>("zebra", 17)] = "strange";
            dict1[new Pair<string, int>("zzz", 14)] = "trip";
            dict1[new Pair<string, int>("foo", 16)] = "goodbye";
            dict1[new Pair<string, int>("foo", 12)] = "another";

            string[] s_array = { "another", "goodbye", "long", "strange", "trip" };

            Assert.AreEqual(5, dict1.Count);
            int i = 0;
            foreach (string s in dict1.Values) {
                Assert.AreEqual(s_array[i], s);
                ++i;
            }
        }

        [Test]
        public void IComparable()
        {
            Pair<int, OddEvenComparable> pair1, pair2;
            IComparable comp;
            object o;

            pair1 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(7, new OddEvenComparable(3));
            comp = pair1;
            o = pair2;
            Assert.IsTrue(comp.CompareTo(o) < 0);

            pair1 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(2));
            comp = pair1;
            o = pair2;
            Assert.IsTrue(comp.CompareTo(o) < 0);

            pair1 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(7));
            comp = pair1;
            o = pair2;
            Assert.IsTrue(comp.CompareTo(o) == 0);

            pair1 = new Pair<int, OddEvenComparable>(7, new OddEvenComparable(7));
            pair2 = new Pair<int, OddEvenComparable>(4, new OddEvenComparable(2));
            comp = pair1;
            o = pair2;
            Assert.IsTrue(comp.CompareTo(o) > 0);

            try {
                int i = comp.CompareTo("foo");
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentException);
            }
        }

        [Test]
        public void FromKeyValuePair()
        {
            KeyValuePair<int, string> kvp1 = new KeyValuePair<int, string>(-13, "hello");
            Pair<int, string> p1 = (Pair<int, string>)kvp1;
            Assert.AreEqual(-13, p1.First);
            Assert.AreEqual("hello", p1.Second);
            Pair<int, string> q1 = new Pair<int, string>(kvp1);
            Assert.AreEqual(-13, q1.First);
            Assert.AreEqual("hello", q1.Second);

            KeyValuePair<string, object> kvp2 = new KeyValuePair<string, object>();
            Pair<string, object> p2 = (Pair<string, object>)kvp2;
            Assert.IsNull(p2.First);
            Assert.IsNull(p2.Second);
            Pair<string, object> q2 = new Pair<string,object>(kvp2);
            Assert.IsNull(q2.First);
            Assert.IsNull(q2.Second);

            object x = new Hashtable();
            KeyValuePair<object, double> kvp3 = new KeyValuePair<object, double>(x, 6.7);
            Pair<object, double> p3 = (Pair<object, double>)kvp3;
            Assert.AreSame(x, p3.First);
            Assert.AreEqual(6.7, p3.Second);
            Pair<object, double> q3 =  new Pair<object, double>(kvp3);
            Assert.AreSame(x, q3.First);
            Assert.AreEqual(6.7, q3.Second);
        }

        [Test]
        public void ToKeyValuePair()
        {
            Pair<int, string> p1 = new Pair<int, string>(-13, "hello");
            KeyValuePair<int, string> kvp1 = (KeyValuePair<int, string>)p1;
            Assert.AreEqual(-13, kvp1.Key);
            Assert.AreEqual("hello", kvp1.Value);
            KeyValuePair<int, string> kv1 = p1.ToKeyValuePair();
            Assert.AreEqual(-13, kv1.Key);
            Assert.AreEqual("hello", kv1.Value);

            Pair<string, object> p2 = new Pair<string, object>();
            KeyValuePair<string, object> kvp2 = (KeyValuePair<string, object>)p2;
            Assert.IsNull(kvp2.Key);
            Assert.IsNull(kvp2.Value);
            KeyValuePair<string, object> kv2 = p2.ToKeyValuePair();
            Assert.IsNull(kv2.Key);
            Assert.IsNull(kv2.Value);

            object x = new Hashtable();
            Pair<object, double> p3 = new Pair<object, double>(x, 6.7);
            KeyValuePair<object, double> kvp3 = (KeyValuePair<object, double>)p3;
            Assert.AreSame(x, kvp3.Key);
            Assert.AreEqual(6.7, kvp3.Value);
            KeyValuePair<object, double> kv3 = p3.ToKeyValuePair();
            Assert.AreSame(x, kv3.Key);
            Assert.AreEqual(6.7, kv3.Value);
        }

        [Test]
        public void Serialize()
        {
            Pair<int, string> p1 = new Pair<int, string>(-12, "hello");
            Pair<string, double> p2 = new Pair<string, double>("hi", 11);
            Pair<int, string> s1 = (Pair<int, string>)InterfaceTests.SerializeRoundTrip(p1);
            Pair<string,double> s2 = (Pair<string, double>)InterfaceTests.SerializeRoundTrip(p2);
            Assert.AreEqual(p1, s1);
            Assert.AreEqual(p2, s2);
        }
    }
}

⌨️ 快捷键说明

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