public static void TestDebuggerAttributes_Null()
        {
            Type proxyType = DebuggerAttributes.GetProxyType(ImmutableSegmentedDictionary.Create <string, int>());
            TargetInvocationException tie = Assert.Throws <TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object?)null));

            Assert.IsType <ArgumentNullException>(tie.InnerException);
        }
        public void Create()
        {
            IEnumerable <KeyValuePair <string, string> > pairs = new Dictionary <string, string>
            {
                { "a", "b" }
            };
            var keyComparer = StringComparer.OrdinalIgnoreCase;

            var dictionary = ImmutableSegmentedDictionary.Create <string, string>();

            Assert.Equal(0, dictionary.Count);
            Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer);

            dictionary = ImmutableSegmentedDictionary.Create <string, string>(keyComparer);
            Assert.Equal(0, dictionary.Count);
            Assert.Same(keyComparer, dictionary.KeyComparer);

            dictionary = ImmutableSegmentedDictionary.CreateRange(pairs);
            Assert.Equal(1, dictionary.Count);
            Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer);

            dictionary = ImmutableSegmentedDictionary.CreateRange(keyComparer, pairs);
            Assert.Equal(1, dictionary.Count);
            Assert.Same(keyComparer, dictionary.KeyComparer);
        }
        public void WithComparerCollisions()
        {
            // First check where collisions have matching values.
            var map = ImmutableSegmentedDictionary
                      .Create <string, string>()
                      .Add("a", "1")
                      .Add("A", "1");

            map = map.WithComparer(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, map.KeyComparer);
            Assert.Equal(1, map.Count);
            Assert.True(map.ContainsKey("a"));
            Assert.Equal("1", map["a"]);

            // Now check where collisions have conflicting values.
            map = ImmutableSegmentedDictionary
                  .Create <string, string>()
                  .Add("a", "1")
                  .Add("A", "2")
                  .Add("b", "3");
            Assert.Throws <ArgumentException>(
                null,
                () => map.WithComparer(StringComparer.OrdinalIgnoreCase)
                );
        }
        public void DebuggerAttributesValid()
        {
            DebuggerAttributes.ValidateDebuggerDisplayReferences(
                ImmutableSegmentedDictionary.Create <int, int>()
                );
            ImmutableSegmentedDictionary <string, int> dict = ImmutableSegmentedDictionary
                                                              .Create <string, int>()
                                                              .Add("One", 1)
                                                              .Add("Two", 2);
            DebuggerAttributeInfo info = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(
                dict
                );

            object rootNode =
                DebuggerAttributes.GetFieldValue(
                    ImmutableSegmentedDictionary.Create <string, string>(),
                    "_root"
                    ) ?? throw new InvalidOperationException();

            DebuggerAttributes.ValidateDebuggerDisplayReferences(rootNode);
            PropertyInfo itemProperty = info.Properties.Single(
                pr =>
                pr.GetCustomAttribute <DebuggerBrowsableAttribute>() !.State
                == DebuggerBrowsableState.RootHidden
                );

            KeyValuePair <string, int>[]? items =
                itemProperty.GetValue(info.Instance) as KeyValuePair <string, int>[];
            Assert.Equal(dict, items);
        }
예제 #5
0
 protected override IImmutableDictionary <TKey, TValue> GetEmptyImmutableDictionary <
     TKey,
     TValue
     >()
 {
     return(ImmutableSegmentedDictionary.Create <TKey, TValue>());
 }
예제 #6
0
        public void KeyComparerCollisions()
        {
            // First check where collisions have matching values.
            var builder = ImmutableSegmentedDictionary
                          .Create <string, string>()
                          .Add("a", "1")
                          .Add("A", "1")
                          .ToBuilder();

            builder.KeyComparer = StringComparer.OrdinalIgnoreCase;
            Assert.Equal(1, builder.Count);
            Assert.True(builder.ContainsKey("a"));

            var set = builder.ToImmutable();

            Assert.Same(StringComparer.OrdinalIgnoreCase, set.KeyComparer);
            Assert.Equal(1, set.Count);
            Assert.True(set.ContainsKey("a"));

            // Now check where collisions have conflicting values.
            builder = ImmutableSegmentedDictionary
                      .Create <string, string>()
                      .Add("a", "1")
                      .Add("A", "2")
                      .Add("b", "3")
                      .ToBuilder();
            Assert.Throws <ArgumentException>(
                null,
                () => builder.KeyComparer = StringComparer.OrdinalIgnoreCase
                );
        }
