public void EnumeratorRecyclingMisuse()
        {
            var collection     = ImmutableTrieDictionary.Create <int, int>().Add(5, 3);
            var enumerator     = collection.GetEnumerator();
            var enumeratorCopy = enumerator;

            Assert.True(enumerator.MoveNext());
            Assert.False(enumerator.MoveNext());
            enumerator.Dispose();
            Assert.Throws <ObjectDisposedException>(() => enumerator.MoveNext());
            Assert.Throws <ObjectDisposedException>(() => enumerator.Reset());
            Assert.Throws <ObjectDisposedException>(() => enumerator.Current);
            Assert.Throws <ObjectDisposedException>(() => enumeratorCopy.MoveNext());
            Assert.Throws <ObjectDisposedException>(() => enumeratorCopy.Reset());
            Assert.Throws <ObjectDisposedException>(() => enumeratorCopy.Current);
            enumerator.Dispose(); // double-disposal should not throw
            enumeratorCopy.Dispose();

            // We expect that acquiring a new enumerator will use the same underlying Stack<T> object,
            // but that it will not throw exceptions for the new enumerator.
            enumerator = collection.GetEnumerator();
            Assert.True(enumerator.MoveNext());
            Assert.False(enumerator.MoveNext());
            Assert.Throws <InvalidOperationException>(() => enumerator.Current);
            enumerator.Dispose();
        }
        public void WithComparersCollisions()
        {
            // First check where collisions have matching values.
            var map = ImmutableTrieDictionary.Create <string, string>()
                      .Add("a", "1").Add("A", "1");

            map = map.WithComparers(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 = ImmutableTrieDictionary.Create <string, string>()
                  .Add("a", "1").Add("A", "2").Add("b", "3");
            Assert.Throws <ArgumentException>(null, () => map.WithComparers(StringComparer.OrdinalIgnoreCase));

            // Force all values to be considered equal.
            map = map.WithComparers(StringComparer.OrdinalIgnoreCase, EverythingEqual <string> .Default);
            Assert.Same(StringComparer.OrdinalIgnoreCase, map.KeyComparer);
            Assert.Same(EverythingEqual <string> .Default, map.ValueComparer);
            Assert.Equal(2, map.Count);
            Assert.True(map.ContainsKey("a"));
            Assert.True(map.ContainsKey("b"));
        }
        public void WithComparers()
        {
            var map = ImmutableTrieDictionary.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.WithComparers(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"));

            var cultureComparer = StringComparer.CurrentCulture;

            map = map.WithComparers(StringComparer.OrdinalIgnoreCase, cultureComparer);
            Assert.Same(StringComparer.OrdinalIgnoreCase, map.KeyComparer);
            Assert.Same(cultureComparer, map.ValueComparer);
            Assert.Equal(2, map.Count);
            Assert.True(map.ContainsKey("a"));
            Assert.True(map.ContainsKey("A"));
            Assert.True(map.ContainsKey("b"));
        }
        public void KeyComparerCollisions()
        {
            // First check where collisions have matching values.
            var builder = ImmutableTrieDictionary.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 = ImmutableTrieDictionary.Create <string, string>()
                      .Add("a", "1").Add("A", "2").Add("b", "3").ToBuilder();
            Assert.Throws <ArgumentException>(null, () => builder.KeyComparer = StringComparer.OrdinalIgnoreCase);

            // Force all values to be considered equal.
            builder.ValueComparer = EverythingEqual <string> .Default;
            Assert.Same(EverythingEqual <string> .Default, builder.ValueComparer);
            builder.KeyComparer = StringComparer.OrdinalIgnoreCase; // should not throw because values will be seen as equal.
            Assert.Equal(2, builder.Count);
            Assert.True(builder.ContainsKey("a"));
            Assert.True(builder.ContainsKey("b"));
        }
        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 ContainsValue()
        {
            var map     = ImmutableTrieDictionary.Create <string, int>().Add("five", 5);
            var builder = map.ToBuilder();

            Assert.True(builder.ContainsValue(5));
            Assert.False(builder.ContainsValue(4));
        }
        public void WithComparersEmptyCollection()
        {
            var map = ImmutableTrieDictionary.Create <string, string>();

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

            builder.Add("five", 5);
            Assert.Equal(1, builder.Count);
            builder.Clear();
            Assert.Equal(0, builder.Count);
        }
        public void GetValueOrDefaultOfIImmutableDictionary()
        {
            IImmutableDictionary <string, int> empty     = ImmutableTrieDictionary.Create <string, int>();
            IImmutableDictionary <string, int> populated = ImmutableTrieDictionary.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 GetValueOrDefaultOfConcreteType()
        {
            var empty     = ImmutableTrieDictionary.Create <string, int>().ToBuilder();
            var populated = ImmutableTrieDictionary.Create <string, int>().Add("a", 5).ToBuilder();

            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 CollisionExceptionMessageContainsKey()
        {
            var map = ImmutableTrieDictionary.Create <string, string>()
                      .Add("firstKey", "1").Add("secondKey", "2");
            var exception = Assert.Throws <ArgumentException>(null, () => map.Add("firstKey", "3"));

            // if (!PlatformDetection.IsNetNative) //.Net Native toolchain removes exception messages.
            // {
            //     Assert.Contains("firstKey", exception.Message);
            // }
        }
        public void AddRange()
        {
            var builder = ImmutableTrieDictionary.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 KeyComparerEmptyCollection()
        {
            var builder = ImmutableTrieDictionary.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 Setup()
 {
     immutableDictionary     = ImmutableDictionary.Create <string, int>();
     immutableTrieDictionary = ImmutableTrieDictionary.Create <string, int>();
     dictionary = new Dictionary <string, int>();
     for (int i = 0; i < N; i++)
     {
         var key = $"{i},{i}";
         immutableDictionary     = immutableDictionary.SetItem(key, i);
         immutableTrieDictionary = immutableTrieDictionary.SetItem(key, i);
         dictionary[key]         = i;
     }
 }
        public void ToImmutableDictionaryOptimized()
        {
            var dictionary = ImmutableTrieDictionary.Create <string, string>();
            var result     = dictionary.ToImmutableTrieDictionary();

            Assert.Same(dictionary, result);

            var cultureComparer = StringComparer.CurrentCulture;

            result = dictionary.WithComparers(cultureComparer, StringComparer.OrdinalIgnoreCase);
            Assert.Same(cultureComparer, result.KeyComparer);
            Assert.Same(StringComparer.OrdinalIgnoreCase, result.ValueComparer);
        }
        public void RemoveRange()
        {
            var builder =
                ImmutableTrieDictionary.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 Create()
        {
            IEnumerable <KeyValuePair <string, string> > pairs = new Dictionary <string, string> {
                { "a", "b" }
            };
            var keyComparer   = StringComparer.OrdinalIgnoreCase;
            var valueComparer = StringComparer.CurrentCulture;

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

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

            dictionary = ImmutableTrieDictionary.Create <string, string>(keyComparer);
            Assert.Equal(0, dictionary.Count);
            Assert.Same(keyComparer, dictionary.KeyComparer);
            Assert.Same(EqualityComparer <string> .Default, dictionary.ValueComparer);

            dictionary = ImmutableTrieDictionary.Create(keyComparer, valueComparer);
            Assert.Equal(0, dictionary.Count);
            Assert.Same(keyComparer, dictionary.KeyComparer);
            Assert.Same(valueComparer, dictionary.ValueComparer);

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

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

            dictionary = ImmutableTrieDictionary.CreateRange(keyComparer, valueComparer, pairs);
            Assert.Equal(1, dictionary.Count);
            Assert.Same(keyComparer, dictionary.KeyComparer);
            Assert.Same(valueComparer, dictionary.ValueComparer);
        }
        public void KeyComparer()
        {
            var builder = ImmutableTrieDictionary.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"));
        }
 protected override ImmutableTrieDictionary <string, TValue> Empty <TValue>(StringComparer comparer)
 {
     return(ImmutableTrieDictionary.Create <string, TValue>(comparer));
 }
        /*
         *      [Fact]
         *      [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Cannot do DebuggerAttribute testing on UapAot: requires internal Reflection on framework types.")]
         *      public void DebuggerAttributesValid()
         *      {
         *          DebuggerAttributes.ValidateDebuggerDisplayReferences(ImmutableTrieDictionary.CreateBuilder<string, int>());
         *          ImmutableTrieDictionary<int, string>.Builder builder = ImmutableTrieDictionary.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);
         *      }
         *
         *      [Fact]
         *      [SkipOnTargetFramework(TargetFrameworkMonikers.UapAot, "Cannot do DebuggerAttribute testing on UapAot: requires internal Reflection on framework types.")]
         *      public static void TestDebuggerAttributes_Null()
         *      {
         *          Type proxyType = DebuggerAttributes.GetProxyType(ImmutableHashSet.Create<string>());
         *          TargetInvocationException tie = Assert.Throws<TargetInvocationException>(() => Activator.CreateInstance(proxyType, (object)null));
         *          Assert.IsType<ArgumentNullException>(tie.InnerException);
         *      }
         */

        protected override IImmutableDictionary <TKey, TValue> GetEmptyImmutableDictionary <TKey, TValue>()
        {
            return(ImmutableTrieDictionary.Create <TKey, TValue>());
        }
Esempio n. 21
0
 public void Setup()
 {
     immutableDictionary     = ImmutableDictionary.Create <string, int>();
     immutableTrieDictionary = ImmutableTrieDictionary.Create <string, int>();
     dictionary = new Dictionary <string, int>();
 }