コード例 #1
0
        public void TestRangeEnumerate()
        {
            BTreeDictionary <int, string> data = new BTreeDictionary <int, string>(Comparer);

            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(data.TryAdd(i, i.ToString()));
            }

            int ix = 0;

            foreach (KeyValuePair <int, string> kv in data.EnumerateRange(-500, 5000))
            {
                Assert.AreEqual(ix++, kv.Key);
            }
            Assert.AreEqual(100, ix);

            foreach (
                KeyValuePair <int, int> range in
                new Dictionary <int, int> {
                { 6, 25 }, { 7, 25 }, { 8, 25 }, { 9, 25 }, { 22, 25 }, { 28, 28 }
            })
            {
                ix = range.Key;
                foreach (KeyValuePair <int, string> kv in data.EnumerateRange(ix, range.Value))
                {
                    Assert.AreEqual(ix++, kv.Key);
                }
                Assert.AreEqual(range.Value, ix - 1);
            }
        }
コード例 #2
0
        public void TestTryRoutines()
        {
            BTreeDictionary <int, string> data = new BTreeDictionary <int, string>(Comparer);

            Assert.IsTrue(data.TryAdd(1, "a"));
            Assert.IsFalse(data.TryAdd(1, "a"));

            Assert.IsTrue(data.TryUpdate(1, "a"));
            Assert.IsTrue(data.TryUpdate(1, "c"));
            Assert.IsTrue(data.TryUpdate(1, "d", "c"));
            Assert.IsFalse(data.TryUpdate(1, "f", "c"));
            Assert.AreEqual("d", data[1]);
            Assert.IsTrue(data.TryUpdate(1, "a", data[1]));
            Assert.AreEqual("a", data[1]);
            Assert.IsFalse(data.TryUpdate(2, "b"));

            string val;

            Assert.IsTrue(data.TryRemove(1, out val) && val == "a");
            Assert.IsFalse(data.TryRemove(2, out val));
            Assert.AreNotEqual(val, "a");
        }
コード例 #3
0
        public void TestEnumerateFrom()
        {
            BTreeDictionary <int, string> data = new BTreeDictionary <int, string>(Comparer);

            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(data.TryAdd(i, i.ToString()));
            }

            Assert.AreEqual(50, new List <KeyValuePair <int, string> >(data.EnumerateFrom(50)).Count);
            Assert.AreEqual(25, new List <KeyValuePair <int, string> >(data.EnumerateFrom(75)).Count);

            for (int i = 0; i < 100; i++)
            {
                int first = -1;
                foreach (KeyValuePair <int, string> kv in data.EnumerateFrom(i))
                {
                    first = kv.Key;
                    break;
                }
                Assert.AreEqual(i, first);
            }
        }
        void SequencedTest(int start, int incr, int stop, string name)
        {
            int          count = Math.Abs(start - stop) / Math.Abs(incr);
            const string myTestValue1 = "T1", myTestValue2 = "t2";
            string       test;

            BTreeDictionary <int, string> data = new BTreeDictionary <int, string>(Comparer);
            Stopwatch time = new Stopwatch();

            time.Start();
            //large order-forward
            for (int i = start; i != stop; i += incr)
            {
                if (!data.TryAdd(i, myTestValue1))
                {
                    throw new ApplicationException();
                }
            }

            Trace.TraceInformation("{0} insert  {1} in {2}", name, count, time.ElapsedMilliseconds);
            time.Reset();
            time.Start();

            for (int i = start; i != stop; i += incr)
            {
                if (!data.TryGetValue(i, out test) || test != myTestValue1)
                {
                    throw new ApplicationException();
                }
            }

            Trace.TraceInformation("{0} seek    {1} in {2}", name, count, time.ElapsedMilliseconds);
            time.Reset();
            time.Start();

            for (int i = start; i != stop; i += incr)
            {
                if (!data.TryUpdate(i, myTestValue2))
                {
                    throw new ApplicationException();
                }
            }

            Trace.TraceInformation("{0} modify  {1} in {2}", name, count, time.ElapsedMilliseconds);
            time.Reset();
            time.Start();

            for (int i = start; i != stop; i += incr)
            {
                if (!data.TryGetValue(i, out test) || test != myTestValue2)
                {
                    throw new ApplicationException();
                }
            }

            Trace.TraceInformation("{0} seek#2  {1} in {2}", name, count, time.ElapsedMilliseconds);
            time.Reset();
            time.Start();

            int tmpCount = 0;

            foreach (KeyValuePair <int, string> tmp in data)
            {
                if (tmp.Value != myTestValue2)
                {
                    throw new ApplicationException();
                }
                else
                {
                    tmpCount++;
                }
            }
            if (tmpCount != count)
            {
                throw new ApplicationException();
            }

            Trace.TraceInformation("{0} foreach {1} in {2}", name, count, time.ElapsedMilliseconds);
            time.Reset();
            time.Start();

            for (int i = start; i != stop; i += incr)
            {
                if (!data.Remove(i))
                {
                    throw new ApplicationException();
                }
            }

            Trace.TraceInformation("{0} delete  {1} in {2}", name, count, time.ElapsedMilliseconds);

            for (int i = start; i != stop; i += incr)
            {
                if (data.TryGetValue(i, out test))
                {
                    throw new ApplicationException();
                }
            }
        }