Ejemplo n.º 1
0
        public void EditableLookupAddRangeNullLookup()
        {
            var lookup = new EditableLookup <string, int>();
            ILookup <string, int> mate = null;

            lookup.AddRange(mate);
        }
Ejemplo n.º 2
0
        public void EditableLookupUntypedEnumerator()
        {
            var lookup = new EditableLookup <int, int>();

            lookup.Add(1, 1); // multipliers....
            lookup.Add(1, 2);
            lookup.Add(1, 3);
            lookup.Add(1, 4);
            lookup.Add(2, 5);
            lookup.Add(3, 6);

            // test the lookup enumerator
            IEnumerable e = lookup;
            int         i = 0;

            foreach (object obj in e)
            {
                i++;
            }
            Assert.AreEqual(3, i);

            // test the group enumerator
            e = lookup[1];
            i = 0;
            foreach (object obj in e)
            {
                i++;
            }
            Assert.AreEqual(4, i);
        }
Ejemplo n.º 3
0
        public void EditableLookupAddRangeNullKey()
        {
            var lookup             = new EditableLookup <string, int>();
            IEnumerable <int> mate = null;

            lookup.AddRange("foo", mate);
        }
Ejemplo n.º 4
0
        public void EditableLookupRemove()
        {
            var lookup = new EditableLookup <string, int>();

            lookup.Add("a", 1);
            lookup.Add("a", 2);
            lookup.Add("b", 3);
            lookup.Add("a", 4);
            Assert.IsTrue(lookup.Contains("a"));
            Assert.IsTrue(lookup.Contains("b"));
            Assert.IsTrue(lookup.Contains("b", 3));
            Assert.IsFalse(lookup.Contains("b", 17));

            Assert.IsTrue(lookup.Remove("b"));
            Assert.IsFalse(lookup.Remove("b")); // no longer there
            Assert.AreEqual(1, lookup.Count);
            Assert.AreEqual(3, lookup["a"].Count());
            Assert.IsTrue(lookup.Remove("a", 1));
            Assert.IsFalse(lookup.Remove("a", 1));    // no longer there
            Assert.IsFalse(lookup.Remove("foo", 99)); // never existed

            Assert.AreEqual(6, lookup["a"].Sum());    // 2 + 4
            lookup.Remove("a", 2);
            lookup.Remove("a", 4);
            Assert.IsFalse(lookup.Contains("a"));
            Assert.AreEqual(0, lookup.Count);
        }
Ejemplo n.º 5
0
 public void EditableLookupAddRangeNullKey()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var lookup             = new EditableLookup <string, int>();
         IEnumerable <int> mate = null;
         lookup.AddRange("foo", mate);
     });
 }
Ejemplo n.º 6
0
 public void EditableLookupAddRangeNullLookup()
 {
     Assert.Throws <ArgumentNullException>(() =>
     {
         var lookup = new EditableLookup <string, int>();
         ILookup <string, int> mate = null;
         lookup.AddRange(mate);
     });
 }
Ejemplo n.º 7
0
 public void EditableLookupComparer()
 {
     var lookup = new EditableLookup<string, int>(StringComparer.OrdinalIgnoreCase);
     lookup.Add("a", 1);
     lookup.Add("A", 1);
     Assert.IsTrue(lookup.Contains("a"));
     Assert.IsTrue(lookup.Contains("A"));
     Assert.AreEqual(1, lookup.Count);
     Assert.AreEqual(2, lookup["a"].Count());
 }
Ejemplo n.º 8
0
 public void EditableLookupNullComparer()
 {
     var lookup = new EditableLookup<string, int>(null);
     lookup.Add("a", 1);
     lookup.Add("a", 1);
     Assert.IsTrue(lookup.Contains("a"));
     Assert.IsFalse(lookup.Contains("A"));
     Assert.AreEqual(1, lookup.Count);
     Assert.AreEqual(2, lookup["a"].Count());
 }
Ejemplo n.º 9
0
        public void EditableLookupNullComparer()
        {
            var lookup = new EditableLookup <string, int>(null);

            lookup.Add("a", 1);
            lookup.Add("a", 1);
            Assert.IsTrue(lookup.Contains("a"));
            Assert.IsFalse(lookup.Contains("A"));
            Assert.AreEqual(1, lookup.Count);
            Assert.AreEqual(2, lookup["a"].Count());
        }
Ejemplo n.º 10
0
        public void EditableLookupComparer()
        {
            var lookup = new EditableLookup <string, int>(StringComparer.OrdinalIgnoreCase);

            lookup.Add("a", 1);
            lookup.Add("A", 1);
            Assert.IsTrue(lookup.Contains("a"));
            Assert.IsTrue(lookup.Contains("A"));
            Assert.AreEqual(1, lookup.Count);
            Assert.AreEqual(2, lookup["a"].Count());
        }
