System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System.Core / Test / System.Linq / ParallelEnumerableTests.cs
1 // ParallelEnumerableTests.cs
2 //
3 // Copyright (c) 2008 Jérémie "Garuma" Laval
4 //
5 // Based on Enumerable test suite by Jb Evain (jbevain@novell.com)
6 //
7 // Permission is hereby granted, free of charge, to any person obtaining a copy
8 // of this software and associated documentation files (the "Software"), to deal
9 // in the Software without restriction, including without limitation the rights
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 // copies of the Software, and to permit persons to whom the Software is
12 // furnished to do so, subject to the following conditions:
13 //
14 // The above copyright notice and this permission notice shall be included in
15 // all copies or substantial portions of the Software.
16 //
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 // THE SOFTWARE.
24 //
25 //
26
27 #if NET_4_0
28
29 using System;
30 using System.Threading;
31 using System.Linq;
32
33 using System.Collections;
34 using System.Collections.Generic;
35
36 using NUnit.Framework;
37
38 namespace MonoTests.System.Linq
39 {
40         internal static class AsParallelHelper
41         {
42                 internal static ParallelQuery<T> AsReallyParallel<T> (this IEnumerable<T> source)
43                 {
44                         return source.AsParallel ().WithExecutionMode (ParallelExecutionMode.ForceParallelism);
45                 }
46         }
47
48         [TestFixtureAttribute]
49         public class ParallelEnumerableTests
50         {
51                 IEnumerable<int> baseEnumerable;
52                 
53                 [SetUpAttribute]
54                 public void Setup ()
55                 {
56                         baseEnumerable = Enumerable.Range(1, 10000);
57                 }
58                 
59                 void AreEquivalent (IEnumerable<int> syncEnumerable, IEnumerable<int> asyncEnumerable, int count)
60                 {
61                         int[] sync  = Enumerable.ToArray(syncEnumerable);
62                         int[] async = Enumerable.ToArray(asyncEnumerable);
63                         
64                         // This is not AreEquals because ParallelQuery is non-deterministic (IParallelOrderedEnumerable is)
65                         // thus the order of the initial Enumerable might not be preserved
66                         string error = "";
67
68                         if (sync.Length != async.Length)
69                                 error = string.Format ("Expected size {0} but got {1} #{2}", sync.Length, async.Length, count);
70
71                         Array.Sort (sync);
72                         Array.Sort (async);
73                         int i, j;
74                         for (i = j = 0; i < sync.Length && j < async.Length; ++i) {
75                                 if (sync [i] != async [j])
76                                         error += "missing "  + sync [i] + "";
77                                 else
78                                         ++j;
79                         }
80                         if (error != "")
81                                 Assert.Fail (error);
82                 }
83                 
84                 void AreEquivalent<T> (IEnumerable<T> syncEnumerable, IEnumerable<T> asyncEnumerable, int count)
85                 {
86                         T[] sync  = Enumerable.ToArray(syncEnumerable);
87                         T[] async = Enumerable.ToArray(asyncEnumerable);
88                         
89                         // This is not AreEquals because ParallelQuery is non-deterministic (IParallelOrderedEnumerable is)
90                         // thus the order of the initial Enumerable might not be preserved
91                         CollectionAssert.AreEquivalent(sync, async, "#" + count);
92                 }
93                 
94                 static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
95                 {
96                         if (expected == null) {
97                                 Assert.IsNull (actual);
98                                 return;
99                         }
100
101                         Assert.IsNotNull (actual);
102                         int index = -1;
103
104                         IEnumerator<T> ee = expected.GetEnumerator ();
105                         IEnumerator<T> ea = actual.GetEnumerator ();
106
107                         while (ee.MoveNext ()) {
108                                 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected at index '"+ ++index + "'.");
109                                 Assert.AreEqual (ee.Current, ea.Current, "at index '" + index + "'");
110                         }
111
112                         if (ea.MoveNext ())
113                                 Assert.Fail ("Unexpected element: " + ea.Current);
114                 }
115                 
116                 public static void AssertException<T> (Action action) where T : Exception
117                 {
118                         try {
119                                 action ();
120                         }
121                         catch (T) {
122                                 return;
123                         }
124                         Assert.Fail ("Expected: " + typeof (T).Name);
125                 }
126
127                 static void AssertAreSame<K, V> (K expectedKey, IEnumerable<V> expectedValues, IGrouping<K, V> actual)
128                 {
129                         if (expectedValues == null) {
130                                 Assert.IsNull (actual);
131                                 return;
132                         }
133
134                         Assert.IsNotNull (actual);
135
136                         Assert.AreEqual (expectedKey, actual.Key);
137
138                         var ee = expectedValues.GetEnumerator ();
139                         var ea = actual.GetEnumerator ();
140
141                         while (ee.MoveNext ()) {
142                                 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
143                                 Assert.AreEqual (ee.Current, ea.Current);
144                         }
145
146                         if (ea.MoveNext ())
147                                 Assert.Fail ("Unexpected element: " + ee.Current);
148                 }
149
150                 static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, IEnumerable<IGrouping<K, V>> actual)
151                 {
152                         if (expected == null) {
153                                 Assert.IsNull (actual);
154                                 return;
155                         }
156
157                         Assert.IsNotNull (actual);
158
159                         var ee = expected.GetEnumerator ();
160                         var ea = actual.GetEnumerator ();
161
162                         while (ee.MoveNext ()) {
163                                 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
164                                 AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
165                         }
166
167                         if (ea.MoveNext ())
168                                 Assert.Fail ("Unexpected element: " + ee.Current.Key);
169                 }
170
171                 static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, ILookup<K, V> actual)
172                 {
173                         if (expected == null) {
174                                 Assert.IsNull (actual);
175                                 return;
176                         }
177
178                         Assert.IsNotNull (actual);
179
180                         var ee = expected.GetEnumerator ();
181                         var ea = actual.GetEnumerator ();
182
183                         while (ee.MoveNext ()) {
184                                 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
185                                 AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
186                         }
187
188                         if (ea.MoveNext ())
189                                 Assert.Fail ("Unexpected element: " + ee.Current.Key);
190                 }
191
192                 static void AssertAreSame<K, V> (IDictionary<K, V> expected, IDictionary<K, V> actual)
193                 {
194                         if (expected == null) {
195                                 Assert.IsNull (actual);
196                                 return;
197                         }
198
199                         Assert.IsNotNull (actual);
200
201                         var ee = expected.GetEnumerator ();
202                         var ea = actual.GetEnumerator ();
203
204                         while (ee.MoveNext ()) {
205                                 Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + ", " + ee.Current.Value + "' expected.");
206                                 Assert.AreEqual (ee.Current.Key, ea.Current.Key);
207                                 Assert.AreEqual (ee.Current.Value, ea.Current.Value);
208                         }
209
210                         if (ea.MoveNext ())
211                                 Assert.Fail ("Unexpected element: " + ee.Current.Key + ", " + ee.Current.Value);
212                 }
213
214                 [Test]
215                 public void SelectTestCase ()
216                 {
217                         ParallelTestHelper.Repeat (() => {
218                                 IEnumerable<int> sync  = baseEnumerable.Select (i => i * i);
219                                 IEnumerable<int> async = baseEnumerable.AsParallel ().Select (i => i * i);
220                                 
221                                 AreEquivalent(sync, async, 1);
222                         });
223                 }
224                         
225                 [Test]
226                 public void WhereTestCase ()
227                 {
228                         ParallelTestHelper.Repeat (() => {
229                                 IEnumerable<int> sync  = baseEnumerable.Where(i => i % 2 == 0);
230                                 IEnumerable<int> async = baseEnumerable.AsParallel().Where(i => i % 2 == 0);
231                                 
232                                 AreEquivalent(sync, async, 1);
233                         });
234                 }
235                 
236                 [Test]
237                 public void CountTestCase ()
238                 {
239                         ParallelTestHelper.Repeat (() => {
240                                 int sync  = baseEnumerable.Count();
241                                 int async = baseEnumerable.AsParallel().Count();
242                                 
243                                 Assert.AreEqual(sync, async, "#1");
244                         });
245                 }
246                 
247                 [Test]
248                 public void AggregateTestCase ()
249                 {
250                         ParallelTestHelper.Repeat (() => {
251                                 ParallelQuery<int> range = ParallelEnumerable.Repeat (5, 2643);
252                                 double average = range.Aggregate(() => new double[2],
253                                                                  (acc, elem) => { acc[0] += elem; acc[1]++; return acc; },
254                                 (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
255                                 acc => acc[0] / acc[1]);
256                                 
257                                 Assert.AreEqual(5.0, average, "#1");
258                         });
259                 }
260                 
261                 [Test]
262                 public void TestSimpleExcept ()
263                 {
264                         ParallelTestHelper.Repeat (() => {
265                                 int [] first = {0, 1, 2, 3, 4, 5};
266                                 int [] second = {2, 4, 6};
267                                 int [] result = {0, 1, 3, 5};
268         
269                                 AreEquivalent (result, first.AsReallyParallel ().Except (second.AsParallel ()), 1);
270                         });
271                 }
272
273                 [Test]
274                 public void TestSimpleIntersect ()
275                 {
276                         ParallelTestHelper.Repeat (() => {
277                                 int [] first = {0, 1, 2, 3, 4, 5};
278                                 int [] second = {2, 4, 6};
279                                 int [] result = {2, 4};
280         
281                                 AreEquivalent (result, first.AsReallyParallel ().Intersect (second.AsParallel ()), 1);
282                         });
283                 }
284
285                 [Test]
286                 public void TestSimpleUnion ()
287                 {
288                         ParallelTestHelper.Repeat (() => {
289                                 int [] first = {0, 1, 2, 3, 4, 5};
290                                 int [] second = {2, 4, 6};
291                                 int [] result = {0, 1, 2, 3, 4, 5, 6};
292                                 
293                                 AreEquivalent (result, first.AsReallyParallel ().Union (second.AsParallel ()), 1);
294                         });
295                 }
296
297                 [Test]
298                 public void TestBigUnion ()
299                 {
300                         ParallelTestHelper.Repeat (() => {
301                                 int [] first = Enumerable.Range (1, 10000).ToArray ();
302                                 int [] second = Enumerable.Range (323, 757).ToArray ();
303
304                                 AreEquivalent (first, first.AsReallyParallel ().Union (second.AsParallel ()), 1);
305                         }, 10);
306                 }
307
308                 [Test]
309                 public void TestBigIntersect ()
310                 {
311                         ParallelTestHelper.Repeat (() => {
312                                 int [] first = Enumerable.Range (1, 10000).ToArray ();
313                                 int [] second = Enumerable.Range (323, 757).ToArray ();
314
315                                 AreEquivalent (second, first.AsReallyParallel ().Intersect (second.AsParallel ()), 1);
316                         }, 10);
317                 }
318                 
319                 class Foo {}
320                 class Bar : Foo {}
321
322                 [Test]
323                 public void TestCast ()
324                 {
325                         Bar a = new Bar ();
326                         Bar b = new Bar ();
327                         Bar c = new Bar ();
328
329                         Foo [] foos = new Foo [] {a, b, c};
330                         Bar [] result = new Bar [] {a, b, c};
331
332                         AreEquivalent (result, foos.AsReallyParallel ().Cast<Bar> (), 1);
333                 }
334                 
335                 [Test]
336                 public void TestSkip ()
337                 {
338                         int [] data = {0, 1, 2, 3, 4, 5};
339                         int [] result = {3, 4, 5};
340
341                         AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Skip (3).ToArray ());
342                 }
343                 
344                 [Test]
345                 public void TestSkipIterating ()
346                 {
347                         int [] data = {0, 1, 2, 3, 4, 5};
348                         int [] result = {3, 4, 5};
349
350                         AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Skip (3));
351                 }
352
353                 [Test]
354                 public void TestSkipWhile ()
355                 {
356                         int [] data = {0, 1, 2, 3, 4, 5};
357                         int [] result = {3, 4, 5};
358
359                         AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().SkipWhile (i => i < 3));
360                 }
361
362                 [Test]
363                 public void TestTake ()
364                 {
365                         int [] data = {0, 1, 2, 3, 4, 5};
366                         int [] result = {0, 1, 2};
367
368                         AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Take (3));
369                 }
370
371                 [Test]
372                 public void TestTakeWhile ()
373                 {
374                         int [] data = {0, 1, 2, 3, 4, 5};
375                         int [] result = {0, 1, 2};
376
377                         AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().TakeWhile (i => i < 3));
378                 }
379
380                 [Test]
381                 public void SelectManyTest ()
382                 {
383                         IEnumerable<int> initial = Enumerable.Range (1, 50);
384                         IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
385
386                         ParallelTestHelper.Repeat (() => {
387                                         var actual = initial.AsReallyParallel ().SelectMany ((i) => Enumerable.Range (1, i));
388                                         AreEquivalent (expected, actual, 1);
389                                 });
390                 }
391
392                 [Test]
393                 public void SelectManyOrderedTest ()
394                 {
395                         IEnumerable<int> initial = Enumerable.Range (1, 50);
396                         IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
397
398                         ParallelTestHelper.Repeat (() => {
399                                         var actual = initial.AsReallyParallel ().AsOrdered ().SelectMany ((i) => Enumerable.Range (1, i));
400                                         AssertAreSame (expected, actual);
401                                 });
402                 }
403                 
404                 [Test]
405                 public void TestLast ()
406                 {
407                         int [] data = {1, 2, 3};
408
409                         Assert.AreEqual (3, data.AsReallyParallel ().AsOrdered ().Last ());
410                 }
411
412                 [Test]
413                 public void TestLastOrDefault ()
414                 {
415                         int [] data = {};
416
417                         Assert.AreEqual (default (int), data.AsReallyParallel ().AsOrdered ().LastOrDefault ());
418                 }
419
420                 [Test]
421                 public void TestFirst ()
422                 {
423                         int [] data = {1, 2, 3};
424
425                         Assert.AreEqual (1, data.AsReallyParallel ().AsOrdered ().First ());
426                 }
427
428                 [Test]
429                 public void TestFirstOrDefault ()
430                 {
431                         int [] data = {};
432
433                         Assert.AreEqual (default (int), data.AsReallyParallel ().AsOrdered ().FirstOrDefault ());
434                 }
435                 
436                 [Test]
437                 public void TestReverse ()
438                 {
439                         int [] data = {0, 1, 2, 3, 4};
440                         int [] result = {4, 3, 2, 1, 0};
441
442                         AssertAreSame (result, ((IEnumerable<int>)data).Select ((i) => i).AsReallyParallel ().AsOrdered ().Reverse ());
443                         AssertAreSame (result, ParallelEnumerable.Range (0, 5).WithExecutionMode (ParallelExecutionMode.ForceParallelism).AsOrdered ().Reverse ());
444                 }
445                 
446                 [Test]
447                 public void TestOrderBy ()
448                 {
449                         ParallelTestHelper.Repeat (() => {
450                                 int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
451                                 
452                                 var q = array.AsReallyParallel ().OrderBy ((i) => i);
453                                 AssertIsOrdered (q, array.Length);
454                         });
455                 }
456
457                 class Baz {
458                         string name;
459                         int age;
460
461                         public string Name
462                         {
463                                 get {
464                                         if (string.IsNullOrEmpty (name))
465                                                 return Age.ToString ();
466
467                                         return name + " (" + Age + ")";
468                                 }
469                         }
470
471                         public int Age
472                         {
473                                 get { return age + 1; }
474                         }
475
476                         public Baz (string name, int age)
477                         {
478                                 this.name = name;
479                                 this.age = age;
480                         }
481
482                         public override int GetHashCode ()
483                         {
484                                 return this.Age ^ this.Name.GetHashCode ();
485                         }
486
487                         public override bool Equals (object obj)
488                         {
489                                 Baz b = obj as Baz;
490                                 if (b == null)
491                                         return false;
492
493                                 return b.Age == this.Age && b.Name == this.Name;
494                         }
495
496                         public override string ToString ()
497                         {
498                                 return this.Name;
499                         }
500                 }
501
502                 static IEnumerable<Baz> CreateBazCollection ()
503                 {
504                         return new [] {
505                                 new Baz ("jb", 25),
506                                 new Baz ("ana", 20),
507                                 new Baz ("reg", 28),
508                                 new Baz ("ro", 25),
509                                 new Baz ("jb", 7),
510                         };
511                 }
512
513                 [Test]
514                 public void TestOrderByAgeAscendingTheByNameDescending ()
515                 {
516                         ParallelTestHelper.Repeat (() => {
517                                 var q = from b in CreateBazCollection ().AsReallyParallel ()
518                                                 orderby b.Age ascending, b.Name descending
519                                                 select b;
520         
521                                 var expected = new [] {
522                                         new Baz ("jb", 7),
523                                         new Baz ("ana", 20),
524                                         new Baz ("ro", 25),
525                                         new Baz ("jb", 25),
526                                         new Baz ("reg", 28),
527                                 };
528
529                                 AssertAreSame (expected, q);
530                         });
531                 }
532
533                 class Data {
534                         public int ID { get; set; }
535                         public string Name { get; set; }
536
537                         public override string ToString ()
538                         {
539                                 return ID + " " + Name;
540                         }
541                 }
542
543                 IEnumerable<Data> CreateData ()
544                 {
545                         return new [] {
546                                 new Data { ID = 10, Name = "bcd" },
547                                 new Data { ID = 20, Name = "Abcd" },
548                                 new Data { ID = 20, Name = "Ab" },
549                                 new Data { ID = 10, Name = "Zyx" },
550                         };
551                 }
552
553                 [Test]
554                 public void TestOrderByIdDescendingThenByNameAscending ()
555                 {
556                         ParallelTestHelper.Repeat (() => {
557                                 var q = from d in CreateData ().AsReallyParallel ()
558                                         orderby d.ID descending, d.Name ascending
559                                                 select d;
560                                 
561                                 var list = new List<Data> (q);
562                                 
563                                 Assert.AreEqual ("Ab", list [0].Name);
564                                 Assert.AreEqual ("Abcd", list [1].Name);
565                                 Assert.AreEqual ("bcd", list [2].Name);
566                                 Assert.AreEqual ("Zyx", list [3].Name);
567                         });
568                 }
569
570                 static void AssertIsOrdered (IEnumerable<int> e, int count)
571                 {
572                         int f = int.MinValue;
573                         int c = 0;
574                         
575                         foreach (int i in e) {
576                                 Assert.IsTrue (f <= i, string.Format ("{0} <= {1}", f, i));
577                                 f = i;
578                                 c++;
579                         }
580                         
581                         Assert.AreEqual (count, c);
582                 }
583                 
584                 
585                 [TestAttribute]
586                 public void ElementAtTestCase()
587                 {
588                         //ParallelTestHelper.Repeat (() => {
589                                         Assert.AreEqual(1, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(0), "#1");
590                                         Assert.AreEqual(51, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(50), "#2");
591                                         Assert.AreEqual(489, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(488), "#3");
592                         //});
593                 }
594
595                 [Test]
596                 public void TestJoin ()
597                 {
598                         int num = 100;
599                         Tuple<int, int>[] outer = Enumerable.Range (1, 50).Select ((i) => Tuple.Create (i, num - 2 * i)).ToArray ();
600                         Tuple<int, int>[] inner = Enumerable.Range (1, 50).Reverse ().Select ((i) => Tuple.Create (i, 2 * i)).ToArray ();
601
602                         IEnumerable<int> expected = outer.Join (inner, (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
603
604                         ParallelTestHelper.Repeat (() => {
605                                         ParallelQuery<int> actual = outer.AsReallyParallel ().Join (inner.AsParallel (), (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
606
607                                         AreEquivalent (expected, actual, 1);
608                                 });
609                 }
610
611                 [Test]
612                 public void TestGroupBy ()
613                 {
614                         int num = 100;
615                         Tuple<int, int>[] source = Enumerable.Range (0, num).Select ((i) => Tuple.Create (i / 10, i)).ToArray ();
616
617                         ParallelTestHelper.Repeat (() => {
618                                         ParallelQuery<IGrouping<int, int>> actual = source.AsReallyParallel ().GroupBy ((e) => e.Item1, (e) => e.Item2, EqualityComparer<int>.Default);
619
620                                         foreach (var group in actual) {
621                                                 Assert.GreaterOrEqual (group.Key, 0);
622                                                 Assert.Less (group.Key, num / 10);
623
624                                                 int count = 0;
625                                                 foreach (var e in group) {
626                                                         count++;
627                                                         Assert.GreaterOrEqual (e, group.Key * 10);
628                                                         Assert.Less (e, (group.Key + 1) * 10);
629                                                 }
630
631                                                 Assert.AreEqual (10, count, "count");
632                                         }
633                                 });
634                 }
635                 
636                 [TestAttribute]
637                 public void TakeTestCase()
638                 {
639                         ParallelTestHelper.Repeat (() => {
640                                         ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(2000);
641                                         IEnumerable<int> sync = baseEnumerable.Take(2000);
642
643                                         AreEquivalent(sync, async, 1);
644
645                                         async = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(100);
646                                         sync = baseEnumerable.Take(100);
647
648                                         AreEquivalent(sync, async, 2);
649                                 }, 20);
650                 }
651
652                 [TestAttribute]
653                 public void UnorderedTakeTestCase()
654                 {
655                         ParallelTestHelper.Repeat (() => {
656                                         ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().Take(2000);
657                                         IEnumerable<int> sync = baseEnumerable.Take (2000);
658
659                                         Assert.AreEqual (sync.Count (), async.Count (), "#1");
660
661                                         async = baseEnumerable.AsReallyParallel ().Take(100);
662                                         sync = baseEnumerable.Take(100);
663
664                                         Assert.AreEqual (sync.Count (), async.Count (), "#2");
665                                 }, 20);
666                 }
667                 
668                 [Test]
669                 public void SkipTestCase()
670                 {
671                         ParallelTestHelper.Repeat (() => {
672                                 ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().AsOrdered().Skip(2000);
673                                 IEnumerable<int> sync = baseEnumerable.Skip(2000);
674                                 
675                                 AreEquivalent(sync, async, 1);
676                         }, 20);
677                 }
678
679                 [Test]
680                 public void SkipTestCaseSmall ()
681                 {
682                         ParallelTestHelper.Repeat (() => {
683                                 var async = baseEnumerable.AsReallyParallel ().Skip(100);
684                                 var sync = baseEnumerable.Skip(100);
685                                 
686                                 Assert.AreEqual (sync.Count (), async.Count ());
687                         }, 20);
688                 }
689
690                 [Test]
691                 public void ZipTestCase()
692                 {
693                         ParallelTestHelper.Repeat (() => {
694                                 ParallelQuery<int> async1 = ParallelEnumerable.Range(0, 10000);
695                                 ParallelQuery<int> async2 = ParallelEnumerable.Repeat(1, 10000).Zip(async1, (e1, e2) => e1 + e2);
696                                 
697                                 int[] expected = Enumerable.Range (1, 10000).ToArray ();
698                                 CollectionAssert.AreEquivalent(expected, Enumerable.ToArray (async2), "#1");
699                         });
700                 }
701                 
702                 [Test]
703                 public void RangeTestCase ()
704                 {
705                         ParallelTestHelper.Repeat (() => {
706                                 IEnumerable<int> sync  = Enumerable.Range(1, 1000);
707                                 IEnumerable<int> async = ParallelEnumerable.Range(1, 1000);
708                                 
709                                 AreEquivalent (sync, async, 1);
710                         });
711                 }
712                 
713                 [Test]
714                 public void RepeatTestCase ()
715                 {
716                         ParallelTestHelper.Repeat (() => {
717                                 IEnumerable<int> sync  = Enumerable.Repeat(1, 1000);
718                                 IEnumerable<int> async = ParallelEnumerable.Repeat(1, 1000);
719                                 
720                                 AreEquivalent (sync, async, 1);
721                         });
722                 }
723                 
724                 [Test]
725                 public void TestSum ()
726                 {
727                         int [] data = {1, 2, 3, 4};
728
729                         Assert.AreEqual (10, data.AsReallyParallel ().Sum ());
730                 }
731
732                 [Test]
733                 public void SumOnEmpty ()
734                 {
735                         int [] data = {};
736
737                         Assert.AreEqual (0, data.AsReallyParallel ().Sum ());
738                 }
739
740                 [Test]
741                 public void TestMax ()
742                 {
743                         int [] data = {1, 3, 5, 2};
744
745                         Assert.AreEqual (5, data.AsReallyParallel ().Max ());
746                 }
747
748                 [Test]
749                 public void TestMin ()
750                 {
751                         int [] data = {3, 5, 2, 6, 1, 7};
752
753                         Assert.AreEqual (1, data.AsReallyParallel ().Min ());
754                 }
755                 
756                 [Test]
757                 public void TestToListOrdered ()
758                 {
759                         int [] data = { 2, 3, 5 };
760
761                         var list = data.AsParallel().AsOrdered().WithExecutionMode (ParallelExecutionMode.ForceParallelism).ToList ();
762
763                         AssertAreSame (data, list);
764                         AssertIsOrdered (list, data.Length);
765
766                         Assert.AreEqual (typeof (List<int>), list.GetType ());
767                 }
768
769                 [Test]
770                 public void TestToArrayOrdered ()
771                 {
772                         ICollection<int> coll = new List<int> ();
773                         coll.Add (0);
774                         coll.Add (1);
775                         coll.Add (2);
776
777                         int [] result = {0, 1, 2};
778
779                         var array = coll.AsReallyParallel ().AsOrdered().ToArray ();
780
781                         AssertAreSame (result, array);
782                         AssertIsOrdered (array, result.Length);
783
784                         Assert.AreEqual (typeof (int []), array.GetType ());
785
786                         array = Enumerable.Range (1, 100).Select ((i) => i).AsReallyParallel ().AsOrdered().ToArray ();
787                         result = Enumerable.Range (1, 100).ToArray ();
788
789                         AssertAreSame (result, array);
790                         AssertIsOrdered (array, result.Length);
791
792                         Assert.AreEqual (typeof (int []), array.GetType ());
793
794                 }
795
796                 [Test]
797                 public void TestToList ()
798                 {
799                         int [] data = {3, 5, 2};
800
801                         var list = data.AsReallyParallel ().ToList ();
802
803                         CollectionAssert.AreEquivalent (data, list);
804
805                         Assert.AreEqual (typeof (List<int>), list.GetType ());
806                 }
807
808                 [Test]
809                 public void TestToArray ()
810                 {
811                         ICollection<int> coll = new List<int> ();
812                         coll.Add (0);
813                         coll.Add (1);
814                         coll.Add (2);
815
816                         int [] result = {0, 1, 2};
817
818                         var array = coll.AsReallyParallel ().ToArray ();
819
820                         CollectionAssert.AreEquivalent (result, array);
821
822                         Assert.AreEqual (typeof (int []), array.GetType ());
823                 }
824                 
825                 
826                 [Test]
827                 public void TestAverageOnInt32 ()
828                 {
829                         Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
830                 }
831
832                 [Test]
833                 public void TestAverageOnInt64 ()
834                 {
835                         Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
836                 }
837                 
838                 
839                 [Test]
840                 public void AnyArgumentNullTest ()
841                 {
842                         string [] data = { "2", "1", "5", "3", "4" };
843
844
845                         // Any<TSource> ()
846                         AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (); });
847
848                         // Any<TSource> (Func<TSource, bool>)
849                         AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (x => true); });
850                         AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().Any ((Func<string, bool>) null); });
851                 }
852
853                 [Test]
854                 public void AnyTest ()
855                 {
856                         int [] data = { 5, 2, 3, 1, 6 };
857                         int [] empty = { };
858
859
860                         // Any<TSource> ()
861                         Assert.IsTrue (data.AsReallyParallel ().Any ());
862                         Assert.IsFalse (empty.AsReallyParallel ().Any ());
863
864                         // Any<TSource> (Func<TSource, bool>)
865                         Assert.IsTrue (data.AsReallyParallel ().Any (x => x == 5));
866                         Assert.IsFalse (data.AsReallyParallel ().Any (x => x == 9));
867                         Assert.IsFalse (empty.AsReallyParallel ().Any (x => true));
868                 }
869
870                 
871                 [Test]
872                 public void AllArgumentNullTest ()
873                 {
874                         string [] data = { "2", "1", "5", "3", "4" };
875
876                         AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().All (x => true); });
877                         AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().All ((Func<string, bool>) null); });
878                 }
879
880                 [Test]
881                 public void AllTest ()
882                 {
883                         int [] data = { 5, 2, 3, 1, 6 };
884                         int [] empty = { };
885
886                         Assert.IsTrue (data.AsReallyParallel ().All (x => true));
887                         Assert.IsFalse (data.AsReallyParallel ().All (x => x != 1));
888                         Assert.IsTrue (empty.AsReallyParallel ().All (x => false));
889                 }
890         }
891 }
892
893 #endif