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

📄 listbasetests.cs

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

            index = list.LastIndexOf(new MyDouble(8), 4, 3);
            Assert.IsTrue(index < 0);
        }


        [Test]
        public void FindReadOnly()
        {
            ReadOnlyArrayList<int> list1 = new ReadOnlyArrayList<int>(new int[] { 4, 8, 1, 3, 4, 9 });
            bool found;
            int result;

            Assert.AreEqual(1, list1.Find(delegate(int x) { return (x & 1) == 1; }));
            found = list1.TryFind(delegate(int x) { return (x & 1) == 1; }, out result);
            Assert.IsTrue(found);
            Assert.AreEqual(1, result);

            Assert.AreEqual(4, list1.Find(delegate(int x) { return (x & 1) == 0; }));
            found = list1.TryFind(delegate(int x) { return (x & 1) == 0; }, out result);
            Assert.IsTrue(found);
            Assert.AreEqual(4, result);

            Assert.AreEqual(0, list1.Find(delegate(int x) { return x > 10; }));
            found = list1.TryFind(delegate(int x) { return x > 10; }, out result);
            Assert.IsFalse(found);
            Assert.AreEqual(0, result);

            list1 = new ReadOnlyArrayList<int>(new int[] { 4, 0, 1, 3, 4, 9 });

            Assert.AreEqual(0, list1.Find(delegate(int x) { return x < 3; }));
            found = list1.TryFind(delegate(int x) { return x < 3; }, out result);
            Assert.IsTrue(found);
            Assert.AreEqual(0, result);

            Assert.AreEqual(0, list1.Find(delegate(int x) { return x > 10; }));
            found = list1.TryFind(delegate(int x) { return x > 10; }, out result);
            Assert.IsFalse(found);
            Assert.AreEqual(0, result);

            list1 = new ReadOnlyArrayList<int>(new int[0]);

            Assert.AreEqual(0, list1.Find(delegate(int x) { return x < 3; }));
            found = list1.TryFind(delegate(int x) { return x < 3; }, out result);
            Assert.IsFalse(found);
            Assert.AreEqual(0, result);
        }

        [Test]
        public void FindLastReadOnly()
        {
            ReadOnlyArrayList<int> list1 = new ReadOnlyArrayList<int>(new int[] { 4, 8, 1, 3, 2, 9 });
            bool found;
            int result;

            Assert.AreEqual(9, list1.FindLast(delegate(int x) { return (x & 1) == 1; }));
            found = list1.TryFindLast(delegate(int x) { return (x & 1) == 1; }, out result);
            Assert.IsTrue(found);
            Assert.AreEqual(9, result);

            Assert.AreEqual(2, list1.FindLast(delegate(int x) { return (x & 1) == 0; }));
            found = list1.TryFindLast(delegate(int x) { return (x & 1) == 0; }, out result);
            Assert.IsTrue(found);
            Assert.AreEqual(2, result);

            Assert.AreEqual(0, list1.FindLast(delegate(int x) { return x > 10; }));
            found = list1.TryFindLast(delegate(int x) { return x > 10; }, out result);
            Assert.IsFalse(found);
            Assert.AreEqual(0, result);

            list1 = new ReadOnlyArrayList<int>(new int[] { 4, 8, 1, 3, 0, 9 });

            Assert.AreEqual(0, list1.FindLast(delegate(int x) { return x < 3; }));
            found = list1.TryFindLast(delegate(int x) { return x < 3; }, out result);
            Assert.IsTrue(found);
            Assert.AreEqual(0, result);

            Assert.AreEqual(0, list1.FindLast(delegate(int x) { return x > 10; }));
            found = list1.TryFindLast(delegate(int x) { return x > 10; }, out result);
            Assert.IsFalse(found);
            Assert.AreEqual(0, result);

            list1 = new ReadOnlyArrayList<int>(new int[0]);

            Assert.AreEqual(0, list1.FindLast(delegate(int x) { return x < 3; }));
            found = list1.TryFindLast(delegate(int x) { return x < 3; }, out result);
            Assert.IsFalse(found);
            Assert.AreEqual(0, result);
        }

        [Test]
        public void FindAllReadOnly()
        {
            ReadOnlyArrayList<int> list1 = new ReadOnlyArrayList<int>(new int[] { 4, 8, 1, 3, 6, 9 });
            IEnumerable<int> found;

            found = list1.FindAll(delegate(int x) { return (x & 1) == 1; });
            InterfaceTests.TestEnumerableElements(found, new int[] { 1, 3, 9 });

            found = list1.FindAll(delegate(int x) { return (x & 1) == 0; });
            InterfaceTests.TestEnumerableElements(found, new int[] { 4, 8, 6 });

            found = list1.FindAll(delegate(int x) { return x > 10; });
            InterfaceTests.TestEnumerableElements(found, new int[] { });

            found = list1.FindAll(delegate(int x) { return x < 10; });
            InterfaceTests.TestEnumerableElements(found, new int[] { 4, 8, 1, 3, 6, 9 });

            list1 = new ReadOnlyArrayList<int>(new int[0]);
            found = list1.FindAll(delegate(int x) { return (x & 1) == 1; });
            InterfaceTests.TestEnumerableElements(found, new int[] { });
        }

        [Test]
        public void FindIndexReadOnly()
        {
            ReadOnlyArrayList<int> list1 = new ReadOnlyArrayList<int>(new int[] { 4, 2, 1, 3, 9, 4 });

            Assert.AreEqual(2, list1.FindIndex(delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(0, list1.FindIndex(delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(-1, list1.FindIndex(delegate(int x) { return x > 10; }));
            Assert.AreEqual(4, list1.FindIndex(delegate(int x) { return x > 5; }));
            Assert.AreEqual(3, list1.FindIndex(3, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(5, list1.FindIndex(3, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(-1, list1.FindIndex(5, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(2, list1.FindIndex(1, 4, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(3, list1.FindIndex(3, 2, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(-1, list1.FindIndex(3, 2, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(-1, list1.FindIndex(3, 0, delegate(int x) { return (x & 1) == 1; }));

            list1 = new ReadOnlyArrayList<int>(new int[] { 4, 0, 1, 3, 4, 9 });

            Assert.AreEqual(1, list1.FindIndex(delegate(int x) { return x < 3; }));
            Assert.AreEqual(-1, list1.FindIndex(delegate(int x) { return x > 10; }));

            list1 = new ReadOnlyArrayList<int>(new int[0]);

            Assert.AreEqual(-1, list1.FindIndex(delegate(int x) { return x < 3; }));
        }

        [Test]
        public void FindLastIndexReadOnly()
        {
            ReadOnlyArrayList<int> list1 = new ReadOnlyArrayList<int>(new int[] { 4, 2, 1, 3, 9, 4 });

            Assert.AreEqual(4, list1.FindLastIndex(delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(5, list1.FindLastIndex(delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(-1, list1.FindLastIndex(delegate(int x) { return x > 10; }));
            Assert.AreEqual(2, list1.FindLastIndex(delegate(int x) { return x < 3; }));
            Assert.AreEqual(3, list1.FindLastIndex(3, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(1, list1.FindLastIndex(3, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(-1, list1.FindLastIndex(1, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(1, list1.FindLastIndex(1, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(-1, list1.FindLastIndex(3, delegate(int x) { return x > 10; }));
            Assert.AreEqual(0, list1.FindLastIndex(3, delegate(int x) { return x > 3; }));

            list1 = new ReadOnlyArrayList<int>(new int[] { 4, 0, 8, 3, 4, 9 });

            Assert.AreEqual(3, list1.FindLastIndex(delegate(int x) { return x < 4; }));
            Assert.AreEqual(1, list1.FindLastIndex(delegate(int x) { return x < 1; }));
            Assert.AreEqual(-1, list1.FindLastIndex(delegate(int x) { return x > 10; }));
            Assert.AreEqual(3, list1.FindLastIndex(3, 1, delegate(int x) { return (x & 1) == 1; }));
            Assert.AreEqual(-1, list1.FindLastIndex(3, 1, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(4, list1.FindLastIndex(5, 3, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(2, list1.FindLastIndex(3, 3, delegate(int x) { return (x & 1) == 0; }));
            Assert.AreEqual(2, list1.FindLastIndex(4, 4, delegate(int x) { return x > 7; }));
            Assert.AreEqual(-1, list1.FindLastIndex(3, 4, delegate(int x) { return x > 8; }));

            list1 = new ReadOnlyArrayList<int>(new int[0]);

            Assert.AreEqual(-1, list1.FindLastIndex(delegate(int x) { return x < 3; }));
        }

        [Test]
        public void IndexOfReadOnly()
        {
            ReadOnlyArrayList<int> list = new ReadOnlyArrayList<int>(new int[] { 4, 8, 1, 1, 4, 9, 7, 11, 4, 9, 1, 7, 19, 1, 7 });
            int index;

            index = list.IndexOf(1);
            Assert.AreEqual(2, index);

            index = list.IndexOf(4);
            Assert.AreEqual(0, index);

            index = list.IndexOf(9);
            Assert.AreEqual(5, index);

            index = list.IndexOf(12);
            Assert.AreEqual(-1, index);

            index = list.IndexOf(1, 10);
            Assert.AreEqual(10, index);

            index = list.IndexOf(1, 11);
            Assert.AreEqual(13, index);

            index = list.IndexOf(1, 10);
            Assert.AreEqual(10, index);

            index = list.IndexOf(7, 12);
            Assert.AreEqual(14, index);

            index = list.IndexOf(9, 3, 3);
            Assert.AreEqual(5, index);

            index = list.IndexOf(9, 3, 2);
            Assert.AreEqual(-1, index);

            index = list.IndexOf(4, 5, 5);
            Assert.AreEqual(8, index);

            index = list.IndexOf(7, 11, 4);
            Assert.AreEqual(11, index);

            index = list.IndexOf(7, 12, 3);
            Assert.AreEqual(14, index);


            list = new ReadOnlyArrayList<int>(new int[0]);
            index = list.IndexOf(1);
            Assert.AreEqual(-1, index);
        }

        [Test]
        public void IndexOf2ReadOnly()
        {
            ReadOnlyArrayList<MyDouble> list = new ReadOnlyArrayList<MyDouble>(new MyDouble[] { new MyDouble(4), new MyDouble(8), new MyDouble(1), new MyDouble(1), new MyDouble(4), new MyDouble(9) });
            int index;

            index = list.IndexOf(new MyDouble(1));
            Assert.AreEqual(2, index);

            index = list.IndexOf(new MyDouble(1.1));
            Assert.IsTrue(index < 0);

            index = list.IndexOf(new MyDouble(4), 2);
            Assert.AreEqual(4, index);

            index = list.IndexOf(new MyDouble(8), 2);
            Assert.IsTrue(index < 0);

            index = list.IndexOf(new MyDouble(1), 1, 3);
            Assert.AreEqual(2, index);

            index = list.IndexOf(new MyDouble(4), 1, 3);
            Assert.IsTrue(index < 0);
        }

        [Test]
        public void LastIndexOfReadOnly()
        {
            //                                                                        0  1  2  3  4  5  6  7   8  9 10 11 12 13 14
            ReadOnlyArrayList<int> list = new ReadOnlyArrayList<int>(new int[] { 4, 8, 1, 1, 4, 9, 7, 11, 4, 9, 1, 7, 19, 1, 7 });
            int index;

            index = list.LastIndexOf(1);
            Assert.AreEqual(13, index);

            index = list.LastIndexOf(4);
            Assert.AreEqual(8, index);

            index = list.LastIndexOf(9);
            Assert.AreEqual(9, index);

            index = list.LastIndexOf(12);
            Assert.AreEqual(-1, index);

            index = list.LastIndexOf(1, 13);
            Assert.AreEqual(13, index);

            index = list.LastIndexOf(1, 12);
            Assert.AreEqual(10, index);

            index = list.LastIndexOf(1, 1);
            Assert.AreEqual(-1, index);

            index = list.LastIndexOf(7, 12);
            Assert.AreEqual(11, index);

            index = list.LastIndexOf(7, 6);
            Assert.AreEqual(6, index);

            index = list.LastIndexOf(9, 5, 3);
            Assert.AreEqual(5, index);

            index = list.LastIndexOf(9, 8, 5);
            Assert.AreEqual(5, index);

            index = list.LastIndexOf(9, 3, 2);
            Assert.AreEqual(-1, index);

            index = list.LastIndexOf(4, 5, 6);
            Assert.AreEqual(4, index);

            index = list.LastIndexOf(4, 3, 4);
            Assert.AreEqual(0, index);

            index = list.LastIndexOf(1, 14, 3);
            Assert.AreEqual(13, index);

            index = list.LastIndexOf(1, 0, 0);
            Assert.AreEqual(-1, index);

            index = list.LastIndexOf(1, 14, 0);
            Assert.AreEqual(-1, index);

            list = new ReadOnlyArrayList<int>(new int[0]);
            index = list.LastIndexOf(1);
            Assert.AreEqual(-1, index);
        }

        [Test]
        public void LastIndexOf2ReadOnly()
        {
            ReadOnlyArrayList<MyDouble> list = new ReadOnlyArrayList<MyDouble>(new MyDouble[] { new MyDouble(4), new MyDouble(8), new MyDouble(1), new MyDouble(1), new MyDouble(4), new MyDouble(9) });
            int index;

            index = list.LastIndexOf(new MyDouble(1));
            Assert.AreEqual(3, index);

            index = list.LastIndexOf(new MyDouble(1.1));
            Assert.IsTrue(index < 0);

            index = list.LastIndexOf(new MyDouble(4), 2);
            Assert.AreEqual(0, index);

            index = list.LastIndexOf(new MyDouble(9), 2);
            Assert.IsTrue(index < 0);

            index = list.LastIndexOf(new MyDouble(1), 4, 3);
            Assert.AreEqual(3, index);

            index = list.LastIndexOf(new MyDouble(8), 4, 3);
            Assert.IsTrue(index < 0);
        }

    }
}

⌨️ 快捷键说明

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