public static void Max_ArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Range(0, 1).Max((Func <int, int>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int?>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((int?)0, 1).Max((Func <int?, int?>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <long>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((long)0, 1).Max((Func <long, long>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <long?>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((long?)0, 1).Max((Func <long?, long?>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <float>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((float)0, 1).Max((Func <float, float>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <float?>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((float?)0, 1).Max((Func <float?, float>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <double>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((double)0, 1).Max((Func <double, double>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <double?>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((double?)0, 1).Max((Func <double?, double>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <decimal>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((decimal)0, 1).Max((Func <decimal, decimal>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <decimal?>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((decimal?)0, 1).Max((Func <decimal?, decimal>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <KeyValuePair <int, int> >)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat(0, 1).Max((Func <int, KeyValuePair <int, int> >)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <object>)null).Max()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat(new object(), 1).Max((Func <object, object>)null)); }
private static void GeneratingParallelSequence() { Stopwatch watch = Stopwatch.StartNew(); IEnumerable <int> parallelRange = ParallelEnumerable.Range(0, 5000).Select(i => i * i); foreach (var item in parallelRange) { } watch.Stop(); Console.WriteLine($"Time elapsed using ParallelEnumerable : {watch.ElapsedMilliseconds}"); Stopwatch watch2 = Stopwatch.StartNew(); IEnumerable <int> range = Enumerable.Range(0, 5000).Select(i => i * i); foreach (var item in range) { } watch2.Stop(); Console.WriteLine($"Time elapsed using Enumerable : {watch2.ElapsedMilliseconds}"); Stopwatch watch3 = Stopwatch.StartNew(); IEnumerable <int> rangeRepeat = ParallelEnumerable.Repeat(1, 5000); foreach (var item in rangeRepeat) { } watch3.Stop(); Console.WriteLine($"Time elapsed using Repeat : {watch3.ElapsedMilliseconds}"); Console.ReadLine(); }
public static void Average_ArgumentNullException() { Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat(0, 1).Average((Func <int, int>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <int?>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((int?)0, 1).Average((Func <int?, int?>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <long>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((long)0, 1).Average((Func <long, long>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <long?>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((long?)0, 1).Average((Func <long?, long?>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <float>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((float)0, 1).Average((Func <float, float>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <float?>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((float?)0, 1).Average((Func <float?, float?>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <double>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((double)0, 1).Average((Func <double, double>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <double?>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((double?)0, 1).Average((Func <double?, double?>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <decimal>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((decimal)0, 1).Average((Func <decimal, decimal>)null)); Assert.Throws <ArgumentNullException>(() => ((ParallelQuery <decimal?>)null).Average()); Assert.Throws <ArgumentNullException>(() => ParallelEnumerable.Repeat((decimal?)0, 1).Average((Func <decimal?, decimal?>)null)); }
/// <summary> /// Listing 6-20 /// </summary> static void GeneralParallelRanges() { int[] sourceData = new int[10]; for (int i = 0; i < sourceData.Length; i++) { sourceData[i] = i + 1; } IEnumerable <int> resultRange = ParallelEnumerable.Range(0, 10).Where(item => item % 2 == 0).Select(item => item); IEnumerable <double> resultRepeat = ParallelEnumerable.Repeat(10, 10).Where(item => item % 2 == 0).Select(item => Math.Pow(item, 2)); Console.WriteLine("Range Result!!"); foreach (int x in resultRange) { Console.WriteLine(x); } Console.WriteLine("\nRepeat Result!!"); foreach (double x in resultRepeat) { Console.WriteLine(x); } }
public static void Min_ArgumentNullException() { Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <int>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Range(0, 1).Min((Func <int, int>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <int?>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((int?)0, 1).Min((Func <int?, int?>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <long>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((long)0, 1).Min((Func <long, long>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <long?>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((long?)0, 1).Min((Func <long?, long?>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <float>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((float)0, 1).Min((Func <float, float>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <float?>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((float?)0, 1).Min((Func <float?, float>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <double>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((double)0, 1).Min((Func <double, double>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <double?>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((double?)0, 1).Min((Func <double?, double>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <decimal>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((decimal)0, 1).Min((Func <decimal, decimal>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <decimal?>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((decimal?)0, 1).Min((Func <decimal?, decimal>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <NotComparable>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat(0, 1).Min((Func <int, NotComparable>)null)); Assert.Throws <ArgumentNullException>("source", () => ((ParallelQuery <object>)null).Min()); Assert.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat(new object(), 1).Min((Func <object, object>)null)); }
public static void Sum_ArgumentNullException() { AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <int>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat(0, 1).Sum((Func <int, int>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <int?>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((int?)0, 1).Sum((Func <int?, int?>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <long>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((long)0, 1).Sum((Func <long, long>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <long?>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((long?)0, 1).Sum((Func <long?, long?>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <float>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((float)0, 1).Sum((Func <float, float>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <float?>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((float?)0, 1).Sum((Func <float?, float>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <double>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((double)0, 1).Sum((Func <double, double>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <double?>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((double?)0, 1).Sum((Func <double?, double>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <decimal>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((decimal)0, 1).Sum((Func <decimal, decimal>)null)); AssertExtensions.Throws <ArgumentNullException>("source", () => ((ParallelQuery <decimal?>)null).Sum()); AssertExtensions.Throws <ArgumentNullException>("selector", () => ParallelEnumerable.Repeat((decimal?)0, 1).Sum((Func <decimal?, decimal>)null)); }
public void GeneralStaticExit() { int key = 5; var LockDic = new ConcurrentDictionary <int, bool>(); IEnumerable <TestClass> Objects = ParallelEnumerable.Repeat <TestClass>(new TestClass(), 10); bool WasChanged = false; Action <TestClass> DecreaseAction = new Action <TestClass>(o => { }); Action <TestClass> IncreaseAction = new Action <TestClass>(o => { }); //check for release lock LockDic[key] = true; GeneralTransition <TestClass> .StaticExit(Objects, DecreaseAction, IncreaseAction, ref WasChanged, LockDic, key); Assert.IsFalse(LockDic[key]); //check for set "WasChanged" WasChanged = false; GeneralTransition <TestClass> .StaticExit(Objects, DecreaseAction, IncreaseAction, ref WasChanged, LockDic, key); Assert.IsTrue(WasChanged); //check for do action Objects.AsParallel().ForAll(o => { o.Number = 1; o.Word = "Hello"; }); DecreaseAction = o => o.Number = 2; IncreaseAction = o => o.Word = "Word"; GeneralTransition <TestClass> .StaticExit(Objects, DecreaseAction, IncreaseAction, ref WasChanged, LockDic, key); Assert.IsTrue(Objects.All(o => o.Word == "Word" && o.Number == 2)); }
public void DegeneratingCommuteTest() { var a = new Shielded <int>(); int numInc = 100000; int transactionCount = 0, commuteCount = 0; ParallelEnumerable.Repeat(1, numInc / 2).ForAll(i => Shield.InTransaction(() => { Interlocked.Increment(ref transactionCount); a.Commute((ref int n) => { Interlocked.Increment(ref commuteCount); n++; }); a.Commute((ref int n) => { Interlocked.Increment(ref commuteCount); n++; }); // so, we cause it to degenerate. there was a subtle bug in enlisting which // would allow a degenerated commute to execute before checking the lock! int x = a; })); Assert.AreEqual(numInc, a); // degenerated commutes conflict, which means transaction will repeat. conflict // may be detected before the commute lambda actually gets to execute, so the // trans count can be greater than commute count. Assert.GreaterOrEqual(transactionCount, commuteCount / 2); if (commuteCount == numInc) { Assert.Inconclusive(); } }
static void Main(string[] args) { // Use PLINQ to process a prallel range IEnumerable <double> result1 = from e in ParallelEnumerable.Range(0, 10) where e % 2 == 0 select Math.Pow(e, 2); foreach (var item in result1) { Console.WriteLine("Result 1: {0}", item); } // Use PLINQ to process a parallel repeating sequence IEnumerable <double> result2 = ParallelEnumerable.Repeat(10, 20) .Select(item => Math.Pow(item, 2)); foreach (var item in result2) { Console.WriteLine("Result 2: {0}", item); } // Wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
public static void ToDictionary_DuplicateKeys_ElementSelector_CustomComparator(Labeled <ParallelQuery <int> > labeled, int count) { ParallelQuery <int> query = labeled.Item; AggregateException e = Assert.Throws <AggregateException>(() => ParallelEnumerable.Repeat(0, 2).ToDictionary(x => x, y => y, new ModularCongruenceComparer(2))); Assert.IsType <ArgumentException>(e.InnerException); }
public static void GenerateParallelRanges() { IEnumerable <double> result1 = from e in ParallelEnumerable.Range(0, 10) where e % 2 == 0 select Math.Pow(e, 2); IEnumerable <double> result2 = ParallelEnumerable.Repeat(10, 100).Select(item => Math.Pow(item, 2)); EndOfProgram(); }
public static void Repeat_Exception() { Assert.Throws <ArgumentOutOfRangeException>(() => ParallelEnumerable.Repeat(1, -1)); Assert.Throws <ArgumentOutOfRangeException>(() => ParallelEnumerable.Repeat((long)1024, -1024)); Assert.Throws <ArgumentOutOfRangeException>(() => ParallelEnumerable.Repeat(2.0, -2)); Assert.Throws <ArgumentOutOfRangeException>(() => ParallelEnumerable.Repeat((decimal)8, -8)); Assert.Throws <ArgumentOutOfRangeException>(() => ParallelEnumerable.Repeat("fail", -1)); Assert.Throws <ArgumentOutOfRangeException>(() => ParallelEnumerable.Repeat((string)null, -1)); }
public void RepeatTestCase() { ParallelTestHelper.Repeat(() => { IEnumerable <int> sync = Enumerable.Repeat(1, 1000); IEnumerable <int> async = ParallelEnumerable.Repeat(1, 1000); AreEquivalent(sync, async, 1); }); }
public static void Repeat_Select <T>(T element, int count) { ParallelQuery <T> query = ParallelEnumerable.Repeat(element, count).Select(i => i); int counted = 0; Assert.All(query, e => { counted++; Assert.Equal(element, e); }); Assert.Equal(count, counted); }
public static void Max_AggregateException_NotComparable() { ArgumentException e = AssertThrows.Wrapped <ArgumentException>(() => ParallelEnumerable.Repeat(new NotComparable(0), 2).Max()); Assert.Null(e.ParamName); e = AssertThrows.Wrapped <ArgumentException>(() => ParallelEnumerable.Range(0, 2).Max(x => new NotComparable(x))); Assert.Null(e.ParamName); }
public void ZipTestCase() { ParallelTestHelper.Repeat(() => { ParallelQuery <int> async_res1 = ParallelEnumerable.Range(0, 10000); ParallelQuery <int> async_res2 = ParallelEnumerable.Repeat(1, 10000).Zip(async_res1, (e1, e2) => e1 + e2); int[] expected = Enumerable.Range(1, 10000).ToArray(); AreEquivalent(expected, Enumerable.ToArray(async_res2)); }); }
public void ZipTestCase() { ParallelTestHelper.Repeat(() => { ParallelQuery <int> async1 = ParallelEnumerable.Range(0, 10000); ParallelQuery <int> async2 = ParallelEnumerable.Repeat(1, 10000).Zip(async1, (e1, e2) => e1 + e2); int[] expected = Enumerable.Range(1, 10000).ToArray(); CollectionAssert.AreEquivalent(expected, Enumerable.ToArray(async2), "#1"); }); }
public void AggregateTestCase() { ParallelTestHelper.Repeat(() => { ParallelQuery <int> range = ParallelEnumerable.Repeat(5, 2643); double average = range.Aggregate(() => new double[2], (acc, elem) => { acc[0] += elem; acc[1]++; return(acc); }, (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return(acc1); }, acc => acc[0] / acc[1]); Assert.AreEqual(5.0, average, "#1"); }); }
static void Listing23_12() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); int sum = ParallelEnumerable.Repeat(1, 50000) .Select(i => i) .Sum(); Console.WriteLine("Sum: {0}", sum); Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void Listing24_5() { Console.WriteLine("{0} : Begin", new StackTrace(0, true).GetFrame(0).GetMethod().Name); ParallelQuery <int> pq = ParallelEnumerable.Repeat(2, 10); foreach (int i in pq) { Console.WriteLine("Value {0}", i); } Console.WriteLine("{0} : End", new StackTrace(0, true).GetFrame(0).GetMethod().Name); }
static void PLinqRepeat() { Console.WriteLine("=== " + MethodInfo.GetCurrentMethod().Name + " ==="); var parallel = from p in ParallelEnumerable.Repeat(1, 100) select new { Value = p, Thread.CurrentThread.ManagedThreadId }; foreach (var p in parallel) { Console.WriteLine("Number = {0}, ThreadId = {1}", p.Value, p.ManagedThreadId); } }
public void Normailize_Int(int value, int count) { var list = ParallelEnumerable.Repeat(value, count).ToList(); var norms = list.Normalize().ToList(); var norm = count * value; // NOTE: Parallel.For 구문을 사용하려면, 내부 Action에 Enumerable 이 아닌 Collection 또는 List이어야한다. Parallel.For(0, count, i => { Assert.AreEqual(list[i], value); Assert.AreEqual((double)list[i] / norm, norms[i]); }); }
internal static void Generation() { IEnumerable <double> sequentialQuery = Enumerable .Repeat(0, 5) // Return IEnumerable<int>. .Concat(Enumerable.Range(0, 5)) // Enumerable.Concat. .Where(int32 => int32 > 0) // Enumerable.Where. .Select(int32 => Math.Sqrt(int32)); // Enumerable.Select. ParallelQuery <double> parallelQuery = ParallelEnumerable .Repeat(0, 5) // Return ParallelQuery<int>. .Concat(ParallelEnumerable.Range(0, 5)) // ParallelEnumerable.Concat. .Where(int32 => int32 > 0) // ParallelEnumerable.Where. .Select(int32 => Math.Sqrt(int32)); // ParallelEnumerable.Select. }
public void ZipTestCase() { ParallelTestHelper.Repeat(() => { ParallelQuery <int> async_res1 = ParallelEnumerable.Range(0, 10000); ParallelQuery <int> async_res2 = ParallelEnumerable.Repeat(1, 10000).Zip(async_res1, (e1, e2) => e1 + e2); int[] expected = Enumerable.Range(1, 10000).ToArray(); var actual = Enumerable.ToArray(async_res2); // Work around quadratic behavior in NUnitLite's CollectionTally class Array.Sort(expected); Array.Sort(actual); AreEquivalent(expected, actual); }); }
static void Main(string[] args) { IEnumerable <MyClass> Objects = ParallelEnumerable.Repeat <MyClass>(new MyClass(), 2); PetriController <MyClass> pn = new PetriController <MyClass>(); pn.SetReset(o => { o.Number = 0; o.Word = "aaa"; }); pn.SetObjects(Objects.ToList()); pn.AddTransition(o => true, o => o.Number == 0 && o.Word == "aaa", o => { }, o => { o.Number = 1; o.Word = "bbb"; }, ExecutionMode.Synch, 0); pn.AddTransition(o => true, o => o.Number == 1 && o.Word == "bbb", o => { }, o => { o.Number = 2; o.Word = "ccc"; }, ExecutionMode.Synch, 0); pn.AddGeneralTransition(o => true, o => o.Number == 2 && o.Word == "ccc", o => { }, o => { o.Number = 3; o.Word = "ddd"; }, ExecutionMode.Synch, 0); pn.Run(); }
static void Main103(string[] args) { // use PLINQ to process a parallel range //Range() method generates a sequence of stepped integer values IEnumerable <double> result1 = from e in ParallelEnumerable.Range(0, 10) where e % 2 == 0 select Math.Pow(e, 2); // use PLINQ to process a parallel repeating sequence ///Repeat() method generates a sequence that contains the same value repeated over and over. IEnumerable <double> result2 = ParallelEnumerable.Repeat(10, 100) .Select(item => Math.Pow(item, 2)); Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
public void WasChangedGeneralTransitionTest() { //test1 IEnumerable <TestClass> Objects = ParallelEnumerable.Repeat <TestClass>(new TestClass(), 2); Objects.AsParallel().ForAll(o => { o.Number = 2; o.Word = "ccc"; }); GeneralLocker gLocker = new GeneralLocker(); GeneralTransition <TestClass> genTrans = new GeneralTransition <TestClass>(o => true, o => o.Number == 2 && o.Word == "ccc", o => { }, o => { o.Number = 3; o.Word = "ddd"; }, ExecutionMode.Synch, 0, gLocker, 0, Objects.ToList()); genTrans.Do(); Assert.IsTrue(gLocker.WasChanged); }
static void Main(string[] args) { // use PLINQ to process a parallel range IEnumerable <double> result1 = from e in ParallelEnumerable.Range(0, 10) where e % 2 == 0 select Math.Pow(e, 2); // use PLINQ to process a repeating sequence IEnumerable <double> result2 = ParallelEnumerable.Repeat(10, 100) .Select(item => Math.Pow(item, 2)); // wait for input before exiting Console.WriteLine("Press enter to finish"); Console.ReadLine(); }
public void Initialize_Triggers_CustomerArrived_If_Database_Contains_Any_Customer() { bool customerArrived = false; var defaultList = ParallelEnumerable.Repeat(new CustomerForTest(), 1000); _repository.GetAll().Returns(defaultList); _customersQueue = new PersistenceCustomersQueue <CustomerForTest>(_repository); _customersQueue.CustomerArrived += (sender, args) => customerArrived = true; var starTime = DateTime.Now; //wait until the event triggered or timeout. while (!customerArrived && starTime > DateTime.Now.Subtract(TimeSpan.FromSeconds(5))) { Thread.Sleep(100); } Assert.IsTrue(customerArrived); }
public void OrderedSideEffects() { int numRuns = 10000; var x = new Shielded <int>(); var normalFx = new int[numRuns]; int place = -1; ParallelEnumerable.Repeat(0, numRuns) .WithExecutionMode(ParallelExecutionMode.ForceParallelism) .ForAll(_ => { Shield.InTransaction(() => { int old = x; x.Value = old + 1; Shield.SideEffect(() => { int taken = Interlocked.Increment(ref place); normalFx[taken] = old; }); }); }); var syncFx = new int[numRuns]; place = -1; ParallelEnumerable.Repeat(0, numRuns) .WithExecutionMode(ParallelExecutionMode.ForceParallelism) .ForAll(_ => { Shield.InTransaction(() => { int old = x; x.Value = old + 1; Shield.SyncSideEffect(() => { int taken = Interlocked.Increment(ref place); syncFx[taken] = old; }); }); }); Assert.IsTrue(IsSorted(syncFx)); if (IsSorted(normalFx)) { Assert.Inconclusive(); } }