ParallelEnumerableTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  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. class Foo {}
  230. class Bar : Foo {}
  231. [Test]
  232. public void TestCast ()
  233. {
  234. Bar a = new Bar ();
  235. Bar b = new Bar ();
  236. Bar c = new Bar ();
  237. Foo [] foos = new Foo [] {a, b, c};
  238. Bar [] result = new Bar [] {a, b, c};
  239. AreEquivalent (result, foos.AsReallyParallel ().Cast<Bar> (), 1);
  240. }
  241. [Test]
  242. public void TestSkip ()
  243. {
  244. int [] data = {0, 1, 2, 3, 4, 5};
  245. int [] result = {3, 4, 5};
  246. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Skip (3).ToArray ());
  247. }
  248. [Test]
  249. public void TestSkipIterating ()
  250. {
  251. int [] data = {0, 1, 2, 3, 4, 5};
  252. int [] result = {3, 4, 5};
  253. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Skip (3));
  254. }
  255. [Test]
  256. public void TestSkipWhile ()
  257. {
  258. int [] data = {0, 1, 2, 3, 4, 5};
  259. int [] result = {3, 4, 5};
  260. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().SkipWhile (i => i < 3));
  261. }
  262. [Test]
  263. public void TestTake ()
  264. {
  265. int [] data = {0, 1, 2, 3, 4, 5};
  266. int [] result = {0, 1, 2};
  267. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().Take (3));
  268. }
  269. [Test]
  270. public void TestTakeWhile ()
  271. {
  272. int [] data = {0, 1, 2, 3, 4, 5};
  273. int [] result = {0, 1, 2};
  274. AssertAreSame (result, data.AsReallyParallel ().AsOrdered ().TakeWhile (i => i < 3));
  275. }
  276. [Test]
  277. public void SelectManyTest ()
  278. {
  279. IEnumerable<int> initial = Enumerable.Range (1, 50);
  280. IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
  281. ParallelTestHelper.Repeat (() => {
  282. var actual = initial.AsReallyParallel ().SelectMany ((i) => Enumerable.Range (1, i));
  283. AreEquivalent (expected, actual, 1);
  284. });
  285. }
  286. [Test]
  287. public void SelectManyOrderedTest ()
  288. {
  289. IEnumerable<int> initial = Enumerable.Range (1, 50);
  290. IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
  291. ParallelTestHelper.Repeat (() => {
  292. var actual = initial.AsReallyParallel ().AsOrdered ().SelectMany ((i) => Enumerable.Range (1, i));
  293. AssertAreSame (expected, actual);
  294. });
  295. }
  296. [Test]
  297. public void TestLast ()
  298. {
  299. int [] data = {1, 2, 3};
  300. Assert.AreEqual (3, data.AsReallyParallel ().AsOrdered ().Last ());
  301. }
  302. [Test]
  303. public void TestLastOrDefault ()
  304. {
  305. int [] data = {};
  306. Assert.AreEqual (default (int), data.AsReallyParallel ().AsOrdered ().LastOrDefault ());
  307. }
  308. [Test]
  309. public void TestFirst ()
  310. {
  311. int [] data = {1, 2, 3};
  312. Assert.AreEqual (1, data.AsReallyParallel ().AsOrdered ().First ());
  313. }
  314. [Test]
  315. public void TestFirstOrDefault ()
  316. {
  317. int [] data = {};
  318. Assert.AreEqual (default (int), data.AsReallyParallel ().AsOrdered ().FirstOrDefault ());
  319. }
  320. [Test]
  321. public void TestReverse ()
  322. {
  323. int [] data = {0, 1, 2, 3, 4};
  324. int [] result = {4, 3, 2, 1, 0};
  325. AssertAreSame (result, ((IEnumerable<int>)data).Select ((i) => i).AsReallyParallel ().AsOrdered ().Reverse ());
  326. AssertAreSame (result, ParallelEnumerable.Range (0, 5).WithExecutionMode (ParallelExecutionMode.ForceParallelism).AsOrdered ().Reverse ());
  327. }
  328. [Test]
  329. public void TestOrderBy ()
  330. {
  331. ParallelTestHelper.Repeat (() => {
  332. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  333. var q = array.AsReallyParallel ().OrderBy ((i) => i);
  334. AssertIsOrdered (q, array.Length);
  335. });
  336. }
  337. class Baz {
  338. string name;
  339. int age;
  340. public string Name
  341. {
  342. get {
  343. if (string.IsNullOrEmpty (name))
  344. return Age.ToString ();
  345. return name + " (" + Age + ")";
  346. }
  347. }
  348. public int Age
  349. {
  350. get { return age + 1; }
  351. }
  352. public Baz (string name, int age)
  353. {
  354. this.name = name;
  355. this.age = age;
  356. }
  357. public override int GetHashCode ()
  358. {
  359. return this.Age ^ this.Name.GetHashCode ();
  360. }
  361. public override bool Equals (object obj)
  362. {
  363. Baz b = obj as Baz;
  364. if (b == null)
  365. return false;
  366. return b.Age == this.Age && b.Name == this.Name;
  367. }
  368. public override string ToString ()
  369. {
  370. return this.Name;
  371. }
  372. }
  373. static IEnumerable<Baz> CreateBazCollection ()
  374. {
  375. return new [] {
  376. new Baz ("jb", 25),
  377. new Baz ("ana", 20),
  378. new Baz ("reg", 28),
  379. new Baz ("ro", 25),
  380. new Baz ("jb", 7),
  381. };
  382. }
  383. [Test]
  384. public void TestOrderByAgeAscendingTheByNameDescending ()
  385. {
  386. ParallelTestHelper.Repeat (() => {
  387. var q = from b in CreateBazCollection ().AsReallyParallel ()
  388. orderby b.Age ascending, b.Name descending
  389. select b;
  390. var expected = new [] {
  391. new Baz ("jb", 7),
  392. new Baz ("ana", 20),
  393. new Baz ("ro", 25),
  394. new Baz ("jb", 25),
  395. new Baz ("reg", 28),
  396. };
  397. AssertAreSame (expected, q);
  398. });
  399. }
  400. class Data {
  401. public int ID { get; set; }
  402. public string Name { get; set; }
  403. public override string ToString ()
  404. {
  405. return ID + " " + Name;
  406. }
  407. }
  408. IEnumerable<Data> CreateData ()
  409. {
  410. return new [] {
  411. new Data { ID = 10, Name = "bcd" },
  412. new Data { ID = 20, Name = "Abcd" },
  413. new Data { ID = 20, Name = "Ab" },
  414. new Data { ID = 10, Name = "Zyx" },
  415. };
  416. }
  417. [Test]
  418. public void TestOrderByIdDescendingThenByNameAscending ()
  419. {
  420. ParallelTestHelper.Repeat (() => {
  421. var q = from d in CreateData ().AsReallyParallel ()
  422. orderby d.ID descending, d.Name ascending
  423. select d;
  424. var list = new List<Data> (q);
  425. Assert.AreEqual ("Ab", list [0].Name);
  426. Assert.AreEqual ("Abcd", list [1].Name);
  427. Assert.AreEqual ("bcd", list [2].Name);
  428. Assert.AreEqual ("Zyx", list [3].Name);
  429. });
  430. }
  431. static void AssertIsOrdered (IEnumerable<int> e, int count)
  432. {
  433. int f = int.MinValue;
  434. int c = 0;
  435. foreach (int i in e) {
  436. Assert.IsTrue (f <= i, string.Format ("{0} <= {1}", f, i));
  437. f = i;
  438. c++;
  439. }
  440. Assert.AreEqual (count, c);
  441. }
  442. [TestAttribute]
  443. public void ElementAtTestCase()
  444. {
  445. //ParallelTestHelper.Repeat (() => {
  446. Assert.AreEqual(1, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(0), "#1");
  447. Assert.AreEqual(51, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(50), "#2");
  448. Assert.AreEqual(489, baseEnumerable.AsReallyParallel ().AsOrdered ().ElementAt(488), "#3");
  449. //});
  450. }
  451. [Test]
  452. public void TestJoin ()
  453. {
  454. int num = 100;
  455. Tuple<int, int>[] outer = Enumerable.Range (1, 50).Select ((i) => Tuple.Create (i, num - 2 * i)).ToArray ();
  456. Tuple<int, int>[] inner = Enumerable.Range (1, 50).Reverse ().Select ((i) => Tuple.Create (i, 2 * i)).ToArray ();
  457. IEnumerable<int> expected = outer.Join (inner, (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  458. ParallelTestHelper.Repeat (() => {
  459. ParallelQuery<int> actual = outer.AsReallyParallel ().Join (inner.AsParallel (), (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  460. AreEquivalent (expected, actual, 1);
  461. });
  462. }
  463. [Test]
  464. public void TestGroupBy ()
  465. {
  466. int num = 100;
  467. Tuple<int, int>[] source = Enumerable.Range (0, num).Select ((i) => Tuple.Create (i / 10, i)).ToArray ();
  468. ParallelTestHelper.Repeat (() => {
  469. ParallelQuery<IGrouping<int, int>> actual = source.AsReallyParallel ().GroupBy ((e) => e.Item1, (e) => e.Item2, EqualityComparer<int>.Default);
  470. foreach (var group in actual) {
  471. Assert.GreaterOrEqual (group.Key, 0);
  472. Assert.Less (group.Key, num / 10);
  473. int count = 0;
  474. foreach (var e in group) {
  475. count++;
  476. Assert.GreaterOrEqual (e, group.Key * 10);
  477. Assert.Less (e, (group.Key + 1) * 10);
  478. }
  479. Assert.AreEqual (10, count, "count");
  480. }
  481. });
  482. }
  483. [TestAttribute]
  484. public void TakeTestCase()
  485. {
  486. ParallelTestHelper.Repeat (() => {
  487. ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(2000);
  488. IEnumerable<int> sync = baseEnumerable.Take(2000);
  489. AreEquivalent(sync, async, 1);
  490. async = baseEnumerable.AsReallyParallel ().AsOrdered ().Take(100);
  491. sync = baseEnumerable.Take(100);
  492. AreEquivalent(sync, async, 2);
  493. }, 20);
  494. }
  495. [TestAttribute]
  496. public void UnorderedTakeTestCase()
  497. {
  498. ParallelTestHelper.Repeat (() => {
  499. ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().Take(2000);
  500. IEnumerable<int> sync = baseEnumerable.Take (2000);
  501. Assert.AreEqual (sync.Count (), async.Count (), "#1");
  502. async = baseEnumerable.AsReallyParallel ().Take(100);
  503. sync = baseEnumerable.Take(100);
  504. Assert.AreEqual (sync.Count (), async.Count (), "#2");
  505. }, 20);
  506. }
  507. [Test]
  508. public void SkipTestCase()
  509. {
  510. ParallelTestHelper.Repeat (() => {
  511. ParallelQuery<int> async = baseEnumerable.AsReallyParallel ().AsOrdered().Skip(2000);
  512. IEnumerable<int> sync = baseEnumerable.Skip(2000);
  513. AreEquivalent(sync, async, 1);
  514. async = baseEnumerable.AsReallyParallel ().Skip(100);
  515. sync = baseEnumerable.Skip(100);
  516. Assert.AreEqual(sync.Count(), async.Count(), "#2");
  517. }, 20);
  518. }
  519. [Test]
  520. public void ZipTestCase()
  521. {
  522. ParallelTestHelper.Repeat (() => {
  523. ParallelQuery<int> async1 = ParallelEnumerable.Range(0, 10000);
  524. ParallelQuery<int> async2 = ParallelEnumerable.Repeat(1, 10000).Zip(async1, (e1, e2) => e1 + e2);
  525. int[] expected = Enumerable.Range (1, 10000).ToArray ();
  526. CollectionAssert.AreEquivalent(expected, Enumerable.ToArray (async2), "#1");
  527. });
  528. }
  529. [Test]
  530. public void RangeTestCase ()
  531. {
  532. ParallelTestHelper.Repeat (() => {
  533. IEnumerable<int> sync = Enumerable.Range(1, 1000);
  534. IEnumerable<int> async = ParallelEnumerable.Range(1, 1000);
  535. AreEquivalent (sync, async, 1);
  536. });
  537. }
  538. [Test]
  539. public void RepeatTestCase ()
  540. {
  541. ParallelTestHelper.Repeat (() => {
  542. IEnumerable<int> sync = Enumerable.Repeat(1, 1000);
  543. IEnumerable<int> async = ParallelEnumerable.Repeat(1, 1000);
  544. AreEquivalent (sync, async, 1);
  545. });
  546. }
  547. [Test]
  548. public void TestSum ()
  549. {
  550. int [] data = {1, 2, 3, 4};
  551. Assert.AreEqual (10, data.AsReallyParallel ().Sum ());
  552. }
  553. [Test]
  554. public void SumOnEmpty ()
  555. {
  556. int [] data = {};
  557. Assert.AreEqual (0, data.AsReallyParallel ().Sum ());
  558. }
  559. [Test]
  560. public void TestMax ()
  561. {
  562. int [] data = {1, 3, 5, 2};
  563. Assert.AreEqual (5, data.AsReallyParallel ().Max ());
  564. }
  565. [Test]
  566. public void TestMin ()
  567. {
  568. int [] data = {3, 5, 2, 6, 1, 7};
  569. Assert.AreEqual (1, data.AsReallyParallel ().Min ());
  570. }
  571. [Test]
  572. public void TestToListOrdered ()
  573. {
  574. int [] data = { 2, 3, 5 };
  575. var list = data.AsParallel().AsOrdered().WithExecutionMode (ParallelExecutionMode.ForceParallelism).ToList ();
  576. AssertAreSame (data, list);
  577. AssertIsOrdered (list, data.Length);
  578. Assert.AreEqual (typeof (List<int>), list.GetType ());
  579. }
  580. [Test]
  581. public void TestToArrayOrdered ()
  582. {
  583. ICollection<int> coll = new List<int> ();
  584. coll.Add (0);
  585. coll.Add (1);
  586. coll.Add (2);
  587. int [] result = {0, 1, 2};
  588. var array = coll.AsReallyParallel ().AsOrdered().ToArray ();
  589. AssertAreSame (result, array);
  590. AssertIsOrdered (array, result.Length);
  591. Assert.AreEqual (typeof (int []), array.GetType ());
  592. array = Enumerable.Range (1, 100).Select ((i) => i).AsReallyParallel ().AsOrdered().ToArray ();
  593. result = Enumerable.Range (1, 100).ToArray ();
  594. AssertAreSame (result, array);
  595. AssertIsOrdered (array, result.Length);
  596. Assert.AreEqual (typeof (int []), array.GetType ());
  597. }
  598. [Test]
  599. public void TestToList ()
  600. {
  601. int [] data = {3, 5, 2};
  602. var list = data.AsReallyParallel ().ToList ();
  603. CollectionAssert.AreEquivalent (data, list);
  604. Assert.AreEqual (typeof (List<int>), list.GetType ());
  605. }
  606. [Test]
  607. public void TestToArray ()
  608. {
  609. ICollection<int> coll = new List<int> ();
  610. coll.Add (0);
  611. coll.Add (1);
  612. coll.Add (2);
  613. int [] result = {0, 1, 2};
  614. var array = coll.AsReallyParallel ().ToArray ();
  615. CollectionAssert.AreEquivalent (result, array);
  616. Assert.AreEqual (typeof (int []), array.GetType ());
  617. }
  618. [Test]
  619. public void TestAverageOnInt32 ()
  620. {
  621. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  622. }
  623. [Test]
  624. public void TestAverageOnInt64 ()
  625. {
  626. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  627. }
  628. [Test]
  629. public void AnyArgumentNullTest ()
  630. {
  631. string [] data = { "2", "1", "5", "3", "4" };
  632. // Any<TSource> ()
  633. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (); });
  634. // Any<TSource> (Func<TSource, bool>)
  635. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (x => true); });
  636. AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().Any ((Func<string, bool>) null); });
  637. }
  638. [Test]
  639. public void AnyTest ()
  640. {
  641. int [] data = { 5, 2, 3, 1, 6 };
  642. int [] empty = { };
  643. // Any<TSource> ()
  644. Assert.IsTrue (data.AsReallyParallel ().Any ());
  645. Assert.IsFalse (empty.AsReallyParallel ().Any ());
  646. // Any<TSource> (Func<TSource, bool>)
  647. Assert.IsTrue (data.AsReallyParallel ().Any (x => x == 5));
  648. Assert.IsFalse (data.AsReallyParallel ().Any (x => x == 9));
  649. Assert.IsFalse (empty.AsReallyParallel ().Any (x => true));
  650. }
  651. [Test]
  652. public void AllArgumentNullTest ()
  653. {
  654. string [] data = { "2", "1", "5", "3", "4" };
  655. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().All (x => true); });
  656. AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().All ((Func<string, bool>) null); });
  657. }
  658. [Test]
  659. public void AllTest ()
  660. {
  661. int [] data = { 5, 2, 3, 1, 6 };
  662. int [] empty = { };
  663. Assert.IsTrue (data.AsReallyParallel ().All (x => true));
  664. Assert.IsFalse (data.AsReallyParallel ().All (x => x != 1));
  665. Assert.IsTrue (empty.AsReallyParallel ().All (x => false));
  666. }
  667. }
  668. }
  669. #endif