Exemple #1
0
        public void TryGetFullKeysByPartialKeyTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, bool subKeyEqualityByRef, bool keyEqualityByRef)
        {
            Init(strategy, subKeyEqualityByRef, keyEqualityByRef);
            var subKeyComparer = subKeyEqualityByRef.SubKeyComparerFor <Employee <int> >();
            var keyComparer    = keyEqualityByRef.KeyComparerFor <Employee <int> >();

            foreach (var key in searchKeys)
            {
                bool result = map.TryGetFullKeysByPartialKey(key, out var value);
                result.Should().Be(true);
                value.Should().NotBeNullOrEmpty().And.HaveCount(1).And.HaveElementAt(0, key);
            }

            foreach (var partKey in partKeys)
            {
                bool result = map.TryGetFullKeysByPartialKey(partKey, out var value);
                result.Should().Be(true);
                value.Should().NotBeNullOrEmpty()
                .And.HaveCount(x => x > 0)
                .And.NotContainNulls()
                .And.OnlyHaveUniqueItems()
                .And.OnlyContain(key => map.Keys.Contains(key, keyComparer))
                .And.OnlyContain(key => partKey.Intersect(key, subKeyComparer).Count() == partKey.Count());
            }
        }
Exemple #2
0
        private IMultiKeyMap <T, K, V> AddHelper <T, K, V>(T t, K k, V v,
                                                           MultiKeyMaps.MultiKeyCreationStrategy strategy) where K : class, IEnumerable <T>
        {
            KeyValuePair <K, V> expectedEntry = new KeyValuePair <K, V>(k, v);

            var multiDict = MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(strategy);

            multiDict.Should().NotBeNull().And.BeEmpty().And.BeAssignableTo(typeof(IMultiKeyMap <T, K, V>))
            .And.BeAssignableTo(typeof(IDictionary <K, V>));

            multiDict.Add(k, v);
            multiDict.Should().NotBeEmpty().And.ContainKey(k).And.ContainValue(v).And.Contain(expectedEntry).And.HaveCount(1);

            bool result = multiDict.TryGetFullKeysByPartialKey(k, out var actualKeys);

            result.Should().BeTrue();
            actualKeys.Should().NotBeNullOrEmpty().And.HaveCount(1).And.HaveElementAt(0, k);

            result = multiDict.TryGetValuesByPartialKey(k, out var actualValues);
            result.Should().BeTrue();
            actualValues.Should().NotBeNullOrEmpty().And.HaveCount(1).And.HaveElementAt(0, v);

            result = multiDict.TryGetEntriesByPartialKey(k, out var actualEntries);
            result.Should().BeTrue();
            actualEntries.Should().NotBeNullOrEmpty().And.HaveCount(1).And.HaveElementAt(0, expectedEntry);

            return(multiDict);
        }
        public void PositionedTryGetValuesByPartialKeyTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertTryGetValuesByPartialKey(new List <int>()
            {
                3, 2
            }, new int[] { -1, 1 }, new List <object>()
            {
                new { A = 1, B = "hi" }, new { A = 1, B = "hi4" }
            });
            helper2.AssertTryGetValuesByPartialKey(new List <string>()
            {
                "6", "2"
            }, new int[] { 5, -1 }, new List <long>()
            {
                4
            });
            helper3.AssertTryGetValuesByPartialKey(new[] { GetClass1("M"), GetClass1("b") }, new int[] { 2, -1 }, new List <string>()
            {
                "1"
            });
            helper4.AssertTryGetValuesByPartialKey(new[] { GetValueTuple(3.3), GetValueTuple(2.2) }, new int[] { -1, -1 }, new List <bool>()
            {
                true, false
            });
        }
Exemple #4
0
 private void DictionaryBasedAddHelper <T, K, V>(
     MultiKeyMaps.MultiKeyCreationStrategy strategy, K[] ks, V[] vs,
     IEqualityComparer <T> subKeyComparer = null, IEqualityComparer <K> fullKeyComparer = null) where K : class, IEnumerable <T>
 {
     subKeyComparer  = subKeyComparer ?? EqualityComparer <T> .Default;
     fullKeyComparer = fullKeyComparer ?? EqualityComparer <K> .Default;
     AddHelper(() => MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(subKeyComparer, fullKeyComparer, strategy), ks, vs, subKeyComparer, fullKeyComparer);
 }
        public void SetIndexerTwiceTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertSetIndexerTwice();
            helper2.AssertSetIndexerTwice();
            helper3.AssertSetIndexerTwice();
            helper4.AssertSetIndexerTwice();
        }
        public void AddNullKeyTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertAddNullKey();
            helper2.AssertAddNullKey();
            helper3.AssertAddNullKey();
            helper4.AssertAddNullKey();
        }
        public void RemoveTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertRemove();
            helper2.AssertRemove();
            helper3.AssertRemove();
            helper4.AssertRemove();
        }
        public void EmptyInstanceTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertEmptyInstance();
            helper2.AssertEmptyInstance();
            helper3.AssertEmptyInstance();
            helper4.AssertEmptyInstance();
        }
