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

📄 collectionbasetests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 2 页
字号:
//******************************
// 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.
//******************************

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

namespace Wintellect.PowerCollections.Tests
{
    // A simple read-write collection.
    class ReadWriteTestCollection<T> : CollectionBase<T>
    {
        private List<T> items;

        public ReadWriteTestCollection(T[] items)
        {
            this.items = new List<T>(items);
        }

        public override int Count
        {
            get { return items.Count; }
        }

        public override IEnumerator<T> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        public override void Add(T item)
        {
            items.Add(item);
        }

        public override bool Remove(T item)
        {
            return items.Remove(item);
        }

        public override void Clear()
        {
            items.Clear();
        }
    }

    [TestFixture]
    public class CollectionBaseTests
    {
        // A simple read-only collection.
        class ReadOnlyTestCollection<T> : ReadOnlyCollectionBase<T>
        {
            private T[] items;

            public ReadOnlyTestCollection(T[] items)
            {
                this.items = items;
            }

            public override int Count
            {
                get { return items.Length; }
            }

            public override IEnumerator<T> GetEnumerator()
            {
                for (int i = 0; i < items.Length; ++i)
                    yield return items[i];
            }
        }

        [Test]
        public void ReadOnlyCollection()
        {
            string[] s = { "Hello", "Goodbye", "Eric", "Clapton", "Rules" };

            ReadOnlyTestCollection<string> coll = new ReadOnlyTestCollection<string>(s);

            InterfaceTests.TestCollection<string>((ICollection)coll, s, true);
            InterfaceTests.TestReadonlyCollectionGeneric<string>((ICollection<string>)coll, s, true, "ReadOnlyTestCollection");
        }

        [Test]
        public void ReadWriteCollection()
        {
            string[] s = { "Hello", "Goodbye", "Eric", "Clapton", "Rules" };

            ReadWriteTestCollection<string> coll = new ReadWriteTestCollection<string>(s);

            InterfaceTests.TestCollection<string>((ICollection)coll, s, true);
            InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)coll, s, true);
        }

        [Test]
        public void ConvertToString()
        {
            string[] array = { "Hello", "Goodbye", null, "Clapton", "Rules" };
            string s;

            ReadWriteTestCollection<string> coll1 = new ReadWriteTestCollection<string>(array);
            s = coll1.ToString();
            Assert.AreEqual("{Hello,Goodbye,null,Clapton,Rules}", s);

            ReadOnlyTestCollection<string> coll2 = new ReadOnlyTestCollection<string>(array);
            s = coll2.ToString();
            Assert.AreEqual("{Hello,Goodbye,null,Clapton,Rules}", s);

            ReadWriteTestCollection<string> coll3 = new ReadWriteTestCollection<string>(new string[0]);
            s = coll3.ToString();
            Assert.AreEqual("{}", s);

            ReadOnlyTestCollection<string> coll4= new ReadOnlyTestCollection<string>(new string[0]);
            s = coll4.ToString();
            Assert.AreEqual("{}", s);

            ReadWriteTestCollection<int> coll5 = new ReadWriteTestCollection<int>(new int[] { 1, 2, 3 });
            s = coll5.ToString();
            Assert.AreEqual("{1,2,3}", s);

            ReadOnlyTestCollection<int> coll6 = new ReadOnlyTestCollection<int>(new int[] { 1, 2, 3 });
            s = coll6.ToString();
            Assert.AreEqual("{1,2,3}", s);


        }

        [Test]
        public void DebuggerDisplay()
        {
            string[] array = { "Hello", "Goodbye", null, "Clapton", "Rules" };
            string s;

            ReadWriteTestCollection<string> coll1 = new ReadWriteTestCollection<string>(array);
            s = coll1.DebuggerDisplayString();
            Assert.AreEqual("{Hello,Goodbye,null,Clapton,Rules}", s);

            ReadOnlyTestCollection<string> coll2 = new ReadOnlyTestCollection<string>(array);
            s = coll2.DebuggerDisplayString();
            Assert.AreEqual("{Hello,Goodbye,null,Clapton,Rules}", s);

            ReadWriteTestCollection<string> coll3 = new ReadWriteTestCollection<string>(new string[0]);
            s = coll3.DebuggerDisplayString();
            Assert.AreEqual("{}", s);

            ReadOnlyTestCollection<string> coll4 = new ReadOnlyTestCollection<string>(new string[0]);
            s = coll4.DebuggerDisplayString();
            Assert.AreEqual("{}", s);

            ReadWriteTestCollection<int> coll5 = new ReadWriteTestCollection<int>(new int[] { 1, 2, 3 });
            s = coll5.DebuggerDisplayString();
            Assert.AreEqual("{1,2,3}", s);

            ReadOnlyTestCollection<int> coll6 = new ReadOnlyTestCollection<int>(new int[] { 1, 2, 3 });
            s = coll6.DebuggerDisplayString();
            Assert.AreEqual("{1,2,3}", s);

            int[] bigarray = new int[1000];
            for (int i = 0; i < bigarray.Length; ++i)
                bigarray[i] = i;

            string expected = "{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,...}";

            ReadWriteTestCollection<int> coll7 = new ReadWriteTestCollection<int>(bigarray);
            s = coll7.DebuggerDisplayString();
            Assert.AreEqual(expected, s);

            ReadOnlyTestCollection<int> coll8 = new ReadOnlyTestCollection<int>(bigarray);
            s = coll8.DebuggerDisplayString();
            Assert.AreEqual(expected, s);
        }

        // Tests the built-in List<T> class. Makes sure that our tests are reasonable.
        [Test]
        public void CheckList()
        {
            string[] s = { "Hello", "Goodbye", "Eric", "Clapton", "Rules" };

            List<string> coll = new List<string>(s);

            InterfaceTests.TestCollection<string>((ICollection)coll, s, true);
            InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)coll, s, true);

            IList<string> ro = new List<string>(s).AsReadOnly();
            InterfaceTests.TestReadonlyCollectionGeneric<string>(ro, s, true, null);
        }

        // Tests the Keys and Values collections of Dictionary. Makes sure that our tests are reasonable.
        [Test]  
        public void CheckDictionaryKeyValues()
        {
            Dictionary<string, int> dict = new Dictionary<string, int>();
            dict["Eric"] = 3;
            dict["Clapton"] = 1;
            dict["Rules"] = 4;
            dict["The"] = 1;
            dict["Universe"] = 5;

⌨️ 快捷键说明

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