Esempio n. 1
0
        public void TestTryGetValue()
        {
            var set = new TreeSet <string>(StringComparer.OrdinalIgnoreCase);

            Assert.True(set.Add("a"));
            Assert.False(set.Add("A"));

            Assert.True(set.TryGetValue("a", out string value));
            Assert.Equal("a", value);

            Assert.True(set.TryGetValue("A", out value));
            Assert.Equal("a", value);

            Assert.False(set.TryGetValue("b", out value));
            Assert.Null(value);
        }
        public void TestTryGetValueWithCollidingHashCodes()
        {
            var set = new TreeSet <string>(new ZeroHashCodeEqualityComparer <string>(StringComparer.OrdinalIgnoreCase));

            Assert.True(set.Add("a"));
            Assert.False(set.Add("A"));

            Assert.True(set.TryGetValue("a", out string value));
            Assert.Equal("a", value);

            Assert.True(set.TryGetValue("A", out value));
            Assert.Equal("a", value);

            Assert.False(set.TryGetValue("b", out value));
            Assert.Null(value);

            // The test below forces coverage of an edge case. We don't know if the hash code for 'aa' or 'bb' comes
            // first, so write the test in a way that either will cover the early-exit branch in TryGetValue.
            set = new TreeSet <string>(new SubsetHashCodeEqualityComparer <string>(StringComparer.Ordinal, StringComparer.OrdinalIgnoreCase));
            Assert.True(set.Add("aa"));
            Assert.True(set.Add("Aa"));
            Assert.True(set.Add("bb"));
            Assert.True(set.Add("Bb"));

            Assert.True(set.TryGetValue("aa", out value));
            Assert.Equal("aa", value);

            Assert.True(set.TryGetValue("Aa", out value));
            Assert.Equal("Aa", value);

            Assert.False(set.TryGetValue("AA", out value));
            Assert.Null(value);

            Assert.True(set.TryGetValue("bb", out value));
            Assert.Equal("bb", value);

            Assert.True(set.TryGetValue("Bb", out value));
            Assert.Equal("Bb", value);

            Assert.False(set.TryGetValue("BB", out value));
            Assert.Null(value);
        }