private static void RunAvgTest1 <T>(int count) { string testMethodFail = string.Format("RunAvgTest1<{0}>(count={1}): FAILED.", typeof(T), count); if (typeof(T) == typeof(int)) { int[] ints = new int[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < ints.Length; i++) { ints[i] = i; } double realAvg = ints.AsParallel().Average(); if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(ints), expectAvg, realAvg)); } } else if (typeof(T) == typeof(int?)) { int?[] ints = new int?[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < ints.Length; i++) { ints[i] = i; } double realAvg = ints.AsParallel().Average().Value; if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(ints), expectAvg, realAvg)); } } if (typeof(T) == typeof(long)) { long[] longs = new long[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < longs.Length; i++) { longs[i] = i; } double realAvg = longs.AsParallel().Average(); if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(longs), expectAvg, realAvg)); } } else if (typeof(T) == typeof(long?)) { long?[] longs = new long?[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < longs.Length; i++) { longs[i] = i; } double realAvg = longs.AsParallel().Average().Value; if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(longs), expectAvg, realAvg)); } } else if (typeof(T) == typeof(float)) { float[] floats = new float[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < floats.Length; i++) { floats[i] = i; } double realAvg = floats.AsParallel().Average(); if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(floats), expectAvg, realAvg)); } } else if (typeof(T) == typeof(float?)) { float?[] floats = new float?[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < floats.Length; i++) { floats[i] = i; } double realAvg = floats.AsParallel().Average().Value; if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(floats), expectAvg, realAvg)); } } else if (typeof(T) == typeof(double)) { double[] doubles = new double[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < doubles.Length; i++) { doubles[i] = i; } double realAvg = doubles.AsParallel().Average(); if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(doubles), expectAvg, realAvg)); } } else if (typeof(T) == typeof(double?)) { double?[] doubles = new double?[count]; double expectAvg = ((double)count - 1) / 2; for (int i = 0; i < doubles.Length; i++) { doubles[i] = i; } double realAvg = doubles.AsParallel().Average().Value; if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(doubles), expectAvg, realAvg)); } } else if (typeof(T) == typeof(decimal)) { decimal[] decimals = new decimal[count]; decimal expectAvg = ((decimal)count - 1) / 2; for (int i = 0; i < decimals.Length; i++) { decimals[i] = i; } decimal realAvg = decimals.AsParallel().Average(); if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(decimals), expectAvg, realAvg)); } } else if (typeof(T) == typeof(decimal?)) { decimal?[] decimals = new decimal?[count]; decimal expectAvg = ((decimal)count - 1) / 2; for (int i = 0; i < decimals.Length; i++) { decimals[i] = i; } decimal realAvg = decimals.AsParallel().Average().Value; if (!expectAvg.Equals(realAvg)) { Assert.True(false, string.Format(testMethodFail + " > LINQ says: {0}, Expect: {0}, real: {1}", Enumerable.Average(decimals), expectAvg, realAvg)); } } }
private static void RunSumTest1 <T>(int count) { if (typeof(T) == typeof(int)) { int expectSum = 0; int[] ints = new int[count]; for (int i = 0; i < ints.Length; i++) { ints[i] = i; expectSum += i; } int realSum = ints.AsParallel().Sum(); if (!expectSum.Equals(realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(long)) { long expectSum = 0; long[] longs = new long[count]; for (int i = 0; i < longs.Length; i++) { longs[i] = i; expectSum += i; } long realSum = longs.AsParallel().Sum(); if (!expectSum.Equals(realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(float)) { float expectSum = 0; float[] floats = new float[count]; for (int i = 0; i < floats.Length; i++) { float val = (float)i / 10; floats[i] = val; expectSum += val; } float realSum = floats.AsParallel().Sum(); if (!AreEpsEqual(expectSum, realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(double)) { double expectSum = 0; double[] doubles = new double[count]; for (int i = 0; i < doubles.Length; i++) { double val = (double)i / 100; doubles[i] = val; expectSum += val; } double realSum = doubles.AsParallel().Sum(); if (!AreEpsEqual(expectSum, realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(decimal)) { decimal expectSum = 0; decimal[] decimals = new decimal[count]; for (int i = 0; i < decimals.Length; i++) { decimal val = (decimal)i / 100; decimals[i] = val; expectSum += val; } decimal realSum = decimals.AsParallel().Sum(); //round the numbers for the comparison if (!AreEpsEqual(expectSum, realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(int?)) { int? expectSum = 0; int?[] ints = new int?[count]; for (int i = 0; i < ints.Length; i++) { ints[i] = i; expectSum += i; } int?realSum = ints.AsParallel().Sum(); if (!expectSum.Equals(realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(long?)) { long? expectSum = 0; long?[] longs = new long?[count]; for (int i = 0; i < longs.Length; i++) { longs[i] = i; expectSum += i; } long?realSum = longs.AsParallel().Sum(); if (!expectSum.Equals(realSum)) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(float?)) { float? expectSum = 0; float?[] floats = new float?[count]; for (int i = 0; i < floats.Length; i++) { float?val = (float)i / 10; floats[i] = val; expectSum += val; } bool passed = true; float?realSum = floats.AsParallel().Sum(); if (!expectSum.HasValue || !realSum.HasValue) { passed = expectSum.HasValue.Equals(realSum.HasValue); } else { passed = AreEpsEqual(expectSum.Value, realSum.Value); } if (!passed) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(double?)) { double? expectSum = 0; double?[] doubles = new double?[count]; for (int i = 0; i < doubles.Length; i++) { double?val = (double)i / 100; doubles[i] = val; expectSum += val; } double?realSum = doubles.AsParallel().Sum(); bool passed = true; if (!expectSum.HasValue || !realSum.HasValue) { passed = expectSum.HasValue.Equals(realSum.HasValue); } else { passed = AreEpsEqual(expectSum.Value, realSum.Value); } if (!passed) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } else if (typeof(T) == typeof(decimal?)) { decimal? expectSum = 0; decimal?[] decimals = new decimal?[count]; for (int i = 0; i < decimals.Length; i++) { decimal?val = (decimal)i / 100; decimals[i] = val; expectSum += val; } decimal?realSum = decimals.AsParallel().Sum(); bool passed = true; if (!expectSum.HasValue || !realSum.HasValue) { passed = expectSum.HasValue.Equals(realSum.HasValue); } else { passed = AreEpsEqual(expectSum.Value, realSum.Value); } if (!passed) { Assert.True(false, string.Format("RunSumTest1<{0}>(count={1}): FAILED. > Expect: {2}, real: {3}", typeof(T), count, expectSum, realSum)); } } }
private static void RunMaxTest1 <T>(int dataSize, int maxSlot) { string methodFailed = string.Format("RunMaxTest1<{0}>(dataSize={1}, maxSlot={2}): FAILED.", typeof(T), dataSize, maxSlot); if (typeof(T) == typeof(int)) { int maxNum = dataSize + 100; int[] ints = new int[dataSize]; for (int i = 0; i < ints.Length; i++) { ints[i] = i; } if (dataSize > 0) { ints[maxSlot] = maxNum; } int max = ints.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(int?)) { int? maxNum = dataSize == 0 ? (int?)null : dataSize + 100; int?[] ints = new int?[dataSize]; for (int i = 0; i < ints.Length; i++) { ints[i] = i; } if (dataSize > 0) { ints[maxSlot] = maxNum; } int?max = ints.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(long)) { long maxNum = dataSize + 100; long[] longs = new long[dataSize]; for (int i = 0; i < longs.Length; i++) { longs[i] = i; } if (dataSize > 0) { longs[maxSlot] = maxNum; } long max = longs.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(long?)) { long? maxNum = dataSize == 0 ? (long?)null : dataSize + 100; long?[] longs = new long?[dataSize]; for (int i = 0; i < longs.Length; i++) { longs[i] = i; } if (dataSize > 0) { longs[maxSlot] = maxNum; } long?max = longs.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(float)) { float maxNum = dataSize + 100; float[] floats = new float[dataSize]; for (int i = 0; i < floats.Length; i++) { floats[i] = i; } if (dataSize > 0) { floats[maxSlot] = maxNum; } float max = floats.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(float?)) { float? maxNum = dataSize == 0 ? (float?)null : dataSize + 100; float?[] floats = new float?[dataSize]; for (int i = 0; i < floats.Length; i++) { floats[i] = i; } if (dataSize > 0) { floats[maxSlot] = maxNum; } float?max = floats.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(double)) { double maxNum = dataSize + 100; double[] doubles = new double[dataSize]; for (int i = 0; i < doubles.Length; i++) { doubles[i] = i; } if (dataSize > 0) { doubles[maxSlot] = maxNum; } double max = doubles.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(double?)) { double? maxNum = dataSize == 0 ? (double?)null : dataSize + 100; double?[] doubles = new double?[dataSize]; for (int i = 0; i < doubles.Length; i++) { doubles[i] = i; } if (dataSize > 0) { doubles[maxSlot] = maxNum; } double?max = doubles.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(decimal)) { decimal maxNum = dataSize + 100; decimal[] decimals = new decimal[dataSize]; for (int i = 0; i < decimals.Length; i++) { decimals[i] = i; } if (dataSize > 0) { decimals[maxSlot] = maxNum; } decimal max = decimals.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } if (typeof(T) == typeof(decimal?)) { decimal? maxNum = dataSize == 0 ? (decimal?)null : dataSize + 100; decimal?[] decimals = new decimal?[dataSize]; for (int i = 0; i < decimals.Length; i++) { decimals[i] = i; } if (dataSize > 0) { decimals[maxSlot] = maxNum; } decimal?max = decimals.AsParallel().Max(); if (!maxNum.Equals(max)) { Assert.True(false, string.Format(methodFailed + " > Expect: {0}, real: {1}", maxNum, max)); } } }