public void TestEmptyDictionary() { var dictionary = ImmutableTreeDictionary.Create <int, int>(); Assert.Same(ImmutableTreeDictionary <int, int> .Empty, dictionary); ImmutableTreeDictionary <int, int> .KeyCollection keys = dictionary.Keys; ImmutableTreeDictionary <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()); }
public void TestCollectionConstructorUsesCorrectComparer() { object key1 = new object(); object key2 = new object(); KeyValuePair <object, int>[] pairs = { new KeyValuePair <object, int>(key1, 1), new KeyValuePair <object, int>(key2, 2), }; var objectDictionary = ImmutableTreeDictionary.CreateRange(ZeroHashCodeEqualityComparer <object> .Default, pairs); Assert.Same(ZeroHashCodeEqualityComparer <object> .Default, objectDictionary.KeyComparer); Assert.Equal(2, objectDictionary.Count); Assert.Equal(new[] { new KeyValuePair <object, int>(key1, 1), new KeyValuePair <object, int>(key2, 2) }, objectDictionary); var stringDictionary = ImmutableTreeDictionary.Create <string, int>(); Assert.Same(EqualityComparer <string> .Default, stringDictionary.KeyComparer); stringDictionary = ImmutableTreeDictionary.Create <string, int>(StringComparer.OrdinalIgnoreCase); Assert.Same(StringComparer.OrdinalIgnoreCase, stringDictionary.KeyComparer); KeyValuePair <object, int>[] pairsWithDuplicateKey = { new KeyValuePair <object, int>(key1, 1), new KeyValuePair <object, int>(key2, 2), new KeyValuePair <object, int>(key1, 3), }; Assert.Throws <ArgumentException>(() => ImmutableTreeDictionary.CreateRange(ZeroHashCodeEqualityComparer <object> .Default, pairsWithDuplicateKey)); }
public void Init() { var r = new Random(4); _keys = new List <T>(); _immDict = ImmutableDictionary <T, int> .Empty; _sasaTrie = Trie <T, int> .Empty; _sGuh = ImmutableTrieDictionary.Create <T, int>(); _tvl = ImmutableTreeDictionary <T, int> .Empty; _imToolsHashMap = ImHashMap <T, int> .Empty; _apexHashMap = HashMap <T, int> .Empty; for (int i = 0; i < Count; ++i) { var k = GenerateValue(r); _immDict = _immDict.SetItem(k, i); _sasaTrie = _sasaTrie.Add(k, i); _sGuh = _sGuh.Add(k, i); _tvl = _tvl.Add(k, i); _imToolsHashMap = _imToolsHashMap.AddOrUpdate(k, i); _apexHashMap = _apexHashMap.SetItem(k, i); _keys.Add(k); } int n = _keys.Count; while (n > 1) { n--; int k = r.Next(n + 1); var value = _keys[k]; _keys[k] = _keys[n]; _keys[n] = value; } }
public void TestTryGetKey() { ImmutableTreeDictionary <int, int> .Builder dictionary = ImmutableTreeDictionary.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 = ImmutableTreeDictionary.Create <int, int>(); Assert.True(dictionary.IsFixedSize); Assert.True(dictionary.IsReadOnly); Assert.True(dictionary.IsSynchronized); dictionary = Enumerable.Range(0, 11).ToImmutableTreeDictionary(x => x, x => x + 1); Assert.Throws <ArgumentNullException>("key", () => dictionary[key: null !]);
public void TestGetValueOrDefault() { ImmutableTreeDictionary <int, int> .Builder dictionary = ImmutableTreeDictionary.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 TestImmutableTreeDictionaryCreateRange() { 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 = ImmutableTreeDictionary.CreateRange(pairs); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); }
public void TestRemoveRange() { ImmutableTreeDictionary <int, int> .Builder dictionary = ImmutableTreeDictionary.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 ImmutableTreeDictionary.Create var dictionary = ImmutableTreeDictionary.CreateRange(pairs); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); dictionary = ImmutableTreeDictionary.CreateRange <string, int>(keyComparer: null, pairs); Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); dictionary = ImmutableTreeDictionary.CreateRange(StringComparer.OrdinalIgnoreCase, pairs); Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(new KeyOfPairEqualityComparer <string, int>(StringComparer.OrdinalIgnoreCase).GetHashCode), dictionary); // Construction using ImmutableTreeDictionary.ToImmutableTreeDictionary dictionary = pairs.ToImmutableTreeDictionary(); Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); dictionary = pairs.ToImmutableTreeDictionary(keyComparer: null); Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); dictionary = pairs.ToImmutableTreeDictionary(StringComparer.OrdinalIgnoreCase); Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(new KeyOfPairEqualityComparer <string, int>(StringComparer.OrdinalIgnoreCase).GetHashCode), dictionary); // Construction using ImmutableTreeDictionary.ToImmutableTreeDictionary, where the source is already an // ImmutableTreeDictionary<TKey, TValue> dictionary = pairs.ToImmutableTreeDictionary().ToImmutableTreeDictionary(); Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); dictionary = pairs.ToImmutableTreeDictionary().ToImmutableTreeDictionary(keyComparer: null); Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(KeyOfPairEqualityComparer <string, int> .Default.GetHashCode), dictionary); dictionary = pairs.ToImmutableTreeDictionary().ToImmutableTreeDictionary(StringComparer.OrdinalIgnoreCase); Assert.Same(StringComparer.OrdinalIgnoreCase, dictionary.KeyComparer); Assert.Equal(pairs.OrderBy(new KeyOfPairEqualityComparer <string, int>(StringComparer.OrdinalIgnoreCase).GetHashCode), dictionary); }
public void TestSingleElementDictionary() { var key = Generator.GetInt32().ToString(); var value = Generator.GetInt32(); ImmutableTreeDictionary <string, int> dictionary = ImmutableTreeDictionary.Create <string, int>().Add(key, value); Assert.Equal(new[] { new KeyValuePair <string, int>(key, value) }, dictionary); dictionary = ImmutableTreeDictionary.Create <string, int>(keyComparer: null).Add(key, value); Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer); Assert.Equal(new[] { new KeyValuePair <string, int>(key, value) }, dictionary); dictionary = ImmutableTreeDictionary.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 TestDefaultComparer() { Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>().KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>().ValueComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.Create <int, int>().KeyComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.Create <int, int>().ValueComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.Create <IComparable, IComparable>().KeyComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.Create <IComparable, IComparable>().ValueComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.CreateRange <object, object>(Enumerable.Empty <KeyValuePair <object, object> >()).KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.CreateRange <object, object>(Enumerable.Empty <KeyValuePair <object, object> >()).ValueComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.CreateRange <int, int>(Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.CreateRange <int, int>(Enumerable.Empty <KeyValuePair <int, int> >()).ValueComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.CreateRange <IComparable, IComparable>(Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).KeyComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.CreateRange <IComparable, IComparable>(Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).ValueComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>(keyComparer: null).KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>(keyComparer: null).ValueComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.Create <int, int>(keyComparer: null).KeyComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.Create <int, int>(keyComparer: null).ValueComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.Create <IComparable, IComparable>(keyComparer: null).KeyComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.Create <IComparable, IComparable>(keyComparer: null).ValueComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.CreateRange <object, object>(keyComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.CreateRange <object, object>(keyComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).ValueComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.CreateRange <int, int>(keyComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.CreateRange <int, int>(keyComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).ValueComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).KeyComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).ValueComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: null).KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: null).ValueComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.Create <int, int>(keyComparer: null, valueComparer: null).KeyComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.Create <int, int>(keyComparer: null, valueComparer: null).ValueComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.Create <IComparable, IComparable>(keyComparer: null, valueComparer: null).KeyComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.Create <IComparable, IComparable>(keyComparer: null, valueComparer: null).ValueComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.CreateRange <object, object>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.CreateRange <object, object>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <object, object> >()).ValueComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.CreateRange <int, int>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer); Assert.Same(EqualityComparer <int> .Default, ImmutableTreeDictionary.CreateRange <int, int>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <int, int> >()).ValueComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).KeyComparer); Assert.Same(EqualityComparer <IComparable> .Default, ImmutableTreeDictionary.CreateRange <IComparable, IComparable>(keyComparer: null, valueComparer: null, Enumerable.Empty <KeyValuePair <IComparable, IComparable> >()).ValueComparer); }
public void TestExplicitComparer() { ZeroHashCodeEqualityComparer <object> objComparer = ZeroHashCodeEqualityComparer <object> .Default; ZeroHashCodeEqualityComparer <int> intComparer = ZeroHashCodeEqualityComparer <int> .Default; ZeroHashCodeEqualityComparer <IComparable> comparableComparer = ZeroHashCodeEqualityComparer <IComparable> .Default; Assert.Same(objComparer, ImmutableTreeDictionary.Create <object, int>(keyComparer: objComparer).KeyComparer); Assert.Same(intComparer, ImmutableTreeDictionary.Create <int, int>(keyComparer: intComparer).KeyComparer); Assert.Same(comparableComparer, ImmutableTreeDictionary.Create <IComparable, int>(keyComparer: comparableComparer).KeyComparer); Assert.Same(objComparer, ImmutableTreeDictionary.CreateRange <object, int>(keyComparer: objComparer, Enumerable.Empty <KeyValuePair <object, int> >()).KeyComparer); Assert.Same(intComparer, ImmutableTreeDictionary.CreateRange <int, int>(keyComparer: intComparer, Enumerable.Empty <KeyValuePair <int, int> >()).KeyComparer); Assert.Same(comparableComparer, ImmutableTreeDictionary.CreateRange <IComparable, int>(keyComparer: comparableComparer, Enumerable.Empty <KeyValuePair <IComparable, int> >()).KeyComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: objComparer).KeyComparer); Assert.Same(objComparer, ImmutableTreeDictionary.Create <object, object>(keyComparer: null, valueComparer: objComparer).ValueComparer); Assert.Same(EqualityComparer <object> .Default, ImmutableTreeDictionary.Create <object, object>().Add(new object(), null !).WithComparers(keyComparer: null, valueComparer: objComparer).KeyComparer); Assert.Same(objComparer, ImmutableTreeDictionary.Create <object, object>().Add(new object(), null !).WithComparers(keyComparer: null, valueComparer: objComparer).ValueComparer); }
public void TestImmutableTreeDictionaryCreateRangeValidation() { Assert.Throws <ArgumentNullException>("items", () => ImmutableTreeDictionary.CreateRange <string, int>(null !)); Assert.Throws <ArgumentNullException>("items", () => ImmutableTreeDictionary.CreateRange <string, int>(EqualityComparer <string> .Default, null !)); Assert.Throws <ArgumentNullException>("items", () => ImmutableTreeDictionary.CreateRange <string, int>(EqualityComparer <string> .Default, EqualityComparer <int> .Default, null !)); Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary()); Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary(EqualityComparer <string> .Default)); Assert.Throws <ArgumentNullException>("items", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary(EqualityComparer <string> .Default, EqualityComparer <int> .Default)); Assert.Throws <ArgumentNullException>("source", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary(x => x)); Assert.Throws <ArgumentNullException>("source", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary(x => x.Key, EqualityComparer <string> .Default)); Assert.Throws <ArgumentNullException>("source", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary(x => x.Key, x => x.Value, EqualityComparer <string> .Default)); Assert.Throws <ArgumentNullException>("source", () => default(IEnumerable <KeyValuePair <string, int> >) !.ToImmutableTreeDictionary(x => x.Key, x => x.Value, EqualityComparer <string> .Default, EqualityComparer <int> .Default)); Assert.Throws <ArgumentNullException>("keySelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableTreeDictionary(keySelector: null !, EqualityComparer <string> .Default)); Assert.Throws <ArgumentNullException>("keySelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableTreeDictionary(keySelector: null !, x => x.Value, EqualityComparer <string> .Default)); Assert.Throws <ArgumentNullException>("keySelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableTreeDictionary(keySelector: null !, x => x.Value, EqualityComparer <string> .Default, EqualityComparer <int> .Default)); Assert.Throws <ArgumentNullException>("elementSelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableTreeDictionary <KeyValuePair <string, int>, string, int>(x => x.Key, elementSelector: null !, EqualityComparer <string> .Default)); Assert.Throws <ArgumentNullException>("elementSelector", () => Enumerable.Empty <KeyValuePair <string, int> >().ToImmutableTreeDictionary(x => x.Key, elementSelector: null !, EqualityComparer <string> .Default, EqualityComparer <int> .Default)); }
public void TestIDictionary() { IDictionary dictionary = ImmutableTreeDictionary.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 !]);
public void TestIDictionaryT() { IDictionary <int, int> dictionary = ImmutableTreeDictionary.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); }
public void TestImmutableTreeDictionaryConstructor() { ImmutableTreeDictionary <int, int> .Builder dictionary = ImmutableTreeDictionary.CreateBuilder <int, int>(); Assert.Empty(dictionary); }