ParallelEnumerableTests.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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. public void SelectManyOrderedTest ()
  298. {
  299. IEnumerable<int> initial = Enumerable.Range (1, 50);
  300. IEnumerable<int> expected = initial.SelectMany ((i) => Enumerable.Range (1, i));
  301. ParallelTestHelper.Repeat (() => {
  302. var actual = initial.AsParallel ().AsOrdered ().SelectMany ((i) => Enumerable.Range (1, i));
  303. AssertAreSame (expected, actual);
  304. });
  305. }
  306. [Test]
  307. public void TestLast ()
  308. {
  309. int [] data = {1, 2, 3};
  310. Assert.AreEqual (3, data.AsParallel ().AsOrdered ().Last ());
  311. }
  312. [Test]
  313. public void TestLastOrDefault ()
  314. {
  315. int [] data = {};
  316. Assert.AreEqual (default (int), data.AsParallel ().AsOrdered ().LastOrDefault ());
  317. }
  318. [Test]
  319. public void TestFirst ()
  320. {
  321. int [] data = {1, 2, 3};
  322. Assert.AreEqual (1, data.AsParallel ().AsOrdered ().First ());
  323. }
  324. [Test]
  325. public void TestFirstOrDefault ()
  326. {
  327. int [] data = {};
  328. Assert.AreEqual (default (int), data.AsParallel ().AsOrdered ().FirstOrDefault ());
  329. }
  330. [Test]
  331. public void TestReverse ()
  332. {
  333. int [] data = {0, 1, 2, 3, 4};
  334. int [] result = {4, 3, 2, 1, 0};
  335. AssertAreSame (result, ((IEnumerable<int>)data).Select ((i) => i).AsParallel ().AsOrdered ().Reverse ());
  336. AssertAreSame (result, ParallelEnumerable.Range (0, 5).AsParallel ().AsOrdered ().Reverse ());
  337. }
  338. [Test]
  339. public void TestOrderBy ()
  340. {
  341. ParallelTestHelper.Repeat (() => {
  342. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  343. var q = array.AsReallyParallel ().OrderBy ((i) => i);
  344. AssertIsOrdered (q, array.Length);
  345. });
  346. }
  347. class Baz {
  348. string name;
  349. int age;
  350. public string Name
  351. {
  352. get {
  353. if (string.IsNullOrEmpty (name))
  354. return Age.ToString ();
  355. return name + " (" + Age + ")";
  356. }
  357. }
  358. public int Age
  359. {
  360. get { return age + 1; }
  361. }
  362. public Baz (string name, int age)
  363. {
  364. this.name = name;
  365. this.age = age;
  366. }
  367. public override int GetHashCode ()
  368. {
  369. return this.Age ^ this.Name.GetHashCode ();
  370. }
  371. public override bool Equals (object obj)
  372. {
  373. Baz b = obj as Baz;
  374. if (b == null)
  375. return false;
  376. return b.Age == this.Age && b.Name == this.Name;
  377. }
  378. public override string ToString ()
  379. {
  380. return this.Name;
  381. }
  382. }
  383. static IEnumerable<Baz> CreateBazCollection ()
  384. {
  385. return new [] {
  386. new Baz ("jb", 25),
  387. new Baz ("ana", 20),
  388. new Baz ("reg", 28),
  389. new Baz ("ro", 25),
  390. new Baz ("jb", 7),
  391. };
  392. }
  393. [Test]
  394. public void TestOrderByAgeAscendingTheByNameDescending ()
  395. {
  396. ParallelTestHelper.Repeat (() => {
  397. var q = from b in CreateBazCollection ().AsReallyParallel ()
  398. orderby b.Age ascending, b.Name descending
  399. select b;
  400. var expected = new [] {
  401. new Baz ("jb", 7),
  402. new Baz ("ana", 20),
  403. new Baz ("ro", 25),
  404. new Baz ("jb", 25),
  405. new Baz ("reg", 28),
  406. };
  407. AssertAreSame (expected, q);
  408. });
  409. }
  410. class Data {
  411. public int ID { get; set; }
  412. public string Name { get; set; }
  413. public override string ToString ()
  414. {
  415. return ID + " " + Name;
  416. }
  417. }
  418. IEnumerable<Data> CreateData ()
  419. {
  420. return new [] {
  421. new Data { ID = 10, Name = "bcd" },
  422. new Data { ID = 20, Name = "Abcd" },
  423. new Data { ID = 20, Name = "Ab" },
  424. new Data { ID = 10, Name = "Zyx" },
  425. };
  426. }
  427. [Test]
  428. public void TestOrderByIdDescendingThenByNameAscending ()
  429. {
  430. ParallelTestHelper.Repeat (() => {
  431. var q = from d in CreateData ().AsReallyParallel ()
  432. orderby d.ID descending, d.Name ascending
  433. select d;
  434. var list = new List<Data> (q);
  435. Assert.AreEqual ("Ab", list [0].Name);
  436. Assert.AreEqual ("Abcd", list [1].Name);
  437. Assert.AreEqual ("bcd", list [2].Name);
  438. Assert.AreEqual ("Zyx", list [3].Name);
  439. });
  440. }
  441. static void AssertIsOrdered (IEnumerable<int> e, int count)
  442. {
  443. int f = int.MinValue;
  444. int c = 0;
  445. foreach (int i in e) {
  446. Assert.IsTrue (f <= i, string.Format ("{0} <= {1}", f, i));
  447. f = i;
  448. c++;
  449. }
  450. Assert.AreEqual (count, c);
  451. }
  452. [Test]
  453. public void ElementAtTestCase()
  454. {
  455. //ParallelTestHelper.Repeat (() => {
  456. Assert.AreEqual(1, baseEnumerable.AsParallel ().AsOrdered ().ElementAt(0), "#1");
  457. Assert.AreEqual(51, baseEnumerable.AsParallel ().AsOrdered ().ElementAt(50), "#2");
  458. Assert.AreEqual(489, baseEnumerable.AsParallel ().AsOrdered ().ElementAt(488), "#3");
  459. //});
  460. }
  461. [Test]
  462. public void TestJoin ()
  463. {
  464. int num = 100;
  465. Tuple<int, int>[] outer = Enumerable.Range (1, 50).Select ((i) => Tuple.Create (i, num - 2 * i)).ToArray ();
  466. Tuple<int, int>[] inner = Enumerable.Range (1, 50).Reverse ().Select ((i) => Tuple.Create (i, 2 * i)).ToArray ();
  467. IEnumerable<int> expected = outer.Join (inner, (e) => e.Item1, (e) => e.Item1, (e1, e2) => e1.Item2 + e2.Item2, EqualityComparer<int>.Default);
  468. ParallelTestHelper.Repeat (() => {
  469. ParallelQuery<int> actual = outer.AsReallyParallel ().Join (inner.AsParallel (),
  470. (e) => e.Item1,
  471. (e) => e.Item1,
  472. (e1, e2) => e1.Item2 + e2.Item2,
  473. EqualityComparer<int>.Default);
  474. AreEquivalent (expected, actual);
  475. });
  476. }
  477. [Test]
  478. public void SmallJoinTest ()
  479. {
  480. var items = new [] { 1, 2, 3 };
  481. var items2 = new [] { 1, 2, 3, 4 };
  482. var actual = items.AsReallyParallel ().Join (items2.AsParallel (), i => i, i => i, (e1, e2) => e1 + e2);
  483. AreEquivalent (new[] { 2, 4, 6 }, actual);
  484. }
  485. [Test]
  486. public void TestGroupBy ()
  487. {
  488. int num = 100;
  489. Tuple<int, int>[] source = Enumerable.Range (0, num).Select ((i) => Tuple.Create (i / 10, i)).ToArray ();
  490. ParallelTestHelper.Repeat (() => {
  491. ParallelQuery<IGrouping<int, int>> actual = source.AsReallyParallel ().GroupBy ((e) => e.Item1, (e) => e.Item2, EqualityComparer<int>.Default);
  492. foreach (var group in actual) {
  493. Assert.IsTrue (group.Key >= 0);
  494. Assert.IsTrue (group.Key < num / 10);
  495. int count = 0;
  496. foreach (var e in group) {
  497. count++;
  498. Assert.IsTrue (e >= group.Key * 10);
  499. Assert.IsTrue (e < (group.Key + 1) * 10);
  500. }
  501. Assert.AreEqual (10, count, "count");
  502. }
  503. });
  504. }
  505. [Test]
  506. public void TakeTestCase()
  507. {
  508. ParallelTestHelper.Repeat (() => {
  509. ParallelQuery<int> async_res = baseEnumerable.AsParallel ().AsOrdered ().Take(800);
  510. IEnumerable<int> sync = baseEnumerable.Take(800);
  511. AreEquivalent(sync, async_res);
  512. async_res = baseEnumerable.AsParallel ().AsOrdered ().Take(100);
  513. sync = baseEnumerable.Take(100);
  514. AreEquivalent(sync, async_res);
  515. });
  516. }
  517. [TestAttribute]
  518. public void UnorderedTakeTestCase()
  519. {
  520. ParallelTestHelper.Repeat (() => {
  521. ParallelQuery<int> async_res = baseEnumerable.AsReallyParallel ().Take(800);
  522. IEnumerable<int> sync = baseEnumerable.Take (800);
  523. Assert.AreEqual (sync.Count (), async_res.Count (), "#1");
  524. async_res = baseEnumerable.AsReallyParallel ().Take(100);
  525. sync = baseEnumerable.Take(100);
  526. Assert.AreEqual (sync.Count (), async_res.Count (), "#2");
  527. });
  528. }
  529. [Test]
  530. public void SkipTestCase()
  531. {
  532. ParallelTestHelper.Repeat (() => {
  533. ParallelQuery<int> async_res = baseEnumerable.AsParallel ().AsOrdered().Skip (800);
  534. IEnumerable<int> sync = baseEnumerable.Skip (800);
  535. AreEquivalent (sync, async_res);
  536. });
  537. }
  538. [Test]
  539. public void SkipTestCaseSmall ()
  540. {
  541. ParallelTestHelper.Repeat (() => {
  542. var async_res = baseEnumerable.AsReallyParallel ().Skip(100);
  543. var sync = baseEnumerable.Skip(100);
  544. Assert.AreEqual (sync.Count (), async_res.Count ());
  545. });
  546. }
  547. [Test]
  548. public void ZipTestCase()
  549. {
  550. ParallelTestHelper.Repeat (() => {
  551. ParallelQuery<int> async_res1 = ParallelEnumerable.Range(0, 10000);
  552. ParallelQuery<int> async_res2 = ParallelEnumerable.Repeat(1, 10000).Zip(async_res1, (e1, e2) => e1 + e2);
  553. int[] expected = Enumerable.Range (1, 10000).ToArray ();
  554. var actual = Enumerable.ToArray (async_res2);
  555. // Work around quadratic behavior in NUnitLite's CollectionTally class
  556. Array.Sort (expected);
  557. Array.Sort (actual);
  558. AreEquivalent(expected, actual);
  559. });
  560. }
  561. [Test]
  562. public void Range ()
  563. {
  564. ParallelTestHelper.Repeat (() => {
  565. IEnumerable<int> sync = Enumerable.Range(1, 1000);
  566. IEnumerable<int> async_res = ParallelEnumerable.Range(1, 1000);
  567. AreEquivalent (sync, async_res);
  568. });
  569. }
  570. [Test]
  571. public void Range_StartOffset ()
  572. {
  573. ParallelTestHelper.Repeat (() => {
  574. IEnumerable<int> sync = Enumerable.Range (30, 10);
  575. IEnumerable<int> async_res = ParallelEnumerable.Range (30, 10);
  576. AreEquivalent (sync, async_res);
  577. });
  578. }
  579. [Test]
  580. public void RepeatTestCase ()
  581. {
  582. ParallelTestHelper.Repeat (() => {
  583. IEnumerable<int> sync = Enumerable.Repeat(1, 1000);
  584. IEnumerable<int> async_res = ParallelEnumerable.Repeat(1, 1000);
  585. AreEquivalent (sync, async_res);
  586. });
  587. }
  588. [Test]
  589. public void TestSum ()
  590. {
  591. int [] data = {1, 2, 3, 4};
  592. Assert.AreEqual (10, data.AsReallyParallel ().Sum ());
  593. }
  594. [Test]
  595. public void SumOnEmpty ()
  596. {
  597. int [] data = {};
  598. Assert.AreEqual (0, data.AsReallyParallel ().Sum ());
  599. }
  600. [Test]
  601. public void TestMax ()
  602. {
  603. int [] data = {1, 3, 5, 2};
  604. Assert.AreEqual (5, data.AsReallyParallel ().Max ());
  605. }
  606. [Test]
  607. public void TestMin ()
  608. {
  609. int [] data = {3, 5, 2, 6, 1, 7};
  610. Assert.AreEqual (1, data.AsReallyParallel ().Min ());
  611. }
  612. [Test]
  613. public void TestToListOrdered ()
  614. {
  615. int [] data = { 2, 3, 5 };
  616. var list = data.AsParallel().AsOrdered().WithExecutionMode (ParallelExecutionMode.ForceParallelism).ToList ();
  617. AssertAreSame (data, list);
  618. AssertIsOrdered (list, data.Length);
  619. Assert.AreEqual (typeof (List<int>), list.GetType ());
  620. }
  621. [Test]
  622. public void TestToArrayOrdered ()
  623. {
  624. ICollection<int> coll = new List<int> ();
  625. coll.Add (0);
  626. coll.Add (1);
  627. coll.Add (2);
  628. int [] result = {0, 1, 2};
  629. var array = coll.AsParallel ().AsOrdered().ToArray ();
  630. AssertAreSame (result, array);
  631. AssertIsOrdered (array, result.Length);
  632. Assert.AreEqual (typeof (int []), array.GetType ());
  633. array = Enumerable.Range (1, 100).Select ((i) => i).AsParallel ().AsOrdered().ToArray ();
  634. result = Enumerable.Range (1, 100).ToArray ();
  635. AssertAreSame (result, array);
  636. AssertIsOrdered (array, result.Length);
  637. Assert.AreEqual (typeof (int []), array.GetType ());
  638. }
  639. [Test]
  640. public void TestToList ()
  641. {
  642. int [] data = {3, 5, 2};
  643. var list = data.AsReallyParallel ().ToList ();
  644. AreEquivalent (data, list);
  645. Assert.AreEqual (typeof (List<int>), list.GetType ());
  646. }
  647. [Test]
  648. public void TestToArray ()
  649. {
  650. ICollection<int> coll = new List<int> ();
  651. coll.Add (0);
  652. coll.Add (1);
  653. coll.Add (2);
  654. int [] result = {0, 1, 2};
  655. var array = coll.AsReallyParallel ().ToArray ();
  656. AreEquivalent (result, array);
  657. Assert.AreEqual (typeof (int []), array.GetType ());
  658. }
  659. [Test]
  660. public void TestAverageOnInt32 ()
  661. {
  662. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  663. }
  664. [Test]
  665. public void TestAverageOnInt64 ()
  666. {
  667. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  668. }
  669. [Test]
  670. public void AnyArgumentNullTest ()
  671. {
  672. string [] data = { "2", "1", "5", "3", "4" };
  673. // Any<TSource> ()
  674. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (); });
  675. // Any<TSource> (Func<TSource, bool>)
  676. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().Any (x => true); });
  677. AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().Any ((Func<string, bool>) null); });
  678. }
  679. [Test]
  680. public void AnyTest ()
  681. {
  682. int [] data = { 5, 2, 3, 1, 6 };
  683. int [] empty = { };
  684. // Any<TSource> ()
  685. Assert.IsTrue (data.AsReallyParallel ().Any ());
  686. Assert.IsFalse (empty.AsReallyParallel ().Any ());
  687. // Any<TSource> (Func<TSource, bool>)
  688. Assert.IsTrue (data.AsReallyParallel ().Any (x => x == 5));
  689. Assert.IsFalse (data.AsReallyParallel ().Any (x => x == 9));
  690. Assert.IsFalse (empty.AsReallyParallel ().Any (x => true));
  691. }
  692. [Test]
  693. public void AllArgumentNullTest ()
  694. {
  695. string [] data = { "2", "1", "5", "3", "4" };
  696. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).AsReallyParallel ().All (x => true); });
  697. AssertException<ArgumentNullException> (delegate () { data.AsReallyParallel ().All ((Func<string, bool>) null); });
  698. }
  699. [Test]
  700. public void AllTest ()
  701. {
  702. int [] data = { 5, 2, 3, 1, 6 };
  703. int [] empty = { };
  704. Assert.IsTrue (data.AsReallyParallel ().All (x => true));
  705. Assert.IsFalse (data.AsReallyParallel ().All (x => x != 1));
  706. Assert.IsTrue (empty.AsReallyParallel ().All (x => false));
  707. }
  708. [Test]
  709. public void SequenceEqualsTest ()
  710. {
  711. var data1 = new int[] { 1, 2, 3 };
  712. var data2 = new int[] { 1, 2, 4 };
  713. var data3 = new int[] { 1, 2, 3, 4 };
  714. Assert.IsTrue (data1.AsParallel ().SequenceEqual (data1.AsParallel ()));
  715. Assert.IsTrue (data2.AsParallel ().SequenceEqual (data2.AsParallel ()));
  716. Assert.IsTrue (data3.AsParallel ().SequenceEqual (data3.AsParallel ()));
  717. Assert.IsFalse (data1.AsParallel ().SequenceEqual (data2.AsParallel ()));
  718. Assert.IsFalse (data1.AsParallel ().SequenceEqual (data3.AsParallel ()));
  719. Assert.IsFalse (data2.AsParallel ().SequenceEqual (data3.AsParallel ()));
  720. }
  721. [Test]
  722. public void ContainsTest ()
  723. {
  724. var data1 = new int[] { 1, 2, 3 };
  725. var data2 = new int[] { 1, 2, 4 };
  726. var data3 = new int[] { 1, 2, 3, 4 };
  727. Assert.IsTrue (data1.AsReallyParallel ().Contains (3));
  728. Assert.IsFalse (data2.AsReallyParallel ().Contains (3));
  729. Assert.IsTrue (data3.AsReallyParallel ().Contains (3));
  730. Assert.IsFalse (data3.AsReallyParallel ().Contains (5));
  731. Assert.IsTrue (data2.AsReallyParallel ().Contains (2));
  732. }
  733. }
  734. }