Exemplo n.º 1
0
        public void CanReadValuesAndNodesThroughAReadOnlyWrapper()
        {
            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);

            Assert.That(wrapper[2], Is.EqualTo("2"));
            Assert.That(wrapper.ContainsKey(2), Is.True);
            Assert.That(wrapper.Find(2), Is.EqualTo(tree.Find(2)));
            Assert.That(wrapper.Contains(tree.Find(2)), Is.True);

            Assert.That(wrapper.TryGetValue(2, out string value), Is.True);
            Assert.That(value, Is.EqualTo("2"));

            Assert.That(wrapper.FindOrInsert(2, null, out RedBlackTreeNode <int, string> node), Is.False);
            Assert.That(node, Is.EqualTo(tree.Find(2)));

            Assert.That(wrapper.ContainsKey(6), Is.False);
            Assert.That(wrapper.TryGetValue(6, out value), Is.False);
            Assert.That(wrapper.Contains(tree.Find(6)), Is.False);
        }
Exemplo n.º 2
0
        public void CannotMutateTheTreeThroughAReadOnlyWrapper()
        {
            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);

            Assert.Throws <ImmutableException>(() => wrapper.Add(6, "6"));
            Assert.Throws <ImmutableException>(() => wrapper.Add(new RedBlackTreeNode <int, string>(6, "6")));
            Assert.Throws <ImmutableException>(() => wrapper.Clear());
            Assert.Throws <ImmutableException>(() => wrapper.DeleteNode(wrapper.MinimumNode));
            Assert.Throws <ImmutableException>(() => wrapper.FindOrInsert(6, "6", out RedBlackTreeNode <int, string> newNode));
            Assert.Throws <ImmutableException>(() => wrapper.Remove(5));
            Assert.Throws <ImmutableException>(() => wrapper.Remove(wrapper.MinimumNode));
            Assert.Throws <ImmutableException>(() => wrapper.RemoveRange(1, 3));
            Assert.Throws <ImmutableException>(() => wrapper[2] = "foo");
            Assert.Throws <ImmutableException>(() => wrapper[6] = "bar");
        }