public static void Sum_Decimal(Labeled <ParallelQuery <int> > labeled, int count, decimal sum) { ParallelQuery <int> query = labeled.Item; Assert.Equal(sum, query.Select(x => (decimal)x).Sum()); Assert.Equal(sum, query.Select(x => (decimal?)x).Sum()); Assert.Equal(default(decimal), query.Select(x => (decimal?)null).Sum()); Assert.Equal(-sum, query.Sum(x => - (decimal)x)); Assert.Equal(default(decimal), query.Sum(x => (decimal?)null)); }
public static void Sum_Long(Labeled <ParallelQuery <int> > labeled, int count, long sum) { ParallelQuery <int> query = labeled.Item; Assert.Equal(sum, query.Select(x => (long)x).Sum()); Assert.Equal(sum, query.Select(x => (long?)x).Sum()); Assert.Equal(default(long), query.Select(x => (long?)null).Sum()); Assert.Equal(-sum, query.Sum(x => - (long)x)); Assert.Equal(-sum, query.Sum(x => - (long?)x)); Assert.Equal(default(long), query.Sum(x => (long?)null)); }
public static void Sum_Int(Labeled <ParallelQuery <int> > labeled, int count, int sum) { ParallelQuery <int> query = labeled.Item; Assert.Equal(sum, query.Sum()); Assert.Equal(sum, query.Select(x => (int?)x).Sum()); Assert.Equal(default(int), query.Select(x => (int?)null).Sum()); Assert.Equal(-sum, query.Sum(x => - x)); Assert.Equal(-sum, query.Sum(x => - (int?)x)); Assert.Equal(default(int), query.Sum(x => (int?)null)); }
public static void Sum_Long_SomeNull(Labeled <ParallelQuery <int> > labeled, int count, long sum) { ParallelQuery <int> query = labeled.Item; Assert.Equal(Functions.SumRange(0L, count / 2), query.Select(x => x < count / 2 ? (long?)x : null).Sum()); Assert.Equal(-Functions.SumRange(0L, count / 2), query.Sum(x => x < count / 2 ? -(long?)x : null)); }
public static void Sum_Int_AllNull(Labeled <ParallelQuery <int> > labeled, int count, int sum) { ParallelQuery <int> query = labeled.Item; Assert.Equal(0, query.Select(x => (int?)null).Sum()); Assert.Equal(0, query.Sum(x => (int?)null)); }
public static void Sum_Decimal_SomeNull(Labeled <ParallelQuery <int> > labeled, int count, decimal sum) { ParallelQuery <int> query = labeled.Item; Assert.Equal(Functions.SumRange(0, count / 2), query.Select(x => x < count / 2 ? (decimal?)x : null).Sum()); Assert.Equal(-Functions.SumRange(0, count / 2), query.Sum(x => x < count / 2 ? -(decimal?)x : null)); }
protected static ulong ReduceToInterval(ulong start, ulong end, ParallelQuery <SimulationResult> d) { ulong reducedtime = 0UL; var takenResults = new HashSet <SimulationResult>(); foreach (var m in d.OrderBy(t => t.Start)) { if (m.Start < start) { reducedtime += start - m.Start; takenResults.Add(m); } if (m.End > end) { reducedtime += (m.End - end); takenResults.Add(m); } if (m.Start > start && m.End < end) { break; } } foreach (var m in d.OrderByDescending(t => t.Start)) { if (m.Start < start && !takenResults.Contains(m)) { reducedtime += start - m.Start; } if (m.End > end && !takenResults.Contains(m)) { reducedtime += (m.End - end); } if (m.Start > start && m.End < end) { break; } } return(d.Sum(t => t.Total, new object()) - reducedtime); }
private static bool RunBicubicInterpolationTest(int m, int n, int loops) { TestHarness.TestLog("RunBicubicInterpolationTest(m={0}, n={1}, loops={2})", m, n, loops); Random r = new Random(33); // same seed for predictable test results. // Generate our image out of random #s. int[,] img = new int[m, n]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { img[i, j] = r.Next(100); } } // Just pick some random weights. double[] bicubic_weights = new double[] { 0.15, 0.25, 0.25, 0.15, 0.25, 0.5, 0.5, 0.25, 0.8, 0.25, 0.5, 0.5, 0.25, 0.15, 0.25, 0.25, 0.15 }; ParallelQuery <double> W = bicubic_weights.AsParallel(); PerfHelpers.DrivePerfComparison( delegate { // HACK: current version of LINQ doesn't have the right SelectMany // overload yet. So we just compare to PLINQ w/ a DOP of 1 instead. int oldDop = System.Linq.Parallel.Scheduling.DefaultDegreeOfParallelism; System.Linq.Parallel.Scheduling.DefaultDegreeOfParallelism = 1; try { double[] img2 = ( from x in ParallelEnumerable.Range(0, m) from y in ParallelEnumerable.Range(0, n) select (from x2 in Enumerable.Range(m - 3, 7) from y2 in Enumerable.Range(n - 3, 7) where x2 >= 0 && x2 < m && y2 >= 0 && y2 < n && (x2 == x || (x2 - (m - 3) % 2) == 0) && (y2 == y || (y2 - (n - 3) % 2) == 0) select img[x2, y2] ).AsParallel().Zip(W, (i, j) => new Pair <int, double>(i, j)).Sum((p) => p.First * p.Second) / W.Sum()).ToArray(); } finally { System.Linq.Parallel.Scheduling.DefaultDegreeOfParallelism = oldDop; } }, delegate { double[] img2 = ( from x in ParallelEnumerable.Range(0, m) from y in ParallelEnumerable.Range(0, n) select (from x2 in Enumerable.Range(m - 3, 7) from y2 in Enumerable.Range(n - 3, 7) where x2 >= 0 && x2 < m && y2 >= 0 && y2 < n && (x2 == x || (x2 - (m - 3) % 2) == 0) && (y2 == y || (y2 - (n - 3) % 2) == 0) select img[x2, y2] ).AsParallel().Zip(W, (i, j) => new Pair <int, double>(i, j)).Sum((p) => p.First * p.Second) / W.Sum()).ToArray(); }, loops); return(true); }