Пример #1
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]);
        }
Пример #2
0
        public void Add()
        {
            var dict = new SmallDictionary <int, string>();

            dict.Add(2, "abc");
            Assert.AreEqual(1, dict.Count);
            dict.Add(5, "def");
            Assert.AreEqual(2, dict.Count);
            dict.Add(11, "third");
            Assert.AreEqual(3, dict.Count);
            Assert.AreEqual("abc", dict[2]);
            Assert.AreEqual("def", dict[5]);
            Assert.AreEqual("third", dict[11]);
        }
Пример #3
0
        public void GetMissingKeyOne()
        {
            var dict = new SmallDictionary <int, string>();

            dict.Add(2, "abc");
            var temp = dict[3];
        }
Пример #4
0
        public void AddZeroKey()
        {
            var dict = new SmallDictionary <int, string>();

            Assert.That(() => dict.Add(0, "abc"),
                        Throws.TypeOf <ArgumentException>());
        }
Пример #5
0
        public void KeysAndValues()
        {
            var dict = new SmallDictionary <int, string>();

            VerifyIntArrays(new int[0], dict.Keys.ToArray());
            VerifyStringArrays(new string[0], dict.Values.ToArray());

            dict.Add(2, "abc");
            VerifyIntArrays(new int[] { 2 }, dict.Keys.ToArray());
            VerifyStringArrays(new string[] { "abc" }, dict.Values.ToArray());

            // This test is too strict, it enforces a particular order of the results.
            dict.Add(5, "def");
            dict.Add(11, "third");
            VerifyIntArrays(new int[] { 2, 5, 11 }, dict.Keys.ToArray());
            VerifyStringArrays(new string[] { "abc", "def", "third" }, dict.Values.ToArray());
        }
Пример #6
0
        public void GetMissingKeyOne()
        {
            var dict = new SmallDictionary <int, string>();

            dict.Add(2, "abc");
            Assert.That(() => dict[3],
                        Throws.TypeOf <KeyNotFoundException>());
        }
Пример #7
0
        public void Enumerator()
        {
            var dict = new SmallDictionary <int, string>();

            foreach (var kvp in dict)
            {
                Assert.Fail("Should get no iterations looping over empty dictionary");
            }
            dict.Add(2, "abc");
            int count = 0;

            foreach (var kvp in dict)
            {
                count++;
                Assert.AreEqual(2, kvp.Key);
                Assert.AreEqual("abc", kvp.Value);
            }
            Assert.AreEqual(1, count);

            dict.Add(5, "def");
            dict.Add(11, "third");
            count = 0;
            Dictionary <int, string> normalDict = new Dictionary <int, string>(dict);

            Assert.AreEqual("abc", normalDict[2]);
            Assert.AreEqual("def", normalDict[5]);
            Assert.AreEqual("third", normalDict[11]);
            Assert.AreEqual(3, normalDict.Count);
            foreach (var kvp in dict)
            {
                count++;
                Assert.IsTrue(normalDict.ContainsKey(kvp.Key));
                Assert.AreEqual(normalDict[kvp.Key], kvp.Value);
                normalDict.Remove(kvp.Key);
            }
            Assert.AreEqual(3, count);
        }
Пример #8
0
        public void AddZeroKey()
        {
            var dict = new SmallDictionary <int, string>();

            dict.Add(0, "abc");
        }