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

📄 tripletests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 2 页
字号:
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement 
// contained in the file "License.txt" accompanying this file.
//******************************

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

namespace Wintellect.PowerCollections.Tests
{
    /// <summary>
    /// Tests for the Triple struct.
    /// </summary>
    [TestFixture]
    public class TripleTests
    {
        /// <summary>
        /// Class that doesn't implement any IComparable.
        /// </summary>
        class Unorderable
        {
            public override bool Equals(object obj)
            {
                return obj is Unorderable;
            }

            public override int GetHashCode()
            {
                return 42;
            }
        }

        /// <summary>
        /// Comparable that compares ints, sorting odds before evens.
        /// </summary>
        class OddEvenComparable : System.IComparable
        {
            public int val;

            public OddEvenComparable(int v)
            {
                val = v;
            }

            public int CompareTo(object other)
            {
                int e1 = val;
                int e2 = ((OddEvenComparable)other).val;
                if ((e1 & 1) == 1 && (e2 & 1) == 0)
                    return -1;
                else if ((e1 & 1) == 0 && (e2 & 1) == 1)
                    return 1;
                else if (e1 < e2)
                    return -1;
                else if (e1 > e2)
                    return 1;
                else
                    return 0;
            }

            public override bool Equals(object obj)
            {
                if (obj is OddEvenComparable)
                    return CompareTo((OddEvenComparable)obj) == 0;
                else
                    return false;
            }

            public override int GetHashCode()
            {
                return val.GetHashCode();
            }

        }

        /// <summary>
        /// Comparable that compares ints, sorting odds before evens.
        /// </summary>
        class GOddEvenComparable : System.IComparable<GOddEvenComparable>
        {
            public int val;

            public GOddEvenComparable(int v)
            {
                val = v;
            }

            public int CompareTo(GOddEvenComparable other)
            {
                int e1 = val;
                int e2 = other.val;
                if ((e1 & 1) == 1 && (e2 & 1) == 0)
                    return -1;
                else if ((e1 & 1) == 0 && (e2 & 1) == 1)
                    return 1;
                else if (e1 < e2)
                    return -1;
                else if (e1 > e2)
                    return 1;
                else
                    return 0;
            }

            public override bool Equals(object other)
            {
                return (other is GOddEvenComparable) && CompareTo((GOddEvenComparable)other) == 0;
            }

            public override int GetHashCode()
            {
                return val.GetHashCode();
            }
        }

        /// <summary>
        /// Test basic Triple creation.  
        /// </summary>
        [Test]
        public void Creation()
        {
            Triple<int, double, string> p1 = new Triple<int, double, string>();

            Assert.AreEqual(0, p1.First);
            Assert.AreEqual(0.0, p1.Second);
            Assert.IsNull(p1.Third);

            Triple<int, string, char> p2 = new Triple<int, string, char>(42, "hello", 'X');

            Assert.AreEqual(42, p2.First);
            Assert.AreEqual("hello", p2.Second);
            Assert.AreEqual('X', p2.Third);

            Triple<string, object, IEnumerable<int>> p3 = new Triple<string, object, IEnumerable<int>>();

            Assert.IsNull(p3.First);
            Assert.IsNull(p3.Second);
            Assert.IsNull(p3.Third);

            object o = new object();
            Triple<Triple<string, int, char>, object, Pair<double, float>> p4 =
                new Triple<Triple<string, int, char>, object, Pair<double, float>>(new Triple<string, int, char>("foo", 12, 'X'), o, new Pair<double,float>(3.45, -1.2F));
            Triple<string, int, char> p5 = p4.First;

            Assert.AreEqual("foo", p5.First);
            Assert.AreEqual(12, p5.Second);
            Assert.AreEqual('X', p5.Third);
            Assert.AreSame(o, p4.Second);
            Assert.AreEqual(3.45, p4.Third.First);
            Assert.AreEqual(-1.2F, p4.Third.Second);
        }

        /// <summary>
        /// Test get and set of First and Second, and Third.
        /// </summary>
        [Test]
        public void Elements()
        {
            Triple<int, string, double> p1 = new Triple<int, string, double>();
            string s = new string('z', 3);

            p1.First = 217;
            p1.Second = s;
            p1.Third = 3.14;
            Assert.AreEqual(217, p1.First);
            Assert.AreSame(s, p1.Second);
            Assert.AreEqual(3.14, p1.Third);

            object o = new System.Collections.BitArray(4);
            Triple<string, int, object> p2 = new Triple<string, int, object>("hello", 1, new System.Text.StringBuilder());
            p2.Second = 212;
            p2.First = s;
            p2.Third = o;

            Assert.AreEqual(212, p2.Second);
            Assert.AreSame(s, p2.First);
            Assert.AreSame(o, p2.Third);
            p2.First = null;
            Assert.IsNull(p2.First);
            p2.Third = null;
            Assert.IsNull(p2.Third);
        }

