ParallelEnumerableTests.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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, Ignore]
  302. public void TestFirst ()
  303. {
  304. int [] data = {1, 2, 3};
  305. Assert.AreEqual (1, data.AsParallel ().First ());
  306. }
  307. [Test, Ignore]
  308. public void TestFirstOrDefault ()
  309. {
  310. int [] data = {};
  311. Assert.AreEqual (default (int), data.AsParallel ().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 q = CreateBazCollection ().AsParallel ().OrderBy ((b) => b.Age).ThenByDescending ((b) => b.Name);
  384. var expected = new [] {
  385. new Baz ("jb", 7),
  386. new Baz ("ana", 20),
  387. new Baz ("ro", 25),
  388. new Baz ("jb", 25),
  389. new Baz ("reg", 28),
  390. };
  391. foreach (Baz b in q) {
  392. Console.Write(b.Name + ", " + b.Age + "; ");
  393. }
  394. AssertAreSame (expected, q);
  395. });
  396. }
  397. class Data {
  398. public int ID { get; set; }
  399. public string Name { get; set; }
  400. public override string ToString ()
  401. {
  402. return ID + " " + Name;
  403. }
  404. }
  405. IEnumerable<Data> CreateData ()
  406. {
  407. return new [] {
  408. new Data { ID = 10, Name = "bcd" },
  409. new Data { ID = 20, Name = "Abcd" },
  410. new Data { ID = 20, Name = "Ab" },
  411. new Data { ID = 10, Name = "Zyx" },
  412. };
  413. }
  414. [Test]
  415. public void TestOrderByIdDescendingThenByNameAscending ()
  416. {
  417. ParallelTestHelper.Repeat (() => {
  418. var q = from d in CreateData ().AsParallel()
  419. orderby d.ID descending, d.Name ascending
  420. select d;
  421. var list = new List<Data> (q);
  422. Assert.AreEqual ("Ab", list [0].Name);
  423. Assert.AreEqual ("Abcd", list [1].Name);
  424. Assert.AreEqual ("bcd", list [2].Name);
  425. Assert.AreEqual ("Zyx", list [3].Name);
  426. });
  427. }
  428. static void AssertIsOrdered (IEnumerable<int> e, int count)
  429. {
  430. int f = int.MinValue;
  431. int c = 0;
  432. foreach (int i in e) {
  433. Assert.IsTrue (f <= i, string.Format ("{0} <= {1}", f, i));
  434. f = i;
  435. c++;
  436. }
  437. Assert.AreEqual (count, c);
  438. }
  439. [TestAttribute]
  440. public void ElementAtTestCase()
  441. {
  442. ParallelTestHelper.Repeat (() => {
  443. Assert.AreEqual(1, baseEnumerable.AsParallel ().ElementAt(0), "#1");
  444. Assert.AreEqual(51, baseEnumerable.AsParallel ().ElementAt(50), "#2");
  445. Assert.AreEqual(489, baseEnumerable.AsParallel ().ElementAt(488), "#3");
  446. });
  447. }
  448. [Test]
  449. public void TestJoin ()
  450. {
  451. int num = 100;
  452. Tuple<int, int>[] outer = Enumerable.Range (1, 50).Select ((i) => Tuple.Create (i, num - 2 * i)).ToArray ();
  453. Tuple<int, int>[] inner = Enumerable.Range (1, 50).Reverse ().Select ((i) => Tuple.Create (i, 2 * i)).ToArray ();
  454. IEnumerable<int> expected = outer.Join (inner, (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  455. ParallelTestHelper.Repeat (() => {
  456. ParallelQuery<int> actual = outer.AsParallel ().Join (inner.AsParallel (), (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  457. AreEquivalent (expected, actual, 1);
  458. });
  459. }
  460. [Test]
  461. public void TestGroupBy ()
  462. {
  463. int num = 100;
  464. Tuple<int, int>[] source = Enumerable.Range (0, num).Select ((i) => Tuple.Create (i / 10, i)).ToArray ();
  465. ParallelTestHelper.Repeat (() => {
  466. ParallelQuery<IGrouping<int, int>> actual = source.AsParallel ().GroupBy ((e) => e.Item1, (e) => e.Item2, EqualityComparer<int>.Default);
  467. foreach (var group in actual) {
  468. Assert.GreaterOrEqual (group.Key, 0);
  469. Assert.Less (group.Key, num / 10);
  470. int count = 0;
  471. foreach (var e in group) {
  472. count++;
  473. Assert.GreaterOrEqual (e, group.Key * 10);
  474. Assert.Less (e, (group.Key + 1) * 10);
  475. }
  476. Assert.AreEqual (10, count, "count");
  477. }
  478. });
  479. }
  480. [TestAttribute]
  481. public void TakeTestCase()
  482. {
  483. ParallelTestHelper.Repeat (() => {
  484. ParallelQuery<int> async = baseEnumerable.AsParallel().AsOrdered ().Take(2000);
  485. IEnumerable<int> sync = baseEnumerable.Take(2000);
  486. AreEquivalent(sync, async, 1);
  487. async = baseEnumerable.AsParallel().AsOrdered ().Take(100);
  488. sync = baseEnumerable.Take(100);
  489. AreEquivalent(sync, async, 2);
  490. }, 20);
  491. }
  492. [TestAttribute]
  493. public void UnorderedTakeTestCase()
  494. {
  495. ParallelTestHelper.Repeat (() => {
  496. ParallelQuery<int> async = baseEnumerable.AsParallel().Take(2000);
  497. IEnumerable<int> sync = baseEnumerable.Take (2000);
  498. Assert.AreEqual (sync.Count (), async.Count (), "#1");
  499. async = baseEnumerable.AsParallel().Take(100);
  500. sync = baseEnumerable.Take(100);
  501. Assert.AreEqual (sync.Count (), async.Count (), "#2");
  502. }, 20);
  503. }
  504. [Test]
  505. public void SkipTestCase()
  506. {
  507. ParallelTestHelper.Repeat (() => {
  508. ParallelQuery<int> async = baseEnumerable.AsParallel().AsOrdered().Skip(2000);
  509. IEnumerable<int> sync = baseEnumerable.Skip(2000);
  510. AreEquivalent(sync, async, 1);
  511. async = baseEnumerable.AsParallel().Skip(100);
  512. sync = baseEnumerable.Skip(100);
  513. Assert.AreEqual(sync.Count(), async.Count(), "#2");
  514. }, 20);
  515. }
  516. [Test]
  517. public void ZipTestCase()
  518. {
  519. ParallelTestHelper.Repeat (() => {
  520. ParallelQuery<int> async1 = ParallelEnumerable.Range(0, 10000);
  521. ParallelQuery<int> async2 = ParallelEnumerable.Repeat(1, 10000).Zip(async1, (e1, e2) => e1 + e2);
  522. int[] expected = Enumerable.Range (1, 10000).ToArray ();
  523. CollectionAssert.AreEquivalent(expected, Enumerable.ToArray (async2), "#1");
  524. });
  525. }
  526. [Test]
  527. public void RangeTestCase ()
  528. {
  529. ParallelTestHelper.Repeat (() => {
  530. IEnumerable<int> sync = Enumerable.Range(1, 1000);
  531. IEnumerable<int> async = ParallelEnumerable.Range(1, 1000);
  532. AreEquivalent (sync, async, 1);
  533. });
  534. }
  535. [Test]
  536. public void RepeatTestCase ()
  537. {
  538. ParallelTestHelper.Repeat (() => {
  539. IEnumerable<int> sync = Enumerable.Repeat(1, 1000);
  540. IEnumerable<int> async = ParallelEnumerable.Repeat(1, 1000);
  541. AreEquivalent (sync, async, 1);
  542. });
  543. }
  544. [Test]
  545. public void TestSum ()
  546. {
  547. int [] data = {1, 2, 3, 4};
  548. Assert.AreEqual (10, data.AsParallel().Sum ());
  549. }
  550. [Test]
  551. public void SumOnEmpty ()
  552. {
  553. int [] data = {};
  554. Assert.AreEqual (0, data.AsParallel().Sum ());
  555. }
  556. [Test]
  557. public void TestMax ()
  558. {
  559. int [] data = {1, 3, 5, 2};
  560. Assert.AreEqual (5, data.AsParallel().Max ());
  561. }
  562. [Test]
  563. public void TestMin ()
  564. {
  565. int [] data = {3, 5, 2, 6, 1, 7};
  566. Assert.AreEqual (1, data.AsParallel().Min ());
  567. }
  568. [Test]
  569. public void TestToListOrdered ()
  570. {
  571. int [] data = { 2, 3, 5 };
  572. var list = data.AsParallel().AsOrdered().ToList ();
  573. AssertAreSame (data, list);
  574. AssertIsOrdered (list, data.Length);
  575. Assert.AreEqual (typeof (List<int>), list.GetType ());
  576. }
  577. [Test]
  578. public void TestToArrayOrdered ()
  579. {
  580. ICollection<int> coll = new List<int> ();
  581. coll.Add (0);
  582. coll.Add (1);
  583. coll.Add (2);
  584. int [] result = {0, 1, 2};
  585. var array = coll.AsParallel().AsOrdered().ToArray ();
  586. AssertAreSame (result, array);
  587. AssertIsOrdered (array, result.Length);
  588. Assert.AreEqual (typeof (int []), array.GetType ());
  589. array = Enumerable.Range (1, 100).Select ((i) => i).AsParallel().AsOrdered().ToArray ();
  590. result = Enumerable.Range (1, 100).ToArray ();
  591. AssertAreSame (result, array);
  592. AssertIsOrdered (array, result.Length);
  593. Assert.AreEqual (typeof (int []), array.GetType ());
  594. }
  595. [Test]
  596. public void TestToList ()
  597. {
  598. int [] data = {3, 5, 2};
  599. var list = data.AsParallel().ToList ();
  600. CollectionAssert.AreEquivalent (data, list);
  601. Assert.AreEqual (typeof (List<int>), list.GetType ());
  602. }
  603. [Test]
  604. public void TestToArray ()
  605. {
  606. ICollection<int> coll = new List<int> ();
  607. coll.Add (0);
  608. coll.Add (1);
  609. coll.Add (2);
  610. int [] result = {0, 1, 2};
  611. var array = coll.AsParallel().ToArray ();
  612. CollectionAssert.AreEquivalent (result, array);
  613. Assert.AreEqual (typeof (int []), array.GetType ());
  614. }
  615. [Test]
  616. public void TestAverageOnInt32 ()
  617. {
  618. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  619. }
  620. [Test]
  621. public void TestAverageOnInt64 ()
  622. {
  623. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  624. }
  625. [Test]
  626. public void AnyArgumentNullTest ()
  627. {
  628. string [] data = { "2", "1", "5", "3", "4" };
  629. // Any<TSource> ()
  630. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsParallel ().Any (); });
  631. // Any<TSource> (Func<TSource, bool>)
  632. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsParallel ().Any (x => true); });
  633. AssertException<ArgumentNullException> (delegate () { data.AsParallel ().Any ((Func<string, bool>) null); });
  634. }
  635. [Test]
  636. public void AnyTest ()
  637. {
  638. int [] data = { 5, 2, 3, 1, 6 };
  639. int [] empty = { };
  640. // Any<TSource> ()
  641. Assert.IsTrue (data.AsParallel ().Any ());
  642. Assert.IsFalse (empty.AsParallel ().Any ());
  643. // Any<TSource> (Func<TSource, bool>)
  644. Assert.IsTrue (data.AsParallel ().Any (x => x == 5));
  645. Assert.IsFalse (data.AsParallel ().Any (x => x == 9));
  646. Assert.IsFalse (empty.AsParallel ().Any (x => true));
  647. }
  648. [Test]
  649. public void AllArgumentNullTest ()
  650. {
  651. string [] data = { "2", "1", "5", "3", "4" };
  652. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsParallel ().All (x => true); });
  653. AssertException<ArgumentNullException> (delegate () { data.AsParallel ().All ((Func<string, bool>) null); });
  654. }
  655. [Test]
  656. public void AllTest ()
  657. {
  658. int [] data = { 5, 2, 3, 1, 6 };
  659. int [] empty = { };
  660. Assert.IsTrue (data.AsParallel ().All (x => true));
  661. Assert.IsFalse (data.AsParallel ().All (x => x != 1));
  662. Assert.IsTrue (empty.AsParallel ().All (x => false));
  663. }
  664. }
  665. }
  666. #endif