public void CanReadThroughAsDictionary() { RedBlackTree <int, string> tree = new RedBlackTree <int, string> { { 1, "1" }, { 2, "2" }, { 3, "3" }, { 4, "4" }, { 5, "5" }, }; ReadOnlyRedBlackTree <int, string> wrapper = new ReadOnlyRedBlackTree <int, string>(tree); IDictionary <int, string> dictionary = wrapper.AsDictionary(); Assert.That(dictionary[2], Is.EqualTo("2")); Assert.That(dictionary.Count, Is.EqualTo(5)); Assert.That(dictionary.Contains(new KeyValuePair <int, string>(2, "2")), Is.True); Assert.That(dictionary.Contains(new KeyValuePair <int, string>(2, "6")), Is.False); Assert.That(dictionary.Contains(new KeyValuePair <int, string>(6, "2")), Is.False); Assert.That(dictionary.Contains(new KeyValuePair <int, string>(6, "6")), Is.False); Assert.That(dictionary.Contains(new KeyValuePair <int, string>(2, null)), Is.False); Assert.That(dictionary.ContainsKey(2), Is.True); Assert.That(dictionary.ContainsKey(6), Is.False); Assert.That(dictionary.IsReadOnly, Is.True); Assert.That(dictionary.TryGetValue(2, out string value), Is.True); Assert.That(value, Is.EqualTo("2")); Assert.That(dictionary.TryGetValue(6, out value), Is.False); }
public void CanReadAsCollectionThroughAsDictionary() { RedBlackTree <int, string> tree = new RedBlackTree <int, string> { { 1, "1" }, { 2, "2" }, { 3, "3" }, { 4, "4" }, { 5, "5" }, }; ReadOnlyRedBlackTree <int, string> wrapper = new ReadOnlyRedBlackTree <int, string>(tree); IDictionary <int, string> dictionary = wrapper.AsDictionary(); KeyValuePair <int, string>[] array = new KeyValuePair <int, string> [5]; dictionary.CopyTo(array, 0); CollectionAssert.AreEquivalent(array, new[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2"), new KeyValuePair <int, string>(3, "3"), new KeyValuePair <int, string>(4, "4"), new KeyValuePair <int, string>(5, "5"), }); CollectionAssert.AreEquivalent(dictionary.Keys, new[] { 1, 2, 3, 4, 5 }); CollectionAssert.AreEquivalent(dictionary.Values, new[] { "1", "2", "3", "4", "5" }); int index = 0; array = new KeyValuePair <int, string> [5]; foreach (KeyValuePair <int, string> pair in dictionary) { array[index++] = pair; } CollectionAssert.AreEquivalent(array, new[] { new KeyValuePair <int, string>(1, "1"), new KeyValuePair <int, string>(2, "2"), new KeyValuePair <int, string>(3, "3"), new KeyValuePair <int, string>(4, "4"), new KeyValuePair <int, string>(5, "5"), }); }
public void CannotMutateThroughAsDictionary() { RedBlackTree <int, string> tree = new RedBlackTree <int, string> { { 1, "1" }, { 2, "2" }, { 3, "3" }, { 4, "4" }, { 5, "5" }, }; ReadOnlyRedBlackTree <int, string> wrapper = new ReadOnlyRedBlackTree <int, string>(tree); IDictionary <int, string> dictionary = wrapper.AsDictionary(); Assert.Throws <ImmutableException>(() => dictionary[3] = "foo"); Assert.Throws <ImmutableException>(() => dictionary.Add(6, "6")); Assert.Throws <ImmutableException>(() => dictionary.Add(new KeyValuePair <int, string>(6, "6"))); Assert.Throws <ImmutableException>(() => dictionary.Clear()); Assert.Throws <ImmutableException>(() => dictionary.Remove(3)); Assert.Throws <ImmutableException>(() => dictionary.Remove(new KeyValuePair <int, string>(3, "3"))); Assert.Throws <ImmutableException>(() => dictionary.Keys.Add(6)); Assert.Throws <ImmutableException>(() => dictionary.Values.Add("bar")); }