ParallelEnumerableTests.cs 24 KB

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