示例#1
0
        public void TestFirstAndLast()
        {
            BTreeDictionary <int, string> data = new BTreeDictionary <int, string>(Comparer);

            data.Add(1, "a");
            data.Add(2, "b");
            data.Add(3, "c");
            data.Add(4, "d");
            data.Add(5, "e");

            Assert.AreEqual(1, data.First().Key);
            Assert.AreEqual("a", data.First().Value);
            data.Remove(1);
            Assert.AreEqual(2, data.First().Key);
            Assert.AreEqual("b", data.First().Value);

            Assert.AreEqual(5, data.Last().Key);
            Assert.AreEqual("e", data.Last().Value);
            data.Remove(5);
            Assert.AreEqual(4, data.Last().Key);
            Assert.AreEqual("d", data.Last().Value);

            data.Remove(4);
            data.Remove(3);

            KeyValuePair <int, string> kv;

            Assert.IsTrue(data.TryGetLast(out kv));
            Assert.IsTrue(data.TryGetFirst(out kv));
            data.Remove(2);
            Assert.IsFalse(data.TryGetLast(out kv));
            Assert.IsFalse(data.TryGetFirst(out kv));

            try
            {
                data.First();
                Assert.Fail("Should raise InvalidOperationException");
            }
            catch (InvalidOperationException)
            {
            }
            try
            {
                data.Last();
                Assert.Fail("Should raise InvalidOperationException");
            }
            catch (InvalidOperationException)
            {
            }
        }