Пример #1
0
        public void ThreeItems()
        {
            var dict = new SmallDictionary <int, string>();

            dict[2]  = "abc";
            dict[5]  = "def";
            dict[11] = "third";
            Assert.AreEqual(3, dict.Count);
            Assert.AreEqual("abc", dict[2]);
            Assert.AreEqual("def", dict[5]);
            Assert.AreEqual("third", dict[11]);
            string output;

            Assert.IsFalse(dict.TryGetValue(3, out output));
            Assert.IsNull(output);
            Assert.IsTrue(dict.TryGetValue(2, out output));
            Assert.AreEqual("abc", output);
            Assert.IsTrue(dict.TryGetValue(5, out output));
            Assert.AreEqual("def", output);
            Assert.IsTrue(dict.TryGetValue(11, out output));
            Assert.AreEqual("third", output);

            Assert.IsFalse(dict.ContainsKey(3));
            Assert.IsTrue(dict.ContainsKey(2));
            Assert.IsTrue(dict.ContainsKey(5));
            Assert.IsTrue(dict.ContainsKey(11));
        }
Пример #2
0
        public void OneItem()
        {
            var dict = new SmallDictionary <int, string>();

            dict[2] = "abc";
            Assert.AreEqual(1, dict.Count);
            Assert.AreEqual("abc", dict[2]);
            string output;

            Assert.IsFalse(dict.TryGetValue(3, out output));
            Assert.IsNull(output);
            Assert.IsTrue(dict.TryGetValue(2, out output));
            Assert.AreEqual("abc", output);
            Assert.IsFalse(dict.ContainsKey(3));
            Assert.IsTrue(dict.ContainsKey(2));
        }
Пример #3
0
        public void Remove()
        {
            var dict = new SmallDictionary <int, string>();

            Assert.IsFalse(dict.Remove(2), "Remove a missing item from an empty dictionary");
            dict.Add(2, "abc");
            Assert.IsFalse(dict.Remove(3), "Remove a missing item from a dictionary with one item");
            Assert.IsTrue(dict.Remove(2), "Remove the only item from a dictionary");
            Assert.AreEqual(0, dict.Count, "Nothing remains after removing the only item");
            dict.Add(2, "abc");
            dict.Add(5, "def");
            dict.Add(11, "third");
            Assert.IsFalse(dict.Remove(7), "Remove a missing item from a dictionary with three items");
            Assert.IsTrue(dict.Remove(2), "Remove the first item from a dictionary with three items");
            Assert.AreEqual(2, dict.Count, "Two items remain after removing the first of three");
            string output;

            Assert.IsFalse(dict.TryGetValue(2, out output), "Removed item (first) should be gone from original three");
            Assert.AreEqual("def", dict[5]);
            Assert.AreEqual("third", dict[11]);
            dict.Add(2, "abc");
            Assert.IsTrue(dict.Remove(5), "Remove first of two items in others");
            Assert.AreEqual(2, dict.Count);
            Assert.IsFalse(dict.TryGetValue(5, out output));
            Assert.AreEqual("abc", dict[2]);
            Assert.AreEqual("third", dict[11]);

            Assert.IsTrue(dict.Remove(2), "Remove only item in others");
            Assert.AreEqual(1, dict.Count);
            Assert.IsFalse(dict.TryGetValue(2, out output));
            Assert.AreEqual("third", dict[11]);

            Assert.IsTrue(dict.Remove(11), "Remove only item in dictionary which previously had three");
            Assert.AreEqual(0, dict.Count);
            Assert.IsFalse(dict.TryGetValue(11, out output));

            dict.Add(2, "abc");
            dict.Add(5, "def");
            dict.Add(11, "third");
            dict.Add(13, "fourth");
            Assert.IsTrue(dict.Remove(11), "Remove middle of three items in others");
            Assert.AreEqual(3, dict.Count);
            Assert.IsFalse(dict.TryGetValue(11, out output));
            Assert.AreEqual("abc", dict[2]);
            Assert.AreEqual("def", dict[5]);
            Assert.AreEqual("fourth", dict[13]);

            Assert.IsTrue(dict.Remove(13), "Remove last of two items in others");
            Assert.AreEqual(2, dict.Count);
            Assert.IsFalse(dict.TryGetValue(13, out output));
            Assert.AreEqual("abc", dict[2]);
            Assert.AreEqual("def", dict[5]);

            Assert.IsTrue(dict.Remove(2), "Remove first item when others contains exactly one");
            Assert.AreEqual(1, dict.Count);
            Assert.IsFalse(dict.TryGetValue(2, out output));
            Assert.AreEqual("def", dict[5]);
        }
Пример #4
0
        public void Empty()
        {
            var dict = new SmallDictionary <int, string>();

            Assert.AreEqual(0, dict.Count);
            string output;

            Assert.IsFalse(dict.TryGetValue(2, out output));
            Assert.IsNull(output);
            Assert.IsFalse(dict.ContainsKey(3));
        }