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

📄 settests.cs

📁 C#写的类似于STL的集合类,首先是C#编写,可以用于.net变程.
💻 CS
📖 第 1 页 / 共 3 页
字号:
        [Test, ExpectedException(typeof(InvalidOperationException))]
        public void FailFastEnumerator2()
        {
            Set<double> set1 = new Set<double>();

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

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

        [Test]
        public void Clone()
        {
            Set<int> set1 = new Set<int>(new int[] { 1, 7, 9, 11, 13, 15, -17, 19, -21 });
            Set<int> set2, set3;

            set2 = set1.Clone();
            set3 = (Set<int>)((ICloneable)set1).Clone();

            Assert.IsFalse(set2 == set1);
            Assert.IsFalse(set3 == set1);

            // Modify set1, make sure set2, set3 don't change.
            set1.Remove(9);
            set1.Remove(-17);
            set1.Add(8);

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

            set1 = new Set<int>();
            set2 = set1.Clone();
            Assert.IsFalse(set2 == set1);
            Assert.IsTrue(set1.Count == 0 && set2.Count == 0);
        }


        // 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>(Set<T> s1, Set<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(1, found);
            }
        }



        [Test]
        public void CloneContents()
        {
            Set<MyInt> set1 = new Set<MyInt>();

            set1.Add(new MyInt(143));
            set1.Add(new MyInt(2));
            set1.Add(new MyInt(9));
            set1.Add(null);
            set1.Add(new MyInt(14));
            set1.Add(new MyInt(111));
            Set<MyInt> set2 = set1.CloneContents();
            CompareClones(set1, set2);

            Set<int> set3 = new Set<int>(new int[] { 144, 5, 23, 1, 8 });
            Set<int> set4 = set3.CloneContents();
            CompareClones(set3, set4);

            Set<UtilTests.CloneableStruct> set5 = new Set<UtilTests.CloneableStruct>();
            set5.Add(new UtilTests.CloneableStruct(143));
            set5.Add(new UtilTests.CloneableStruct(5));
            set5.Add(new UtilTests.CloneableStruct(23));
            set5.Add(new UtilTests.CloneableStruct(1));
            set5.Add(new UtilTests.CloneableStruct(8));
            Set<UtilTests.CloneableStruct> set6 = set5.CloneContents();

            Assert.AreEqual(set5.Count, set6.Count);

            // Check that the sets are equal, but not identical (e.g., have been cloned via ICloneable).
            foreach (UtilTests.CloneableStruct item in set5) {
                int found = 0;
                foreach (UtilTests.CloneableStruct other in set6) {
                    if (object.Equals(item, other)) {
                        found += 1;
                        Assert.IsFalse(item.Identical(other));
                    }
                }
                Assert.AreEqual(1, found);
            }

        }

        class NotCloneable { }

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

            set1.Add(new NotCloneable());
            set1.Add(new NotCloneable());

            Set<NotCloneable> set2 = set1.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()
        {
            Set<int> set1 = new Set<int>(new ModularComparer(5));
            bool b;

            b = set1.Add(4); Assert.IsFalse(b);
            b = set1.Add(11); Assert.IsFalse(b);
            b = set1.Add(9); Assert.IsTrue(b);
            b = set1.Add(15); Assert.IsFalse(b);

            Assert.IsTrue(set1.Contains(25));
            Assert.IsTrue(set1.Contains(26));
            Assert.IsFalse(set1.Contains(27));

            InterfaceTests.TestReadWriteCollectionGeneric(set1, new int[] { 11, 9, 15 }, false);
        }

        [Test]
        public void ComparerProperty()
        {
            IEqualityComparer<int> comparer1 = new ModularComparer(5);
            Set<int> set1 = new Set<int>(comparer1);
            Assert.AreSame(comparer1, set1.Comparer);
            Set<decimal> set2 = new Set<decimal>();
            Assert.AreSame(EqualityComparer<decimal>.Default, set2.Comparer);
            Set<string> set3 = new Set<string>(StringComparer.InvariantCultureIgnoreCase);
            Assert.AreSame(StringComparer.InvariantCultureIgnoreCase, set3.Comparer);
        }

        // Simple class for testing that the generic IEquatable is used.
        class GenComparable : IEquatable<GenComparable>
        {
            public int value;
            public GenComparable(int value)
            {
                this.value = value;
            }

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

            public override bool Equals(object obj)
            {
                throw new NotSupportedException();
            }

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

            public override string ToString()
            {
                return value.ToString();
            }
        
            #region IEquatable<GenComparable> Members

            bool IEquatable<GenComparable>.Equals(GenComparable other)
            {
                return this.value == other.value;
            }

            #endregion

}

        // Make sure that IEquatable<T>.Equals is used for equality comparison.
        [Test]
        public void GenericIEquatable()
        {
            Set<GenComparable> set1 = new Set<GenComparable>();
            bool b;

            b = set1.Add(new GenComparable(4)); Assert.IsFalse(b);
            b = set1.Add(new GenComparable(11)); Assert.IsFalse(b);
            b = set1.Add(new GenComparable(4)); Assert.IsTrue(b);
            b = set1.Add(new GenComparable(15)); Assert.IsFalse(b);

            Assert.IsTrue(set1.Contains(new GenComparable(4)));
            Assert.IsTrue(set1.Contains(new GenComparable(15)));
            Assert.IsFalse(set1.Contains(new GenComparable(27)));
        }

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

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

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

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

            foreach (string s in s_array)
                set1.Add(s);
            string[] a2 = set1.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 Subset()
        {
            Set<int> set1 = new Set<int>(new int[] { 1, 3, 6, 7, 8, 9, 10 });
            Set<int> set2 = new Set<int>();
            Set<int> set3 = new Set<int>(new int[] { 3, 8, 9 });
            Set<int> set4 = new Set<int>(new int[] { 3, 8, 9 });
            Set<int> set5 = new Set<int>(new int[] { 1, 2, 6, 8, 9, 10 });

            Assert.IsTrue(set1.IsSupersetOf(set2));
            Assert.IsTrue(set2.IsSubsetOf(set1));
            Assert.IsTrue(set1.IsProperSupersetOf(set2));
            Assert.IsTrue(set2.IsProperSubsetOf(set1));

            Assert.IsTrue(set1.IsSupersetOf(set3));
            Assert.IsTrue(set3.IsSubsetOf(set1));
            Assert.IsTrue(set1.IsProperSupersetOf(set3));
            Assert.IsTrue(set3.IsProperSubsetOf(set1));

            Assert.IsFalse(set3.IsSupersetOf(set1));
            Assert.IsFalse(set1.IsSubsetOf(set3));
            Assert.IsFalse(set3.IsProperSupersetOf(set1));
            Assert.IsFalse(set1.IsProperSubsetOf(set3));

            Assert.IsFalse(set1.IsSupersetOf(set5));

⌨️ 快捷键说明

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