Exemple #1
0
        public void ContinuousZipIsCorrectByRandomCheck(SortedMap <int, int> sm1, SortedMap <int, int> sm2, int seed)
        {
            var series = new ISeries <int, int>[] { sm1.Repeat(), sm2 };

            int[] expectedKeys;
            int[] expectedValues;
            int   size;
            SortedMap <int, int> expectedMap;

            using (Benchmark.Run("Manual join", sm1.Count + sm2.Count))
            {
                var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray();

                expectedKeys = new int[allKeys.Length];

                expectedValues = new int[allKeys.Length];
                size           = 0;
                for (int i = 0; i < allKeys.Length; i++)
                {
                    var val = 0;
                    KeyValuePair <int, int> temp;
                    var hasFirst = sm1.TryFindAt(allKeys[i], Lookup.LE, out temp);
                    if (hasFirst)
                    {
                        val += temp.Value;
                        var hasSecond = sm2.TryFindAt(allKeys[i], Lookup.EQ, out temp);
                        if (hasSecond)
                        {
                            val += temp.Value;
                            expectedKeys[size]   = allKeys[i];
                            expectedValues[size] = val;
                            size++;
                        }
                    }
                }
                expectedMap = SortedMap <int, int> .OfSortedKeysAndValues(expectedKeys, expectedValues, size);
            }

            SortedMap <int, int> sum;
            Series <int, int>    ser;

            using (Benchmark.Run("Zip join", sm1.Count + sm2.Count))
            {
                sum = sm1.Repeat().Zip(sm2, (v1, v2) => v1 + v2).ToSortedMap();
            }
            Assert.AreEqual(expectedMap.Count, sum.Count, "Expected size");
            foreach (var kvp in expectedMap)
            {
                Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            }

            //using (Benchmark.Run("ZipN join", sm1.Count + sm2.Count))
            //{
            //    ser = series.ZipOld((k, varr) => varr.Sum());
            //    sum = ser.ToSortedMap();
            //}
            //Assert.AreEqual(expectedMap.Count, sum.Count, "Expected size");
            //foreach (var kvp in expectedMap)
            //{
            //    Assert.AreEqual(kvp.Value, sum[kvp.Key]);
            //}

            var sum1 = new SortedMap <int, int>();

            using (Benchmark.Run("Zip Async join", sm1.Count + sm2.Count))
            {
                var zip  = sm1.Repeat().Zip(sm2, (v1, v2) => v1 + v2);
                var cur  = zip.GetAsyncCursor();
                var last = zip.Last.Present.Key;
                Task.Run(async() =>
                {
                    var prev = default(int);
                    while (await cur.MoveNextAsync())
                    {
                        if (cur.CurrentKey == prev)
                        {
                            Console.WriteLine($"Break on equal keys condition, seed {seed}");
                        }
                        prev = cur.CurrentKey;
                        sum1.Add(cur.CurrentKey, cur.CurrentValue);
                        //if (prev == last)
                        //{
                        //    Console.WriteLine("Next should be false");
                        //}
                    }
                })
                .Wait();
            }
            Assert.AreEqual(expectedMap.Count, sum1.Count, "Results of sync and async moves must be equal");
            foreach (var kvp in expectedMap)
            {
                Assert.AreEqual(kvp.Value, sum1[kvp.Key]);
            }

            // TODO this uses Subscribe which is not implemented yet
            //var sum2 = new SortedMap<int, int>();
            //using (Benchmark.Run("ZipN Async join", sm1.Count + sm2.Count))
            //{
            //    var cur = ser.GetCursor();

            //    var cur2 = cur.Clone();

            //    Task.Run(async () =>
            //        {
            //            while (await cur2.MoveNextAsync(CancellationToken.None))
            //            {
            //                sum2.Add(cur2.CurrentKey, cur2.CurrentValue);
            //            }
            //        })
            //        .Wait();
            //}

            //Assert.AreEqual(sum.Count, sum2.Count, "Results of sync and async moves must be equal");
            //foreach (var kvp in expectedMap)
            //{
            //    Assert.AreEqual(kvp.Value, sum2[kvp.Key]);
            //}
        }