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

📄 biglisttests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 5 页
字号:
            InterfaceTests.TestListGeneric<int>(list1, new int[] { 1, 2, 3, 4, 1, 2, 3, 12, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }, null);
            list1 = new BigList<int>(new BigList<int>(new int[] { 1, 2, 3, 4 }), 7);
            list1[17] = 13;
            InterfaceTests.TestListGeneric<int>(list1, new int[] { 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 13, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4 }, null);

            // Large cases.
            int i;
            list1 = new BigList<int>(new int[] { 0, 1, 2 }, 1789345);
            Assert.AreEqual(list1.Count, 1789345 * 3);
            list1[1765] = 12;
            i = 0;
            foreach (int x in list1) {
                if (i != 1765)
                    Assert.AreEqual(i % 3, x);
                else
                    Assert.AreEqual(12, x);
                ++i;
            }

            list1 = new BigList<int>(new BigList<int>(new int[] { 0, 1, 2 }, 1354), 1789);
            Assert.AreEqual(list1.Count, 1354 * 1789 * 3);
            list1[17645] = 12;
            i = 0;
            foreach (int x in list1) {
                if (i != 17645)
                    Assert.AreEqual(i % 3, x);
                else
                    Assert.AreEqual(12, x);
                ++i;
            }

            list1 = new BigList<int>(new int[] { 6 }, int.MaxValue - 1);
            Assert.AreEqual(int.MaxValue - 1, list1.Count);

            try {
                list1 = new BigList<int>((BigList<int>)null, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentNullException);
            }

            try {
                list1 = new BigList<int>((IEnumerable<int>)null, 5);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentNullException);
            }

            try {
                list1 = new BigList<int>(new int[] { 1, 2, 3 }, -1);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                list1 = new BigList<int>(new BigList<int>(new int[] { 1, 2, 3 }), -1);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                list1 = new BigList<int>(new BigList<int>(new int[] { 1, 2, 3 }), 2000000000);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                list1 = new BigList<int>(new int[] { 1, 2, 3 }, 1000000000);
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }
        }

        [Test]
        public void AsReadOnly()
        {
            BigList<int> list1 = CreateList(0, 400);
            int[] elements = new int[400];
            IList<int> list2 = list1.AsReadOnly();

            for (int i = 0; i < 400; ++i)
                elements[i] = i;

            InterfaceTests.TestReadOnlyListGeneric<int>(list2, elements, null);

            list1.Add(27);
            list1.AddToFront(98);
            list1[17] = 9;

            elements = new int[402];
            list2 = list1.AsReadOnly();

            for (int i = 0; i < 401; ++i)
                elements[i] = i -1;

            elements[0] = 98;
            elements[401] = 27;
            elements[17] = 9;

            InterfaceTests.TestReadOnlyListGeneric<int>(list2, elements, null);

            list1 = new BigList<int>();
            list2 = list1.AsReadOnly();
            InterfaceTests.TestReadOnlyListGeneric<int>(list2, new int[0], null);
            list1.Add(4);
            InterfaceTests.TestReadOnlyListGeneric<int>(list2, new int[] { 4 }, null);
        }

        [Test]
        public void Exists()
        {
            BigList<int> list1 = CreateList(0, 400);
            list1[57] = 7123;

            bool result;

            result = list1.Exists(delegate(int x) { return x > 7120; });
            Assert.IsTrue(result);
            result = list1.Exists(delegate(int x) { return x > 7124; });
            Assert.IsFalse(result);
            result = list1.Exists(delegate(int x) { return x % 187 == 44; });
            Assert.IsTrue(result);
            result = list1.Exists(delegate(int x) { return x >= 0; });
            Assert.IsTrue(result);

            list1 = new BigList<int>();
            result = list1.Exists(delegate(int x) { return true; });
            Assert.IsFalse(result);
        }

        [Test]
        public void TrueForAll()
        {
            BigList<int> list1 = CreateList(0, 400);
            list1[57] = 7123;

            bool result;

            result = list1.TrueForAll(delegate(int x) { return x < 7124; });
            Assert.IsTrue(result);
            result = list1.TrueForAll(delegate(int x) { return x < 500; });
            Assert.IsFalse(result);
            result = list1.TrueForAll(delegate(int x) { return x % 187 == 44; });
            Assert.IsFalse(result);
            result = list1.TrueForAll(delegate(int x) { return x >= 0; });
            Assert.IsTrue(result);

            list1 = new BigList<int>();
            result = list1.TrueForAll(delegate(int x) { return false; });
            Assert.IsTrue(result);

            List<int> list2 = new List<int>();
            result = list2.TrueForAll(delegate(int x) { return false; });
            Assert.IsTrue(result);
        }

        [Test]
        public void ConvertAll()
        {
            BigList<int> list1 = CreateList(0, 400);
            BigList<string> list2;

            list2 = list1.ConvertAll<string>(delegate(int x) { return (x * 2).ToString(); });
            string[] expected = new string[400];
            for (int i = 0; i < 400; ++i)
                expected[i] = (2 * i).ToString();
#if DEBUG
            list2.Validate();
#endif //DEBUG
            InterfaceTests.TestReadWriteListGeneric<string>(list2, expected);

            list1 = new BigList<int>();
            list2 = list1.ConvertAll<string>(delegate(int x) { return (x * 2).ToString(); });
#if DEBUG
            list2.Validate();
#endif //DEBUG
            InterfaceTests.TestReadWriteListGeneric<string>(list2, new string[0]);
        }

        [Test]
        public void Find()
        {
            BigList<int> list1 = new BigList<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 BigList<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 BigList<int>();

            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 FindLast()
        {
            BigList<int> list1 = new BigList<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 BigList<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 BigList<int>();

            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 FindAll()
        {
            BigList<int> list1 = new BigList<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 BigList<int>();
            found = list1.FindAll(delegate(int x) { return (x & 1) == 1; });
            InterfaceTests.TestEnumerableElements(found, new int[] { });
        }

        [Test]
        public void FindIndex()
        {
            BigList<int> list1 = new BigList<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 BigList<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 BigList<int>();

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

        [Test]
        public void FindLastIndex()
        {
            BigList<int> list1 = new BigList<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 BigList<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 BigList<int>();

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

        [Test]
        public void IndexOf()
        {
            BigList<int> list = new BigList<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);

 

⌨️ 快捷键说明

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