Ejemplo n.º 11
0
 public void EditableLookupBasic()
 {
     var lookup = new EditableLookup<string, int>();
     lookup.Add("a", 1);
     lookup.Add("a", 1);
     Assert.IsTrue(lookup.Contains("a"));
     Assert.IsFalse(lookup.Contains("A"));
     Assert.AreEqual(1, lookup.Count);
     Assert.AreEqual(2, lookup["a"].Count());
     Assert.AreEqual(0, lookup["foo"].Count());
 }
Ejemplo n.º 12
0
 public void EditableLookupAddRangeEmptyKey()
 {
     var lookup = new EditableLookup<string, int>();
     int[] data = new int[0];
     lookup.Add("foo", 1);
     Assert.AreEqual(1, lookup.Count);
     lookup.AddRange("bar", data);
     // want to check that empty insert hasn't left
     // a rogue group
     Assert.AreEqual(1, lookup.Count);
     Assert.IsFalse(lookup.Contains("bar"));
 }
Ejemplo n.º 13
0
        public void EditableLookupBasic()
        {
            var lookup = new EditableLookup <string, int>();

            lookup.Add("a", 1);
            lookup.Add("a", 1);
            Assert.IsTrue(lookup.Contains("a"));
            Assert.IsFalse(lookup.Contains("A"));
            Assert.AreEqual(1, lookup.Count);
            Assert.AreEqual(2, lookup["a"].Count());
            Assert.AreEqual(0, lookup["foo"].Count());
        }
Ejemplo n.º 14
0
        public void EditableLookupAddRangeEmptyKey()
        {
            var lookup = new EditableLookup <string, int>();

            int[] data = new int[0];
            lookup.Add("foo", 1);
            Assert.AreEqual(1, lookup.Count);
            lookup.AddRange("bar", data);
            // want to check that empty insert hasn't left
            // a rogue group
            Assert.AreEqual(1, lookup.Count);
            Assert.IsFalse(lookup.Contains("bar"));
        }
Ejemplo n.º 15
0
        public void EditableLookupEnumerator()
        {
            var lookup = new EditableLookup <int, int>();

            lookup.Add(1, 1); // multipliers....
            lookup.Add(1, 2);
            lookup.Add(1, 3);
            lookup.Add(2, 4);
            lookup.Add(3, 5);
            int sum = lookup.Sum(x => x.Key * x.Sum());

            Assert.AreEqual(6 + 8 + 15, sum);
        }
Ejemplo n.º 16
0
        public void EditableLookupAddRangeKey()
        {
            var lookup = new EditableLookup<string, int>();
            lookup.Add("a", 1);
            lookup.Add("b", 2);
            // add to existing key
            lookup.AddRange("a", new int[] { 3, 4, 5 });
            // add to new key
            lookup.AddRange("c", new int[] { 6, 7});

            Assert.AreEqual(3, lookup.Count);
            Assert.AreEqual(13, lookup["a"].Sum());
            Assert.AreEqual(13, lookup["c"].Sum());
        }
Ejemplo n.º 17
0
        private IEnumerable <KeyValuePair <string, int?> > GetPipelineSample()
        {
            var lookup = new EditableLookup <string, int?>();

            lookup.AddRange("foo", new int?[] { 1, 1, null, 1 });
            lookup.AddRange("bar", new int?[] { 2, 2, 2 });
            lookup.AddRange("test", new int?[] { 9, null, 3 });
            foreach (var group in lookup)
            {
                foreach (var item in group)
                {
                    yield return(new KeyValuePair <string, int?>(group.Key, item));
                }
            }
        }
        /// <summary>
        /// Converts an IDataProducer into a lookup.
        /// </summary>
        /// <param name="elementSelector">A transform function to produce a result element value from each element.</param>
        /// <param name="keySelector">A function to extract a key from each element.</param>
        /// <param name="keyComparer">Used to compare keys.</param>
        /// <param name="source">The data source.</param>
        /// <remarks>This will force all values to be buffered</remarks>
        public static ILookup <TKey, TElement> ToLookup <TSource, TKey, TElement>(
            this IDataProducer <TSource> source,
            Func <TSource, TKey> keySelector, Func <TSource, TElement> elementSelector, IEqualityComparer <TKey> keyComparer)
        {
            source.ThrowIfNull("source");
            keySelector.ThrowIfNull("keySelector");
            elementSelector.ThrowIfNull("elementSelector");
            keyComparer.ThrowIfNull("keyComparer");

            EditableLookup <TKey, TElement> lookup = new EditableLookup <TKey, TElement>(keyComparer);

            source.DataProduced += t => lookup.Add(keySelector(t), elementSelector(t));
            source.EndOfData    += () => lookup.TrimExcess();
            return(lookup);
        }
