public void Add_KeyAlreadyExists() { var map = new MapField <string, string>(); map.Add("foo", "bar"); Assert.Throws <ArgumentException>(() => map.Add("foo", "baz")); }
public void EqualityHandlesNullValues() { var map1 = new MapField <string, ForeignMessage>(); map1.Add("a", new ForeignMessage { C = 10 }); map1.Add("b", null); var map2 = new MapField <string, ForeignMessage>(); map2.Add("a", new ForeignMessage { C = 10 }); map2.Add("b", null); EqualityTester.AssertEquality(map1, map2); // Check the null value isn't ignored entirely... Assert.IsTrue(map1.Remove("b")); EqualityTester.AssertInequality(map1, map2); map1.Add("b", new ForeignMessage()); EqualityTester.AssertInequality(map1, map2); map1["b"] = null; EqualityTester.AssertEquality(map1, map2); }
private void TestNullValues <T>(T nonNullValue) { var map = new MapField <int, T>(); var nullValue = (T)(object)null; Assert.Throws <ArgumentNullException>(() => map.Add(0, nullValue)); Assert.Throws <ArgumentNullException>(() => map[0] = nullValue); map.Add(1, nonNullValue); map[1] = nonNullValue; }
public void AddPreservesInsertionOrder() { var map = new MapField <string, string>(); map.Add("a", "v1"); map.Add("b", "v2"); map.Add("c", "v3"); map.Remove("b"); map.Add("d", "v4"); CollectionAssert.AreEqual(new[] { "a", "c", "d" }, map.Keys); CollectionAssert.AreEqual(new[] { "v1", "v3", "v4" }, map.Values); }
public void EqualityIsKeySensitive() { var map1 = new MapField <string, string>(); map1.Add("first key", "v1"); map1.Add("second key", "v2"); var map2 = new MapField <string, string>(); map2.Add("third key", "v1"); map2.Add("fourth key", "v2"); EqualityTester.AssertInequality(map1, map2); }
public void EqualityIsOrderInsensitive() { var map1 = new MapField <string, string>(); map1.Add("a", "v1"); map1.Add("b", "v2"); var map2 = new MapField <string, string>(); map2.Add("b", "v2"); map2.Add("a", "v1"); EqualityTester.AssertEquality(map1, map2); }
public void EqualityIsValueSensitive() { // Note: Without some care, it's a little easier than one might // hope to see hash collisions, but only in some environments... var map1 = new MapField <string, string>(); map1.Add("a", "first value"); map1.Add("b", "second value"); var map2 = new MapField <string, string>(); map2.Add("a", "third value"); map2.Add("b", "fourth value"); EqualityTester.AssertInequality(map1, map2); }
public void Add_IEnumerable() { var map1 = new MapField <string, string> { { "x", "y" }, { "a", "b" } }; IReadOnlyDictionary <string, string> map2 = new Dictionary <string, string> { { "before", "" }, { "after", "" } }; var expected = new MapField <string, string> { { "x", "y" }, { "a", "b" }, { "before", "" }, { "after", "" }, }; map1.Add(map2); Assert.AreEqual(expected, map1); CollectionAssert.AreEqual(new[] { "x", "a", "before", "after" }, map1.Keys); }
/// <summary> /// Creates a deep clone of this object. /// </summary> /// <returns> /// A deep clone of this object. /// </returns> public MapField <TKey, TValue> Clone() { var clone = new MapField <TKey, TValue>(); // Keys are never cloneable. Values might be. if (typeof(IDeepCloneable <TValue>).IsAssignableFrom(typeof(TValue))) { foreach (var pair in list) { clone.Add(pair.Key, ((IDeepCloneable <TValue>)pair.Value).Clone()); } } else { // Nothing is cloneable, so we don't need to worry. clone.Add(this); } return(clone); }
public void CopyTo_Pair() { var map = new MapField <string, string>(); map.Add("foo", "bar"); ICollection <KeyValuePair <string, string> > collection = map; KeyValuePair <string, string>[] array = new KeyValuePair <string, string> [3]; collection.CopyTo(array, 1); Assert.AreEqual(NewKeyValuePair("foo", "bar"), array[1]); }
public void Clear() { var map = new MapField <string, string> { { "x", "y" } }; Assert.AreEqual(1, map.Count); map.Clear(); Assert.AreEqual(0, map.Count); map.Add("x", "y"); Assert.AreEqual(1, map.Count); }
public void Remove_Key() { var map = new MapField <string, string>(); map.Add("foo", "bar"); Assert.AreEqual(1, map.Count); Assert.IsFalse(map.Remove("missing")); Assert.AreEqual(1, map.Count); Assert.IsTrue(map.Remove("foo")); Assert.AreEqual(0, map.Count); Assert.Throws <ArgumentNullException>(() => map.Remove(null)); }
public void Remove_Pair() { var map = new MapField <string, string>(); map.Add("foo", "bar"); ICollection <KeyValuePair <string, string> > collection = map; Assert.AreEqual(1, map.Count); Assert.IsFalse(collection.Remove(NewKeyValuePair("wrong key", "bar"))); Assert.AreEqual(1, map.Count); Assert.IsFalse(collection.Remove(NewKeyValuePair("foo", "wrong value"))); Assert.AreEqual(1, map.Count); Assert.IsTrue(collection.Remove(NewKeyValuePair("foo", "bar"))); Assert.AreEqual(0, map.Count); Assert.Throws <ArgumentException>(() => collection.Remove(new KeyValuePair <string, string>(null, ""))); }
public void Add_ForbidsNullKeys() { var map = new MapField <string, ForeignMessage>(); Assert.Throws <ArgumentNullException>(() => map.Add(null, new ForeignMessage())); }