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

📄 biglisttests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 5 页
字号:
//******************************
// Written by Peter Golde
// Copyright (c) 2004-2005, Wintellect
//
// Use and restribution of this code is subject to the license agreement 
// contained in the file "License.txt" accompanying this file.
//******************************

#region Using directives

using System;
using System.Collections.Generic;
using System.Collections;
using NUnit.Framework;

#endregion

namespace Wintellect.PowerCollections.Tests
{
    [TestFixture]
    public class BigListTests
    {
        // Append a bunch of items one after each other. Then
        // index and foreach to make sure that they are in order.
        [Test]
        public void AppendItem()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();
            int i;

            for (i = 1; i <= SIZE; ++i) {
                biglist1.Add(i);
#if DEBUG
                if (i % 50 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 123 == 5)
                    biglist1.Clone();
            }

#if DEBUG
            biglist1.Validate();
#endif //DEBUG

            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist1)
                Assert.AreEqual(i++, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist2)
                Assert.AreEqual(i++, x);
        }

        // Prepend a bunch of items one after each other. Then
        // index and foreach to make sure that they are in order.
        [Test]
        public void PrependItem()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();
            int i;

            for (i = 1; i <= SIZE; ++i) {
                biglist1.AddToFront(i);
#if DEBUG
                if (i % 50 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 123 == 5)
                    biglist1.Clone();
            }

#if DEBUG
            biglist1.Validate();
#endif //DEBUG

            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[SIZE - i]);
            }

            i = SIZE;
            foreach (int x in biglist1)
                Assert.AreEqual(i--, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[SIZE - i]);
            }

            i = SIZE;
            foreach (int x in biglist2)
                Assert.AreEqual(i--, x);
        }

        // Try Create an enumerable.
        [Test]
        public void CreateFromEnumerable()
        {
            const int SIZE = 8000;
            int[] items = new int[SIZE];
            BigList<int> biglist1;
            int i;

            for (i = 0; i < SIZE; ++i)
                items[i] = i + 1;
            biglist1 = new BigList<int>(items);

#if DEBUG
            biglist1.Validate();
#endif //DEBUG
            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist1)
                Assert.AreEqual(i++, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist2)
                Assert.AreEqual(i++, x);
        }


        [Test]
        public void CreateFromEnumerable2()
        {
            int[] array = new int[0];
            BigList<int> biglist1 = new BigList<int>(array);
            Assert.AreEqual(0, biglist1.Count);
        }

        [Test]
        public void AppendAll()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();

            int i = 1, j = 0;
            while (i <= SIZE) {
                int[] array = new int[j];
                for (int x = 0; x < j; ++x)
                    array[x] = i + x;
                biglist1.AddRange(array);
#if DEBUG
                if (i % 30 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 13 <= 2)
                    biglist1.Clone();
                i += j;
                j += 1;
                if (j == 20)
                    j = 0;
            }
            int size = i - 1;

            Assert.AreEqual(size, biglist1.Count);
#if DEBUG
            biglist1.Validate();
#endif //DEBUG

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist1)
                Assert.AreEqual(i++, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= SIZE; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist2)
                Assert.AreEqual(i++, x);
        }

        [Test]
        public void PrependAll()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();

            int i = 1, j = 0;
            while (i <= SIZE) {
                int[] array = new int[j];
                for (int x = 0; x < j; ++x)
                    array[j - x - 1] = i + x;
                biglist1.AddRangeToFront(array);
#if DEBUG
                if (i % 30 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 13 <= 2)
                    biglist1.Clone();
                i += j;
                j += 1;
                if (j == 20)
                    j = 0;
            }
            int size = i - 1;

            Assert.AreEqual(size, biglist1.Count);
#if DEBUG
            biglist1.Validate();
#endif //DEBUG

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[size - i]);
            }

            i = size;
            foreach (int x in biglist1)
                Assert.AreEqual(i--, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[size - i]);
            }

            i = size;
            foreach (int x in biglist2)
                Assert.AreEqual(i--, x);
        }

        [Test]
        public void AppendBigList()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();

            int i = 1, j = 0;
            while (i <= SIZE) {
                int[] array = new int[j];
                for (int x = 0; x < j; ++x)
                    array[x] = i + x;
                BigList<int> biglistOther = new BigList<int>(array);
                if (j % 3 == 0)
                    biglistOther.Clone();
                biglist1.AddRange(biglistOther);
#if DEBUG
                if (i % 30 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 13 <= 2)
                    biglist1.Clone();
                i += j;
                j += 1;
                if (j == 20)
                    j = 0;
            }
            int size = i - 1;

            Assert.AreEqual(size, biglist1.Count);
#if DEBUG
            biglist1.Validate();
#endif //DEBUG

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist1)
                Assert.AreEqual(i++, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist2)
                Assert.AreEqual(i++, x);
        }

        [Test]
        public void AppendBigList2()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();

            biglist1.Add(1);
            biglist1.Add(2);
            int i = 3, j = 11;
            while (i <= SIZE) {
                int[] array = new int[j];
                BigList<int> biglistOther = new BigList<int>();
                for (int x = 0; x < j; ++x)
                    biglistOther.AddToFront(i + (j - x - 1));
                if (j % 7 == 0)
                    biglistOther.Clone();
                biglist1.AddRange(biglistOther);
#if DEBUG
                if (i % 30 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 13 <= 2)
                    biglist1.Clone();
                i += j;
                j += 1;
                if (j == 20)
                    j = 0;
            }
            int size = i - 1;

            Assert.AreEqual(size, biglist1.Count);
#if DEBUG
            biglist1.Validate();
#endif //DEBUG

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist1)
                Assert.AreEqual(i++, x);

            BigList<int> biglist2 = biglist1.Clone();

            for (i = 1; i <= size; ++i) {
                Assert.AreEqual(i, biglist1[i - 1]);
            }

            i = 1;
            foreach (int x in biglist2)
                Assert.AreEqual(i++, x);
        }

        [Test]
        public void PrependBigList()
        {
            const int SIZE = 8000;
            BigList<int> biglist1 = new BigList<int>();

            int i = 1, j = 0;
            while (i <= SIZE) {
                int[] array = new int[j];
                for (int x = 0; x < j; ++x)
                    array[j - x - 1] = i + x;
                BigList<int> biglistOther = new BigList<int>(array);
                if (j % 3 == 0)
                    biglistOther.Clone();
                biglist1.AddRangeToFront(biglistOther);
#if DEBUG
                if (i % 30 == 0)
                    biglist1.Validate();
#endif //DEBUG
                if (i % 13 <= 2)
                    biglist1.Clone();
                i += j;
                j += 1;

⌨️ 快捷键说明

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