コード例 #1
0
        public void TestEmptyDictionary()
        {
            var dictionary = ImmutableSortedTreeDictionary.Create <int, int>();

            Assert.Same(ImmutableSortedTreeDictionary <int, int> .Empty, dictionary);

            ImmutableSortedTreeDictionary <int, int> .KeyCollection   keys   = dictionary.Keys;
            ImmutableSortedTreeDictionary <int, int> .ValueCollection values = dictionary.Values;

            Assert.Empty(dictionary);
            Assert.Empty(keys);
            Assert.Empty(values);

#pragma warning disable xUnit2013 // Do not use equality check to check for collection size.
            Assert.Equal(0, dictionary.Count);
            Assert.Equal(0, keys.Count);
            Assert.Equal(0, values.Count);
#pragma warning restore xUnit2013 // Do not use equality check to check for collection size.

            Assert.False(dictionary.ContainsKey(0));
            Assert.False(dictionary.ContainsValue(0));
            Assert.False(dictionary.TryGetValue(0, out _));
            Assert.Throws <KeyNotFoundException>(() => dictionary[0]);

#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection
            Assert.False(keys.Contains(0));
            Assert.False(values.Contains(0));
#pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection

            Assert.Same(dictionary, dictionary.Clear());
        }
コード例 #2
0
        public void TestCollectionConstructorUsesCorrectComparer()
        {
            var key1 = new StrongBox <int>(1);
            var key2 = new StrongBox <int>(2);

            KeyValuePair <StrongBox <int>, int>[] pairs =
            {
                new KeyValuePair <StrongBox <int>, int>(key1, 1),
                new KeyValuePair <StrongBox <int>, int>(key2, 2),
            };

            var comparer         = new ComparisonComparer <StrongBox <int> >((x, y) => Comparer <int> .Default.Compare(x.Value, y.Value));
            var objectDictionary = ImmutableSortedTreeDictionary.CreateRange(comparer, pairs);

            Assert.Same(comparer, objectDictionary.KeyComparer);
            Assert.Equal(2, objectDictionary.Count);
            Assert.Equal(new[] { new KeyValuePair <StrongBox <int>, int>(key1, 1), new KeyValuePair <StrongBox <int>, int>(key2, 2) }, objectDictionary);

            var stringDictionary = ImmutableSortedTreeDictionary.Create <string, int>();

            Assert.Same(Comparer <string> .Default, stringDictionary.KeyComparer);

            stringDictionary = ImmutableSortedTreeDictionary.Create <string, int>(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, stringDictionary.KeyComparer);

            KeyValuePair <StrongBox <int>, int>[] pairsWithDuplicateKey =
            {
                new KeyValuePair <StrongBox <int>, int>(key1, 1),
                new KeyValuePair <StrongBox <int>, int>(key2, 2),
                new KeyValuePair <StrongBox <int>, int>(key1, 3),
            };

            Assert.Throws <ArgumentException>(() => ImmutableSortedTreeDictionary.CreateRange(comparer, pairsWithDuplicateKey));
        }
コード例 #3
0
        public void TestIDictionary()
        {
            IDictionary dictionary = ImmutableSortedTreeDictionary.Create <int, int>();

            Assert.True(dictionary.IsFixedSize);
            Assert.True(dictionary.IsReadOnly);
            Assert.True(dictionary.IsSynchronized);

            dictionary = Enumerable.Range(0, 11).ToImmutableSortedTreeDictionary(x => x, x => x + 1);

            Assert.Throws <ArgumentNullException>("key", () => dictionary[key: null !]);
コード例 #4
0
        public void TestSingleElementDictionary()
        {
            var key   = Generator.GetInt32().ToString();
            var value = Generator.GetInt32();
            ImmutableSortedTreeDictionary <string, int> dictionary = ImmutableSortedTreeDictionary.Create <string, int>().Add(key, value);

            Assert.Equal(new[] { new KeyValuePair <string, int>(key, value) }, dictionary);

            dictionary = ImmutableSortedTreeDictionary.Create <string, int>(keyComparer: null).Add(key, value);
            Assert.Same(Comparer <string> .Default, dictionary.KeyComparer);
            Assert.Equal(new[] { new KeyValuePair <string, int>(key, value) }, dictionary);

            dictionary = ImmutableSortedTreeDictionary.Create <string, int>(StringComparer.OrdinalIgnoreCase).Add(key, value);
            Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer);
            Assert.Equal(new[] { new KeyValuePair <string, int>(key, value) }, dictionary);
        }
