ParallelEnumerableTests.cs 19 KB

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