ParallelEnumerableTests.cs 22 KB

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