        [Test]
        public void Equals()
        {
            Triple<int, string, double> p1 = new Triple<int, string, double>(42, new string('z', 3), 4.5);
            Triple<int, string, double> p2 = new Triple<int, string, double>(53, new string('z', 3), 4.5);
            Triple<int, string, double> p3 = new Triple<int, string, double>(42, new string('z', 4), 2.1);
            Triple<int, string, double> p4 = new Triple<int, string, double>(42, new string('z', 3), 4.5);
            Triple<int, string, double> p5 = new Triple<int, string, double>(122, new string('y', 3), 3.14);
            Triple<int, string, double> p6 = new Triple<int, string, double>(122, null, 3.14);
            Triple<int, string, double> p7 = new Triple<int, string, double>(122, null, 3.14);
            bool f;

            f = p1.Equals(p2); Assert.IsFalse(f);
            f = p1.Equals(p3); Assert.IsFalse(f);
            f = p1.Equals(p4); Assert.IsTrue(f);
            f = p1.Equals(p5); Assert.IsFalse(f);
            f = p1.Equals("hi"); Assert.IsFalse(f);
            f = p6.Equals(p7); Assert.IsTrue(f);
            f = p1 == p2; Assert.IsFalse(f);
            f = p1 == p3; Assert.IsFalse(f);
            f = p1 == p4; Assert.IsTrue(f);
            f = p1 == p5; Assert.IsFalse(f);
            f = p6 == p7; Assert.IsTrue(f);
            f = p1 != p2; Assert.IsTrue(f);
            f = p1 != p3; Assert.IsTrue(f);
            f = p1 != p4; Assert.IsFalse(f);
            f = p1 != p5; Assert.IsTrue(f);
            f = p6 != p7; Assert.IsFalse(f);
        }

        [Test]
        public void HashCode()
        {
            Triple<int, string, double> p1 = new Triple<int, string, double>(42, new string('z', 3), 4.5);
            Triple<int, string, double> p2 = new Triple<int, string, double>(53, new string('z', 3), 4.5);
            Triple<int, string, double> p3 = new Triple<int, string, double>(42, new string('z', 4), 2.1);
            Triple<int, string, double> p4 = new Triple<int, string, double>(42, new string('z', 3), 4.5);
            Triple<int, string, double> p5 = new Triple<int, string, double>(122, new string('y', 3), 3.14);
            Triple<int, string, double> p6 = new Triple<int, string, double>(122, null, 3.14);
            Triple<int, string, double> p7 = new Triple<int, string, double>(122, null, 3.14);

            int h1 = p1.GetHashCode();
            int h2 = p2.GetHashCode();
            int h3 = p3.GetHashCode();
            int h4 = p4.GetHashCode();
            int h5 = p5.GetHashCode();
            int h6 = p6.GetHashCode();
            int h7 = p7.GetHashCode();

            bool f;
            f = h1 == h2; Assert.IsFalse(f);
            f = h1 == h3; Assert.IsFalse(f);
            f = h1 == h4; Assert.IsTrue(f);
            f = h1 == h5; Assert.IsFalse(f);
            f = h6 == h7; Assert.IsTrue(f);
        }

        [Test]
        public void Stringize()
        {
            Triple<int, string, StringBuilder> p1 = new Triple<int, string, StringBuilder>(42, new string('z', 3), new StringBuilder("foo"));
            Triple<int, string, StringBuilder> p2 = new Triple<int, string, StringBuilder>(0, "hello", null);
            Triple<int, string, StringBuilder> p3 = new Triple<int, string, StringBuilder>(-122, null, new StringBuilder());
            Triple<string, int, StringBuilder> p4 = new Triple<string, int, StringBuilder>(null, 11, new StringBuilder("Eric"));

            Assert.AreEqual("First: 42, Second: zzz, Third: foo", p1.ToString());
            Assert.AreEqual("First: 0, Second: hello, Third: null", p2.ToString());
            Assert.AreEqual("First: -122, Second: null, Third: ", p3.ToString());
            Assert.AreEqual("First: null, Second: 11, Third: Eric", p4.ToString());
        }

        [Test]
        [ExpectedException(typeof(NotSupportedException), "Type \"Wintellect.PowerCollections.Tests.TripleTests+Unorderable\" does not implement IComparable<Wintellect.PowerCollections.Tests.TripleTests+Unorderable> or IComparable.")]
        public void UncomparableFirst()
        {
            Triple<Unorderable, int, string> triple1, triple2;
            triple1 = new Triple<Unorderable, int, string>(new Unorderable(), 5, "hello");
            triple2 = new Triple<Unorderable, int, string>(new Unorderable(), 7, "world");
            int compare = triple1.CompareTo(triple2);
        }

        [Test]
        [ExpectedException(typeof(NotSupportedException), "Type \"Wintellect.PowerCollections.Tests.TripleTests+Unorderable\" does not implement IComparable<Wintellect.PowerCollections.Tests.TripleTests+Unorderable> or IComparable.")]
        public void UncomparableSecond()
        {
            Triple<int, Unorderable, string> triple1, triple2;
            triple1 = new Triple<int, Unorderable, string>(3, new Unorderable(), "Eric");
            triple2 = new Triple<int, Unorderable, string>(3, new Unorderable(), "Clapton");
            int compare = triple1.CompareTo(triple2);
        }

        [Test]
        [ExpectedException(typeof(NotSupportedException), "Type \"Wintellect.PowerCollections.Tests.TripleTests+Unorderable\" does not implement IComparable<Wintellect.PowerCollections.Tests.TripleTests+Unorderable> or IComparable.")]
        public void UncomparableThird()
        {
            Triple<int, string, Unorderable> triple1, triple2;
            triple1 = new Triple<int, string, Unorderable>(3, "Oasis", new Unorderable());
            triple2 = new Triple<int, string, Unorderable>(3, "Oasis", new Unorderable());
            int compare = triple1.CompareTo(triple2);
        }

        [Test]
        public void EqualUncomparable()
        {
            Triple<Unorderable, int, string> triple1, triple2, triple3;
            triple1 = new Triple<Unorderable, int, string>(new Unorderable(), 5, "hello");
            triple2 = new Triple<Unorderable, int, string>(new Unorderable(), 7, "world");
            triple3 = new Triple<Unorderable, int, string>(new Unorderable(), 5, "hello");
            Assert.IsFalse(triple1.Equals(triple2));

⌨️ 快捷键说明

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