ParallelEnumerableTests.cs 23 KB

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