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

📄 bagtests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 3 页
字号:
            Bag<int> bag2, bag3;

            bag2 = bag1.Clone();
            bag3 = (Bag<int>)((ICloneable)bag1).Clone();

            Assert.IsFalse(bag2 == bag1);
            Assert.IsFalse(bag3 == bag1);

            // Modify bag1, make sure bag2, bag3 don't change.
            bag1.Remove(9);
            bag1.Remove(-17);
            bag1.Add(8);

            InterfaceTests.TestReadWriteCollectionGeneric(bag2, new int[] { -21, -17, 1, 1, 7, 7, 9, 11, 13, 15, 19 }, false);
            InterfaceTests.TestReadWriteCollectionGeneric(bag3, new int[] { -21, -17, 1, 1, 7, 7, 9, 11, 13, 15, 19 }, false);

            bag1 = new Bag<int>();
            bag2 = bag1.Clone();
            Assert.IsFalse(bag2 == bag1);
            Assert.IsTrue(bag1.Count == 0 && bag2.Count == 0);

        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void InconsistentComparisons1()
        {
            Bag<int> bagOdds = new Bag<int>(new int[] { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25 });
            Bag<int> bagDigits = new Bag<int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new GOddEvenEqualityComparer());
            bagOdds.SymmetricDifferenceWith(bagDigits);
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void InconsistentComparisons2()
        {
            Bag<string> bag1 = new Bag<string>(new string[] { "foo", "Bar" }, StringComparer.CurrentCulture);
            Bag<string> bag2 = new Bag<string>(new string[] { "bada", "bing" }, StringComparer.InvariantCulture);
            bag1.Intersection(bag2);
        }

        [Test]
        public void ConsistentComparisons()
        {
            Bag<string> bag1 = new Bag<string>(new string[] { "foo", "Bar" }, StringComparer.InvariantCulture);
            Bag<string> bag2 = new Bag<string>(new string[] { "bada", "bing" }, StringComparer.InvariantCulture);
            bag1.Difference(bag2);
        }


        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void FailFastEnumerator1()
        {
            Bag<double> bag1 = new Bag<double>();

            double d = 1.218034;
            for (int i = 0; i < 50; ++i) {
                bag1.Add(d);
                d = d * 1.3451 - .31;
            }

            // should throw once the bag is modified.
            foreach (double k in bag1) {
                if (k > 3.0)
                    bag1.Add(1.0);
            }
        }

        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void FailFastEnumerator2()
        {
            Bag<double> bag1 = new Bag<double>();

            double d = 1.218034;
            for (int i = 0; i < 50; ++i) {
                bag1.Add(d);
                d = d * 1.3451 - .31;
            }

            // should throw once the bag is modified.
            foreach (double k in bag1) {
                if (k > 3.0)
                    bag1.Clear();
            }
        }


        // 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 != null && obj is MyInt && ((MyInt)obj).value == value);
            }

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

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

        void CompareClones<T>(Bag<T> s1, Bag<T> s2)
        {
            Assert.AreEqual(s1.Count, s2.Count);

            // Check that the sets are equal, but not reference equals (e.g., have been cloned).
            foreach (T item in s1) {
                int found = 0;
                foreach (T other in s2) {
                    if (object.Equals(item, other)) {
                        found += 1;
                        if (item != null)
                            Assert.IsFalse(object.ReferenceEquals(item, other));
                    }
                }
                Assert.AreEqual(s1.NumberOfCopies(item), found);
            }
        }

        [Test]
        public void CloneContents()
        {
            Bag<MyInt> bag1 = new Bag<MyInt>();

            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);
            Bag<MyInt> bag2 = bag1.CloneContents();
            CompareClones(bag1, bag2);

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

            Bag<UtilTests.CloneableStruct> bag5 = new Bag<UtilTests.CloneableStruct>();
            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));
            Bag<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).
            foreach (UtilTests.CloneableStruct item in bag5) {
                int found = 0;
                foreach (UtilTests.CloneableStruct other in bag6) {
                    if (object.Equals(item, other)) {
                        found += 1;
                        Assert.IsFalse(item.Identical(other));
                    }
                }
                Assert.AreEqual(bag5.NumberOfCopies(item), found);
            }

        }

        class NotCloneable { }

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

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

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

        // Strange comparer that uses modulo arithmetic.
        class ModularComparer : IEqualityComparer<int>
        {
            private int mod;

            public ModularComparer(int mod)
            {
                this.mod = mod;
            }

            public bool Equals(int x, int y)
            {
                return (x % mod) == (y % mod);
            }

            public int GetHashCode(int obj)
            {
                return (obj % mod).GetHashCode();
            }
        }

        [Test]
        public void CustomIComparer()
        {
            IEqualityComparer<int> myComparer = new ModularComparer(5);

            Bag<int> bag1 = new Bag<int>(myComparer);
            bag1.Add(3);
            bag1.Add(8);
            bag1.Add(12);
            bag1.Add(9);
            bag1.Add(13);
            bag1.Add(17);
            InterfaceTests.TestReadWriteCollectionGeneric<int>(bag1, new int[] { 3, 3, 3, 9, 12, 12 }, false);
        }

        [Test]
        public void ComparerProperty()
        {
            IEqualityComparer<int> comparer1 = new ModularComparer(5);
            Bag<int> bag1 = new Bag<int>(comparer1);
            Assert.AreSame(comparer1, bag1.Comparer);
            Bag<decimal> bag2 = new Bag<decimal>();
            Assert.AreSame(EqualityComparer<decimal>.Default, bag2.Comparer);
            Bag<string> bag3 = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);
            Assert.AreSame(StringComparer.InvariantCultureIgnoreCase, bag3.Comparer);
        }

        [Test]
        public void Initialize()
        {
            List<int> list = new List<int>(new int[] { 12, 3, 9, 8, 9 });
            Bag<int> set1 = new Bag<int>(list);
            Bag<int> set2 = new Bag<int>(list, new ModularComparer(6));

            InterfaceTests.TestReadWriteCollectionGeneric<int>(set1, new int[] { 3, 8, 9, 9, 12 }, false);
            InterfaceTests.TestReadWriteCollectionGeneric<int>(set2, new int[] { 3, 3, 3, 8, 12 }, false);
        }

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

            InterfaceTests.TestEnumerableElementsAnyOrder(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 SerializeStrings()
        {
            Bag<string> d = new Bag<string>(StringComparer.InvariantCultureIgnoreCase);

            d.Add("foo");
            d.Add("world");
            d.Add("hello");
            d.Add("elvis");
            d.Add("ELVIS");
            d.Add(null);
            d.Add("Foo");
            d.AddMany(new string[] { "1", "2", "3", "4", "5", "6" });
            d.AddMany(new string[] { "7", "8", "9", "1", "2", "3" });

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


            InterfaceTests.TestReadWriteCollectionGeneric<string>((ICollection<string>)result, new string[] { "1", "2", "3", "4", "5", "6", "elvis", "elvis", "hello", "foo", "foo", "WORLD", null, "7", "8", "9", "1", "2", "3" }, false, StringComparer.InvariantCultureIgnoreCase.Equals);

        }

        [Serializable]
        class UniqueStuff
        {
            public InterfaceTests.Unique[] objects;
            public Bag<InterfaceTests.Unique> bag;
        }


        [Test]
        public void SerializeUnique()
        {
            UniqueStuff d = new UniqueStuff(), result = new UniqueStuff();
            InterfaceTests.Unique u1 = new InterfaceTests.Unique("cool"), u2 = new InterfaceTests.Unique("elvis");

            d.objects = new InterfaceTests.Unique[] { 
                new InterfaceTests.Unique("1"), new InterfaceTests.Unique("2"), new InterfaceTests.Unique("3"), new InterfaceTests.Unique("4"), new InterfaceTests.Unique("5"), new InterfaceTests.Unique("6"), 
                u1, u2, new InterfaceTests.Unique("hello"), new InterfaceTests.Unique("foo"), new InterfaceTests.Unique("world"), u2, new InterfaceTests.Unique(null), null,
                new InterfaceTests.Unique("7"), new InterfaceTests.Unique("8"), new InterfaceTests.Unique("9"), u1, u2, new InterfaceTests.Unique("3") };
            d.bag = new Bag<InterfaceTests.Unique>();

            d.bag.Add(d.objects[9]);
            d.bag.Add(d.objects[10]);
            d.bag.Add(d.objects[8]);
            d.bag.Add(d.objects[11]);
            d.bag.Add(d.objects[7]);
            d.bag.Add(d.objects[12]);
            d.bag.Add(d.objects[6]);
            d.bag.Add(d.objects[13]);
            d.bag.AddMany(new InterfaceTests.Unique[] { d.objects[0], d.objects[1], d.objects[2], d.objects[3], d.objects[4], d.objects[5] });
            d.bag.AddMany(new InterfaceTests.Unique[] { d.objects[14], d.objects[15], d.objects[16], d.objects[17], d.objects[18], d.objects[19] });

            result = (UniqueStuff)InterfaceTests.SerializeRoundTrip(d);

            InterfaceTests.TestReadWriteCollectionGeneric<InterfaceTests.Unique>(result.bag, result.objects, false);

            for (int i = 0; i < result.objects.Length; ++i) {
                if (result.objects[i] != null)
                    Assert.IsFalse(object.Equals(result.objects[i], d.objects[i]));
            }
        }


    }
}

⌨️ 快捷键说明

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