示例#1
0
 protected override IImmutableDictionary <TKey, TValue> GetEmptyImmutableDictionary <
     TKey,
     TValue
     >()
 {
     return(ImmutableSegmentedDictionary.Create <TKey, TValue>());
 }
示例#2
0
        public void ToImmutableDictionary()
        {
            ImmutableSegmentedDictionary <int, int> .Builder builder =
                ImmutableSegmentedDictionary.CreateBuilder <int, int>();
            builder.Add(0, 0);
            builder.Add(1, 1);
            builder.Add(2, 2);

            var dictionary = builder.ToImmutableSegmentedDictionary();

            Assert.Equal(0, dictionary[0]);
            Assert.Equal(1, dictionary[1]);
            Assert.Equal(2, dictionary[2]);

            builder[1] = 5;
            Assert.Equal(5, builder[1]);
            Assert.Equal(1, dictionary[1]);

            builder.Clear();
            Assert.True(builder.ToImmutableSegmentedDictionary().IsEmpty);
            Assert.False(dictionary.IsEmpty);

            ImmutableSegmentedDictionary <int, int> .Builder?nullBuilder = null;
            Assert.Throws <ArgumentNullException>(
                "builder",
                () => nullBuilder !.ToImmutableSegmentedDictionary()
                );
        }
        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);
        }
示例#4
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 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 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 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)
                );
        }
示例#8
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
                );
        }
        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 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 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 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);
        }
 private protected static void ContainsValueTestHelper <TKey, TValue>(
     ImmutableSegmentedDictionary <TKey, TValue> map,
     TKey key,
     TValue value
     ) where TKey : notnull
 {
     Assert.False(map.ContainsValue(value));
     Assert.True(map.Add(key, value).ContainsValue(value));
 }
        public void CreateBuilder()
        {
            var builder = ImmutableSegmentedDictionary.CreateBuilder <string, string>();

            Assert.Same(EqualityComparer <string> .Default, builder.KeyComparer);

            builder = ImmutableSegmentedDictionary.CreateBuilder <string, string>(StringComparer.Ordinal);
            Assert.Same(StringComparer.Ordinal, builder.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);
        }
示例#16
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));
        }
示例#19
0
 internal static IDictionary <TKey, TValue> ToBuilder <TKey, TValue>(this IImmutableDictionary <TKey, TValue> dictionary)
     where TKey : notnull
 {
     return(dictionary switch
     {
         ImmutableDictionary <TKey, TValue> d => d.ToBuilder(),
         ImmutableSortedDictionary <TKey, TValue> d => d.ToBuilder(),
         ImmutableSegmentedDictionary <TKey, TValue> d => d.ToBuilder(),
         null => throw new ArgumentNullException(nameof(dictionary)),
         _ => throw ExceptionUtilities.UnexpectedValue(dictionary),
     });
        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 DebuggerAttributesValid()
        {
            DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableSegmentedDictionary.CreateBuilder <string, int>());
            ImmutableSegmentedDictionary <int, string> .Builder builder = ImmutableSegmentedDictionary.CreateBuilder <int, string>();
            builder.Add(1, "One");
            builder.Add(2, "Two");
            DebuggerAttributeInfo info         = DebuggerAttributes.ValidateDebuggerTypeProxyProperties(builder);
            PropertyInfo          itemProperty = info.Properties.Single(pr => pr.GetCustomAttribute <DebuggerBrowsableAttribute>() !.State == DebuggerBrowsableState.RootHidden);

            KeyValuePair <int, string>[]? items = itemProperty.GetValue(info.Instance) as KeyValuePair <int, string>[];
            Assert.Equal(builder, items);
        }
        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"]);
        }