Exemple #9
0
        private IMultiKeyMap <T, K, V> CreatesEmptyInstanceHelper <T, K, V>(T t, K k, V v,
                                                                            MultiKeyMaps.MultiKeyCreationStrategy strategy) where K : class, IEnumerable <T>
        {
            var multiDict = MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(strategy);

            Assert.IsNotNull(multiDict);
            Assert.IsInstanceOfType(multiDict, typeof(IMultiKeyMap <T, K, V>));
            Assert.IsInstanceOfType(multiDict, typeof(IDictionary <K, V>));
            Assert.IsTrue(multiDict.Count == 0);
            return(multiDict);
        }
Exemple #10
0
        private IMultiKeyMap <T, K, V> CreateMultiKeyDictionaryWithComparersHelper <T, K, V>(
            IEqualityComparer <T> subKeyComparer, IEqualityComparer <K> fullKeyComparer,
            T t, K k, V v,
            MultiKeyMaps.MultiKeyCreationStrategy strategy) where K : class, IEnumerable <T>
        {
            var multiDict = MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(subKeyComparer, fullKeyComparer, strategy);

            Assert.IsNotNull(multiDict);
            Assert.IsInstanceOfType(multiDict, typeof(IMultiKeyMap <T, K, V>));
            Assert.IsInstanceOfType(multiDict, typeof(IDictionary <K, V>));
            Assert.IsTrue(multiDict.Count == 0);
            return(multiDict);
        }
        public void SerializationTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            var myObj1 = MultiKeyMaps.CreateMultiKeyDictionary <int, int[], string>();

            myObj1.Add(new int[] { 1, 2 }, "hi");
            myObj1.Add(new int[] { 3, 4 }, "bye");

            byte[] serialized = SerializeHelper(myObj1);
            var    myObj2     = DeserializeHelper <IMultiKeyMap <int, int[], string> >(serialized);

            myObj2.ShouldAllBeEquivalentTo(myObj1);
        }
        public void TryGetEntriesByPartialKeyTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertTryGetEntriesByPartialKey(new List <int>()
            {
                3, 2
            });
            helper2.AssertTryGetEntriesByPartialKey(new List <string>()
            {
                "6", "2"
            });
            helper3.AssertTryGetEntriesByPartialKey(new[] { GetClass1("M"), GetClass1("b") });
            helper4.AssertTryGetEntriesByPartialKey(new[] { GetValueTuple(3.3), GetValueTuple(2.2) });
        }
Exemple #13
0
        Setup(MultiKeyMaps.MultiKeyCreationStrategy strategy, bool subKeyEqualityByRef, bool keyEqualityByRef)
        {
            var subKeyComparer = subKeyEqualityByRef.SubKeyComparerFor <Employee <int> >();
            var keyComparer    = keyEqualityByRef.KeyComparerFor <Employee <int> >();

            Func <int, Employee <int> > subKeyConverter = x => new Employee <int>(String.Join("", Enumerable.Range(x, 10)), x);
            Func <int, string>          valueConverter  = x => x.ToString();

            var tuple = InitData(subKeyComparer, keyComparer, strategy, subKeyConverter, valueConverter, SearchCount, RecordCount, KeySize);

            var map = MultiKeyMaps.CreateMultiKeyDictionary <Employee <int>, IEnumerable <Employee <int> >, string>(subKeyComparer, keyComparer, strategy);

            map.CopyFrom(tuple.dict);
            return(map, tuple.dict, tuple.keys, tuple.partKeys, tuple.positivePositions, tuple.positions);
        }
