示例#1
0
        public static void LinkedDictionaryTest()
        {
            LinkedDictionary <string, int> ldEmpty = new LinkedDictionary <string, int>();
            LinkedDictionary <string, int> a       = new LinkedDictionary <string, int>();

            a.Add("a1", 1);
            a.Add("a12", 12);
            a.Add("a123", 123);
            a.Add("a4", 4);
            a.Add("a5", 5);

            #region Pseudo-LINQ-Extensions
            Assert.AreEqual("a1", a.First().Key);
            Assert.AreEqual(1, a.First().Value);
            Assert.AreEqual("a5", a.Last().Key);
            Assert.AreEqual(5, a.Last().Value);
            Assert.Throws <InvalidOperationException>(() => ldEmpty.First());
            Assert.Throws <InvalidOperationException>(() => ldEmpty.Last());
            Assert.DoesNotThrow(() => ldEmpty.FirstOrDefault());
            Assert.DoesNotThrow(() => ldEmpty.LastOrDefault());
            Assert.AreEqual(default(KeyValuePair <string, int>), ldEmpty.FirstOrDefault());
            Assert.AreEqual(default(KeyValuePair <string, int>), ldEmpty.LastOrDefault());

            Assert.DoesNotThrow(() => ldEmpty.Reverse());
            List <KeyValuePair <string, int> > reversed = new List <KeyValuePair <string, int> >(a.Reverse());
            Assert.AreEqual(a.Count, reversed.Count);
            Assert.AreEqual("a5", reversed[0].Key);
            Assert.AreEqual(5, reversed[0].Value);
            Assert.AreEqual("a4", reversed[1].Key);
            Assert.AreEqual(4, reversed[1].Value);
            Assert.AreEqual("a123", reversed[2].Key);
            Assert.AreEqual(123, reversed[2].Value);
            Assert.AreEqual("a12", reversed[3].Key);
            Assert.AreEqual(12, reversed[3].Value);
            Assert.AreEqual("a1", reversed[4].Key);
            Assert.AreEqual(1, reversed[4].Value);
            #endregion

            Assert.AreEqual(5, a.Count);

            Assert.IsTrue(a.ContainsKey("a4"));
            Assert.IsTrue(a.ContainsKey("a123"));

            Assert.IsTrue(a.Remove("a123"));
            Assert.IsFalse(a.Remove("a123"));
            Assert.IsFalse(a.ContainsKey("a123"));

            Assert.AreEqual(4, a.Count);

            List <string> keys   = new List <string>(a.Keys);
            List <int>    values = new List <int>(a.Values);
            List <KeyValuePair <string, int> > kv = new List <KeyValuePair <string, int> >(a);

            Assert.AreEqual("a1", keys[0]);
            Assert.AreEqual("a12", keys[1]);
            Assert.AreEqual("a4", keys[2]);
            Assert.AreEqual("a5", keys[3]);

            Assert.AreEqual(1, values[0]);
            Assert.AreEqual(12, values[1]);
            Assert.AreEqual(4, values[2]);
            Assert.AreEqual(5, values[3]);

            Assert.AreEqual("a1", kv[0].Key);
            Assert.AreEqual("a12", kv[1].Key);
            Assert.AreEqual("a4", kv[2].Key);
            Assert.AreEqual("a5", kv[3].Key);

            Assert.AreEqual(1, kv[0].Value);
            Assert.AreEqual(12, kv[1].Value);
            Assert.AreEqual(4, kv[2].Value);
            Assert.AreEqual(5, kv[3].Value);

            a.Clear();
            Assert.AreEqual(0, a.Count);
        }