ParallelEnumerableTests.cs 18 KB

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