ParallelEnumerableTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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. var expected = first;
  224. var actual = first.AsReallyParallel ().Union (second.AsParallel ()).ToArray ();
  225. // Work around quadratic behavior in NUnitLite's CollectionTally class
  226. Array.Sort (expected);
  227. Array.Sort (actual);
  228. AreEquivalent (expected, actual);
  229. });
  230. }
  231. [Test]
  232. public void TestBigIntersect ()
  233. {
  234. ParallelTestHelper.Repeat (() => {
  235. int [] first = Enumerable.Range (1, 10000).ToArray ();
  236. int [] second = Enumerable.Range (323, 757).ToArray ();
  237. AreEquivalent (second, first.AsReallyParallel ().Intersect (second.AsParallel ()));
  238. });
  239. }
  240. class Foo {}
  241. class Bar : Foo {}
  242. [Test]
  243. public void TestCast ()
  244. {
  245. Bar a = new Bar ();
  246. Bar b = new Bar ();
  247. Bar c = new Bar ();
  248. Foo [] foos = new Foo [] {a, b, c};
  249. Bar [] result = new Bar [] {a, b, c};
  250. AreEquivalent (result, foos.AsReallyParallel ().Cast<Bar> ());
  251. }
  252. [Test]
  253. public void TestSkip ()
  254. {
  255. int [] data = {0, 1, 2, 3, 4, 5};
  256. int [] result = {3, 4, 5};
  257. AssertAreSame (result, data.AsParallel ().AsOrdered ().Skip (3).ToArray ());
  258. }
  259. [Test]
  260. public void TestSkipIterating ()
  261. {
  262. int [] data = {0, 1, 2, 3, 4, 5};
  263. int [] result = {3, 4, 5};
  264. AssertAreSame (result, data.AsParallel ().AsOrdered ().Skip (3));
  265. }
  266. [Test]
  267. public void TestSkipWhile ()
  268. {
  269. int [] data = {0, 1, 2, 3, 4, 5};
  270. int [] result = {3, 4, 5};
  271. AssertAreSame (result, data.AsParallel ().AsOrdered ().SkipWhile (i => i < 3));
  272. }
  273. [Test]
  274. public void TestTake ()
  275. {
  276. int [] data = {0, 1, 2, 3, 4, 5};
  277. int [] result = {0, 1, 2};
  278. AssertAreSame (result, data.AsParallel ().AsOrdered ().Take (3));
  279. }
  280. [Test]
  281. public void TestTakeWhile ()
  282. {
  283. int [] data = {0, 1, 2, 3, 4, 5};
  284. int [] result = {0, 1, 2};
  285. AssertAreSame (result, data.AsParallel ().AsOrdered ().TakeWhile (i => i < 3));
  286. }
  287. [Test]
  288. public void SelectManyTest ()
  289. {
  290. IEnumerable<int> initial = Enumerable.Range (1, 50);
  291. IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
  292. ParallelTestHelper.Repeat (() => {
  293. var actual = initial.AsReallyParallel ().SelectMany ((i) => Enumerable.Range (1, i));
  294. AreEquivalent (expected, actual);
  295. });
  296. }
  297. [Test]
  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. }
  736. #endif