コード例 #5
0
        public void TestDefaultComparer()
        {
            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>().KeyComparer);
            Assert.Same(EqualityComparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>().ValueComparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeDictionary.Create <int, int>().KeyComparer);
            Assert.Same(EqualityComparer <int> .Default, ImmutableSortedTreeDictionary.Create <int, int>().ValueComparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeDictionary.Create <IComparable, IComparable>().KeyComparer);
            Assert.Same(EqualityComparer <IComparable> .Default, ImmutableSortedTreeDictionary.Create <IComparable, IComparable>().ValueComparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.CreateRange <object, object>(Enumerable.Empty <KeyValuePair <object, object> >()).KeyComparer);
            Assert.Same(EqualityComparer <object> .Default, ImmutableSortedTreeDictionary.CreateRange <object, object>(Enumerable.Empty <KeyValuePair <object, object> >()).ValueComparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeDictionary.CreateRange <int, int>(Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer);
            Assert.Same(EqualityComparer <int> .Default, ImmutableSortedTreeDictionary.CreateRange <int, int>(Enumerable.Empty <KeyValuePair <int, int> >()).ValueComparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeDictionary.CreateRange <IComparable, IComparable>(Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).KeyComparer);
            Assert.Same(EqualityComparer <IComparable> .Default, ImmutableSortedTreeDictionary.CreateRange <IComparable, IComparable>(Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).ValueComparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null).KeyComparer);
            Assert.Same(EqualityComparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null).ValueComparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeDictionary.Create <int, int>(keyComparer: null).KeyComparer);
            Assert.Same(EqualityComparer <int> .Default, ImmutableSortedTreeDictionary.Create <int, int>(keyComparer: null).ValueComparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeDictionary.Create <IComparable, IComparable>(keyComparer: null).KeyComparer);
            Assert.Same(EqualityComparer <IComparable> .Default, ImmutableSortedTreeDictionary.Create <IComparable, IComparable>(keyComparer: null).ValueComparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.CreateRange <object, object>(keyComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).KeyComparer);
            Assert.Same(EqualityComparer <object> .Default, ImmutableSortedTreeDictionary.CreateRange <object, object>(keyComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).ValueComparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeDictionary.CreateRange <int, int>(keyComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer);
            Assert.Same(EqualityComparer <int> .Default, ImmutableSortedTreeDictionary.CreateRange <int, int>(keyComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).ValueComparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).KeyComparer);
            Assert.Same(EqualityComparer <IComparable> .Default, ImmutableSortedTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).ValueComparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: null).KeyComparer);
            Assert.Same(EqualityComparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: null).ValueComparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeDictionary.Create <int, int>(keyComparer: null, valueComparer: null).KeyComparer);
            Assert.Same(EqualityComparer <int> .Default, ImmutableSortedTreeDictionary.Create <int, int>(keyComparer: null, valueComparer: null).ValueComparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeDictionary.Create <IComparable, IComparable>(keyComparer: null, valueComparer: null).KeyComparer);
            Assert.Same(EqualityComparer <IComparable> .Default, ImmutableSortedTreeDictionary.Create <IComparable, IComparable>(keyComparer: null, valueComparer: null).ValueComparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.CreateRange <object, object>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).KeyComparer);
            Assert.Same(EqualityComparer <object> .Default, ImmutableSortedTreeDictionary.CreateRange <object, object>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).ValueComparer);
            Assert.Same(Comparer <int> .Default, ImmutableSortedTreeDictionary.CreateRange <int, int>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer);
            Assert.Same(EqualityComparer <int> .Default, ImmutableSortedTreeDictionary.CreateRange <int, int>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).ValueComparer);
            Assert.Same(Comparer <IComparable> .Default, ImmutableSortedTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).KeyComparer);
            Assert.Same(EqualityComparer <IComparable> .Default, ImmutableSortedTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).ValueComparer);
        }
コード例 #6
0
        public void TestExplicitComparer()
        {
            var objComparer        = new ComparisonComparer <object>((x, y) => 0);
            var intComparer        = new ComparisonComparer <int>((x, y) => 0);
            var comparableComparer = new ComparisonComparer <IComparable>((x, y) => 0);

            ZeroHashCodeEqualityComparer <object?> objValueComparer = ZeroHashCodeEqualityComparer <object?> .Default;

            Assert.Same(objComparer, ImmutableSortedTreeDictionary.Create <object, int>(keyComparer: objComparer).KeyComparer);
            Assert.Same(intComparer, ImmutableSortedTreeDictionary.Create <int, int>(keyComparer: intComparer).KeyComparer);
            Assert.Same(comparableComparer, ImmutableSortedTreeDictionary.Create <IComparable, int>(keyComparer: comparableComparer).KeyComparer);

            Assert.Same(objComparer, ImmutableSortedTreeDictionary.CreateRange <object, int>(keyComparer: objComparer, Enumerable.Empty <KeyValuePair <object, int> >()).KeyComparer);
            Assert.Same(intComparer, ImmutableSortedTreeDictionary.CreateRange <int, int>(keyComparer: intComparer, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer);
            Assert.Same(comparableComparer, ImmutableSortedTreeDictionary.CreateRange <IComparable, int>(keyComparer: comparableComparer, Enumerable.Empty <KeyValuePair <IComparable, int> >()).KeyComparer);

            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: objValueComparer).KeyComparer);
            Assert.Same(objValueComparer, ImmutableSortedTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: objValueComparer).ValueComparer);
            Assert.Same(Comparer <object> .Default, ImmutableSortedTreeDictionary.Create <object, object?>().Add(new object(), null).WithComparers(keyComparer: null, valueComparer: objValueComparer).KeyComparer);
            Assert.Same(objValueComparer, ImmutableSortedTreeDictionary.Create <object, object?>().Add(new object(), null).WithComparers(keyComparer: null, valueComparer: objValueComparer).ValueComparer);
        }