Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[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, 1000);
57                 }
58                 
59                 void AreEquivalent (IEnumerable<int> syncEnumerable, IEnumerable<int> async_resEnumerable, int count)
60                 {
61                         int[] sync  = Enumerable.ToArray(syncEnumerable);
62                         int[] async_res = Enumerable.ToArray(async_resEnumerable);
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_res.Length)
69                                 error = string.Format ("Expected size {0} but got {1} #{2}", sync.Length, async_res.Length, count);
70
71                         Array.Sort (sync);
72                         Array.Sort (async_res);
73                         int i, j;
74                         for (i = j = 0; i < sync.Length && j < async_res.Length; ++i) {
75                                 if (sync [i] != async_res [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> async_resEnumerable, int count)
85                 {
86                         T[] sync  = Enumerable.ToArray(syncEnumerable);
87                         T[] async_res = Enumerable.ToArray(async_resEnumerable);
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_res, "#" + 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_res = baseEnumerable.AsParallel ().Select (i => i * i);
220                                 
221                                 AreEquivalent(sync, async_res, 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_res = baseEnumerable.AsParallel().Where(i => i % 2 == 0);
231                                 
232                                 AreEquivalent(sync, async_res, 1);
233                         });
234                 }
235                 
236                 [Test]
237                 public void CountTestCase ()
238                 {
239                         ParallelTestHelper.Repeat (() => {
240                                 int sync  = baseEnumerable.Count();
241                                 int async_res = baseEnumerable.AsParallel().Count();
242                                 
243                                 Assert.AreEqual(sync, async_res, "#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).AsReallyParallel ().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                 [Test]
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 (),
606                                                                                             (e) => e.Item1,
607                                                                                             (e) => e.Item1,
608                                                                                             (e1, e2) => e1.Item2 + e2.Item2,
609                                                                                             EqualityComparer<int>.Default);
610                                 AreEquivalent (expected, actual, 1);
611                         });
612                 }
613
614                 [Test]
615                 [Category ("NotWorking")] // Deadlocks randomly
616                 public void TestGroupBy ()
617                 {
618                         int num = 100;
619                         Tuple<int, int>[] source = Enumerable.Range (0, num).Select ((i) => Tuple.Create (i / 10, i)).ToArray ();
620
621                         ParallelTestHelper.Repeat (() => {
622                                 ParallelQuery<IGrouping<int, int>> actual = source.AsReallyParallel ().GroupBy ((e) => e.Item1, (e) => e.Item2, EqualityComparer<int>.Default);
623                                 foreach (var group in actual) {
624                                         Assert.GreaterOrEqual (group.Key, 0);
625                                         Assert.Less (group.Key, num / 10);
626
627                                         int count = 0;
628                                         foreach (var e in group) {
629                                                 count++;
630                                                 Assert.GreaterOrEqual (e, group.Key * 10);
631                                                 Assert.Less (e, (group.Key + 1) * 10);
632                                         }
633
634                                         Assert.AreEqual (10, count, "count");
635                                 }
636                         });
637                 }
638                 
639                 [Test]
640                 public void TakeTestCase()
641                 {
642                         ParallelTestHelper.Repeat (() => {
643                                 ParallelQuery<int> async_res = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(800);
644                                 IEnumerable<int> sync = baseEnumerable.Take(800);
645
646                                 AreEquivalent(sync, async_res, 1);
647
648                                 async_res = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(100);
649                                 sync = baseEnumerable.Take(100);
650
651                                 AreEquivalent(sync, async_res, 2);
652                         });
653                 }
654
655                 [TestAttribute]
656                 public void UnorderedTakeTestCase()
657                 {
658                         ParallelTestHelper.Repeat (() => {
659                                 ParallelQuery<int> async_res = baseEnumerable.AsReallyParallel ().Take(800);
660                                 IEnumerable<int> sync = baseEnumerable.Take (800);
661
662                                 Assert.AreEqual (sync.Count (), async_res.Count (), "#1");
663
664                                 async_res = baseEnumerable.AsReallyParallel ().Take(100);
665                                 sync = baseEnumerable.Take(100);
666
667                                 Assert.AreEqual (sync.Count (), async_res.Count (), "#2");
668                         });
669                 }
670                 
671                 [Test]
672                 public void SkipTestCase()
673                 {
674                         ParallelTestHelper.Repeat (() => {
675                                 ParallelQuery<int> async_res = baseEnumerable.AsReallyParallel ().AsOrdered().Skip (800);
676                                 IEnumerable<int> sync = baseEnumerable.Skip (800);
677                                 
678                                 AreEquivalent (sync, async_res, 1);
679                         });
680                 }
681
682                 [Test]
683                 public void SkipTestCaseSmall ()
684                 {
685                         ParallelTestHelper.Repeat (() => {
686                                 var async_res = baseEnumerable.AsReallyParallel ().Skip(100);
687                                 var sync = baseEnumerable.Skip(100);
688                                 
689                                 Assert.AreEqual (sync.Count (), async_res.Count ());
690                         }, 20);
691                 }
692
693                 [Test]
694                 public void ZipTestCase()
695                 {
696                         ParallelTestHelper.Repeat (() => {
697                                 ParallelQuery<int> async_res1 = ParallelEnumerable.Range(0, 10000);
698                                 ParallelQuery<int> async_res2 = ParallelEnumerable.Repeat(1, 10000).Zip(async_res1, (e1, e2) => e1 + e2);
699                                 
700                                 int[] expected = Enumerable.Range (1, 10000).ToArray ();
701                                 CollectionAssert.AreEquivalent(expected, Enumerable.ToArray (async_res2), "#1");
702                         });
703                 }
704                 
705                 [Test]
706                 public void RangeTestCase ()
707                 {
708                         ParallelTestHelper.Repeat (() => {
709                                 IEnumerable<int> sync  = Enumerable.Range(1, 1000);
710                                 IEnumerable<int> async_res = ParallelEnumerable.Range(1, 1000);
711                                 
712                                 AreEquivalent (sync, async_res, 1);
713                         });
714                 }
715                 
716                 [Test]
717                 public void RepeatTestCase ()
718                 {
719                         ParallelTestHelper.Repeat (() => {
720                                 IEnumerable<int> sync  = Enumerable.Repeat(1, 1000);
721                                 IEnumerable<int> async_res = ParallelEnumerable.Repeat(1, 1000);
722                                 
723                                 AreEquivalent (sync, async_res, 1);
724                         });
725                 }
726                 
727                 [Test]
728                 public void TestSum ()
729                 {
730                         int [] data = {1, 2, 3, 4};
731
732                         Assert.AreEqual (10, data.AsReallyParallel ().Sum ());
733                 }
734
735                 [Test]
736                 public void SumOnEmpty ()
737                 {
738                         int [] data = {};
739
740                         Assert.AreEqual (0, data.AsReallyParallel ().Sum ());
741                 }
742
743                 [Test]
744                 public void TestMax ()
745                 {
746                         int [] data = {1, 3, 5, 2};
747
748                         Assert.AreEqual (5, data.AsReallyParallel ().Max ());
749                 }
750
751                 [Test]
752                 public void TestMin ()
753                 {
754                         int [] data = {3, 5, 2, 6, 1, 7};
755
756                         Assert.AreEqual (1, data.AsReallyParallel ().Min ());
757                 }
758                 
759                 [Test]
760                 public void TestToListOrdered ()
761                 {
762                         int [] data = { 2, 3, 5 };
763
764                         var list = data.AsParallel().AsOrdered().WithExecutionMode (ParallelExecutionMode.ForceParallelism).ToList ();
765
766                         AssertAreSame (data, list);
767                         AssertIsOrdered (list, data.Length);
768
769                         Assert.AreEqual (typeof (List<int>), list.GetType ());
770                 }
771
772                 [Test]
773                 public void TestToArrayOrdered ()
774                 {
775                         ICollection<int> coll = new List<int> ();
776                         coll.Add (0);
777                         coll.Add (1);
778                         coll.Add (2);
779
780                         int [] result = {0, 1, 2};
781
782                         var array = coll.AsReallyParallel ().AsOrdered().ToArray ();
783
784                         AssertAreSame (result, array);
785                         AssertIsOrdered (array, result.Length);
786
787                         Assert.AreEqual (typeof (int []), array.GetType ());
788
789                         array = Enumerable.Range (1, 100).Select ((i) => i).AsReallyParallel ().AsOrdered().ToArray ();
790                         result = Enumerable.Range (1, 100).ToArray ();
791
792                         AssertAreSame (result, array);
793                         AssertIsOrdered (array, result.Length);
794
795                         Assert.AreEqual (typeof (int []), array.GetType ());
796
797                 }
798
799                 [Test]
800                 public void TestToList ()
801                 {
802                         int [] data = {3, 5, 2};
803
804                         var list = data.AsReallyParallel ().ToList ();
805
806                         CollectionAssert.AreEquivalent (data, list);
807
808                         Assert.AreEqual (typeof (List<int>), list.GetType ());
809                 }
810
811                 [Test]
812                 public void TestToArray ()
813                 {
814                         ICollection<int> coll = new List<int> ();
815                         coll.Add (0);
816                         coll.Add (1);
817                         coll.Add (2);
818
819                         int [] result = {0, 1, 2};
820
821                         var array = coll.AsReallyParallel ().ToArray ();
822
823                         CollectionAssert.AreEquivalent (result, array);
824
825                         Assert.AreEqual (typeof (int []), array.GetType ());
826                 }
827                 
828                 
829                 [Test]
830                 public void TestAverageOnInt32 ()
831                 {
832                         Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
833                 }
834
835                 [Test]
836                 public void TestAverageOnInt64 ()
837                 {
838                         Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
839                 }
840                 
841                 
842                 [Test]
843                 public void AnyArgumentNullTest ()
844                 {
845                         string [] data = { "2", "1", "5", "3", "4" };
846
847
848                         // Any<TSource> ()
849                         AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (); });
850
851                         // Any<TSource> (Func<TSource, bool>)
852                         AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (x => true); });
853                         AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().Any ((Func<string, bool>) null); });
854                 }
855
856                 [Test]
857                 public void AnyTest ()
858                 {
859                         int [] data = { 5, 2, 3, 1, 6 };
860                         int [] empty = { };
861
862
863                         // Any<TSource> ()
864                         Assert.IsTrue (data.AsReallyParallel ().Any ());
865                         Assert.IsFalse (empty.AsReallyParallel ().Any ());
866
867                         // Any<TSource> (Func<TSource, bool>)
868                         Assert.IsTrue (data.AsReallyParallel ().Any (x => x == 5));
869                         Assert.IsFalse (data.AsReallyParallel ().Any (x => x == 9));
870                         Assert.IsFalse (empty.AsReallyParallel ().Any (x => true));
871                 }
872
873                 
874                 [Test]
875                 public void AllArgumentNullTest ()
876                 {
877                         string [] data = { "2", "1", "5", "3", "4" };
878
879                         AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().All (x => true); });
880                         AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().All ((Func<string, bool>) null); });
881                 }
882
883                 [Test]
884                 public void AllTest ()
885                 {
886                         int [] data = { 5, 2, 3, 1, 6 };
887                         int [] empty = { };
888
889                         Assert.IsTrue (data.AsReallyParallel ().All (x => true));
890                         Assert.IsFalse (data.AsReallyParallel ().All (x => x != 1));
891                         Assert.IsTrue (empty.AsReallyParallel ().All (x => false));
892                 }
893         }
894 }
895
896 #endif