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

📄 orderedbagtests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 4 页
字号:
//******************************
// 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 OrderedBagTests
    {
        class ComparableClass1 : IComparable<ComparableClass1>
        {
            public int Value = 0;
            int IComparable<ComparableClass1>.CompareTo(ComparableClass1 other)
            {
                if (Value > other.Value)
                    return 1;
                else if (Value < other.Value)
                    return -1;
                else
                    return 0;
            }
        }

        class ComparableClass2 : IComparable
        {
            public int Value = 0;
            int IComparable.CompareTo(object other)
            {
                if (other is ComparableClass2) {
                    ComparableClass2 o = (ComparableClass2)other;

                    if (Value > o.Value)
                        return 1;
                    else if (Value < o.Value)
                        return -1;
                    else
                        return 0;
                }
                else
                    throw new ArgumentException("Argument of wrong type.", "other");
            }
        }

        // Not comparable, because the type parameter on ComparableClass is incorrect.
        class UncomparableClass1 : IComparable<ComparableClass1>
        {
            public int Value = 0;
            int IComparable<ComparableClass1>.CompareTo(ComparableClass1 other)
            {
                if (Value > other.Value)
                    return 1;
                else if (Value < other.Value)
                    return -1;
                else
                    return 0;
            }
        }

        class UncomparableClass2
        {
            public int Value = 0;
        }

        [Test]
        public void RandomAddDelete()
        {
            const int SIZE = 5000;
            int[] count = new int[SIZE];
            Random rand = new Random();
            OrderedBag<int> bag1 = new OrderedBag<int>();
            bool b;

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

                // Check that number of copies is equal.
                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 in order.
            int c = 0;
            foreach (int x in count)
                c += x;
            Assert.AreEqual(c, bag1.Count);

            int[] vals = new int[c];
            int j = 0;
            for (int i = 0; i < count.Length; ++i) {
                for (int x = 0; x < count[i]; ++x)
                    vals[j++] = i;
            }

            int last = -1;
            int index = 0;
            foreach (int v in bag1) {
                Assert.IsTrue(v >= last);
                Assert.AreEqual(v, bag1[index]);
                if (v > last) {
                    Assert.AreEqual(index, bag1.IndexOf(v));
                    if (last > 0)
                        Assert.AreEqual(index - 1, bag1.LastIndexOf(last));
                }
                for (int i = last; i < v; ++i)
                    Assert.IsTrue(i < 0 || count[i] == 0);
                Assert.IsTrue(count[v] > 0);
                --count[v];
                last = v;
                ++index;
            }

            InterfaceTests.TestReadOnlyListGeneric<int>(bag1.AsList(), vals, null);

            int[] array = bag1.ToArray();
            Assert.IsTrue(Algorithms.EqualCollections(array, vals));
        }

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

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

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


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

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

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

        [Test]
        public void Add()
        {
            OrderedBag<string> bag1 = new OrderedBag<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" }, true, null);
        }

        [Test]
        public void GetItemByIndex()
        {
            OrderedBag<string> bag1 = new OrderedBag<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); 

            Assert.AreEqual(bag1[0], null);
            Assert.AreEqual(bag1[1], null);
            Assert.AreEqual(bag1[2], "");
            Assert.AreEqual(bag1[3], "Eric");
            Assert.AreEqual(bag1[4], "foo");
            Assert.AreEqual(bag1[5], "foo");
            Assert.AreEqual(bag1[6], "Hello");
            Assert.AreEqual(bag1[7], "HELLO");
            Assert.AreEqual(bag1[8], "hello");

            try {
                string s = bag1[-1];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                string s = bag1[9];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                string s = bag1[Int32.MaxValue];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }

            try {
                string s = bag1[Int32.MinValue];
                Assert.Fail("Should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is ArgumentOutOfRangeException);
            }
        }

        [Test]
        public void IndexOf()
        {
            OrderedBag<string> bag1 = new OrderedBag<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);

            Assert.AreEqual(0, bag1.IndexOf(null));
            Assert.AreEqual(1, bag1.LastIndexOf(null));
            Assert.AreEqual(2, bag1.IndexOf(""));
            Assert.AreEqual(2, bag1.LastIndexOf(""));
            Assert.AreEqual(3, bag1.IndexOf("eric"));
            Assert.AreEqual(3, bag1.LastIndexOf("Eric"));
            Assert.AreEqual(4, bag1.IndexOf("foo"));
            Assert.AreEqual(5, bag1.LastIndexOf("Foo"));
            Assert.AreEqual(6, bag1.IndexOf("heLlo"));
            Assert.AreEqual(8, bag1.LastIndexOf("hEllo"));
        }

        [Test]
        public void AsList()
        {
            OrderedBag<string> bag1 = new OrderedBag<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.TestReadOnlyListGeneric(bag1.AsList(), new string[] { null, null, "", "Eric", "foo", "foo", "Hello", "HELLO", "hello" }, null, StringComparer.InvariantCultureIgnoreCase.Equals);

            OrderedBag<string> bag2 = new OrderedBag<string>(StringComparer.InvariantCultureIgnoreCase);
            InterfaceTests.TestReadOnlyListGeneric(bag2.AsList(), new string[] { }, null);

        }

        [Test]
        public void CountAndClear()
        {
            OrderedBag<string> bag1 = new OrderedBag<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()
        {
            OrderedBag<string> bag1 = new OrderedBag<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.TestEnumerableElements(bag1, new string[] {null, null, "foo", "hello", "HELLO"});
            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);

⌨️ 快捷键说明

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