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

📄 bagtests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 3 页
字号:
//******************************
// 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 BagTests
    {
        [Test]
        public void RandomAddDelete()
        {
            const int SIZE = 5000; 
            int[] count = new int[SIZE];
            Random rand = new Random(3);
            Bag<int> bag1 = new Bag<int>();
            bool b;

            // Add and delete values at random.
            for (int i = 0; i < SIZE * 100; ++i) {
                int v = rand.Next(SIZE);

                Assert.AreEqual(count[v], bag1.NumberOfCopies(v));
                if (count[v] > 0)
                    Assert.IsTrue(bag1.Contains(v));

                if (count[v] == 0 || rand.Next(2) == 1) {
                    // Add to the bag.
                    bag1.Add(v);
                    count[v] += 1;
                }
                else {
                    // Remove from the bag.
                    b = bag1.Remove(v);
                    Assert.IsTrue(b);
                    count[v] -= 1;
                }
            }

            // Make sure the bag has all the correct values, not necessarily in order.
            int c = 0;
            foreach (int x in count)
                c += x;
            Assert.AreEqual(c, bag1.Count);

            foreach (int v in bag1) {
                --count[v];
            }

            foreach (int x in count)
                Assert.AreEqual(0, x);
        }

        [Test]
        public void ICollectionInterface()
        {
            string[] s_array = { "Foo", "hello", "Eric", null, "Clapton", "hello", "goodbye", "C#", null };
            Bag<string> bag1 = new Bag<string>();

            foreach (string s in s_array)
                bag1.Add(s);

            Array.Sort(s_array);
            InterfaceTests.TestCollection<string>((ICollection)bag1, s_array, false);
        }


        [Test]
        public void GenericICollectionInterface()
        {
            string[] s_array = { "Foo", "hello", "Eric", null, "Clapton", "hello", "goodbye", "C#", null };
            Bag<string> bag1 = new Bag<string>();

            foreach (string s in s_array)
                bag1.Add(s);

            Array.Sort(s_array);
            InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)bag1, s_array, false);
        }

        [Test]
        public void Add()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);

            bag1.Add("Hello");
            bag1.Add("foo");
            bag1.Add("");
            bag1.Add("HELLO");
            bag1.Add("foo");
            bag1.Add(null);
            bag1.Add("hello");
            bag1.Add("Eric");
            bag1.Add(null);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { null, null, "", "Eric", "foo", "foo", "Hello", "Hello", "Hello" }, false);
        }

        [Test]
        public void AddRepresentative()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);

            bag1.AddRepresentative("Hello");
            bag1.AddRepresentative("foo");
            bag1.AddRepresentative("");
            bag1.AddRepresentative("HELLO");
            bag1.AddRepresentative("Foo");
            bag1.AddRepresentative("foo");
            bag1.AddRepresentative(null);
            bag1.AddRepresentative("hello");
            bag1.AddRepresentative("Eric");
            bag1.AddRepresentative(null);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { null, null, "", "Eric", "foo", "foo", "foo", "hello", "hello", "hello" }, false);
        }

        [Test]
        public void CountAndClear()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);

            Assert.AreEqual(0, bag1.Count);
            bag1.Add("hello"); Assert.AreEqual(1, bag1.Count);
            bag1.Add("foo"); Assert.AreEqual(2, bag1.Count);
            bag1.Add(""); Assert.AreEqual(3, bag1.Count);
            bag1.Add("HELLO"); Assert.AreEqual(4, bag1.Count);
            bag1.Add("foo"); Assert.AreEqual(5, bag1.Count);
            bag1.Remove(""); Assert.AreEqual(4, bag1.Count);
            bag1.Add(null); Assert.AreEqual(5, bag1.Count);
            bag1.Add("Hello"); Assert.AreEqual(6, bag1.Count);
            bag1.Add("Eric"); Assert.AreEqual(7, bag1.Count);
            bag1.RemoveAllCopies("hElLo"); Assert.AreEqual(4, bag1.Count);
            bag1.Add(null); Assert.AreEqual(5, bag1.Count);
            bag1.Clear();
            Assert.AreEqual(0, bag1.Count);

            bool found = false;
            foreach (string s in bag1)
                found = true;

            Assert.IsFalse(found);
        }

        [Test]
        public void Remove()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);
            bool b;

            b = bag1.Remove("Eric"); Assert.IsFalse(b);
            bag1.Add("hello");
            bag1.Add("foo");
            bag1.Add(null);
            bag1.Add(null);
            bag1.Add("HELLO");
            bag1.Add("Hello");
            b = bag1.Remove("hello"); Assert.IsTrue(b);
            InterfaceTests.TestCollection(bag1, new string[] { null, null, "foo", "hello", "hello" }, false);
            b = bag1.Remove("Hello"); Assert.IsTrue(b);
            b = bag1.Remove(null); Assert.IsTrue(b);
            b = bag1.Remove(null); Assert.IsTrue(b);
            b = bag1.Remove(null); Assert.IsFalse(b);
            bag1.Add("Hello");
            bag1.Add("Eric");
            bag1.Add(null);
            b = bag1.Remove(null); Assert.IsTrue(b);
            bag1.Add("ERIC");
            b = bag1.Remove("eRic"); Assert.IsTrue(b);
            b = bag1.Remove("eRic"); Assert.IsTrue(b);
            bag1.Clear();
            b = bag1.Remove(""); Assert.IsFalse(b);
        }

        [Test]
        public void RemoveAllCopies()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);
            int i;

            i = bag1.RemoveAllCopies("Eric"); Assert.AreEqual(0, i);
            bag1.Add("hello");
            bag1.Add("foo");
            bag1.Add(null);
            bag1.Add(null);
            bag1.Add("hello");
            bag1.Add(null);
            i = bag1.RemoveAllCopies("HELLO"); Assert.AreEqual(2, i);
            i = bag1.RemoveAllCopies("Hello"); Assert.AreEqual(0, i);
            i = bag1.RemoveAllCopies(null); Assert.AreEqual(3, i);
            bag1.Add("Hello");
            bag1.Add("Eric");
            bag1.Add(null);
            i = bag1.RemoveAllCopies(null); Assert.AreEqual(1, i);
            bag1.Add("ERIC");
            i = bag1.RemoveAllCopies("eRic"); Assert.AreEqual(2, i);
        }

        [Test]
        public void NumberOfCopies()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);

            bag1.Add(null);
            bag1.Add("hello");
            bag1.Add("foo");
            Assert.AreEqual(1, bag1.NumberOfCopies("hello"));
            Assert.AreEqual(1, bag1.NumberOfCopies("FOO"));
            Assert.AreEqual(1, bag1.NumberOfCopies(null));
            bag1.Add(null);
            bag1.Add("HELLO");
            bag1.Add("Hello");
            Assert.AreEqual(3, bag1.NumberOfCopies("hello"));
            Assert.AreEqual(2, bag1.NumberOfCopies(null));
            bag1.Remove(null);
            bag1.Remove(null);
            Assert.AreEqual(0, bag1.NumberOfCopies(null));
        }

        [Test]
        public void GetRepresentative()
        {
            Bag<string> bag1 = new Bag<string>(
                new string[] { "foo", null, "FOO", "Eric", "eric", "bar", null, "foO", "ERIC", "eric", null },
                StringComparer.InvariantCultureIgnoreCase);

            int count;
            string rep;

            count = bag1.GetRepresentativeItem("Foo", out rep);
            Assert.AreEqual(3, count);
            Assert.AreEqual("foo", rep);

            count = bag1.GetRepresentativeItem(null, out rep);
            Assert.AreEqual(3, count);
            Assert.AreEqual(null, rep);

            count = bag1.GetRepresentativeItem("silly", out rep);
            Assert.AreEqual(0, count);
            Assert.AreEqual("silly", rep);

            count = bag1.GetRepresentativeItem("ERic", out rep);
            Assert.AreEqual(4, count);
            Assert.AreEqual("Eric", rep);

            count = bag1.GetRepresentativeItem("BAR", out rep);
            Assert.AreEqual(1, count);
            Assert.AreEqual("bar", rep);
        }

        [Test]
        public void ChangeNumberOfCopies()
        {
            Bag<string> bag1 = new Bag<string>(
                new string[] { "foo", null, "FOO", "Eric", "eric", "bar", null, "foO", "ERIC", "eric", null },
                StringComparer.InvariantCultureIgnoreCase);

            bag1.ChangeNumberOfCopies("Foo", 7);
            bag1.ChangeNumberOfCopies(null, 0);
            bag1.ChangeNumberOfCopies("eRIC", 0);
            bag1.ChangeNumberOfCopies("silly", 2);
            bag1.ChangeNumberOfCopies("BAR", 1);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { "foo", "foo", "foo", "foo", "foo", "foo", "foo", "bar", "silly", "silly" }, false);
        }

        [Test]
        public void ToArray()
        {
            string[] s_array = { null, "Foo", "Eric", null, "Clapton", "hello", "Clapton", "goodbye", "C#" };
            Bag<string> bag1 = new Bag<string>();

            string[] a1 = bag1.ToArray();
            Assert.IsNotNull(a1);
            Assert.AreEqual(0, a1.Length);

            foreach (string s in s_array)
                bag1.Add(s);
            string[] a2 = bag1.ToArray();

            Array.Sort(s_array);
            Array.Sort(a2);

            Assert.AreEqual(s_array.Length, a2.Length);
            for (int i = 0; i < s_array.Length; ++i)
                Assert.AreEqual(s_array[i], a2[i]);
        }

        [Test]
        public void AddMany()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);
            bag1.Add("foo");
            bag1.Add("Eric");
            bag1.Add("Clapton");
            string[] s_array = { "FOO", "x", "elmer", "fudd", "Clapton", null };
            bag1.AddMany(s_array);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { null, "Clapton", "Clapton", "elmer", "Eric", "foo", "foo", "fudd", "x" }, false);

            bag1.Clear();
            bag1.Add("foo");
            bag1.Add("Eric");
            bag1.AddMany(bag1);
            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { "Eric", "Eric", "foo", "foo" }, false);
        }

        [Test]
        public void RemoveMany()
        {
            Bag<string> bag1 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);

            bag1.Add("foo");
            bag1.Add("Eric");
            bag1.Add("Clapton");
            bag1.Add(null);
            bag1.Add("Foo");
            bag1.Add("fudd");
            bag1.Add("elmer");
            string[] s_array = { "FOO", "jasmine", "eric", null };
            int count = bag1.RemoveMany(s_array);
            Assert.AreEqual(3, count);

            InterfaceTests.TestReadWriteCollectionGeneric(bag1, new string[] { "Clapton", "elmer", "foo", "fudd" }, false);

            bag1.Clear();
            bag1.Add("foo");
            bag1.Add("Eric");

⌨️ 快捷键说明

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