private void MultiValueMapAddSingleValue(IMultiValueMap <int, Bill> mvMap) { Fill(mvMap, 10000, 50, x => x, valueGen); Assert.Equal(10000 * 50, mvMap.Count); Assert.Equal(10000, mvMap.Keys.Count); Assert.Equal(10000 * 50, mvMap.Values.Count); foreach (var item in mvMap.Keys) { Assert.Equal(50, mvMap[item].Count); Assert.Equal(50, mvMap.CountOf(item)); } Assert.False(mvMap.ContainsKey(10000)); for (var i = 0; i < 10000; i++) { for (var j = 0; j < 50; j++) { var value = valueGen(i, j); Assert.True(mvMap.ContainsValue(i, value)); } Assert.False(mvMap.ContainsValue(i, new Bill { Id = (i * 50) + 10000 + 51 })); } }
private static void Fill <K, V>(IMultiValueMap <K, V> mvMap, int keyCount, int valueCount, Func <int, K> keyGen, Func <int, int, V> valueGen) { for (var i = 0; i < keyCount; i++) { for (var j = 0; j < valueCount; j++) { mvMap.Add(keyGen(i), valueGen(i, j)); } } }
public void GivenTwoArraysWithLongerValArrayWhenZippedToMultiValueMapThenAllExpectedMappingsAreInMap() { int[] keys = new int[] { 1, 2, 1 }; string[] vals = new string[] { "One", "Two", "Three", "dasfash", "One" }; IMultiValueMap <int, string> zipped = keys.ZipToMultiValueMap(vals); Assert.AreEqual(2, zipped.Count, "Expected the resultant array count to be equal to the minimum of the key & value array counts"); Assert.AreEqual(2, zipped[1].Count); Assert.AreEqual(1, zipped[2].Count); }
public MultiValueHashedMap(IMultiValueMap <K, V> items, IEqualityComparer <K> keyComparer, IEqualityComparer <V> valueComparer) : base(valueComparer) { map = new HashedMap <K, ICollection <V> >( items == null ? DefaultCapacity : items.KeyCount, keyComparer); if (items != null) { foreach (var item in items) { Add(item.Key, item.Value); } } }
private MultiValueTreeMap(IMultiValueMap <K, V> items, TreeMap <K, ICollection <V> > map, Equator <V> valueEquator) : base(new EquatorComparer <V>(valueEquator)) { this.map = map; if (items != null) { foreach (var item in items) { Add(item.Key, item.Value); } } }
private void MultiValueMapOperations(IMultiValueMap <int, int> mvMap) { Fill(mvMap, 10000, 50, x => x, (y, z) => y * z); Assert.Equal(10000, mvMap.Keys.Count); for (var i = 0; i < 10000; i++) { Assert.Equal(50, mvMap.CountOf(i)); } for (var i = 3000; i < 5000; i++) { Assert.True(mvMap.Remove(i)); } Assert.Equal(8000, mvMap.Keys.Count); Assert.Equal(8000, mvMap.KeyCount); }
public void GivenMap_WhenTransform_ThenResultIsOfCorrectLengthAndContainsExpectedMappings() { IMultiValueMap <int, string> testMap = new MultiValueMapImpl <int, string>(); testMap.Add(1, "Unity"); testMap.Add(1, "One"); testMap.Add(2, "Two"); //Intentionally add a value that'll map to the same as another value testMap.Add(2, "Dha"); testMap.Add(3, "Three"); IMultiValueMap <int, int> transformedMap = testMap.TransformValues(s => s.Length); Assert.AreEqual(3, transformedMap.Count, "Transformed map should be the same length as original"); Assert.IsTrue(new HashSet <int>(new int[] { 5, 3 }).SetEquals(transformedMap[1])); Assert.IsTrue(new HashSet <int>(new int[] { 3 }).SetEquals(transformedMap[2])); Assert.IsTrue(new HashSet <int>(new int[] { 5 }).SetEquals(transformedMap[3])); }
private void MultiValueMapAddSingleValueAndCollectionValue(IMultiValueMap <int, Bill> mvMap) { Fill(mvMap, 10000, 50, x => x, valueGen); for (var i = 1000; i < 2000; i++) { var bills = new List <Bill>(); for (var j = 0; j < 20; j++) { bills.Add(new Bill { Id = j }); } mvMap.Add(i, bills); } for (var i = 0; i < 1000; i++) { Assert.Equal(50, mvMap.CountOf(i)); } for (var i = 1000; i < 2000; i++) { Assert.Equal(70, mvMap.CountOf(i)); Assert.Equal(70, mvMap[i].Count); } for (var i = 2000; i < 3000; i++) { Assert.Equal(50, mvMap.CountOf(i)); } for (var i = 1000; i < 2000; i++) { for (var j = 0; j < 5; j++) { mvMap.Add(i, new Bill { Id = j + 20 }); } } for (var i = 0; i < 1000; i++) { Assert.Equal(50, mvMap.CountOf(i)); } for (var i = 1000; i < 2000; i++) { Assert.Equal(75, mvMap.CountOf(i)); Assert.Equal(75, mvMap[i].Count); } for (var i = 2000; i < 3000; i++) { Assert.Equal(50, mvMap.CountOf(i)); } for (var i = 1000; i < 2000; i++) { for (var j = 0; j < 25; j++) { Assert.True(mvMap.ContainsValue(i, new Bill { Id = j })); } } }
private void MultiValueMapRemove(IMultiValueMap <int, Bill> mvMap) { Assert.Equal(0, mvMap.Keys.Count); Assert.Equal(0, mvMap.Count); Fill(mvMap, 10000, 50, x => x, valueGen); var total = 0; foreach (var item in mvMap) { total++; } Assert.Equal(10000 * 50, total); List <Bill> billList; Assert.False(mvMap.TryGetValue(10000, out billList)); for (var i = 5000; i < 8000; i++) { List <Bill> bills; Assert.True(mvMap.TryGetValue(i, out bills)); Assert.Equal(50, bills.Count); for (var j = 0; j < 50; j++) { Assert.True(mvMap.ContainsValue(i, valueGen(i, j))); } } for (var i = 5000; i < 8000; i++) { Assert.True(mvMap.Remove(i)); } Assert.Equal(7000, mvMap.Keys.Count); Assert.Equal(10000 * 50 - 3000 * 50, mvMap.Count); for (var i = 5000; i < 8000; i++) { Assert.Throws(typeof(KeyNotFoundException), () => mvMap[i]); Assert.False(mvMap.ContainsKey(i)); Assert.Throws(typeof(KeyNotFoundException), () => mvMap.CountOf(i)); for (var j = 0; j < 50; j++) { Assert.False(mvMap.ContainsValue(i, valueGen(i, j))); } } for (var i = 2000; i < 3000; i++) { for (var j = 10; j < 20; j++) { Assert.True(mvMap.RemoveValue(i, valueGen(i, j))); } } Assert.Equal(7000, mvMap.Keys.Count); for (var i = 2000; i < 3000; i++) { Assert.Equal(40, mvMap.CountOf(i)); for (var j = 0; j < 10; j++) { Assert.True(mvMap.ContainsValue(i, valueGen(i, j))); } for (var j = 10; j < 20; j++) { Assert.False(mvMap.ContainsValue(i, valueGen(i, j))); } for (var j = 20; j < 50; j++) { Assert.True(mvMap.ContainsValue(i, valueGen(i, j))); } } Assert.Equal(50, mvMap.CountOf(1999)); Assert.Equal(50, mvMap.CountOf(3000)); mvMap.Clear(); Assert.Equal(0, mvMap.Keys.Count); Assert.Equal(0, mvMap.Count); Assert.Equal(0, mvMap.Values.Count); }
public MultiValueHashedMap(IMultiValueMap <K, V> items, Equator <K> keyEquator, Equator <V> valueEquator) : this(items, new EquatorComparer <K>(keyEquator), new EquatorComparer <V>(valueEquator)) { }
public MultiValueHashedMap(IMultiValueMap <K, V> items) : this(items, EqualityComparer <K> .Default, EqualityComparer <V> .Default) { }
public MultiValueTreeMap(IMultiValueMap <K, V> mvMap, Comparison <K> keyComparer, Equator <V> valueEquator) : this(mvMap, new TreeMap <K, ICollection <V> >(keyComparer), valueEquator) { }
public MultiValueTreeMap(IMultiValueMap <K, V> mvMap) : this(mvMap, Comparer <K> .Default.Compare, EqualityComparer <V> .Default.Equals) { }