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

📄 orderedbagtests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 4 页
字号:
            bag1.Range(3, true, 8, false).Add(4);
            Assert.IsTrue(bag1.Contains(7));
            Assert.AreEqual(3, bag1.NumberOfCopies(4));
            Assert.IsTrue(bag1.Range(3, true, 11, false).Reversed().Remove(4));
            Assert.AreEqual(2, bag1.NumberOfCopies(4));
        }

        // Simple class for testing cloning.
        class MyInt : ICloneable
        {
            public int value;
            public MyInt(int value)
            {
                this.value = value;
            }

            public object Clone()
            {
                return new MyInt(value);
            }

            public override bool Equals(object obj)
            {
                return (obj is MyInt && ((MyInt)obj).value == value);
            }

            public override int GetHashCode()
            {
                return value.GetHashCode();
            }

            public override string ToString()
            {
                return value.ToString();
            }
        }

        void CompareClones<T>(OrderedBag<T> s1, OrderedBag<T> s2)
        {
            IEnumerator<T> e1 = s1.GetEnumerator();
            IEnumerator<T> e2 = s2.GetEnumerator();

            // Check that the bags are equal, but not reference equals (e.g., have been cloned).
            while (e1.MoveNext()) {
                e2.MoveNext();
                if (e1.Current == null) {
                    Assert.IsNull(e2.Current);
                }
                else {
                    Assert.IsTrue(e1.Current.Equals(e2.Current));
                    Assert.IsFalse(object.ReferenceEquals(e1.Current, e2.Current));
                }
            }
        }

        [Test]
        public void CloneContents()
        {
            OrderedBag<MyInt> bag1 = new OrderedBag<MyInt>(
                delegate(MyInt v1, MyInt v2) {
                if (v1 == null) {
                    return (v2 == null) ? 0 : -1;
                }
                else if (v2 == null)
                    return 1;
                else
                    return v2.value.CompareTo(v1.value);
            });

            MyInt mi = new MyInt(9);
            bag1.Add(new MyInt(14));
            bag1.Add(new MyInt(143));
            bag1.Add(new MyInt(2));
            bag1.Add(mi);
            bag1.Add(null);
            bag1.Add(new MyInt(14));
            bag1.Add(new MyInt(111));
            bag1.Add(mi);
            OrderedBag<MyInt> bag2 = bag1.CloneContents();
            CompareClones(bag1, bag2);

            OrderedBag<int> bag3 = new OrderedBag<int>(new int[] { 144, 1, 5, 23, 1, 8 });
            OrderedBag<int> bag4 = bag3.CloneContents();
            CompareClones(bag3, bag4);

            Comparison<UtilTests.CloneableStruct> comparison = delegate(UtilTests.CloneableStruct s1, UtilTests.CloneableStruct s2) {
                return s1.value.CompareTo(s2.value);
            };
            OrderedBag<UtilTests.CloneableStruct> bag5 = new OrderedBag<UtilTests.CloneableStruct>(comparison);
            bag5.Add(new UtilTests.CloneableStruct(143));
            bag5.Add(new UtilTests.CloneableStruct(1));
            bag5.Add(new UtilTests.CloneableStruct(23));
            bag5.Add(new UtilTests.CloneableStruct(1));
            bag5.Add(new UtilTests.CloneableStruct(8));
            OrderedBag<UtilTests.CloneableStruct> bag6 = bag5.CloneContents();

            Assert.AreEqual(bag5.Count, bag6.Count);

            // Check that the bags are equal, but not identical (e.g., have been cloned via ICloneable).
            IEnumerator<UtilTests.CloneableStruct> e1 = bag5.GetEnumerator();
            IEnumerator<UtilTests.CloneableStruct> e2 = bag6.GetEnumerator();

            // Check that the bags are equal, but not reference equals (e.g., have been cloned).
            while (e1.MoveNext()) {
                e2.MoveNext();
                Assert.IsTrue(e1.Current.Equals(e2.Current));
                Assert.IsFalse(e1.Current.Identical(e2.Current));
            }
        }

        class NotCloneable { }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void CantCloneContents()
        {
            OrderedBag<NotCloneable> bag1 = new OrderedBag<NotCloneable>();

            bag1.Add(new NotCloneable());
            bag1.Add(new NotCloneable());

            OrderedBag<NotCloneable> bag2 = bag1.CloneContents();
        }

        [Test]
        public void CustomComparison()
        {
            Comparison<int> myOrdering = ComparersTests.CompareOddEven;

            OrderedBag<int> bag1 = new OrderedBag<int>(myOrdering);
            bag1.Add(8);
            bag1.Add(12);
            bag1.Add(9);
            bag1.Add(9);
            bag1.Add(3);
            InterfaceTests.TestReadWriteCollectionGeneric<int>(bag1, new int[] { 3, 9, 9, 8, 12 }, true);
        }

        [Test]
        public void CustomIComparer()
        {
            IComparer<int> myComparer = new GOddEvenComparer();

            OrderedBag<int> bag1 = new OrderedBag<int>(myComparer);
            bag1.Add(3);
            bag1.Add(8);
            bag1.Add(12);
            bag1.Add(9);
            bag1.Add(3);
            InterfaceTests.TestReadWriteCollectionGeneric<int>(bag1, new int[] { 3, 3, 9, 8, 12 }, true);
        }

        [Test]
        public void ComparerProperty()
        {
            IComparer<int> comparer1 = new GOddEvenComparer();
            OrderedBag<int> bag1 = new OrderedBag<int>(comparer1);
            Assert.AreSame(comparer1, bag1.Comparer);
            OrderedBag<decimal> bag2 = new OrderedBag<decimal>();
            Assert.AreSame(Comparer<decimal>.Default, bag2.Comparer);
            OrderedBag<string> bag3 = new OrderedBag<string>(StringComparer.OrdinalIgnoreCase);
            Assert.AreSame(StringComparer.OrdinalIgnoreCase, bag3.Comparer);

            Comparison<int> comparison1 = ComparersTests.CompareOddEven;
            OrderedBag<int> bag4 = new OrderedBag<int>(comparison1);
            OrderedBag<int> bag5 = new OrderedBag<int>(comparison1);
            Assert.AreEqual(bag4.Comparer, bag5.Comparer);
            Assert.IsFalse(bag4.Comparer == bag5.Comparer);
            Assert.IsFalse(object.Equals(bag4.Comparer, bag1.Comparer));
            Assert.IsFalse(object.Equals(bag4.Comparer, Comparer<int>.Default));
            Assert.IsTrue(bag4.Comparer.Compare(7, 6) < 0);
        }

        [Test]
        public void Initialize()
        {
            Comparison<int> myOrdering = ComparersTests.CompareOddEven;
            IComparer<int> myComparer = new GOddEvenComparer();
            List<int> list = new List<int>(new int[] { 12, 3, 9, 8, 9, 3 });
            OrderedBag<int> bag1 = new OrderedBag<int>(list);
            OrderedBag<int> bag2 = new OrderedBag<int>(list, myOrdering);
            OrderedBag<int> bag3 = new OrderedBag<int>(list, myComparer);

            InterfaceTests.TestReadWriteCollectionGeneric<int>(bag1, new int[] { 3, 3, 8, 9, 9, 12 }, true);
            InterfaceTests.TestReadWriteCollectionGeneric<int>(bag2, new int[] { 3, 3, 9, 9, 8, 12 }, true);
            InterfaceTests.TestReadWriteCollectionGeneric<int>(bag3, new int[] { 3, 3, 9, 9, 8, 12 }, true);
        }

        [Test]
        public void DistinctItems()
        {
            OrderedBag<string> bag1 = new OrderedBag<string>(
                new string[] { "foo", null, "Foo", "Eric", "FOO", "eric", "bar" }, StringComparer.InvariantCultureIgnoreCase);

            InterfaceTests.TestEnumerableElements(bag1.DistinctItems(), new string[] { null, "bar", "Eric", "foo" });

            // Make sure enumeration stops on change.
            int count = 0;
            try {
                foreach (string s in bag1.DistinctItems()) {
                    if (count == 2)
                        bag1.Add("zippy");
                    ++count;
                }
                Assert.Fail("should throw");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
                Assert.AreEqual(3, count);
            }
        }

        [Test]
        public void Smallest()
        {
            OrderedBag<string> bag1 = new OrderedBag<string>(
                new string[] { "foo", null, "Foo", "Eric", "FOO", "eric", "bar" }, StringComparer.InvariantCultureIgnoreCase);

            string s;

            Assert.AreEqual(7, bag1.Count);

            s = bag1.GetFirst();
            Assert.IsNull(s);
            s = bag1.RemoveFirst();
            Assert.IsNull(s);
            Assert.AreEqual(6, bag1.Count);

            s = bag1.GetFirst();
            Assert.AreEqual("bar", s);
            s = bag1.RemoveFirst();
            Assert.AreEqual("bar", s);
            Assert.AreEqual(5, bag1.Count);

            s = bag1.GetFirst();
            Assert.AreEqual("Eric", s);
            s = bag1.RemoveFirst();
            Assert.AreEqual("Eric", s);
            Assert.AreEqual(4, bag1.Count);

            s = bag1.GetFirst();
            Assert.AreEqual("eric", s);
            s = bag1.RemoveFirst();
            Assert.AreEqual("eric", s);
            Assert.AreEqual(3, bag1.Count);
        }

        [Test]
        public void Largest()
        {
            OrderedBag<string> bag1 = new OrderedBag<string>(
                new string[] { "foo", null, "Foo", "Eric", "FOO", "eric", "bar" }, StringComparer.InvariantCultureIgnoreCase);

            string s;

            Assert.AreEqual(7, bag1.Count);

            s = bag1.GetLast();
            Assert.AreEqual("FOO", s);
            s = bag1.RemoveLast();
            Assert.AreEqual("FOO", s);
            Assert.AreEqual(6, bag1.Count);

            s = bag1.GetLast();
            Assert.AreEqual("Foo", s);
            s = bag1.RemoveLast();
            Assert.AreEqual("Foo", s);
            Assert.AreEqual(5, bag1.Count);

            s = bag1.GetLast();
            Assert.AreEqual("foo", s);
            s = bag1.RemoveLast();
            Assert.AreEqual("foo", s);
            Assert.AreEqual(4, bag1.Count);

            s = bag1.GetLast();
            Assert.AreEqual("eric", s);
            s = bag1.RemoveLast();
            Assert.AreEqual("eric", s);
            Assert.AreEqual(3, bag1.Count);
        }

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

            try {
                bag1.GetFirst();
                Assert.Fail("Should have thrown exception");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                bag1.GetLast();
                Assert.Fail("Should have thrown exception");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                bag1.RemoveFirst();
                Assert.Fail("Should have thrown exception");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }

            try {
                bag1.RemoveLast();
                Assert.Fail("Should have thrown exception");
            }
            catch (Exception e) {
                Assert.IsTrue(e is InvalidOperationException);
            }
        }

        [Test]
        public void SerializeStrings()
        {
            OrderedBag<string> d = new OrderedBag<string>(StringComparer.InvariantCultureIgnoreCase);

            d.Add(null);
            d.Add("hello");
            d.Add("foo");
            d.Add("WORLD");
            d.Add("Hello");
            d.Add("eLVIs");
            d.Add("elvis");
            d.Add(null);
            d.Add("cool");
            d.AddMany(new string[] { "1", "2", "3", "4", "5", "6" });
            d.AddMany(new string[] { "7", "8", "9", "10", "11", "12" });

            OrderedBag<string> result = (OrderedBag<string>)InterfaceTests.SerializeRoundTrip(d);

            InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)result, 
                new string[] { null, null, "1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9", "cool", "eLVIs", "elvis", "foo", "hello", "Hello", "WORLD" }, true);
        }


    }
}

⌨️ 快捷键说明

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