ParallelEnumerableTests.cs 23 KB

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