ParallelEnumerableTests.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. // ParallelEnumerableTests.cs
  2. //
  3. // Copyright (c) 2008 Jérémie "Garuma" Laval
  4. //
  5. // Based on Enumerable test suite by Jb Evain ([email protected])
  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. #if NET_4_0
  27. using System;
  28. using System.Threading;
  29. using System.Linq;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using NUnit.Framework;
  33. namespace MonoTests.System.Linq
  34. {
  35. internal static class AsParallelHelper
  36. {
  37. internal static ParallelQuery<T> AsReallyParallel<T> (this IEnumerable<T> source)
  38. {
  39. return source.AsParallel ().WithExecutionMode (ParallelExecutionMode.ForceParallelism);
  40. }
  41. }
  42. [TestFixtureAttribute]
  43. public class ParallelEnumerableTests
  44. {
  45. IEnumerable<int> baseEnumerable;
  46. [SetUpAttribute]
  47. public void Setup ()
  48. {
  49. baseEnumerable = Enumerable.Range(1, 10000);
  50. }
  51. void AreEquivalent (IEnumerable<int> syncEnumerable, IEnumerable<int> asyncEnumerable, int count)
  52. {
  53. int[] sync = Enumerable.ToArray(syncEnumerable);
  54. int[] async = Enumerable.ToArray(asyncEnumerable);
  55. // This is not AreEquals because ParallelQuery is non-deterministic (IParallelOrderedEnumerable is)
  56. // thus the order of the initial Enumerable might not be preserved
  57. CollectionAssert.AreEquivalent(sync, async, "#" + count);
  58. }
  59. void AreEquivalent<T> (IEnumerable<T> syncEnumerable, IEnumerable<T> asyncEnumerable, int count)
  60. {
  61. T[] sync = Enumerable.ToArray(syncEnumerable);
  62. T[] async = Enumerable.ToArray(asyncEnumerable);
  63. // This is not AreEquals because ParallelQuery is non-deterministic (IParallelOrderedEnumerable is)
  64. // thus the order of the initial Enumerable might not be preserved
  65. CollectionAssert.AreEquivalent(sync, async, "#" + count);
  66. }
  67. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  68. {
  69. if (expected == null) {
  70. Assert.IsNull (actual);
  71. return;
  72. }
  73. Assert.IsNotNull (actual);
  74. int index = -1;
  75. IEnumerator<T> ee = expected.GetEnumerator ();
  76. IEnumerator<T> ea = actual.GetEnumerator ();
  77. while (ee.MoveNext ()) {
  78. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected at index '"+ ++index + "'.");
  79. Assert.AreEqual (ee.Current, ea.Current, "at index '" + index + "'");
  80. }
  81. if (ea.MoveNext ())
  82. Assert.Fail ("Unexpected element: " + ea.Current);
  83. }
  84. public static void AssertException<T> (Action action) where T : Exception
  85. {
  86. try {
  87. action ();
  88. }
  89. catch (T) {
  90. return;
  91. }
  92. Assert.Fail ("Expected: " + typeof (T).Name);
  93. }
  94. static void AssertAreSame<K, V> (K expectedKey, IEnumerable<V> expectedValues, IGrouping<K, V> actual)
  95. {
  96. if (expectedValues == null) {
  97. Assert.IsNull (actual);
  98. return;
  99. }
  100. Assert.IsNotNull (actual);
  101. Assert.AreEqual (expectedKey, actual.Key);
  102. var ee = expectedValues.GetEnumerator ();
  103. var ea = actual.GetEnumerator ();
  104. while (ee.MoveNext ()) {
  105. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  106. Assert.AreEqual (ee.Current, ea.Current);
  107. }
  108. if (ea.MoveNext ())
  109. Assert.Fail ("Unexpected element: " + ee.Current);
  110. }
  111. static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, IEnumerable<IGrouping<K, V>> actual)
  112. {
  113. if (expected == null) {
  114. Assert.IsNull (actual);
  115. return;
  116. }
  117. Assert.IsNotNull (actual);
  118. var ee = expected.GetEnumerator ();
  119. var ea = actual.GetEnumerator ();
  120. while (ee.MoveNext ()) {
  121. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
  122. AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
  123. }
  124. if (ea.MoveNext ())
  125. Assert.Fail ("Unexpected element: " + ee.Current.Key);
  126. }
  127. static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, ILookup<K, V> actual)
  128. {
  129. if (expected == null) {
  130. Assert.IsNull (actual);
  131. return;
  132. }
  133. Assert.IsNotNull (actual);
  134. var ee = expected.GetEnumerator ();
  135. var ea = actual.GetEnumerator ();
  136. while (ee.MoveNext ()) {
  137. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
  138. AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
  139. }
  140. if (ea.MoveNext ())
  141. Assert.Fail ("Unexpected element: " + ee.Current.Key);
  142. }
  143. static void AssertAreSame<K, V> (IDictionary<K, V> expected, IDictionary<K, V> actual)
  144. {
  145. if (expected == null) {
  146. Assert.IsNull (actual);
  147. return;
  148. }
  149. Assert.IsNotNull (actual);
  150. var ee = expected.GetEnumerator ();
  151. var ea = actual.GetEnumerator ();
  152. while (ee.MoveNext ()) {
  153. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + ", " + ee.Current.Value + "' expected.");
  154. Assert.AreEqual (ee.Current.Key, ea.Current.Key);
  155. Assert.AreEqual (ee.Current.Value, ea.Current.Value);
  156. }
  157. if (ea.MoveNext ())
  158. Assert.Fail ("Unexpected element: " + ee.Current.Key + ", " + ee.Current.Value);
  159. }
  160. [Test]
  161. public void SelectTestCase ()
  162. {
  163. ParallelTestHelper.Repeat (() => {
  164. IEnumerable<int> sync = baseEnumerable.Select (i => i * i);
  165. IEnumerable<int> async = baseEnumerable.AsParallel ().Select (i => i * i);
  166. AreEquivalent(sync, async, 1);
  167. });
  168. }
  169. [Test]
  170. public void WhereTestCase ()
  171. {
  172. ParallelTestHelper.Repeat (() => {
  173. IEnumerable<int> sync = baseEnumerable.Where(i => i % 2 == 0);
  174. IEnumerable<int> async = baseEnumerable.AsParallel().Where(i => i % 2 == 0);
  175. AreEquivalent(sync, async, 1);
  176. });
  177. }
  178. [Test]
  179. public void CountTestCase ()
  180. {
  181. ParallelTestHelper.Repeat (() => {
  182. int sync = baseEnumerable.Count();
  183. int async = baseEnumerable.AsParallel().Count();
  184. Assert.AreEqual(sync, async, "#1");
  185. });
  186. }
  187. [Test]
  188. public void AggregateTestCase ()
  189. {
  190. ParallelTestHelper.Repeat (() => {
  191. ParallelQuery<int> range = ParallelEnumerable.Repeat (5, 2643);
  192. double average = range.Aggregate(() => new double[2],
  193. (acc, elem) => { acc[0] += elem; acc[1]++; return acc; },
  194. (acc1, acc2) => { acc1[0] += acc2[0]; acc1[1] += acc2[1]; return acc1; },
  195. acc => acc[0] / acc[1]);
  196. Assert.AreEqual(5.0, average, "#1");
  197. });
  198. }
  199. [Test]
  200. public void TestSimpleExcept ()
  201. {
  202. ParallelTestHelper.Repeat (() => {
  203. int [] first = {0, 1, 2, 3, 4, 5};
  204. int [] second = {2, 4, 6};
  205. int [] result = {0, 1, 3, 5};
  206. AreEquivalent (result, first.AsReallyParallel ().Except (second.AsParallel ()), 1);
  207. });
  208. }
  209. [Test]
  210. public void TestSimpleIntersect ()
  211. {
  212. ParallelTestHelper.Repeat (() => {
  213. int [] first = {0, 1, 2, 3, 4, 5};
  214. int [] second = {2, 4, 6};
  215. int [] result = {2, 4};
  216. AreEquivalent (result, first.AsReallyParallel ().Intersect (second.AsParallel ()), 1);
  217. });
  218. }
  219. [Test]
  220. public void TestSimpleUnion ()
  221. {
  222. ParallelTestHelper.Repeat (() => {
  223. int [] first = {0, 1, 2, 3, 4, 5};
  224. int [] second = {2, 4, 6};
  225. int [] result = {0, 1, 2, 3, 4, 5, 6};
  226. AreEquivalent (result, first.AsReallyParallel ().Union (second.AsParallel ()), 1);
  227. });
  228. }
  229. [Test]
  230. public void TestBigUnion ()
  231. {
  232. ParallelTestHelper.Repeat (() => {
  233. int [] first = Enumerable.Range (1, 10000).ToArray ();
  234. int [] second = Enumerable.Range (323, 757).ToArray ();
  235. AreEquivalent (first, first.AsReallyParallel ().Union (second.AsParallel ()), 1);
  236. }, 10);
  237. }
  238. [Test]
  239. public void TestBigIntersect ()
  240. {
  241. ParallelTestHelper.Repeat (() => {
  242. int [] first = Enumerable.Range (1, 10000).ToArray ();
  243. int [] second = Enumerable.Range (323, 757).ToArray ();
  244. AreEquivalent (second, first.AsReallyParallel ().Intersect (second.AsParallel ()), 1);
  245. }, 10);
  246. }
  247. class Foo {}
  248. class Bar : Foo {}
  249. [Test]
  250. public void TestCast ()
  251. {
  252. Bar a = new Bar ();
  253. Bar b = new Bar ();
  254. Bar c = new Bar ();
  255. Foo [] foos = new Foo [] {a, b, c};
  256. Bar [] result = new Bar [] {a, b, c};
  257. AreEquivalent (result, foos.AsReallyParallel ().Cast<Bar> (), 1);
  258. }
  259. [Test]
  260. public void TestSkip ()
  261. {
  262. int [] data = {0, 1, 2, 3, 4, 5};
  263. int [] result = {3, 4, 5};
  264. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Skip (3).ToArray ());
  265. }
  266. [Test]
  267. public void TestSkipIterating ()
  268. {
  269. int [] data = {0, 1, 2, 3, 4, 5};
  270. int [] result = {3, 4, 5};
  271. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Skip (3));
  272. }
  273. [Test]
  274. public void TestSkipWhile ()
  275. {
  276. int [] data = {0, 1, 2, 3, 4, 5};
  277. int [] result = {3, 4, 5};
  278. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().SkipWhile (i => i < 3));
  279. }
  280. [Test]
  281. public void TestTake ()
  282. {
  283. int [] data = {0, 1, 2, 3, 4, 5};
  284. int [] result = {0, 1, 2};
  285. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Take (3));
  286. }
  287. [Test]
  288. public void TestTakeWhile ()
  289. {
  290. int [] data = {0, 1, 2, 3, 4, 5};
  291. int [] result = {0, 1, 2};
  292. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().TakeWhile (i => i < 3));
  293. }
  294. [Test]
  295. public void SelectManyTest ()
  296. {
  297. IEnumerable<int> initial = Enumerable.Range (1, 50);
  298. IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
  299. ParallelTestHelper.Repeat (() => {
  300. var actual = initial.AsReallyParallel ().SelectMany ((i) => Enumerable.Range (1, i));
  301. AreEquivalent (expected, actual, 1);
  302. });
  303. }
  304. [Test]
  305. public void SelectManyOrderedTest ()
  306. {
  307. IEnumerable<int> initial = Enumerable.Range (1, 50);
  308. IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
  309. ParallelTestHelper.Repeat (() => {
  310. var actual = initial.AsReallyParallel ().AsOrdered ().SelectMany ((i) => Enumerable.Range (1, i));
  311. AssertAreSame (expected, actual);
  312. });
  313. }
  314. [Test]
  315. public void TestLast ()
  316. {
  317. int [] data = {1, 2, 3};
  318. Assert.AreEqual (3, data.AsReallyParallel ().AsOrdered ().Last ());
  319. }
  320. [Test]
  321. public void TestLastOrDefault ()
  322. {
  323. int [] data = {};
  324. Assert.AreEqual (default (int), data.AsReallyParallel ().AsOrdered ().LastOrDefault ());
  325. }
  326. [Test]
  327. public void TestFirst ()
  328. {
  329. int [] data = {1, 2, 3};
  330. Assert.AreEqual (1, data.AsReallyParallel ().AsOrdered ().First ());
  331. }
  332. [Test]
  333. public void TestFirstOrDefault ()
  334. {
  335. int [] data = {};
  336. Assert.AreEqual (default (int), data.AsReallyParallel ().AsOrdered ().FirstOrDefault ());
  337. }
  338. [Test]
  339. public void TestReverse ()
  340. {
  341. int [] data = {0, 1, 2, 3, 4};
  342. int [] result = {4, 3, 2, 1, 0};
  343. AssertAreSame (result, ((IEnumerable<int>)data).Select ((i) => i).AsReallyParallel ().AsOrdered ().Reverse ());
  344. AssertAreSame (result, ParallelEnumerable.Range (0, 5).WithExecutionMode (ParallelExecutionMode.ForceParallelism).AsOrdered ().Reverse ());
  345. }
  346. [Test]
  347. public void TestOrderBy ()
  348. {
  349. ParallelTestHelper.Repeat (() => {
  350. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  351. var q = array.AsReallyParallel ().OrderBy ((i) => i);
  352. AssertIsOrdered (q, array.Length);
  353. });
  354. }
  355. class Baz {
  356. string name;
  357. int age;
  358. public string Name
  359. {
  360. get {
  361. if (string.IsNullOrEmpty (name))
  362. return Age.ToString ();
  363. return name + " (" + Age + ")";
  364. }
  365. }
  366. public int Age
  367. {
  368. get { return age + 1; }
  369. }
  370. public Baz (string name, int age)
  371. {
  372. this.name = name;
  373. this.age = age;
  374. }
  375. public override int GetHashCode ()
  376. {
  377. return this.Age ^ this.Name.GetHashCode ();
  378. }
  379. public override bool Equals (object obj)
  380. {
  381. Baz b = obj as Baz;
  382. if (b == null)
  383. return false;
  384. return b.Age == this.Age && b.Name == this.Name;
  385. }
  386. public override string ToString ()
  387. {
  388. return this.Name;
  389. }
  390. }
  391. static IEnumerable<Baz> CreateBazCollection ()
  392. {
  393. return new [] {
  394. new Baz ("jb", 25),
  395. new Baz ("ana", 20),
  396. new Baz ("reg", 28),
  397. new Baz ("ro", 25),
  398. new Baz ("jb", 7),
  399. };
  400. }
  401. [Test]
  402. public void TestOrderByAgeAscendingTheByNameDescending ()
  403. {
  404. ParallelTestHelper.Repeat (() => {
  405. var q = from b in CreateBazCollection ().AsReallyParallel ()
  406. orderby b.Age ascending, b.Name descending
  407. select b;
  408. var expected = new [] {
  409. new Baz ("jb", 7),
  410. new Baz ("ana", 20),
  411. new Baz ("ro", 25),
  412. new Baz ("jb", 25),
  413. new Baz ("reg", 28),
  414. };
  415. AssertAreSame (expected, q);
  416. });
  417. }
  418. class Data {
  419. public int ID { get; set; }
  420. public string Name { get; set; }
  421. public override string ToString ()
  422. {
  423. return ID + " " + Name;
  424. }
  425. }
  426. IEnumerable<Data> CreateData ()
  427. {
  428. return new [] {
  429. new Data { ID = 10, Name = "bcd" },
  430. new Data { ID = 20, Name = "Abcd" },
  431. new Data { ID = 20, Name = "Ab" },
  432. new Data { ID = 10, Name = "Zyx" },
  433. };
  434. }
  435. [Test]
  436. public void TestOrderByIdDescendingThenByNameAscending ()
  437. {
  438. ParallelTestHelper.Repeat (() => {
  439. var q = from d in CreateData ().AsReallyParallel ()
  440. orderby d.ID descending, d.Name ascending
  441. select d;
  442. var list = new List<Data> (q);
  443. Assert.AreEqual ("Ab", list [0].Name);
  444. Assert.AreEqual ("Abcd", list [1].Name);
  445. Assert.AreEqual ("bcd", list [2].Name);
  446. Assert.AreEqual ("Zyx", list [3].Name);
  447. });
  448. }
  449. static void AssertIsOrdered (IEnumerable<int> e, int count)
  450. {
  451. int f = int.MinValue;
  452. int c = 0;
  453. foreach (int i in e) {
  454. Assert.IsTrue (f <= i, string.Format ("{0} <= {1}", f, i));
  455. f = i;
  456. c++;
  457. }
  458. Assert.AreEqual (count, c);
  459. }
  460. [TestAttribute]
  461. public void ElementAtTestCase()
  462. {
  463. //ParallelTestHelper.Repeat (() => {
  464. Assert.AreEqual(1, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(0), "#1");
  465. Assert.AreEqual(51, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(50), "#2");
  466. Assert.AreEqual(489, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(488), "#3");
  467. //});
  468. }
  469. [Test]
  470. public void TestJoin ()
  471. {
  472. int num = 100;
  473. Tuple<int, int>[] outer = Enumerable.Range (1, 50).Select ((i) => Tuple.Create (i, num - 2 * i)).ToArray ();
  474. Tuple<int, int>[] inner = Enumerable.Range (1, 50).Reverse ().Select ((i) => Tuple.Create (i, 2 * i)).ToArray ();
  475. IEnumerable<int> expected = outer.Join (inner, (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  476. ParallelTestHelper.Repeat (() => {
  477. ParallelQuery<int> actual = outer.AsReallyParallel ().Join (inner.AsParallel (), (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  478. AreEquivalent (expected, actual, 1);
  479. });
  480. }
  481. [Test]
  482. public void TestGroupBy ()
  483. {
  484. int num = 100;
  485. Tuple<int, int>[] source = Enumerable.Range (0, num).Select ((i) => Tuple.Create (i / 10, i)).ToArray ();
  486. ParallelTestHelper.Repeat (() => {
  487. ParallelQuery<IGrouping<int, int>> actual = source.AsReallyParallel ().GroupBy ((e) => e.Item1, (e) => e.Item2, EqualityComparer<int>.Default);
  488. foreach (var group in actual) {
  489. Assert.GreaterOrEqual (group.Key, 0);
  490. Assert.Less (group.Key, num / 10);
  491. int count = 0;
  492. foreach (var e in group) {
  493. count++;
  494. Assert.GreaterOrEqual (e, group.Key * 10);
  495. Assert.Less (e, (group.Key + 1) * 10);
  496. }
  497. Assert.AreEqual (10, count, "count");
  498. }
  499. });
  500. }
  501. [TestAttribute]
  502. public void TakeTestCase()
  503. {
  504. ParallelTestHelper.Repeat (() => {
  505. ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(2000);
  506. IEnumerable<int> sync = baseEnumerable.Take(2000);
  507. AreEquivalent(sync, async, 1);
  508. async = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(100);
  509. sync = baseEnumerable.Take(100);
  510. AreEquivalent(sync, async, 2);
  511. }, 20);
  512. }
  513. [TestAttribute]
  514. public void UnorderedTakeTestCase()
  515. {
  516. ParallelTestHelper.Repeat (() => {
  517. ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().Take(2000);
  518. IEnumerable<int> sync = baseEnumerable.Take (2000);
  519. Assert.AreEqual (sync.Count (), async.Count (), "#1");
  520. async = baseEnumerable.AsReallyParallel ().Take(100);
  521. sync = baseEnumerable.Take(100);
  522. Assert.AreEqual (sync.Count (), async.Count (), "#2");
  523. }, 20);
  524. }
  525. [Test]
  526. public void SkipTestCase()
  527. {
  528. ParallelTestHelper.Repeat (() => {
  529. ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().AsOrdered().Skip(2000);
  530. IEnumerable<int> sync = baseEnumerable.Skip(2000);
  531. AreEquivalent(sync, async, 1);
  532. }, 20);
  533. }
  534. [Test]
  535. public void SkipTestCaseSmall ()
  536. {
  537. ParallelTestHelper.Repeat (() => {
  538. var async = baseEnumerable.AsReallyParallel ().Skip(100);
  539. var sync = baseEnumerable.Skip(100);
  540. Assert.AreEqual (sync.Count (), async.Count ());
  541. }, 20);
  542. }
  543. [Test]
  544. public void ZipTestCase()
  545. {
  546. ParallelTestHelper.Repeat (() => {
  547. ParallelQuery<int> async1 = ParallelEnumerable.Range(0, 10000);
  548. ParallelQuery<int> async2 = ParallelEnumerable.Repeat(1, 10000).Zip(async1, (e1, e2) => e1 + e2);
  549. int[] expected = Enumerable.Range (1, 10000).ToArray ();
  550. CollectionAssert.AreEquivalent(expected, Enumerable.ToArray (async2), "#1");
  551. });
  552. }
  553. [Test]
  554. public void RangeTestCase ()
  555. {
  556. ParallelTestHelper.Repeat (() => {
  557. IEnumerable<int> sync = Enumerable.Range(1, 1000);
  558. IEnumerable<int> async = ParallelEnumerable.Range(1, 1000);
  559. AreEquivalent (sync, async, 1);
  560. });
  561. }
  562. [Test]
  563. public void RepeatTestCase ()
  564. {
  565. ParallelTestHelper.Repeat (() => {
  566. IEnumerable<int> sync = Enumerable.Repeat(1, 1000);
  567. IEnumerable<int> async = ParallelEnumerable.Repeat(1, 1000);
  568. AreEquivalent (sync, async, 1);
  569. });
  570. }
  571. [Test]
  572. public void TestSum ()
  573. {
  574. int [] data = {1, 2, 3, 4};
  575. Assert.AreEqual (10, data.AsReallyParallel ().Sum ());
  576. }
  577. [Test]
  578. public void SumOnEmpty ()
  579. {
  580. int [] data = {};
  581. Assert.AreEqual (0, data.AsReallyParallel ().Sum ());
  582. }
  583. [Test]
  584. public void TestMax ()
  585. {
  586. int [] data = {1, 3, 5, 2};
  587. Assert.AreEqual (5, data.AsReallyParallel ().Max ());
  588. }
  589. [Test]
  590. public void TestMin ()
  591. {
  592. int [] data = {3, 5, 2, 6, 1, 7};
  593. Assert.AreEqual (1, data.AsReallyParallel ().Min ());
  594. }
  595. [Test]
  596. public void TestToListOrdered ()
  597. {
  598. int [] data = { 2, 3, 5 };
  599. var list = data.AsParallel().AsOrdered().WithExecutionMode (ParallelExecutionMode.ForceParallelism).ToList ();
  600. AssertAreSame (data, list);
  601. AssertIsOrdered (list, data.Length);
  602. Assert.AreEqual (typeof (List<int>), list.GetType ());
  603. }
  604. [Test]
  605. public void TestToArrayOrdered ()
  606. {
  607. ICollection<int> coll = new List<int> ();
  608. coll.Add (0);
  609. coll.Add (1);
  610. coll.Add (2);
  611. int [] result = {0, 1, 2};
  612. var array = coll.AsReallyParallel ().AsOrdered().ToArray ();
  613. AssertAreSame (result, array);
  614. AssertIsOrdered (array, result.Length);
  615. Assert.AreEqual (typeof (int []), array.GetType ());
  616. array = Enumerable.Range (1, 100).Select ((i) => i).AsReallyParallel ().AsOrdered().ToArray ();
  617. result = Enumerable.Range (1, 100).ToArray ();
  618. AssertAreSame (result, array);
  619. AssertIsOrdered (array, result.Length);
  620. Assert.AreEqual (typeof (int []), array.GetType ());
  621. }
  622. [Test]
  623. public void TestToList ()
  624. {
  625. int [] data = {3, 5, 2};
  626. var list = data.AsReallyParallel ().ToList ();
  627. CollectionAssert.AreEquivalent (data, list);
  628. Assert.AreEqual (typeof (List<int>), list.GetType ());
  629. }
  630. [Test]
  631. public void TestToArray ()
  632. {
  633. ICollection<int> coll = new List<int> ();
  634. coll.Add (0);
  635. coll.Add (1);
  636. coll.Add (2);
  637. int [] result = {0, 1, 2};
  638. var array = coll.AsReallyParallel ().ToArray ();
  639. CollectionAssert.AreEquivalent (result, array);
  640. Assert.AreEqual (typeof (int []), array.GetType ());
  641. }
  642. [Test]
  643. public void TestAverageOnInt32 ()
  644. {
  645. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  646. }
  647. [Test]
  648. public void TestAverageOnInt64 ()
  649. {
  650. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  651. }
  652. [Test]
  653. public void AnyArgumentNullTest ()
  654. {
  655. string [] data = { "2", "1", "5", "3", "4" };
  656. // Any<TSource> ()
  657. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (); });
  658. // Any<TSource> (Func<TSource, bool>)
  659. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (x => true); });
  660. AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().Any ((Func<string, bool>) null); });
  661. }
  662. [Test]
  663. public void AnyTest ()
  664. {
  665. int [] data = { 5, 2, 3, 1, 6 };
  666. int [] empty = { };
  667. // Any<TSource> ()
  668. Assert.IsTrue (data.AsReallyParallel ().Any ());
  669. Assert.IsFalse (empty.AsReallyParallel ().Any ());
  670. // Any<TSource> (Func<TSource, bool>)
  671. Assert.IsTrue (data.AsReallyParallel ().Any (x => x == 5));
  672. Assert.IsFalse (data.AsReallyParallel ().Any (x => x == 9));
  673. Assert.IsFalse (empty.AsReallyParallel ().Any (x => true));
  674. }
  675. [Test]
  676. public void AllArgumentNullTest ()
  677. {
  678. string [] data = { "2", "1", "5", "3", "4" };
  679. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().All (x => true); });
  680. AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().All ((Func<string, bool>) null); });
  681. }
  682. [Test]
  683. public void AllTest ()
  684. {
  685. int [] data = { 5, 2, 3, 1, 6 };
  686. int [] empty = { };
  687. Assert.IsTrue (data.AsReallyParallel ().All (x => true));
  688. Assert.IsFalse (data.AsReallyParallel ().All (x => x != 1));
  689. Assert.IsTrue (empty.AsReallyParallel ().All (x => false));
  690. }
  691. }
  692. }
  693. #endif