Beispiel #1
0
        public void RangeCursorMovesAfterAwating()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);

            var range = sm.Range(int.MinValue, 2, true, true);

            //Assert.AreEqual(1, range.First.Value);

            var cursor = range.GetAsyncEnumerator();

            var source = cursor.Source;

            var cts = new CancellationTokenSource();

            var t = Task.Run(async() =>
            {
                var moved = await cursor.MoveNextAsync();
                Assert.True(moved);
                moved = await cursor.MoveNextAsync();
                Assert.True(moved);
                moved = await cursor.MoveNextAsync();
                Assert.False(moved);
                Assert.AreEqual(2, cursor.CurrentKey);
                Assert.AreEqual(2, cursor.CurrentValue);
            });

            Thread.Sleep(100);
            sm.Add(2, 2);
            sm.Add(3, 3);
            t.Wait();
        }
Beispiel #2
0
        public void CouldReuseRangeValuesCursor()
        {
            var nonEmpty = new SortedMap <int, double>
            {
                { 1, 1 },
                { 2, 2 }
            };
            var range1 = nonEmpty.Range(0, Int32.MaxValue, true, true);

            //var e = range1.GetEnumerator();

            foreach (var keyValuePair in range1)
            {
                Assert.True(keyValuePair.Value > 0);
            }

            Console.WriteLine("Foreach is OK");

            Assert.True(range1.Any());

            Console.WriteLine("Count is OK");

            Assert.True(range1.Any());

            Console.WriteLine("Any is OK");

            Assert.True(range1.First.Value > 0);

            Console.WriteLine("Navigation is OK");
        }
Beispiel #3
0
        public void RangeCursorStopsBeforeEndKey()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);
            sm.Add(2, 2);
            sm.Add(3, 3);
            var range = sm.Range(int.MinValue, 2, true, true);

            //Assert.AreEqual(1, range.First.Value);

            var cursor = range.GetCursor();

            var source = cursor.Source;

            var cts = new CancellationTokenSource();

            var t = Task.Run(async() =>
            {
                var moved = await cursor.MoveNext(cts.Token);
                Assert.True(moved);
                moved = await cursor.MoveNext(cts.Token);
                Assert.True(moved);
                moved = await cursor.MoveNext(cts.Token);
                Assert.False(moved);
                Assert.AreEqual(2, cursor.CurrentKey);
                Assert.AreEqual(2, cursor.CurrentValue);
            });

            t.Wait();
        }
Beispiel #4
0
        public void CouldCancelRangeCursor()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(-1, -1);
            sm.Add(1, 1);
            sm.Add(2, 2);

            var range = sm.Range(0, 2, true, false);

            Assert.AreEqual(1, range.First.Value);

            var cursor = range.GetCursor();

            var source = cursor.Source;

            Assert.AreEqual(1, source.Count());

            Assert.True(cursor.MoveNext());
            Assert.False(cursor.MoveNext());

            var cts = new CancellationTokenSource();

            var t = Task.Run(() =>
            {
                Thread.Sleep(100);
                var task = cursor.MoveNext(cts.Token);
                Assert.True(task.IsCanceled);
            });

            cts.Cancel();
            t.Wait();
        }
Beispiel #5
0
        public void RangeMustBeEmptyWhenOutOfRange()
        {
            var sm = new SortedMap <DateTime, double>
            {
                { new DateTime(2006, 1, 1), 1.0 }
            };
            var test = sm.Range(new DateTime(2005, 1, 1), new DateTime(2005, 1, 2)).ToSortedMap();

            Assert.IsTrue(test.IsEmpty);
        }
Beispiel #6
0
        public void CouldMapRangeSeriesViaExtensionMethodsBenchmark()
        {
            var sm    = new SortedMap <int, int>();
            var count = 10000000;

            for (int i = 0; i < count; i++)
            {
                sm.AddLast(i, i);
            }

            for (int r = 0; r < 10; r++)
            {
                var sw = new Stopwatch();
                sw.Restart();
                var range = sm.Range(new Opt <int>(0), Opt <int> .Missing, true, true);
                var map   = sm.Map(i => i * 2);
                //var range2 = map.Range(0, int.MaxValue, true, true);
                //var map2 = range2.Map(i => i * 2);
                //var range3 = map2.Range(0, int.MaxValue, true, true);
                //var map3 = range3.Map(i => i * 2);
                long sum = 0;
                foreach (var kvp in map)
                {
                    sum += kvp.Value;
                }
                sw.Stop();
                Assert.IsTrue(sum > 0);

                Console.WriteLine($"Mops {sw.MOPS(count)}");
            }

            //for (int r = 0; r < 10; r++)
            //{
            //    var sw = new Stopwatch();
            //    sw.Restart();
            //    var map = sm.Select(x => new KeyValuePair<int, int>(x.Key, x.Value * 2));
            //    long sum = 0;
            //    foreach (var kvp in map)
            //    {
            //        sum += kvp.Value;
            //    }
            //    sw.Stop();
            //    Assert.IsTrue(sum > 0);

            //    Console.WriteLine($"LINQ Mops {sw.MOPS(count)}");
            //}
        }
Beispiel #7
0
        public void CouldMapRangeSeriesViaExtensionMethods()
        {
            var sm = new SortedMap <int, int>
            {
                { 1, 1 }
            };

            var range  = sm.Range(0, int.MaxValue, true, true);
            var map    = range.Map(i => i * 2);
            var range2 = map.Range(0, int.MaxValue, true, true);
            var map2   = range2.Map(i => i * 2);
            var range3 = map2.Range(0, int.MaxValue, true, true);
            var map3   = range3.Map(i => i * 2);

            Assert.AreEqual(2, map.First.Value);
            Assert.AreEqual(4, map2.First.Value);
            Assert.AreEqual(8, map3.First.Value);
        }
Beispiel #8
0
        public void CouldUseRangeValues()
        {
            SortedMap <int, double> sm = null;

            Assert.Throws <NullReferenceException>(() =>
            {
                var nullRange = sm.Range(0, Int32.MaxValue, true, true);
            });

            var empty = new SortedMap <int, double>();

            var range = empty.Range(0, Int32.MaxValue, true, true);

            Assert.False(range.Any());

            var nonEmpty = new SortedMap <int, double>
            {
                { 1, 1 }
            };
            var range1 = nonEmpty.Range(0, Int32.MaxValue, true, true);

            Assert.True(range1.Any());
        }
Beispiel #9
0
        public void CouldCancelDoAfter()
        {
            var sm = new SortedMap <int, int>();

            sm.Add(1, 1);

            var cursor = sm.GetCursor();

            Assert.True(cursor.MoveNext());
            Assert.False(cursor.MoveNext());

            var cts = new CancellationTokenSource();

            var t = sm.Range(int.MinValue, 2, true, true).Do((k, v) =>
            {
                Console.WriteLine($"{k} - {v}");
            }, cts.Token);

            cts.Cancel();

            Thread.Sleep(1000);

            Assert.True(t.IsCanceled);
        }