public void ValuesWorks() { var d = new MyReadOnlyDictionary(new Dictionary <int, string> { { 3, "b" }, { 6, "z" }, { 9, "x" } }); var actualValues = new string[] { "b", "z", "x" }; var values = d.Values; int i = 0; Assert.True(values is IEnumerable <string>); foreach (var val in values) { Assert.AreEqual(actualValues[i], val); i++; } Assert.AreEqual(actualValues.Length, i); values = ((IReadOnlyDictionary <int, string>)d).Values; Assert.True(values is IEnumerable <string>); i = 0; foreach (var val in values) { Assert.AreEqual(actualValues[i], val); i++; } Assert.AreEqual(actualValues.Length, i); }
public void GetItemWorks() { var d = new MyReadOnlyDictionary(new Dictionary <int, string> { { 3, "b" }, { 6, "z" }, { 9, "x" } }); var di = (IReadOnlyDictionary <int, string>)d; Assert.AreEqual("b", d[3]); Assert.AreEqual("z", di[6]); try { var x = d[1]; Assert.Fail("Should throw"); } catch (Exception) { } try { var x = di[1]; Assert.Fail("Should throw"); } catch (Exception) { } }
public void KeysWorks() { var d = new MyReadOnlyDictionary(new Dictionary <int, string> { { 3, "b" }, { 6, "z" }, { 9, "x" } }); var actualKeys = new int[] { 3, 6, 9 }; var keys = d.Keys; Assert.True(keys is IEnumerable <int>); int i = 0; foreach (var key in keys) { Assert.AreEqual(actualKeys[i], key); i++; } Assert.AreEqual(actualKeys.Length, i); keys = ((IReadOnlyDictionary <int, string>)d).Keys; Assert.True(keys is IEnumerable <int>); i = 0; foreach (var key in keys) { Assert.AreEqual(actualKeys[i], key); i++; } Assert.AreEqual(actualKeys.Length, i); }
public void CountWorks() { var d = new MyReadOnlyDictionary(); Assert.AreEqual(0, d.Count); var d2 = new MyReadOnlyDictionary(new Dictionary <int, string> { { 3, "c" } }); Assert.AreEqual(1, d2.Count); }
public void ContainsKeyWorks() { var d = new MyReadOnlyDictionary(new Dictionary <int, string> { { 3, "b" }, { 6, "z" }, { 9, "x" } }); var di = (IReadOnlyDictionary <int, string>)d; Assert.True(d.ContainsKey(6)); Assert.True(di.ContainsKey(3)); Assert.False(d.ContainsKey(6123)); Assert.False(di.ContainsKey(32)); }
public void TryGetValueWorks() { var d = new MyReadOnlyDictionary(new Dictionary <int, string> { { 3, "b" }, { 6, "z" }, { 9, "x" } }); var di = (IReadOnlyDictionary <int, string>)d; string outVal; Assert.True(d.TryGetValue(6, out outVal)); Assert.AreEqual("z", outVal); Assert.True(di.TryGetValue(3, out outVal)); Assert.AreEqual("b", outVal); outVal = "!!!"; Assert.False(d.TryGetValue(6123, out outVal)); Assert.AreEqual(null, outVal); outVal = "!!!"; Assert.False(di.TryGetValue(32, out outVal)); Assert.AreEqual(null, outVal); }
public void SerializeEnumerable() { var buffer = new byte[4096]; var sb = new StringBuilder(); int failed = 0; failed += SerializeValues <IEnumerable <int> >(sb, buffer, true, null, new List <int> { }, new List <int> { 1 }, new List <int> { 1, 2 }, new int[] { }, new int[] { 2 }, new int[] { 2, 4 }, Enumerable.Empty <int>(), Enumerable.Repeat(3, 0), Enumerable.Repeat(3, 1), Enumerable.Repeat(3, 2), Enumerable.Repeat(3, 3), null); failed += SerializeValues(sb, buffer, true, new List <int> { }, new List <int> { 1 }, new List <int> { 1, 2 }, new List <int> { 1, 2, 3 }); failed += SerializeValues <IList <int?> >(sb, buffer, false, new int?[] { }, new List <int?> { null }, new int?[] { 1 }, new List <int?> { 1, null }, new int?[] { 1, 2 }, new List <int?> { 1, 2, null }, new int?[] { 1, 2, 3 }, new List <int?> { 1, 2, 3, null }); failed += SerializeValues <IReadOnlyCollection <int?> >(sb, buffer, false, new int?[] { }, new List <int?> { null }.AsReadOnly(), new int?[] { 1 }, new List <int?> { 1, null }, new int?[] { 1, 2 }, new List <int?> { 1, 2, null }, new int?[] { 1, 2, 3 }, new List <int?> { 1, 2, 3, null }); failed += SerializeValues(sb, buffer, false, new List <int?> { null }.AsReadOnly()); // Newtonsoft thinks this should be an object not an array :( //failed += SerializeValues<IEnumerable<KeyValuePair<string, int>>>(sb, buffer, true, // new Dictionary<string, int> { }, // new Dictionary<string, int> { { "0", 0 } }, // new Dictionary<string, int> { { "0", 0 }, { "1", 1 } }, // null); failed += SerializeValues(sb, buffer, true, new Dictionary <string, int> { }, new Dictionary <string, int> { { "0", 0 } }, new Dictionary <string, int> { { "0", 0 }, { "1", 1 } }, null); failed += SerializeValues(sb, buffer, false, MyReadOnlyDictionary <string, int> .Create(new Dictionary <string, int> { }), MyReadOnlyDictionary <string, int> .Create(new Dictionary <string, int> { { "0", 0 } }), MyReadOnlyDictionary <string, int> .Create(new Dictionary <string, int> { { "0", 0 }, { "1", 1 } }), null); failed += SerializeValues(sb, buffer, false, new Dictionary <char, int> { }, new Dictionary <char, int> { { '0', 0 } }, new Dictionary <char, int> { { '0', 0 }, { '1', 1 } }, null); failed += SerializeValues(sb, buffer, false, new Dictionary <Guid, int[]> { }, new Dictionary <Guid, int[]> { { Guid.Empty, null } }, new Dictionary <Guid, int[]> { { Guid.Empty, new[] { 0 } }, { Guid.Parse("838f5c03-ed56-41e1-9264-686bb18d0a77"), new[] { 1, 2 } } }, null); ApprovalTests.Approvals.Verify(sb.ToString()); Assert.True(failed == 0, "Look at the approval " + failed + " tests failed"); }