コード例 #1
0
        public void TestSetComparer()
        {
            IEqualityComparer <TreeSet <int> > setComparer = TreeSet <int> .CreateSetComparer();

            Assert.True(setComparer.Equals(TreeSet <int> .CreateSetComparer()));
            Assert.False(setComparer.Equals(null));
            Assert.Equal(setComparer.GetHashCode(), TreeSet <int> .CreateSetComparer().GetHashCode());

            var set   = new TreeSet <int>();
            var other = new TreeSet <int>();

            // Test behavior with nulls
            Assert.True(setComparer.Equals(null, null));
            Assert.False(setComparer.Equals(set, null));
            Assert.False(setComparer.Equals(null, set));

            Assert.Equal(0, setComparer.GetHashCode(null));

            // Test behavior with empty sets
            Assert.True(setComparer.Equals(set, other));
            Assert.Equal(setComparer.GetHashCode(set), setComparer.GetHashCode(other));

            // Test behavior with non-empty sets
            set.UnionWith(Enumerable.Range(0, 10));
            Assert.False(setComparer.Equals(set, other));
            other.UnionWith(Enumerable.Range(0, 5));
            Assert.False(setComparer.Equals(set, other));
            other.UnionWith(Enumerable.Range(5, 5));
            Assert.True(setComparer.Equals(set, other));
            Assert.Equal(setComparer.GetHashCode(set), setComparer.GetHashCode(other));

            // Test behavior with non-empty sets with different comparers
            set.Clear();
            other = new TreeSet <int>(ZeroHashCodeEqualityComparer <int> .Default);
            set.UnionWith(Enumerable.Range(0, 10));
            Assert.False(setComparer.Equals(set, other));
            other.UnionWith(Enumerable.Range(0, 5));
            Assert.False(setComparer.Equals(set, other));
            other.UnionWith(Enumerable.Range(5, 5));
            Assert.True(setComparer.Equals(set, other));
            Assert.Equal(setComparer.GetHashCode(set), setComparer.GetHashCode(other));
        }