/
githubmirror
/
roslyn
Обзор
Документация
Войти
/
githubmirror
/
roslyn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
src/EditorFeatures/Test/Collections/Immutable/Maps/MapTests.cs
123 строки
3 KB
tmat
Adds EqualityComparer<T>.Create polyfill
17 июл 2025, 21:41
17 июл 2025, 21:41
e15133d
Код
Авторство
О чём код?
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. #nullable disable using System.Collections; using System.Collections.Generic; using System.Collections.Immutable; using System.Linq; using Roslyn.Utilities; using Xunit; namespace Microsoft.CodeAnalysis.Editor.UnitTests.Collections.Immutable.Maps; public sealed class MapTests { [Fact] public void TestEnumerator() { var map = ImmutableDictionary.Create<string, int>().Add("1", 1); foreach (var v in map) { Assert.Equal("1", v.Key); Assert.Equal(1, v.Value); } foreach (var v in (IEnumerable)map) { Assert.Equal("1", ((KeyValuePair<string, int>)v).Key); Assert.Equal(1, ((KeyValuePair<string, int>)v).Value); } } [Fact] public void TestCounts() { var map = ImmutableDictionary.Create<string, int>() .Add("1", 1) .Add("2", 2) .Add("3", 3) .Add("4", 4) .Add("5", 5); Assert.Equal(5, map.Count); Assert.Equal(5, map.Keys.Count()); Assert.Equal(5, map.Values.Count()); } [Fact] public void TestRemove() { var map = ImmutableDictionary.Create<string, int>() .Add("1", 1) .Add("2", 2) .Add("3", 3) .Add("4", 4) .Add("5", 5); map = map.Remove("0"); Assert.Equal(5, map.Count); map = map.Remove("1"); Assert.Equal(4, map.Count); map = map.Remove("1"); Assert.Equal(4, map.Count); map = map.Remove("2"); Assert.Equal(3, map.Count); map = map.Remove("3"); Assert.Equal(2, map.Count); map = map.Remove("4"); Assert.Equal(1, map.Count); map = map.Remove("5"); Assert.Empty(map); } [Fact] public void TestPathology() { var comparer = EqualityComparer<string>.Create(EqualityComparer<string>.Default.Equals, _ => 0); var map = ImmutableDictionary.Create<string, int>(comparer) .Add("1", 1) .Add("2", 2) .Add("3", 3) .Add("4", 4) .Add("5", 5); Assert.Equal(5, map.Count); Assert.Equal(1, map["1"]); Assert.Equal(2, map["2"]); Assert.Equal(3, map["3"]); map = map.Remove("0"); Assert.Equal(5, map.Count); map = map.Remove("3"); Assert.Equal(4, map.Count); map = map.Remove("3"); Assert.Equal(4, map.Count); map = map.Remove("2"); Assert.Equal(3, map.Count); map = map.Remove("1"); Assert.Equal(2, map.Count); map = map.Remove("4"); Assert.Equal(1, map.Count); map = map.Remove("6"); Assert.Equal(1, map.Count); map = map.Remove("5"); Assert.Empty(map); } }