示例#25
0
 private static SingleNamespaceOrTypeDeclaration CreateImplicitClass(ImmutableSegmentedDictionary <string, VoidResult> memberNames, SyntaxReference container, SingleTypeDeclaration.TypeDeclarationFlags declFlags)
 {
     return(new SingleTypeDeclaration(
                kind: DeclarationKind.ImplicitClass,
                name: TypeSymbol.ImplicitTypeName,
                arity: 0,
                modifiers: DeclarationModifiers.Internal | DeclarationModifiers.Partial | DeclarationModifiers.Sealed,
                declFlags: declFlags,
                syntaxReference: container,
                nameLocation: new SourceLocation(container),
                memberNames: memberNames,
                children: ImmutableArray <SingleTypeDeclaration> .Empty,
                diagnostics: ImmutableArray <Diagnostic> .Empty));
 }
        public void ToImmutableDictionary()
        {
            IEnumerable <KeyValuePair <string, string> > pairs = new Dictionary <string, string> {
                { "a", "B" }
            };
            var keyComparer = StringComparer.OrdinalIgnoreCase;

            ImmutableSegmentedDictionary <string, string> dictionary = pairs.ToImmutableSegmentedDictionary();

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

            dictionary = pairs.ToImmutableSegmentedDictionary(keyComparer);
            Assert.Equal(1, dictionary.Count);
            Assert.Same(keyComparer, dictionary.KeyComparer);

            dictionary = pairs.ToImmutableSegmentedDictionary(p => p.Key.ToUpperInvariant(), p => p.Value.ToLowerInvariant());
            Assert.Equal(1, dictionary.Count);
            Assert.Equal("A", dictionary.Keys.Single());
            Assert.Equal("b", dictionary.Values.Single());
            Assert.Same(EqualityComparer <string> .Default, dictionary.KeyComparer);

            dictionary = pairs.ToImmutableSegmentedDictionary(p => p.Key.ToUpperInvariant(), p => p.Value.ToLowerInvariant(), keyComparer);
            Assert.Equal(1, dictionary.Count);
            Assert.Equal("A", dictionary.Keys.Single());
            Assert.Equal("b", dictionary.Values.Single());
            Assert.Same(keyComparer, dictionary.KeyComparer);

            var list          = new int[] { 1, 2 };
            var intDictionary = list.ToImmutableSegmentedDictionary(n => (double)n);

            Assert.Equal(1, intDictionary[1.0]);
            Assert.Equal(2, intDictionary[2.0]);
            Assert.Equal(2, intDictionary.Count);

            var stringIntDictionary = list.ToImmutableSegmentedDictionary(n => n.ToString(), StringComparer.OrdinalIgnoreCase);

            Assert.Same(StringComparer.OrdinalIgnoreCase, stringIntDictionary.KeyComparer);
            Assert.Equal(1, stringIntDictionary["1"]);
            Assert.Equal(2, stringIntDictionary["2"]);
            Assert.Equal(2, intDictionary.Count);

            Assert.Throws <ArgumentNullException>("keySelector", () => list.ToImmutableSegmentedDictionary <int, int>(null !));
            Assert.Throws <ArgumentNullException>("keySelector", () => list.ToImmutableSegmentedDictionary <int, int, int>(null !, v => v));
            Assert.Throws <ArgumentNullException>("elementSelector", () => list.ToImmutableSegmentedDictionary <int, int, int>(k => k, null !));

            list.ToDictionary(k => k, v => v, null); // verifies BCL behavior is to not throw.
            list.ToImmutableSegmentedDictionary(k => k, v => v, null);
        }
        public void Clear_HasComparer_ReturnsEmptyWithOriginalComparer()
        {
            ImmutableSegmentedDictionary <string, int> dictionary = new Dictionary <string, int>
            {
                { "a", 1 }
            }.ToImmutableSegmentedDictionary(StringComparer.OrdinalIgnoreCase);

            ImmutableSegmentedDictionary <string, int> clearedDictionary = dictionary.Clear();

            Assert.False(IsSame(ImmutableSegmentedDictionary <string, int> .Empty, clearedDictionary.Clear()));
            Assert.NotEmpty(dictionary);

            clearedDictionary = clearedDictionary.Add("a", 1);
            Assert.True(clearedDictionary.ContainsKey("A"));
        }
        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"));
        }
        private SingleNamespaceOrTypeDeclaration CreateScriptClass(
            CompilationUnitSyntax parent,
            ImmutableArray <SingleTypeDeclaration> children,
            ImmutableSegmentedDictionary <string, VoidResult> memberNames,
            SingleTypeDeclaration.TypeDeclarationFlags declFlags)
        {
            Debug.Assert(parent.Kind() == SyntaxKind.CompilationUnit && _syntaxTree.Options.Kind != SourceCodeKind.Regular);

            // script type is represented by the parent node:
            var parentReference = _syntaxTree.GetReference(parent);
            var fullName        = _scriptClassName.Split('.');

            // Note: The symbol representing the merged declarations uses parentReference to enumerate non-type members.
            SingleNamespaceOrTypeDeclaration decl = new SingleTypeDeclaration(
                kind: _isSubmission ? DeclarationKind.Submission : DeclarationKind.Script,
                name: fullName.Last(),
                arity: 0,
                modifiers: DeclarationModifiers.Internal | DeclarationModifiers.Partial | DeclarationModifiers.Sealed,
                declFlags: declFlags,
                syntaxReference: parentReference,
                nameLocation: new SourceLocation(parentReference),
                memberNames: memberNames,
                children: children,
                diagnostics: ImmutableArray <Diagnostic> .Empty,
                quickAttributes: QuickAttributes.None);

            for (int i = fullName.Length - 2; i >= 0; i--)
            {
                decl = SingleNamespaceDeclaration.Create(
                    name: fullName[i],
                    hasUsings: false,
                    hasExternAliases: false,
                    syntaxReference: parentReference,
                    nameLocation: new SourceLocation(parentReference),
                    children: ImmutableArray.Create(decl),
                    diagnostics: ImmutableArray <Diagnostic> .Empty);
            }

            return(decl);
        }
示例#30
0
        internal SingleTypeDeclaration(
            DeclarationKind kind,
            string name,
            int arity,
            DeclarationModifiers modifiers,
            TypeDeclarationFlags declFlags,
            SyntaxReference syntaxReference,
            SourceLocation nameLocation,
            ImmutableSegmentedDictionary <string, VoidResult> memberNames,
            ImmutableArray <SingleTypeDeclaration> children,
            ImmutableArray <Diagnostic> diagnostics)
            : base(name, syntaxReference, nameLocation, diagnostics)
        {
            Debug.Assert(kind != DeclarationKind.Namespace);

            _kind       = kind;
            _arity      = (ushort)arity;
            _modifiers  = modifiers;
            MemberNames = memberNames;
            _children   = children;
            _flags      = declFlags;
        }