public void CouldScanSeries() { OptimizationSettings.CombineFilterMapDelegates = true; var data = new SortedMap <DateTime, double>(); var count = 5000; for (int i = 0; i < count; i++) { data.Add(DateTime.UtcNow.Date.AddSeconds(i), i); } var sign = 1; var runnningSum = data.Zip(data, (d1, d2) => d1 + d2).Scan(0.0, (st, k, v) => { if (st > 100) { sign = -1; } if (st < -100) { sign = 1; } return(st += sign * v); }); var runnningSumSm = runnningSum.ToSortedMap(); Assert.AreEqual(runnningSum.Count(), count); }
public ContractsTests(ISeries <K, V> testSeries, SortedMap <K, V> materializedSeries, SortedMap <K, V> ephemeralSeries = null) { _testSeries = testSeries ?? throw new ArgumentNullException(nameof(testSeries)); _materializedSeries = materializedSeries ?? throw new ArgumentNullException(nameof(materializedSeries)); _ephemeralSeries = ephemeralSeries; if (!ReferenceEquals(null, ephemeralSeries)) { var eqc = EqualityComparer <V> .Default; // TODO (UX) Spreads signature conflicts with LINQ, easy to fix but not very convenient var intersect = materializedSeries.Zip(ephemeralSeries, (l, r) => { if (!eqc.Equals(l, r)) { throw new ArgumentException("materializedSeries and ephemeralSeries contain different values for the same keys"); } else { return(l); } }); if (intersect.First.IsMissing) { foreach (var kvp in materializedSeries.Take(10)) { ephemeralSeries.Set(kvp.Key, kvp.Value); } } } }
public void DiscreteZipIsCorrectByRandomCheck(SortedMap <int, int> sm1, SortedMap <int, int> sm2, int seed) { var series = new[] { sm1, 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.EQ, 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.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.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]); } //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]); //} }