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));
        }
Exemplo n.º 2
0
        public void TestEmptyDictionary()
        {
            ImmutableSortedTreeDictionary <int, int> .Builder dictionary             = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();
            ImmutableSortedTreeDictionary <int, int> .Builder.KeyCollection   keys   = dictionary.Keys;
            ImmutableSortedTreeDictionary <int, int> .Builder.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.Equal(0, dictionary.GetValueOrDefault(0));
            Assert.Equal(1, dictionary.GetValueOrDefault(0, 1));
            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
        }
Exemplo n.º 3
0
        public void TestTryGetKey()
        {
            ImmutableSortedTreeDictionary <int, int> .Builder dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();
            Assert.False(dictionary.TryGetKey(0, out var key));
            Assert.Equal(0, key);

            dictionary.Add(1, 2);
            Assert.True(dictionary.TryGetKey(1, out key));
            Assert.Equal(1, key);
        }
        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 !]);
Exemplo n.º 5
0
        public void TestGetValueOrDefault()
        {
            ImmutableSortedTreeDictionary <int, int> .Builder dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();
            Assert.Equal(0, dictionary.GetValueOrDefault(1));
            Assert.Equal(0, dictionary.GetValueOrDefault(1, 0));
            Assert.Equal(1, dictionary.GetValueOrDefault(1, 1));

            dictionary.Add(1, 2);
            Assert.Equal(2, dictionary.GetValueOrDefault(1));
            Assert.Equal(2, dictionary.GetValueOrDefault(1, 0));
            Assert.Equal(2, dictionary.GetValueOrDefault(1, 1));
        }
        public void TestImmutableSortedTreeDictionaryCreateRange()
        {
            KeyValuePair <string, int>[] pairs =
            {
                new KeyValuePair <string, int>(Generator.GetInt32().ToString(), Generator.GetInt32()),
                new KeyValuePair <string, int>(Generator.GetInt32().ToString(), Generator.GetInt32()),
                new KeyValuePair <string, int>(Generator.GetInt32().ToString(), Generator.GetInt32()),
            };

            var dictionary = ImmutableSortedTreeDictionary.CreateRange(pairs);

            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);
        }
Exemplo n.º 7
0
        public void TestRemoveRange()
        {
            ImmutableSortedTreeDictionary <int, int> .Builder dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();
            for (int i = 0; i < 10; i++)
            {
                dictionary.Add(i, i);
            }

            int[] itemsToRemove = dictionary.Keys.Where(i => (i & 1) == 0).ToArray();
            dictionary.RemoveRange(itemsToRemove);
            Assert.Equal(new[] { 1, 3, 5, 7, 9 }.Select(x => new KeyValuePair <int, int>(x, x)), dictionary);

            Assert.Throws <ArgumentNullException>("keys", () => dictionary.RemoveRange(null !));
        }
        public void TestMultipleElementDictionary()
        {
            KeyValuePair <string, int>[] pairs =
            {
                new KeyValuePair <string, int>(Generator.GetInt32().ToString(), Generator.GetInt32()),
                new KeyValuePair <string, int>(Generator.GetInt32().ToString(), Generator.GetInt32()),
                new KeyValuePair <string, int>(Generator.GetInt32().ToString(), Generator.GetInt32()),
            };

            // Construction using ImmutableSortedTreeDictionary.Create
            var dictionary = ImmutableSortedTreeDictionary.CreateRange(pairs);

            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);

            dictionary = ImmutableSortedTreeDictionary.CreateRange <string, int>(keyComparer: null, pairs);
            Assert.Same(Comparer <string> .Default, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);

            dictionary = ImmutableSortedTreeDictionary.CreateRange(StringComparer.OrdinalIgnoreCase, pairs);
            Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, new KeyOfPairComparer <string, int>(StringComparer.OrdinalIgnoreCase)), dictionary);

            // Construction using ImmutableSortedTreeDictionary.ToImmutableSortedTreeDictionary
            dictionary = pairs.ToImmutableSortedTreeDictionary();
            Assert.Same(Comparer <string> .Default, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);

            dictionary = pairs.ToImmutableSortedTreeDictionary(keyComparer: null);
            Assert.Same(Comparer <string> .Default, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);

            dictionary = pairs.ToImmutableSortedTreeDictionary(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, new KeyOfPairComparer <string, int>(StringComparer.OrdinalIgnoreCase)), dictionary);

            // Construction using ImmutableSortedTreeDictionary.ToImmutableSortedTreeDictionary, where the source is already an
            // ImmutableSortedTreeDictionary<TKey, TValue>
            dictionary = pairs.ToImmutableSortedTreeDictionary().ToImmutableSortedTreeDictionary();
            Assert.Same(Comparer <string> .Default, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);

            dictionary = pairs.ToImmutableSortedTreeDictionary().ToImmutableSortedTreeDictionary(keyComparer: null);
            Assert.Same(Comparer <string> .Default, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, KeyOfPairComparer <string, int> .Default), dictionary);

            dictionary = pairs.ToImmutableSortedTreeDictionary().ToImmutableSortedTreeDictionary(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer);
            Assert.Equal(pairs.OrderBy(x => x, new KeyOfPairComparer <string, int>(StringComparer.OrdinalIgnoreCase)), dictionary);
        }
        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);
        }
        public void TestImmutableSortedTreeDictionaryCreateRangeValidation()
        {
            Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeDictionary.CreateRange <string, int>(null !));
            Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeDictionary.CreateRange <string, int>(Comparer <string> .Default, null !));
            Assert.Throws <ArgumentNullException>("items", () => ImmutableSortedTreeDictionary.CreateRange <string, int>(Comparer <string> .Default, EqualityComparer <int> .Default, null !));

            Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableSortedTreeDictionary());
            Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableSortedTreeDictionary(Comparer <string> .Default));
            Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableSortedTreeDictionary(Comparer <string> .Default, EqualityComparer <int> .Default));
            Assert.Throws <ArgumentNullException>("source", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableSortedTreeDictionary(x => x.Key, x => x.Value, Comparer <string> .Default));
            Assert.Throws <ArgumentNullException>("source", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableSortedTreeDictionary(x => x.Key, x => x.Value, Comparer <string> .Default, EqualityComparer <int> .Default));

            Assert.Throws <ArgumentNullException>("keySelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableSortedTreeDictionary(keySelector: null !, x => x.Value, Comparer <string> .Default));
            Assert.Throws <ArgumentNullException>("keySelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableSortedTreeDictionary(keySelector: null !, x => x.Value, Comparer <string> .Default, EqualityComparer <int> .Default));

            Assert.Throws <ArgumentNullException>("elementSelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableSortedTreeDictionary <KeyValuePair <string, int>, string, int>(x => x.Key, elementSelector: null !, Comparer <string> .Default));
            Assert.Throws <ArgumentNullException>("elementSelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableSortedTreeDictionary(x => x.Key, elementSelector: null !, Comparer <string> .Default, EqualityComparer <int> .Default));
        }
        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);
        }
        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);
        }
