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

📄 algorithmstests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 5 页
字号:

        [Test]
        public void Fill()
        {
            BigList<string> coll1 = new BigList<string>(new string[4] { "foo", null, "baz", "elvis" });
            Algorithms.Fill(coll1, "xyzzy");
            InterfaceTests.TestListGeneric(coll1, new string[] { "xyzzy", "xyzzy", "xyzzy", "xyzzy" });

            List<string> coll2 = new List<string>();
            Algorithms.Fill(coll2, "xyzzy");
            InterfaceTests.TestListGeneric(coll2, new string[] { });

            IList<string> coll3 = new List<string>(new string[] { "hi", "there" }).AsReadOnly();
            try {
                Algorithms.Fill(coll3, "xyzzy");
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentException);
            }

            BigList<int> coll4 = new BigList<int>(new int[] { 2 }, 10000);
            Algorithms.Fill(coll4, 42);
            Assert.AreEqual(coll4.Count, 10000);
            foreach (int x in coll4)
                Assert.AreEqual(42, x);
        }

        [Test]
        public void FillArray()
        {
            string[] coll1 = { "foo", null, "baz", "elvis" };
            Algorithms.Fill(coll1, "xyzzy");
            InterfaceTests.TestEnumerableElements(coll1, new string[] { "xyzzy", "xyzzy", "xyzzy", "xyzzy" });

            string[] coll2 = { };
            Algorithms.Fill(coll2, "xyzzy");
            InterfaceTests.TestEnumerableElements(coll2, new string[] { });
        }

        [Test]
        public void FillRange()
        {
            BigList<string> coll1 = new BigList<string>(new string[4] { "foo", null, "baz", "elvis" });
            Algorithms.FillRange(coll1, 1, 2, "xyzzy");
            InterfaceTests.TestListGeneric(coll1, new string[] { "foo", "xyzzy", "xyzzy", "elvis" });
            Algorithms.FillRange(coll1, 0, 4, "ben");
            InterfaceTests.TestListGeneric(coll1, new string[] { "ben", "ben", "ben", "ben" });
            Algorithms.FillRange(coll1, 1, 0, "foo");
            InterfaceTests.TestListGeneric(coll1, new string[] { "ben", "ben", "ben", "ben" });
            Algorithms.FillRange(coll1, 0, 2, null);
            InterfaceTests.TestListGeneric(coll1, new string[] { null, null, "ben", "ben" });

            List<string> coll2 = new List<string>();
            Algorithms.FillRange(coll2, 0, 0, "xyzzy");
            InterfaceTests.TestListGeneric(coll2, new string[] { });

            IList<string> coll3 = new List<string>(new string[] { "hi", "there" }).AsReadOnly();
            try {
                Algorithms.FillRange(coll3, 1, 1, "xyzzy");
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentException);
            }

            BigList<int> coll4 = new BigList<int>(new int[] { 2 }, 10000);
            Algorithms.FillRange(coll4, 1, 9998, 42);
            Assert.AreEqual(coll4.Count, 10000);
            int i = 0;
            foreach (int x in coll4) {
                if (i == 0 || i == 9999)
                    Assert.AreEqual(2, x);
                else
                    Assert.AreEqual(42, x);

                ++i;
            }

            try {
                Algorithms.FillRange(coll4, -1, 2, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                Algorithms.FillRange(coll4,3, -2, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                Algorithms.FillRange(coll4, 9999, 2, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
        }

        [Test]
        public void FillArrayRange()
        {
            string[] coll1 = { "foo", null, "baz", "elvis" };
            Algorithms.FillRange(coll1, 1, 2, "xyzzy");
            InterfaceTests.TestEnumerableElements(coll1, new string[] { "foo", "xyzzy", "xyzzy", "elvis" });
            Algorithms.FillRange(coll1, 0, 4, "ben");
            InterfaceTests.TestEnumerableElements(coll1, new string[] { "ben", "ben", "ben", "ben" });
            Algorithms.FillRange(coll1, 1, 0, "foo");
            InterfaceTests.TestEnumerableElements(coll1, new string[] { "ben", "ben", "ben", "ben" });
            Algorithms.FillRange(coll1, 0, 2, null);
            InterfaceTests.TestEnumerableElements(coll1, new string[] { null, null, "ben", "ben" });

            string[] coll2 = { };
            Algorithms.FillRange(coll2, 0, 0, "xyzzy");
            InterfaceTests.TestEnumerableElements(coll2, new string[] { });

            int[] coll4 = new int[10000];

            try {
                Algorithms.FillRange(coll4, -1, 2, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                Algorithms.FillRange(coll4, 3, -2, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                Algorithms.FillRange(coll4, 9999, 2, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
        }

        [Test]
        public void RemoveDuplicates()
        {
            IEnumerable<string> coll1 = EnumerableFromArray(new string[] { "foo", "foo", "foo", "big", "foo", "super", "super", null, null, null, "hello", "there", "sailor", "sailor" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll1), new string[] { "foo", "big", "foo", "super", null, "hello", "there", "sailor" });

            IEnumerable<string> coll2 = EnumerableFromArray(new string[] {  });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll2), new string[] {  });

            IEnumerable<string> coll3 = EnumerableFromArray(new string[] { "foo"});
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll3), new string[] { "foo" });

            IEnumerable<string> coll4 = EnumerableFromArray(new string[] { "foo", "foo" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll4), new string[] { "foo" });

            IEnumerable<string> coll5 = EnumerableFromArray(new string[] { "foo", "bar", "bar" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll5), new string[] { "foo", "bar" });

            IEnumerable<string> coll6 = EnumerableFromArray(new string[] { "foo", "foo", "bar" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll6), new string[] { "foo", "bar" });

            IEnumerable<string> coll11 = EnumerableFromArray(new string[] { "foo", "Foo", "FOO", "big", "foo", "SUPER", "super", null, null, null, "hello", "there", "sailor", "sailor" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll11, StringComparer.InvariantCultureIgnoreCase), new string[] { "foo", "big", "foo", "SUPER", null, "hello", "there", "sailor" });

            IEnumerable<string> coll12 = EnumerableFromArray(new string[] { });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll12, StringComparer.InvariantCultureIgnoreCase), new string[] { });

            IEnumerable<string> coll13 = EnumerableFromArray(new string[] { "foo" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll13, StringComparer.InvariantCultureIgnoreCase), new string[] { "foo" });

            IEnumerable<string> coll14 = EnumerableFromArray(new string[] { "Foo", "foo" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll14, StringComparer.InvariantCultureIgnoreCase), new string[] { "Foo" });

            IEnumerable<string> coll15 = EnumerableFromArray(new string[] { "foo", "bAr", "bar" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll15, StringComparer.InvariantCultureIgnoreCase), new string[] { "foo", "bAr" });

            IEnumerable<string> coll16 = EnumerableFromArray(new string[] { "foo", "fOo", "bar" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll16, StringComparer.InvariantCultureIgnoreCase), new string[] { "foo", "bar" });

            BinaryPredicate<string> pred = delegate(string x, string y) {
                return x[0] == y[0];
            };

            IEnumerable<string> coll17 = EnumerableFromArray(new string[] { "fiddle", "faddle", "bar", "bing", "deli", "zippy", "zack", "zorch" });
            InterfaceTests.TestEnumerableElements(Algorithms.RemoveDuplicates(coll17, pred), new string[] { "fiddle", "bar" , "deli", "zippy"});
        }

        [Test]
        public void RemoveDuplicatesInPlace()
        {
            IList<string> list1 = new List<string>(new string[] { "foo", "foo", "foo", "big", "foo", "super", "super", null, null, null, "hello", "there", "sailor", "sailor" });
            Algorithms.RemoveDuplicatesInPlace(list1);
            InterfaceTests.TestListGeneric(list1, new string[] { "foo", "big", "foo", "super", null, "hello", "there", "sailor" });

            IList<string> list2 = new List<string>(new string[] { });
            Algorithms.RemoveDuplicatesInPlace(list2);
            InterfaceTests.TestListGeneric(list2, new string[] { });

            IList<string> list3 = new List<string>(new string[] { "foo" });
            Algorithms.RemoveDuplicatesInPlace(list3);
            InterfaceTests.TestListGeneric(list3, new string[] { "foo" });

            IList<string> list4 = new List<string>(new string[] { "foo", "foo" });
            Algorithms.RemoveDuplicatesInPlace(list4);
            InterfaceTests.TestListGeneric(list4, new string[] { "foo" });

            IList<string> list5 = new List<string>(new string[] { "foo", "bar", "bar" });
            Algorithms.RemoveDuplicatesInPlace(list5);
            InterfaceTests.TestListGeneric(list5, new string[] { "foo", "bar" });

            IList<string> list6 = new List<string>(new string[] { "foo", "foo", "bar" });
            Algorithms.RemoveDuplicatesInPlace(list6);
            InterfaceTests.TestListGeneric(list6, new string[] { "foo", "bar" });

            IList<string> list11 = new List<string>(new string[] { "foo", "Foo", "FOO", "big", "foo", "SUPER", "super", null, null, null, "hello", "there", "sailor", "sailor" });
            Algorithms.RemoveDuplicatesInPlace(list11, StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestListGeneric(list11, new string[] { "foo", "big", "foo", "SUPER", null, "hello", "there", "sailor" });

            IList<string> list12 = new List<string>(new string[] { });
            Algorithms.RemoveDuplicatesInPlace(list12, StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestListGeneric(list12, new string[] { });

            IList<string> list13 = new List<string>(new string[] { "foo" });
            Algorithms.RemoveDuplicatesInPlace(list13, StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestListGeneric(list13, new string[] { "foo" });

            IList<string> list14 = new List<string>(new string[] { "Foo", "foo" });
            Algorithms.RemoveDuplicatesInPlace(list14, StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestListGeneric(list14, new string[] { "Foo" });

            IList<string> list15 = new List<string>(new string[] { "foo", "bAr", "bar" });
            Algorithms.RemoveDuplicatesInPlace(list15, Str

⌨️ 快捷键说明

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