Beispiel #1
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);
        }
Beispiel #2
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));
        }
Beispiel #3
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 !));
        }
Beispiel #4
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 !]);
Beispiel #5
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);
        }
Beispiel #6
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
        }
Beispiel #7
0
 public void TestImmutableSortedTreeDictionaryConstructor()
 {
     ImmutableSortedTreeDictionary <int, int> .Builder dictionary = ImmutableSortedTreeDictionary.CreateBuilder <int, int>();
     Assert.Empty(dictionary);
 }