Exemple #14
0
 public void CreateMultiKeyDictionaryWithComparers(MultiKeyMaps.MultiKeyCreationStrategy strategy)
 {
     CreatesEmptyInstanceHelper(1, new[] { 1, 2, 3 }, new { A = 1, B = "hi" }, strategy);
     CreatesEmptyInstanceHelper("1", new[] { "1", "2" }, 99, strategy);
     CreatesEmptyInstanceHelper(GetClass1("hi"), new List <Class1 <string> >()
     {
         GetClass1("a"), GetClass1("b")
     }, true, strategy);
     CreatesEmptyInstanceHelper(GetClass1(3.5), new HashSet <Class1 <double> >()
     {
         GetClass1(1.1), GetClass1(2.2)
     }, true, strategy);
     CreatesEmptyInstanceHelper(GetValueTuple(3.5), new HashSet <ValueTuple <double> >()
     {
         GetValueTuple(1.1), GetValueTuple(2.2)
     }, true, strategy);
 }
        public void PositionedTryGetEntriesByPartialKeyTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper1.AssertTryGetEntriesByPartialKey(new List <int>()
            {
                3, 2
            }, new int[] { -1, 1 },
                                                    new List <KeyValuePair <int[], object> >()
            {
                new KeyValuePair <int[], object>(new int[] { 1, 2, 3, 4 }, new { A = 1, B = "hi" }),
                new KeyValuePair <int[], object>(new int[] { 1, 2, 3, 4, 5, 6 }, new { A = 1, B = "hi4" })
            });

            helper2.AssertTryGetEntriesByPartialKey(new List <string>()
            {
                "6", "2"
            }, new int[] { 5, -1 },
                                                    new List <KeyValuePair <string[], long> >()
            {
                new KeyValuePair <string[], long>(new string[] { "1", "2", "3", "4", "5", "6" }, 4)
            });

            helper3.AssertTryGetEntriesByPartialKey(new[] { GetClass1("M"), GetClass1("b") }, new int[] { 2, -1 },
                                                    new List <KeyValuePair <List <Class1 <string> >, string> >()
            {
                new KeyValuePair <List <Class1 <string> >, string>(new List <Class1 <string> >()
                {
                    GetClass1("a"), GetClass1("b"), GetClass1("M")
                }, "1")
            });

            helper4.AssertTryGetEntriesByPartialKey(new[] { GetValueTuple(3.3), GetValueTuple(2.2) }, new int[] { -1, -1 },
                                                    new List <KeyValuePair <List <ValueTuple <double> >, bool> >()
            {
                new KeyValuePair <List <ValueTuple <double> >, bool>(new List <ValueTuple <double> >()
                {
                    GetValueTuple(1.1), GetValueTuple(2.2), GetValueTuple(3.3)
                }, true),
                new KeyValuePair <List <ValueTuple <double> >, bool>(new List <ValueTuple <double> >()
                {
                    GetValueTuple(3.3), GetValueTuple(4.0), GetValueTuple(5.5), GetValueTuple(2.2)
                }, false)
            });
        }
