ParallelEnumerableTests.cs 21 KB

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