public void ContinuousZipIsCorrectByRandomCheck() { var sw = new Stopwatch(); var sm1 = new SortedMap<int, int>(); var sm2 = new SortedMap<int, int>(); var rng = new System.Random(31415926); //31415926 var prev1 = 0; var prev2 = 0; for (int i = 0; i < 100000; i = i + 1) { prev1 = prev1 + rng.Next(1, 11); sm1.Add(prev1, prev1); prev2 = prev2 + rng.Next(1, 11); sm2.Add(prev2, prev2); } sm1.IsMutable = false; sm2.IsMutable = false; //Console.WriteLine("First map:"); //foreach (var kvp in sm1) //{ // Console.WriteLine(kvp.Key); //} //Console.WriteLine("Second map:"); //foreach (var kvp in sm2) { // Console.WriteLine(kvp.Key); //} var series = new[] { sm1.Repeat(), sm2.Repeat(), }; sw.Start(); var allKeys = sm1.keys.Union(sm2.keys).OrderBy(x => x).ToArray(); int[] expectedKeys = new int[allKeys.Length]; int[] expectedValues = new int[allKeys.Length]; var size = 0; for (int i = 0; i < allKeys.Length; i++) { var val = 0; KeyValuePair<int, int> temp; var hasFirst = sm1.TryFind(allKeys[i], Lookup.LE, out temp); if (hasFirst) { val += temp.Value; var hasSecond = sm2.TryFind(allKeys[i], Lookup.LE, out temp); if (hasSecond) { val += temp.Value; expectedKeys[size] = allKeys[i]; expectedValues[size] = val; size++; } } } var expectedMap = SortedMap<int, int>.OfSortedKeysAndValues(expectedKeys, expectedValues, size); sw.Stop(); //Console.WriteLine("Expected map:"); //foreach (var kvp in expectedMap) { // Console.WriteLine(kvp.Key + " ; " + kvp.Value); //} Console.WriteLine("Manual join, elapsed msec: {0}", sw.ElapsedMilliseconds); SortedMap<int, int> sum = new SortedMap<int, int>(); for (int round = 0; round < 1; round++) { sw.Restart(); var ser = series.Zip((k, varr) => varr.Sum()); var cur = ser.GetCursor(); while (cur.MoveNext()) { sum.AddLast(cur.CurrentKey, cur.CurrentValue); } sw.Stop(); Console.WriteLine("Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds); //Console.WriteLine("StateCreation: {0}", RepeatCursor<int, int>.StateCreation); //Console.WriteLine("StateHit: {0}", RepeatCursor<int, int>.StateHit); //Console.WriteLine("StateMiss: {0}", RepeatCursor<int, int>.StateMiss); } //Console.WriteLine("Sync zip map:"); //foreach (var kvp in sum) { // Console.WriteLine(kvp.Key + " ; " + kvp.Value); //} Assert.AreEqual(expectedMap.Count, sum.Count, "Results of sync and expected must be equal"); foreach (var kvp in expectedMap) { Assert.AreEqual(kvp.Value, sum[kvp.Key]); } for (int round = 0; round < 1; round++) { sw.Restart(); var ser = series.Zip((k, varr) => varr.Sum()); var cur = ser.GetCursor(); var cur2 = cur.Clone(); var sum2 = new SortedMap<int, int>(); Task.Run(async () => { while (await cur2.MoveNext(CancellationToken.None)) { sum2.Add(cur2.CurrentKey, cur2.CurrentValue); } }).Wait(); sw.Stop(); Console.WriteLine("Async Zip join, elapsed msec: {0}", sw.ElapsedMilliseconds); 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]); } } Console.WriteLine(""); }
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.TryFind(allKeys[i], Lookup.LE, out temp); if (hasFirst) { val += temp.Value; var hasSecond = sm2.TryFind(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.GetCursor(); var last = zip.Last.Key; Task.Run(async() => { var prev = default(int); while (await cur.MoveNext(CancellationToken.None)) { 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.MoveNext(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]); //} }