Exemple #16
0
        private void CreateMultiKeyDictionaryWithComparersEmptyHelper <T, K, V>(
            IEqualityComparer <T> subKeyComparer, IEqualityComparer <K> fullKeyComparer,
            MultiKeyMaps.MultiKeyCreationStrategy strategy) where K : class, IEnumerable <T>
        {
            var multiDict = MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(subKeyComparer, fullKeyComparer, strategy);

            multiDict.Should().NotBeNull().And.BeEmpty().And.BeAssignableTo(typeof(IMultiKeyMap <T, K, V>))
            .And.BeAssignableTo(typeof(IDictionary <K, V>));

            multiDict.GetType().Should().BeDecoratedWith <SerializableAttribute>();

            Action act = () => MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(null, fullKeyComparer, strategy);

            act.ShouldThrow <ArgumentNullException>().Where(e => e.ParamName.Equals("subKeyComparer"));

            act = () => MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(subKeyComparer, null, strategy);
            act.ShouldThrow <ArgumentNullException>().Where(e => e.ParamName.Equals("fullKeyComparer"));
        }
        public Func <IMultiKeyMap <T, K, V> > Supplier <T, K, V>(
            MultiKeyMaps.MultiKeyCreationStrategy strategy = OptimizedForNonPositionalSearch,
            ComparersStrategy compStrategy = BothNull) where K : class, IEnumerable <T>
        {
            switch (compStrategy)
            {
            case BothNull:
            case BothDefault:
                return(() => MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(strategy));

            case StructuralFullKeyOnly:
                return(() => MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(EqualityComparer <T> .Default,
                                                                             new EnumerableEqualityComparer <T, K>(EqualityComparer <T> .Default), strategy));

            case StructuralBoth:
                return(() => MultiKeyMaps.CreateMultiKeyDictionary <T, K, V>(EqualityComparer <T> .Default,
                                                                             new EnumerableEqualityComparer <T, K>(EqualityComparer <T> .Default), strategy));

            default:
                throw new ArgumentOutOfRangeException("compStrategy");
            }
        }
        public void PositionedTryGetFullKeysByPartialKeyTest(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            InitAll(strategy, compStrategy);

            helper4.AssertTryGetFullKeysByPartialKey(new[] { GetValueTuple(3.3), GetValueTuple(2.2) }, new int[] { -1, -1 }, new HashSet <List <ValueTuple <double> > >()
            {
                new List <ValueTuple <double> >()
                {
                    GetValueTuple(1.1), GetValueTuple(2.2), GetValueTuple(3.3)
                },
                new List <ValueTuple <double> >()
                {
                    GetValueTuple(3.3), GetValueTuple(4.0), GetValueTuple(5.5), GetValueTuple(2.2)
                }
            });
            helper3.AssertTryGetFullKeysByPartialKey(new[] { GetClass1("M"), GetClass1("b") }, new int[] { 2, -1 },
                                                     new HashSet <List <Class1 <string> > >()
            {
                new List <Class1 <string> >()
                {
                    GetClass1("a"), GetClass1("b"), GetClass1("M")
                }
            });
            helper1.AssertTryGetFullKeysByPartialKey(new List <int>()
            {
                3, 2
            }, new int[] { -1, 1 }, new HashSet <int[]>()
            {
                new int[] { 1, 2, 3, 4 }, new int[] { 1, 2, 3, 4, 5, 6 }
            });
            helper2.AssertTryGetFullKeysByPartialKey(new List <string>()
            {
                "6", "2"
            }, new int[] { 5, -1 }, new HashSet <string[]>()
            {
                new string[] { "1", "2", "3", "4", "5", "6" }
            });
        }
        public void InitAll(MultiKeyMaps.MultiKeyCreationStrategy strategy, ComparersStrategy compStrategy)
        {
            helper1.Init(Supplier <int, int[], object>(strategy, compStrategy));
            helper2.Init(Supplier <string, string[], long>(strategy, compStrategy));
            helper3.Init(Supplier <Class1 <string>, List <Class1 <string> >, string>(strategy, compStrategy));
            helper4.Init(Supplier <ValueTuple <double>, List <ValueTuple <double> >, bool>(strategy, compStrategy));

            helper1.Init(
                new int[][] {
                new int[] { 1, 2, 3, 4 }, new int[] { 4, 3, 5 }, new int[] { 5, 1, 8 },
                new int[] { 1, 2, 3, 4, 5, 6 }, new int[] { 30, -10 }
            },

                new object[] {
                new { A = 1, B = "hi" }, new { A = 1, B = "hi2" }, new { A = 1, B = "hi3" },
                new { A = 1, B = "hi4" }, new { A = 1, B = "hi5" }
            });
            helper2.Init(
                new string[][] {
                new string[] { "1", "2", "3", "4" }, new string[] { "4", "3", "5" }, new string[] { "5", "1", "8" },
                new string[] { "1", "2", "3", "4", "5", "6" }, new string[] { "30", "-10" }
            },

                new long[] {
                1, 2, 3, 4, 5
            });
            helper3.Init(
                new List <Class1 <string> >[] {
                new List <Class1 <string> >()
                {
                    GetClass1("a"), GetClass1("b"), GetClass1("M")
                },
                new List <Class1 <string> >()
                {
                    GetClass1("C"), GetClass1("M"), GetClass1("a"), GetClass1("Y")
                },
                new List <Class1 <string> >()
                {
                    GetClass1("d"), GetClass1("v"), GetClass1("Y"), GetClass1("M")
                }
            },

                new string[] { "1", "2", "3" }
                );
            helper4.Init(
                new List <ValueTuple <double> >[] {
                new List <ValueTuple <double> >()
                {
                    GetValueTuple(1.1), GetValueTuple(2.2), GetValueTuple(3.3)
                },
                new List <ValueTuple <double> >()
                {
                    GetValueTuple(3.3), GetValueTuple(4.0), GetValueTuple(5.5), GetValueTuple(2.2)
                },
                new List <ValueTuple <double> >()
                {
                    GetValueTuple(4.4), GetValueTuple(4.0), GetValueTuple(7.0), GetValueTuple(12.5)
                },
                new List <ValueTuple <double> >()
                {
                    GetValueTuple(3.3), GetValueTuple(-2.0), GetValueTuple(99.34)
                },
            },
                new bool[]
            {
                true, false, true, true
            }
                );
        }
Exemple #20
0
 public void CreateMultiKeyDictionaryWithComparers_Empty(MultiKeyMaps.MultiKeyCreationStrategy strategy)
 {
     CreateMultiKeyDictionaryWithComparersEmptyHelper <int, int[], string>(EqualityComparer <int> .Default,
                                                                           EqualityComparer <int[]> .Default, strategy);
 }
Exemple #21
0
 public void Init(MultiKeyMaps.MultiKeyCreationStrategy strategy, bool subKeyEqualityByRef, bool keyEqualityByRef)
 {
     (map, dict, searchKeys, partKeys, positivePositions, positions) = Setup(strategy, subKeyEqualityByRef, keyEqualityByRef);
 }