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

📄 algorithmstests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 5 页
字号:
            Assert.AreEqual(0, index);
            Assert.AreEqual(0, list.Count);

            list = new List<double>(new double[] { -3.1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(1, index);
            Assert.AreEqual(1, list.Count);
            Assert.AreEqual(-3.1, list[0]);

            list = new List<double>(new double[] { 3.1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(0, index);
            Assert.AreEqual(1, list.Count);
            Assert.AreEqual(3.1, list[0]);

            list = new List<double>(new double[] { -2, 3.1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(1, index);
            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(-2, list[0]);
            Assert.AreEqual(3.1, list[1]);

            list = new List<double>(new double[] { 2, -3.1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(1, index);
            Assert.AreEqual(2, list.Count);
            Assert.AreEqual(-3.1, list[0]);
            Assert.AreEqual(2, list[1]);

            list = new List<double>(new double[] { -2, -3.1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(2, index);
            InterfaceTests.TestEnumerableElements(list, new double[] { -2, -3.1 });

            list = new List<double>(new double[] { 2, 3.1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(0, index);
            InterfaceTests.TestEnumerableElements(list, new double[] { 2, 3.1 });

            list = new List<double>(new double[] { 2, 6, -8, -7, 3, -1, -2, 4, 2 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(4, index);
            InterfaceTests.TestEnumerableElements(list.GetRange(0, index), new double[] { -8, -7, -1, -2 });
            InterfaceTests.TestEnumerableElements(list.GetRange(index, list.Count - index), new double[] { 2, 6, 3, 4, 2 });

            double[] array = { 2, 6, -8, -7, 3, -1, -2, 4, 2 };
            index = Algorithms.StablePartition(array, isNegative);
            Assert.AreEqual(4, index);
            InterfaceTests.TestEnumerableElements(Algorithms.Range(array, 0, index), new double[] { -8, -7, -1, -2 });
            InterfaceTests.TestEnumerableElements(Algorithms.Range(array, index, list.Count - index), new double[] { 2, 6, 3, 4, 2 });

            list = new List<double>(new double[] { 2, 6, 9, 1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(0, index);
            InterfaceTests.TestEnumerableElements(list.GetRange(0, index), new double[] {  });
            InterfaceTests.TestEnumerableElements(list.GetRange(index, list.Count - index), new double[] { 2, 6, 9, 1 });

            list = new List<double>(new double[] { -2, -6, -9, -1 });
            index = Algorithms.StablePartition(list, isNegative);
            Assert.AreEqual(4, index);
            InterfaceTests.TestEnumerableElements(list.GetRange(0, index), new double[] { -2, -6, -9, -1 });
            InterfaceTests.TestEnumerableElements(list.GetRange(index, list.Count - index), new double[] {  });
        }

        [Test]
        public void ToArrayEnumerable()
        {
            IEnumerable<double> coll1 = EnumerableFromArray(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
            double[] array1 = Algorithms.ToArray(coll1);
            double[] expected = { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 };
            Assert.AreEqual(expected.Length, array1.Length);
            for (int i = 0; i < array1.Length; ++i) {
                Assert.AreEqual(expected[i], array1[i]);
            }
        }

        [Test]
        public void ToArrayCollection()
        {
            ICollection<double> coll1 = new List<double>(new double[] { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 });
            double[] array1 = Algorithms.ToArray(coll1);
            double[] expected = { 4.5, 1.2, 7.6, -0.04, -7.6, 1.78, 10.11, 187.4 };
            Assert.AreEqual(expected.Length, array1.Length);
            for (int i = 0; i < array1.Length; ++i) {
                Assert.AreEqual(expected[i], array1[i]);
            }
        }

        [Test]
        public void CountEqual()
        {
            IEnumerable<string> coll1 = EnumerableFromArray(new string[] { "foo", "bar", "eric", "Eric", "BAR", "ERIC", "eric" });
            IEnumerable<int> coll2 = EnumerableFromArray(new int[0]);

            Assert.AreEqual(1, Algorithms.CountEqual(coll1, "foo"));
            Assert.AreEqual(2, Algorithms.CountEqual(coll1, "eric"));
            Assert.AreEqual(0, Algorithms.CountEqual(coll1, "clapton"));
            Assert.AreEqual(1, Algorithms.CountEqual(coll1, "foo", StringComparer.CurrentCultureIgnoreCase));
            Assert.AreEqual(4, Algorithms.CountEqual(coll1, "eric", StringComparer.CurrentCultureIgnoreCase));
            Assert.AreEqual(0, Algorithms.CountEqual(coll1, "clapton", StringComparer.CurrentCultureIgnoreCase));
            Assert.AreEqual(0, Algorithms.CountEqual(coll2, 4));
        }

        [Test, ExpectedException(typeof(ArgumentOutOfRangeException))]
        public void NCopiesOfFailure()
        {
            IEnumerable<double> coll1 = Algorithms.NCopiesOf(-1, 3.4);
            foreach (double d in coll1)
                ;
        }

        [Test]
        public void NCopiesOf()
        {
            IEnumerable<double> coll1 = Algorithms.NCopiesOf(17, 3.4);

            int count = 0;
            foreach (double d in coll1) {
                ++count;
                Assert.AreEqual(3.4, d);
            }
            Assert.AreEqual(17, count);

            IEnumerable<int> coll2 = Algorithms.NCopiesOf(0, 4);
            foreach (int i in coll2) {
                Assert.Fail();
            }
        }

        [Test]
        public void Concatenate()
        {
            string[] coll1 = { "hello", "there", "sailor" };
            List<string> coll2 = new List<string>(new string[] { "eric", "clapton" });
            OrderedSet<string> coll3 = new OrderedSet<string>(new string[] { "ghi", "xyz", "abc" });

            Assert.IsTrue(Algorithms.EqualCollections<string>(Algorithms.Concatenate<string>(coll1, new string[0], coll2, coll3, coll1),
                new string[] { "hello", "there", "sailor", "eric", "clapton", "abc", "ghi", "xyz", "hello", "there", "sailor" }));
            Assert.AreEqual(0, Algorithms.ToArray(Algorithms.Concatenate<string>()).Length);
            Assert.IsTrue(Algorithms.EqualCollections<string>(Algorithms.Concatenate(coll3),
                new string[] { "abc", "ghi", "xyz" }));
        }

        [Test]
        public void Replace1()
        {
            IEnumerable<string> enum1 = EnumerableFromArray(new string[0]);
            IEnumerable<string> enum2 = EnumerableFromArray(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });

            IEnumerable<string> result1 = Algorithms.Replace(enum1, "foo", "bar");
            InterfaceTests.TestEnumerableElements(result1, new string[0]);

            result1 = Algorithms.Replace(enum2, "foo", "baz");
            InterfaceTests.TestEnumerableElements(result1, new string[] { "baz", "bar", "FOO", "hello", "hello", "sailor", "baz", "bar" });

            result1 = Algorithms.Replace(enum2, "foo", "bar");
            InterfaceTests.TestEnumerableElements(result1, new string[] { "bar", "bar", "FOO", "hello", "hello", "sailor", "bar", "bar" });

            result1 = Algorithms.Replace(enum2, "X", "bar");
            InterfaceTests.TestEnumerableElements(result1, new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });

            result1 = Algorithms.Replace(enum2, "bar", "X");
            InterfaceTests.TestEnumerableElements(result1, new string[] { "foo", "X", "FOO", "hello", "hello", "sailor", "foo", "X" });

            result1 = Algorithms.Replace(enum2, "foo", "X", StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestEnumerableElements(result1, new string[] { "X", "bar", "X", "hello", "hello", "sailor", "X", "bar" });
        }

        [Test]
        public void Replace2()
        {
            IEnumerable<string> enum1 = EnumerableFromArray(new string[0]);
            IEnumerable<string> enum2 = EnumerableFromArray(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" });

            IEnumerable<string> result1 = Algorithms.Replace(enum1, delegate(string x) { return x == "foo";}, "bar");
            InterfaceTests.TestEnumerableElements(result1, new string[0]);

            result1 = Algorithms.Replace(enum2, delegate(string x) { return x[0] == 'h'; }, "baz");
            InterfaceTests.TestEnumerableElements(result1, new string[] { "foo", "bar", "FOO", "baz", "baz", "sailor", "foo", "bar", "baz" });

            result1 = Algorithms.Replace(enum2, delegate(string x) { return x.Length == 3; }, "X");
            InterfaceTests.TestEnumerableElements(result1, new string[] { "X", "X", "X", "hello", "hello", "sailor", "X", "X", "hi" });

            result1 = Algorithms.Replace(enum2, delegate(string x) { return x.Length == 3; }, null);
            InterfaceTests.TestEnumerableElements(result1, new string[] { null, null, null, "hello", "hello", "sailor", null, null, "hi"});

            result1 = Algorithms.Replace(enum2, delegate(string x) { x = "zip"; return false; }, null);
            InterfaceTests.TestEnumerableElements(result1, new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" });
        }

        [Test]
        public void ReplaceInPlace1()
        {
            IList<string> list1 = new List<string>(new string[0]);
            IList<string> list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });

            Algorithms.ReplaceInPlace(list1, "foo", "bar");
            InterfaceTests.TestListGeneric(list1, new string[0]);

            Algorithms.ReplaceInPlace(list2, "foo", "baz");
            InterfaceTests.TestListGeneric(list2, new string[] { "baz", "bar", "FOO", "hello", "hello", "sailor", "baz", "bar" });

            list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });
            Algorithms.ReplaceInPlace(list2, "foo", "bar");
            InterfaceTests.TestListGeneric(list2, new string[] { "bar", "bar", "FOO", "hello", "hello", "sailor", "bar", "bar" });

            list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });
            Algorithms.ReplaceInPlace(list2, "X", "bar");
            InterfaceTests.TestListGeneric(list2, new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });

            list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });
            Algorithms.ReplaceInPlace(list2, "bar", "X");
            InterfaceTests.TestListGeneric(list2, new string[] { "foo", "X", "FOO", "hello", "hello", "sailor", "foo", "X" });

            list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" });
            Algorithms.ReplaceInPlace(list2, "foo", "X", StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestListGeneric(list2, new string[] { "X", "bar", "X", "hello", "hello", "sailor", "X", "bar" });

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

            array1 = new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar" };
            Algorithms.ReplaceInPlace(array1, "foo", "X", StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestEnumerableElements(array1, new string[] { "X", "bar", "X", "hello", "hello", "sailor", "X", "bar" });
        }

        [Test]
        public void ReplaceInPlace2()
        {
            IList<string> list1 = new List<string>(new string[0]);

            Algorithms.ReplaceInPlace(list1, delegate(string x) { return x == "foo"; }, "bar");
            InterfaceTests.TestListGeneric(list1, new string[0]);

            IList<string> list2 = new List<string>(new string[] { "foo", "bar", "FOO", "hello", "hello", "sailor", "foo", "bar", "hi" });
            Algorithms.ReplaceInPlace(list2, delegate(string x) { return x[0] == 'h'; }, "baz");
            InterfaceTests.TestListGeneric(list2, new string[] { "foo", "bar", "FOO", "baz", "baz", "sailor", "foo", "bar", "baz" });

            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; }, "X");

⌨️ 快捷键说明

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