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

📄 algorithmstests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 5 页
字号:
            InterfaceTests.TestListGeneric(list2, new string[] { "X", "X", "X", "hello", "hello", "sailor", "X", "X", "hi" });

            list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" });
            Algorithms.ReplaceInPlace(list2, delegate(string x) { return x.Length == 3; }, null);
            InterfaceTests.TestListGeneric(list2, new string[] { null, null, null, "hello", "hello", "sailor", null, null, "hi" });

            list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" });
            Algorithms.ReplaceInPlace(list2, delegate(string x) { x = "zip"; return false; }, null);
            InterfaceTests.TestListGeneric(list2, new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" });

            string[] array1 = { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" };
            Algorithms.ReplaceInPlace(array1, delegate(string x) { return x.Length == 3; }, "X");
            InterfaceTests.TestEnumerableElements(array1, new string[] { "X", "X", "X", "hello", "hello", "sailor", "X", "X", "hi" });

        }

        [Test]
        public void EqualCollections1()
        {
            IEnumerable<string> coll1 = EnumerableFromArray(new string[0]);
            IEnumerable<string> coll2 = EnumerableFromArray(new string[0]);
            IEnumerable<string> coll3 = EnumerableFromArray(new string[] { "hello", "there" });
            IEnumerable<string> coll4 = EnumerableFromArray(new string[] { "hello", "there", "sailor" });
            IEnumerable<string> coll5 = EnumerableFromArray(new string[] { "Hello", "There", "Sailor" });

            Assert.IsTrue(Algorithms.EqualCollections(coll4, coll4));
            Assert.IsTrue(Algorithms.EqualCollections(coll1, coll2));
            Assert.IsTrue(Algorithms.EqualCollections(coll4, new List<string>(new string[] { "hello", "there", "sailor" })));
            Assert.IsFalse(Algorithms.EqualCollections(coll4, coll5));
            Assert.IsTrue(Algorithms.EqualCollections(coll4, coll5, StringComparer.InvariantCultureIgnoreCase));
            Assert.IsFalse(Algorithms.EqualCollections(coll3, coll4));
            Assert.IsFalse(Algorithms.EqualCollections(coll4, coll3));
            Assert.IsFalse(Algorithms.EqualCollections(coll4, coll1));
            Assert.IsFalse(Algorithms.EqualCollections(coll1, coll3));
        }

        [Test]
        public void EqualCollections2()
        {
            BinaryPredicate<int> pred = delegate(int x, int y) { return x <= y; };

            IEnumerable<int> coll1 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll2 = EnumerableFromArray(new int[] { 4, 8, 1 });
            IEnumerable<int> coll3 = EnumerableFromArray(new int[] { 7, 19, 3 });
            IEnumerable<int> coll4 = EnumerableFromArray(new int[] { 7, 19, 3, 11 });

            Assert.IsTrue(Algorithms.EqualCollections(coll1, coll1, pred));
            Assert.IsTrue(Algorithms.EqualCollections(coll2, coll3, pred));
            Assert.IsFalse(Algorithms.EqualCollections(coll2, coll4, pred));
            Assert.IsFalse(Algorithms.EqualCollections(coll3, coll2, pred));
        }

        [Test]
        public void LexicographicalCompare1()
        {
            IEnumerable<int> coll1 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll2 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll3 = EnumerableFromArray(new int[] { 4, 8, 1 });
            IEnumerable<int> coll4 = EnumerableFromArray(new int[] { 4, 8, 3 });
            IEnumerable<int> coll5 = EnumerableFromArray(new int[] { 3, 8, 2 });
            IEnumerable<int> coll6 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });
            IEnumerable<int> coll7 = EnumerableFromArray(new int[] { 4, 8, 0, 9 });
            IEnumerable<int> coll8 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });

            Assert.IsTrue(Algorithms.LexicographicalCompare(coll1, coll2) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll2, coll1) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll3) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll1, coll3) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll1) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll4) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll4, coll3) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll5) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll5, coll3) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll6) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll3) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll7) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll7, coll6) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll8) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll8, coll6) == 0);
        }


        [Test]
        public void GetLexicographicalComparer1()
        {
            IEnumerable<int> coll1 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll2 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll3 = EnumerableFromArray(new int[] { 4, 8, 1 });
            IEnumerable<int> coll4 = EnumerableFromArray(new int[] { 4, 8, 3 });
            IEnumerable<int> coll5 = EnumerableFromArray(new int[] { 3, 8, 2 });
            IEnumerable<int> coll6 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });
            IEnumerable<int> coll7 = EnumerableFromArray(new int[] { 4, 8, 0, 9 });
            IEnumerable<int> coll8 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });

            IComparer<IEnumerable<int>> comparer = Algorithms.GetLexicographicalComparer<int>();

            Assert.IsTrue(comparer.Compare(coll1, coll2) == 0);
            Assert.IsTrue(comparer.Compare(coll2, coll1) == 0);
            Assert.IsTrue(comparer.Compare(coll3, coll3) == 0);
            Assert.IsTrue(comparer.Compare(coll1, coll3) < 0);
            Assert.IsTrue(comparer.Compare(coll3, coll1) > 0);
            Assert.IsTrue(comparer.Compare(coll3, coll4) < 0);
            Assert.IsTrue(comparer.Compare(coll4, coll3) > 0);
            Assert.IsTrue(comparer.Compare(coll3, coll5) > 0);
            Assert.IsTrue(comparer.Compare(coll5, coll3) < 0);
            Assert.IsTrue(comparer.Compare(coll3, coll6) < 0);
            Assert.IsTrue(comparer.Compare(coll6, coll3) > 0);
            Assert.IsTrue(comparer.Compare(coll6, coll7) > 0);
            Assert.IsTrue(comparer.Compare(coll7, coll6) < 0);
            Assert.IsTrue(comparer.Compare(coll6, coll8) == 0);
            Assert.IsTrue(comparer.Compare(coll8, coll6) == 0);
        }

        [Test]
        public void LexicographicalCompare2()
        {
            IEnumerable<int> coll1 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll2 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll3 = EnumerableFromArray(new int[] { 4, 8, 1 });
            IEnumerable<int> coll4 = EnumerableFromArray(new int[] { 4, 8, 3 });
            IEnumerable<int> coll5 = EnumerableFromArray(new int[] { 5, 8, 2 });
            IEnumerable<int> coll6 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });
            IEnumerable<int> coll7 = EnumerableFromArray(new int[] { 4, 8, 0, 9 });
            IEnumerable<int> coll8 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });

            Assert.IsTrue(Algorithms.LexicographicalCompare(coll1, coll2, ComparersTests.CompareOddEven) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll2, coll1, ComparersTests.CompareOddEven) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll3, ComparersTests.CompareOddEven) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll1, coll3, ComparersTests.CompareOddEven) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll1, ComparersTests.CompareOddEven) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll4, ComparersTests.CompareOddEven) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll4, coll3, ComparersTests.CompareOddEven) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll5, ComparersTests.CompareOddEven) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll5, coll3, ComparersTests.CompareOddEven) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll6, ComparersTests.CompareOddEven) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll3, ComparersTests.CompareOddEven) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll7, ComparersTests.CompareOddEven) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll7, coll6, ComparersTests.CompareOddEven) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll8, ComparersTests.CompareOddEven) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll8, coll6, ComparersTests.CompareOddEven) == 0);
        }

        [Test]
        public void GetLexicographicalComparer2()
        {
            IEnumerable<int> coll1 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll2 = EnumerableFromArray(new int[0]);
            IEnumerable<int> coll3 = EnumerableFromArray(new int[] { 4, 8, 1 });
            IEnumerable<int> coll4 = EnumerableFromArray(new int[] { 4, 8, 3 });
            IEnumerable<int> coll5 = EnumerableFromArray(new int[] { 5, 8, 2 });
            IEnumerable<int> coll6 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });
            IEnumerable<int> coll7 = EnumerableFromArray(new int[] { 4, 8, 0, 9 });
            IEnumerable<int> coll8 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });

            IComparer<IEnumerable<int>> comparer = Algorithms.GetLexicographicalComparer<int>(ComparersTests.CompareOddEven);

            Assert.IsTrue(comparer.Compare(coll1, coll2) == 0);
            Assert.IsTrue(comparer.Compare(coll2, coll1) == 0);
            Assert.IsTrue(comparer.Compare(coll3, coll3) == 0);
            Assert.IsTrue(comparer.Compare(coll1, coll3) < 0);
            Assert.IsTrue(comparer.Compare(coll3, coll1) > 0);
            Assert.IsTrue(comparer.Compare(coll3, coll4) < 0);
            Assert.IsTrue(comparer.Compare(coll4, coll3) > 0);
            Assert.IsTrue(comparer.Compare(coll3, coll5) > 0);
            Assert.IsTrue(comparer.Compare(coll5, coll3) < 0);
            Assert.IsTrue(comparer.Compare(coll3, coll6) < 0);
            Assert.IsTrue(comparer.Compare(coll6, coll3) > 0);
            Assert.IsTrue(comparer.Compare(coll6, coll7) < 0);
            Assert.IsTrue(comparer.Compare(coll7, coll6) > 0);
            Assert.IsTrue(comparer.Compare(coll6, coll8) == 0);
            Assert.IsTrue(comparer.Compare(coll8, coll6) == 0);
        }

        [Test]
        public void LexicographicalCompare3()
        {
            IEnumerable<string> coll1 = EnumerableFromArray(new string[0]);
            IEnumerable<string> coll2 = EnumerableFromArray(new string[0]);
            IEnumerable<string> coll3 = EnumerableFromArray(new string[] { "eric", "foo", "a" });
            IEnumerable<string> coll4 = EnumerableFromArray(new string[] { "Eric", "Foo", "a"});
            IEnumerable<string> coll5 = EnumerableFromArray(new string[] { "Eric", "B", "c" });
            IEnumerable<string> coll6 = EnumerableFromArray(new string[] { "Eric", "FOO", "a", "b" });
            IEnumerable<string> coll7 = EnumerableFromArray(new string[] { "Snap", "eric" });

            Assert.IsTrue(Algorithms.LexicographicalCompare(coll1, coll2, StringComparer.InvariantCultureIgnoreCase) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll2, coll1, StringComparer.InvariantCultureIgnoreCase) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll3, StringComparer.InvariantCultureIgnoreCase) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll1, coll3, StringComparer.InvariantCultureIgnoreCase) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll1, StringComparer.InvariantCultureIgnoreCase) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll4, coll3, StringComparer.InvariantCultureIgnoreCase) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll4, StringComparer.InvariantCultureIgnoreCase) == 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll5, StringComparer.InvariantCultureIgnoreCase) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll5, coll3, StringComparer.InvariantCultureIgnoreCase) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll6, StringComparer.InvariantCultureIgnoreCase) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll6, coll3, StringComparer.InvariantCultureIgnoreCase) > 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll3, coll7, StringComparer.InvariantCultureIgnoreCase) < 0);
            Assert.IsTrue(Algorithms.LexicographicalCompare(coll7, coll3, StringComparer.InvariantCultureIgnoreCase) > 0);
        }


        [Test]
        public void GetLexicographicalComparer3()
        {
            IEnumerable<string> coll1 = EnumerableFromArray(new string[0]);
            IEnumerable<string> coll2 = EnumerableFromArray(new string[0]);
            IEnumerable<string> coll3 = EnumerableFromArray(new string[] { "eric", "foo", "a" });
            IEnumerable<string> coll4 = EnumerableFromArray(new string[] { "Eric", "Foo", "a" });
            IEnumerable<string> coll5 = EnumerableFromArray(new string[] { "Eric", "B", "c" });
            IEnumerable<string> coll6 = EnumerableFromArray(new string[] { "Eric", "FOO", "a", "b" });
            IEnumerable<string> coll7 = EnumerableFromArray(new string[] { "Snap", "eric" });

            IComparer<IEnumerable<string>> comparer = Algorithms.GetLexicographicalComparer(StringComparer.InvariantCultureIgnoreCase);

            Assert.IsTrue(comparer.Compare(coll1, coll2) == 0);
            Assert.IsTrue(comparer.Compare(coll2, coll1) == 0);
            Assert.IsTrue(comparer.Compare(coll3, coll3) == 0);
            Assert.IsTrue(comparer.Compare(coll1, coll3) < 0);
            Assert.IsTrue(comparer.Compare(coll3, coll1) > 0);
            Assert.IsTrue(comparer.Compare(coll4, coll3) == 0);
            Assert.IsTrue(comparer.Compare(coll3, coll4) == 0);
            Assert.IsTrue(comparer.Compare(coll3, coll5) > 0);
            Assert.IsTrue(comparer.Compare(coll5, coll3) < 0);
            Assert.IsTrue(comparer.Compare(coll3, coll6) < 0);
            Assert.IsTrue(comparer.Compare(coll6, coll3) > 0);
            Assert.IsTrue(comparer.Compare(coll3, coll7) < 0);
            Assert.IsTrue(comparer.Compare(coll7, coll3) > 0);
        }
        /*
                [Test]
                public void LexicographicsEqualityComparer1()
                {
                    IEnumerable<int> coll1 = EnumerableFromArray(new int[0]);
                    IEnumerable<int> coll2 = EnumerableFromArray(new int[0]);
                    IEnumerable<int> coll3 = EnumerableFromArray(new int[] { 4, 8, 1 });
                    IEnumerable<int> coll4 = EnumerableFromArray(new int[] { 4, 8, 3 });
                    IEnumerable<int> coll5 = EnumerableFromArray(new int[] { 3, 8, 2 });
                    IEnumerable<int> coll6 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });
                    IEnumerable<int> coll7 = EnumerableFromArray(new int[] { 4, 8, 0, 9 });
                    IEnumerable<int> coll8 = EnumerableFromArray(new int[] { 4, 8, 1, 9 });

                    IEqualityComparer<IEnumerable<int>> comparer = Algorithms.LexicographicalEqualityComparer<int>();

⌨️ 快捷键说明

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