예제 #7
0
        public void KeyComparer()
        {
            var builder = ImmutableSegmentedDictionary
                          .Create <string, string>()
                          .Add("a", "1")
                          .Add("B", "1")
                          .ToBuilder();

            Assert.Same(EqualityComparer <string> .Default, builder.KeyComparer);
            Assert.True(builder.ContainsKey("a"));
            Assert.False(builder.ContainsKey("A"));

            builder.KeyComparer = StringComparer.OrdinalIgnoreCase;
            Assert.Same(StringComparer.OrdinalIgnoreCase, builder.KeyComparer);
            Assert.Equal(2, builder.Count);
            Assert.True(builder.ContainsKey("a"));
            Assert.True(builder.ContainsKey("A"));
            Assert.True(builder.ContainsKey("b"));

            var set = builder.ToImmutable();

            Assert.Same(StringComparer.OrdinalIgnoreCase, set.KeyComparer);
            Assert.True(set.ContainsKey("a"));
            Assert.True(set.ContainsKey("A"));
            Assert.True(set.ContainsKey("b"));
        }
        public void ContainsValue()
        {
            var map     = ImmutableSegmentedDictionary.Create <string, int>().Add("five", 5);
            var builder = map.ToBuilder();

            Assert.True(builder.ContainsValue(5));
            Assert.False(builder.ContainsValue(4));
        }
        public void CollisionExceptionMessageContainsKey()
        {
            var map = ImmutableSegmentedDictionary.Create <string, string>()
                      .Add("firstKey", "1").Add("secondKey", "2");
            var exception = Assert.Throws <ArgumentException>(null, () => map.Add("firstKey", "3"));

            Assert.Contains("firstKey", exception.Message);
        }
        public void Indexer_KeyNotFoundException_ContainsKeyInMessage()
        {
            var map = ImmutableSegmentedDictionary.Create <string, string>()
                      .Add("a", "1").Add("b", "2");
            var exception = Assert.Throws <KeyNotFoundException>(() => map["c"]);

            Assert.Contains("'c'", exception.Message);
        }
        public void WithComparerEmptyCollection()
        {
            var map = ImmutableSegmentedDictionary.Create <string, string>();

            Assert.Same(EqualityComparer <string> .Default, map.KeyComparer);
            map = map.WithComparer(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, map.KeyComparer);
        }
        public void Clear()
        {
            var builder = ImmutableSegmentedDictionary.Create <string, int>().ToBuilder();

            builder.Add("five", 5);
            Assert.Equal(1, builder.Count);
            builder.Clear();
            Assert.Equal(0, builder.Count);
        }
예제 #13
0
        public void Indexer_KeyNotFoundException_ContainsKeyInMessage()
        {
            var map = ImmutableSegmentedDictionary.Create <string, string>()
                      .Add("a", "1").Add("b", "2");
            var missingKey = "__ThisKeyDoesNotExist__";
            var exception  = Assert.Throws <KeyNotFoundException>(() => map[missingKey]);

            Assert.Contains(missingKey, exception.Message);
        }
        public void GetValueOrDefaultOfConcreteType()
        {
            var empty     = ImmutableSegmentedDictionary.Create <string, int>();
            var populated = ImmutableSegmentedDictionary.Create <string, int>().Add("a", 5);

            Assert.Equal(0, empty.GetValueOrDefault("a"));
            Assert.Equal(1, empty.GetValueOrDefault("a", 1));
            Assert.Equal(5, populated.GetValueOrDefault("a"));
            Assert.Equal(5, populated.GetValueOrDefault("a", 1));
        }
        public void GetValueOrDefaultOfIImmutableDictionary()
        {
            IImmutableDictionary <string, int> empty     = ImmutableSegmentedDictionary.Create <string, int>();
            IImmutableDictionary <string, int> populated = ImmutableSegmentedDictionary.Create <string, int>().Add("a", 5);

            Assert.Equal(0, empty.GetValueOrDefault("a"));
            Assert.Equal(1, empty.GetValueOrDefault("a", 1));
            Assert.Equal(5, populated.GetValueOrDefault("a"));
            Assert.Equal(5, populated.GetValueOrDefault("a", 1));
        }
        public void AddRange()
        {
            var builder = ImmutableSegmentedDictionary.Create <string, int>().ToBuilder();

            builder.AddRange(new Dictionary <string, int> {
                { "a", 1 }, { "b", 2 }
            });
            Assert.Equal(2, builder.Count);
            Assert.Equal(1, builder["a"]);
            Assert.Equal(2, builder["b"]);
        }
        public void ToImmutableDictionaryOptimized()
        {
            var dictionary = ImmutableSegmentedDictionary.Create <string, string>();
            var result     = dictionary.ToImmutableSegmentedDictionary();

            Assert.True(IsSame(dictionary, result));

            var cultureComparer = StringComparer.CurrentCulture;

            result = dictionary.WithComparer(cultureComparer);
            Assert.Same(cultureComparer, result.KeyComparer);
        }
        public void KeyComparerEmptyCollection()
        {
            var builder = ImmutableSegmentedDictionary.Create <string, string>()
                          .Add("a", "1").Add("B", "1").ToBuilder();

            Assert.Same(EqualityComparer <string> .Default, builder.KeyComparer);
            builder.KeyComparer = StringComparer.OrdinalIgnoreCase;
            Assert.Same(StringComparer.OrdinalIgnoreCase, builder.KeyComparer);
            var set = builder.ToImmutable();

            Assert.Same(StringComparer.OrdinalIgnoreCase, set.KeyComparer);
        }
        public void RemoveRange()
        {
            var builder =
                ImmutableSegmentedDictionary.Create <string, int>()
                .AddRange(new Dictionary <string, int> {
                { "a", 1 }, { "b", 2 }, { "c", 3 }
            })
                .ToBuilder();

            Assert.Equal(3, builder.Count);
            builder.RemoveRange(new[] { "a", "b" });
            Assert.Equal(1, builder.Count);
            Assert.Equal(3, builder["c"]);
        }
        public void WithComparer()
        {
            var map = ImmutableSegmentedDictionary.Create <string, string>().Add("a", "1").Add("B", "1");

            Assert.Same(EqualityComparer <string> .Default, map.KeyComparer);
            Assert.True(map.ContainsKey("a"));
            Assert.False(map.ContainsKey("A"));

            map = map.WithComparer(StringComparer.OrdinalIgnoreCase);
            Assert.Same(StringComparer.OrdinalIgnoreCase, map.KeyComparer);
            Assert.Equal(2, map.Count);
            Assert.True(map.ContainsKey("a"));
            Assert.True(map.ContainsKey("A"));
            Assert.True(map.ContainsKey("b"));
        }
 protected override IImmutableDictionary <string, TValue> Empty <TValue>(
     StringComparer comparer
     )
 {
     return(ImmutableSegmentedDictionary.Create <string, TValue>(comparer));
 }