Ejemplo n.º 19
0
        public void EditableLookupAddRangeKey()
        {
            var lookup = new EditableLookup <string, int>();

            lookup.Add("a", 1);
            lookup.Add("b", 2);
            // add to existing key
            lookup.AddRange("a", new int[] { 3, 4, 5 });
            // add to new key
            lookup.AddRange("c", new int[] { 6, 7 });

            Assert.AreEqual(3, lookup.Count);
            Assert.AreEqual(13, lookup["a"].Sum());
            Assert.AreEqual(13, lookup["c"].Sum());
        }
Ejemplo n.º 20
0
        public void EditableLookupAddRangeLookup()
        {
            var lookup = new EditableLookup <string, int>();

            lookup.Add("a", 1);
            lookup.Add("b", 2);

            var lookup2 = new EditableLookup <string, int>();

            lookup2.AddRange("a", new int[] { 3, 4, 5 });
            lookup2.AddRange("c", new int[] { 6, 7 });

            lookup.AddRange(lookup2);

            Assert.AreEqual(3, lookup.Count);
            Assert.AreEqual(13, lookup["a"].Sum());
            Assert.AreEqual(13, lookup["c"].Sum());
        }
Ejemplo n.º 21
0
 public void EditableLookupAddRangeNullLookup()
 {
     var lookup = new EditableLookup<string, int>();
     ILookup<string, int> mate = null;
     lookup.AddRange(mate);
 }
Ejemplo n.º 22
0
        public void EditableLookupUntypedEnumerator()
        {
            var lookup = new EditableLookup<int, int>();
            lookup.Add(1, 1); // multipliers....
            lookup.Add(1, 2);
            lookup.Add(1, 3);
            lookup.Add(1, 4);
            lookup.Add(2, 5);
            lookup.Add(3, 6);

            // test the lookup enumerator
            IEnumerable e = lookup;
            int i = 0;
            foreach (object obj in e)
            {
                i++;
            }
            Assert.AreEqual(3, i);

            // test the group enumerator
            e = lookup[1];
            i = 0;
            foreach (object obj in e)
            {
                i++;
            }
            Assert.AreEqual(4, i);
        }
Ejemplo n.º 23
0
 public void EditableLookupAddRangeNullKey()
 {
     var lookup = new EditableLookup<string, int>();
     IEnumerable<int> mate = null;
     lookup.AddRange("foo", mate);
 }
Ejemplo n.º 24
0
        public void EditableLookupAddRangeLookup()
        {
            var lookup = new EditableLookup<string, int>();
            lookup.Add("a", 1);
            lookup.Add("b", 2);

            var lookup2 = new EditableLookup<string, int>();
            lookup2.AddRange("a", new int[] { 3, 4, 5 });
            lookup2.AddRange("c", new int[] { 6, 7 });

            lookup.AddRange(lookup2);

            Assert.AreEqual(3, lookup.Count);
            Assert.AreEqual(13, lookup["a"].Sum());
            Assert.AreEqual(13, lookup["c"].Sum());
        }
Ejemplo n.º 25
0
 public InMemoryDataStore()
 {
     data =
         new EditableLookup<string, string>(
             StringComparer.InvariantCultureIgnoreCase);
 }
Ejemplo n.º 26
0
        public void EditableLookupRemove()
        {
            var lookup = new EditableLookup<string, int>();
            lookup.Add("a", 1);
            lookup.Add("a", 2);
            lookup.Add("b", 3);
            lookup.Add("a", 4);
            Assert.IsTrue(lookup.Contains("a"));
            Assert.IsTrue(lookup.Contains("b"));
            Assert.IsTrue(lookup.Contains("b", 3));
            Assert.IsFalse(lookup.Contains("b", 17));

            Assert.IsTrue(lookup.Remove("b"));
            Assert.IsFalse(lookup.Remove("b")); // no longer there
            Assert.AreEqual(1, lookup.Count);
            Assert.AreEqual(3, lookup["a"].Count());
            Assert.IsTrue(lookup.Remove("a", 1));
            Assert.IsFalse(lookup.Remove("a", 1)); // no longer there
            Assert.IsFalse(lookup.Remove("foo", 99)); // never existed

            Assert.AreEqual(6, lookup["a"].Sum()); // 2 + 4
            lookup.Remove("a", 2);
            lookup.Remove("a", 4);
            Assert.IsFalse(lookup.Contains("a"));
            Assert.AreEqual(0, lookup.Count);            
        }
Ejemplo n.º 27
0
 public InMemoryDataStore()
 {
     data =
         new EditableLookup <string, string>(
             StringComparer.InvariantCultureIgnoreCase);
 }
Ejemplo n.º 28
0
 public void EditableLookupEnumerator()
 {
     var lookup = new EditableLookup<int, int>();
     lookup.Add(1, 1); // multipliers....
     lookup.Add(1, 2);
     lookup.Add(1, 3);
     lookup.Add(2, 4);
     lookup.Add(3, 5);
     int sum = lookup.Sum(x => x.Key * x.Sum());
     Assert.AreEqual(6 + 8 + 15, sum);
 }