Exemplo n.º 13
0
        public void TestIDictionary()
        {
            IDictionary dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();

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

            Assert.Throws <ArgumentNullException>("key", () => dictionary.Add(key: null !, value: 1));
            Assert.Throws <ArgumentException>("value", () => dictionary.Add(key: 1, value: null));
            Assert.Throws <ArgumentException>("key", () => dictionary.Add(key: "string value", value: 0));
            Assert.Throws <ArgumentException>("value", () => dictionary.Add(key: 0, value: "string value"));

            for (int i = 0; i < 11; i++)
            {
                dictionary.Add(i, i + 1);
            }

            // Adding the same key/value pair does not throw or change the collection size
            Assert.Equal(11, dictionary.Count);
            dictionary.Add(10, 11);
            Assert.Equal(11, dictionary.Count);

            Assert.Throws <ArgumentNullException>("key", () => dictionary[key: null !]);
Exemplo n.º 14
0
        public void TestIDictionaryT()
        {
            IDictionary <int, int> dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();

            for (int i = 0; i < 10; i++)
            {
                dictionary.Add(i, i);
            }

            Assert.Equal(Enumerable.Range(0, 10).Select(x => new KeyValuePair <int, int>(x, x)), dictionary);

            Assert.Equal(10, dictionary.Count);
            Assert.False(dictionary.IsReadOnly);
            Assert.Equal(10, dictionary.Keys.Count);
            Assert.False(dictionary.Keys.IsReadOnly);
            Assert.Equal(10, dictionary.Values.Count);
            Assert.False(dictionary.Values.IsReadOnly);

            // Adding the same key/value pair does not throw or change the collection size
            Assert.Equal(10, dictionary.Count);
            Assert.Throws <ArgumentException>(() => dictionary.Add(9, 10));
            dictionary.Add(9, 9);
            Assert.Equal(10, dictionary.Count);

            Assert.Equal(Enumerable.Range(0, 10), dictionary.Keys);
            Assert.Equal(Enumerable.Range(0, 10), dictionary.Values);

            Assert.Throws <NotSupportedException>(() => dictionary.Values.Remove(9));
            Assert.False(dictionary.Keys.Remove(10));
            Assert.True(dictionary.Keys.Remove(9));

            Assert.Equal(9, dictionary.Count);
            Assert.Equal(9, dictionary.Keys.Count);
            Assert.False(dictionary.Keys.IsReadOnly);
            Assert.Equal(9, dictionary.Values.Count);
            Assert.False(dictionary.Values.IsReadOnly);

            Assert.Equal(Enumerable.Range(0, 9), dictionary.Keys);
            Assert.Equal(Enumerable.Range(0, 9), dictionary.Values);

            Assert.Throws <NotSupportedException>(() => dictionary.Keys.Add(0));
            Assert.Throws <ArgumentNullException>("array", () => dictionary.Keys.CopyTo(null !, 0));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Keys.CopyTo(new int[dictionary.Count], -1));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Keys.CopyTo(new int[dictionary.Count], dictionary.Count + 1));
            Assert.Throws <ArgumentException>(() => dictionary.Keys.CopyTo(new int[dictionary.Count], 1));

            IEnumerator <KeyValuePair <int, int> > enumerator = dictionary.GetEnumerator();

            Assert.NotNull(enumerator);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <int, int>(0, 0), enumerator.Current);

            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <int, int>(1, 1), enumerator.Current);

            enumerator.Reset();
            Assert.True(enumerator.MoveNext());
            Assert.Equal(new KeyValuePair <int, int>(0, 0), enumerator.Current);

            IEnumerator <int> keyEnumerator = dictionary.Keys.GetEnumerator();

            Assert.NotNull(keyEnumerator);
            Assert.True(keyEnumerator.MoveNext());
            Assert.Equal(0, keyEnumerator.Current);

            Assert.True(keyEnumerator.MoveNext());
            Assert.Equal(1, keyEnumerator.Current);

            keyEnumerator.Reset();
            Assert.True(keyEnumerator.MoveNext());
            Assert.Equal(0, keyEnumerator.Current);

            Assert.Throws <NotSupportedException>(() => dictionary.Values.Add(0));
            Assert.Throws <ArgumentNullException>("array", () => dictionary.Values.CopyTo(null !, 0));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Values.CopyTo(new int[dictionary.Count], -1));
            Assert.Throws <ArgumentOutOfRangeException>("arrayIndex", () => dictionary.Values.CopyTo(new int[dictionary.Count], dictionary.Count + 1));
            Assert.Throws <ArgumentException>(() => dictionary.Values.CopyTo(new int[dictionary.Count], 1));

            IEnumerator <int> valueEnumerator = dictionary.Values.GetEnumerator();

            Assert.NotNull(valueEnumerator);
            Assert.True(valueEnumerator.MoveNext());
            Assert.Equal(0, valueEnumerator.Current);

            Assert.True(valueEnumerator.MoveNext());
            Assert.Equal(1, valueEnumerator.Current);

            valueEnumerator.Reset();
            Assert.True(valueEnumerator.MoveNext());
            Assert.Equal(0, valueEnumerator.Current);

            IReadOnlyDictionary <int, int> readOnlyDictionary = (IReadOnlyDictionary <int, int>)dictionary;

            Assert.Equal(dictionary.Keys, readOnlyDictionary.Keys);
            Assert.Equal(dictionary.Values, readOnlyDictionary.Values);

            dictionary.Add(new KeyValuePair <int, int>(11, 11));
            Assert.Equal(11, dictionary[11]);
            Assert.True(dictionary.Contains(new KeyValuePair <int, int>(11, 11)));
            Assert.False(dictionary.Contains(new KeyValuePair <int, int>(11, 12)));
            Assert.False(dictionary.Contains(new KeyValuePair <int, int>(12, 12)));
            Assert.False(dictionary.Remove(new KeyValuePair <int, int>(11, 12)));
            Assert.True(dictionary.Contains(new KeyValuePair <int, int>(11, 11)));
            Assert.True(dictionary.Remove(new KeyValuePair <int, int>(11, 11)));
            Assert.False(dictionary.Contains(new KeyValuePair <int, int>(11, 11)));

            Assert.NotEmpty(dictionary);
            dictionary.Keys.Clear();
            Assert.Empty(dictionary);
            Assert.Empty(dictionary.Keys);
            Assert.Empty(dictionary.Values);

            dictionary[0] = 1;
            Assert.NotEmpty(dictionary);
            dictionary.Values.Clear();
            Assert.Empty(dictionary);
            Assert.Empty(dictionary.Keys);
            Assert.Empty(dictionary.Values);
        }
Exemplo n.º 15
0
 public void TestImmutableSortedTreeDictionaryConstructor()
 {
     ImmutableSortedTreeDictionary <int, int> .Builder dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();
     Assert.Empty(dictionary);
 }