EnumerableTest.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. //
  2. // EnumerableTest.cs
  3. //
  4. // Author:
  5. // Jb Evain ([email protected])
  6. //
  7. // (C) 2007 Novell, Inc. (http://www.novell.com)
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.IO;
  30. using System.Collections;
  31. using System.Collections.Generic;
  32. using System.Linq;
  33. using System.Linq.Expressions;
  34. using NUnit.Framework;
  35. namespace MonoTests.System.Linq {
  36. [TestFixture]
  37. public class EnumerableTest {
  38. [Test]
  39. public void TestSimpleExcept ()
  40. {
  41. int [] first = {0, 1, 2, 3, 4, 5};
  42. int [] second = {2, 4, 6};
  43. int [] result = {0, 1, 3, 5};
  44. AssertAreSame (result, first.Except (second));
  45. }
  46. [Test]
  47. public void TestSimpleIntersect ()
  48. {
  49. int [] first = {0, 1, 2, 3, 4, 5};
  50. int [] second = {2, 4, 6};
  51. int [] result = {2, 4};
  52. AssertAreSame (result, first.Intersect (second));
  53. }
  54. [Test]
  55. public void TestSimpleUnion ()
  56. {
  57. int [] first = {0, 1, 2, 3, 4, 5};
  58. int [] second = {2, 4, 6};
  59. int [] result = {0, 1, 2, 3, 4, 5, 6};
  60. AssertAreSame (result, first.Union (second));
  61. }
  62. class Foo {}
  63. class Bar : Foo {}
  64. [Test]
  65. public void TestCast ()
  66. {
  67. Bar a = new Bar ();
  68. Bar b = new Bar ();
  69. Bar c = new Bar ();
  70. Foo [] foos = new Foo [] {a, b, c};
  71. Bar [] result = new Bar [] {a, b, c};
  72. AssertAreSame (result, foos.Cast<Bar> ());
  73. }
  74. class Bingo : IEnumerable<int>, IEnumerable<string> {
  75. IEnumerator<int> IEnumerable<int>.GetEnumerator ()
  76. {
  77. yield return 42;
  78. yield return 12;
  79. }
  80. IEnumerator<string> IEnumerable<string>.GetEnumerator ()
  81. {
  82. yield return "foo";
  83. yield return "bar";
  84. }
  85. public IEnumerator GetEnumerator ()
  86. {
  87. return (this as IEnumerable<int>).GetEnumerator ();
  88. }
  89. }
  90. [Test]
  91. public void TestCastToImplementedType ()
  92. {
  93. var ints = new int [] { 42, 12 };
  94. var strs = new string [] { "foo", "bar" };
  95. var bingo = new Bingo ();
  96. AssertAreSame (ints, bingo.Cast<int> ());
  97. AssertAreSame (strs, bingo.Cast<string> ());
  98. }
  99. [Test]
  100. public void TestLast ()
  101. {
  102. int [] data = {1, 2, 3};
  103. Assert.AreEqual (3, data.Last ());
  104. }
  105. [Test]
  106. public void TestLastOrDefault ()
  107. {
  108. int [] data = {};
  109. Assert.AreEqual (default (int), data.LastOrDefault ());
  110. }
  111. [Test]
  112. public void TestFirst ()
  113. {
  114. int [] data = {1, 2, 3};
  115. Assert.AreEqual (1, data.First ());
  116. }
  117. [Test]
  118. public void TestFirstOrDefault ()
  119. {
  120. int [] data = {};
  121. Assert.AreEqual (default (int), data.FirstOrDefault ());
  122. }
  123. [Test]
  124. public void TestSequenceEqual ()
  125. {
  126. int [] first = {0, 1, 2, 3, 4, 5};
  127. int [] second = {0, 1, 2};
  128. int [] third = {0, 1, 2, 3, 4, 5};
  129. Assert.IsFalse (first.SequenceEqual (second));
  130. Assert.IsTrue (first.SequenceEqual (third));
  131. }
  132. [Test]
  133. public void TestSkip ()
  134. {
  135. int [] data = {0, 1, 2, 3, 4, 5};
  136. int [] result = {3, 4, 5};
  137. AssertAreSame (result, data.Skip (3));
  138. }
  139. [Test]
  140. public void TestSkipWhile ()
  141. {
  142. int [] data = {0, 1, 2, 3, 4, 5};
  143. int [] result = {3, 4, 5};
  144. AssertAreSame (result, data.SkipWhile (i => i < 3));
  145. }
  146. [Test]
  147. public void TestTake ()
  148. {
  149. int [] data = {0, 1, 2, 3, 4, 5};
  150. int [] result = {0, 1, 2};
  151. AssertAreSame (result, data.Take (3));
  152. }
  153. [Test]
  154. public void TestTakeWhile ()
  155. {
  156. int [] data = {0, 1, 2, 3, 4, 5};
  157. int [] result = {0, 1, 2};
  158. AssertAreSame (result, data.TakeWhile (i => i < 3));
  159. }
  160. [Test]
  161. public void TestSelect ()
  162. {
  163. int [] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  164. int [] result = {1, 3, 5, 7, 9};
  165. AssertAreSame (result, data.Where (i => i % 2 != 0));
  166. }
  167. [Test]
  168. public void TestReverse ()
  169. {
  170. int [] data = {0, 1, 2, 3, 4};
  171. int [] result = {4, 3, 2, 1, 0};
  172. AssertAreSame (result, data.Reverse ());
  173. AssertAreSame (result, Enumerable.Range (0, 5).Reverse ());
  174. }
  175. [Test]
  176. public void ReverseArrays ()
  177. {
  178. int[] source = { 1, 2, 3 };
  179. var query = source.Reverse ();
  180. using (var enumerator = query.GetEnumerator ()) {
  181. enumerator.MoveNext ();
  182. Assert.AreEqual (3, enumerator.Current);
  183. source [1] = 42;
  184. enumerator.MoveNext ();
  185. Assert.AreEqual (2, enumerator.Current);
  186. enumerator.MoveNext ();
  187. Assert.AreEqual (1, enumerator.Current);
  188. }
  189. }
  190. [Test]
  191. public void TestSum ()
  192. {
  193. int [] data = {1, 2, 3, 4};
  194. Assert.AreEqual (10, data.Sum ());
  195. }
  196. [Test]
  197. public void SumOnEmpty ()
  198. {
  199. int [] data = {};
  200. Assert.AreEqual (0, data.Sum ());
  201. }
  202. [Test]
  203. public void TestMax ()
  204. {
  205. int [] data = {1, 3, 5, 2};
  206. Assert.AreEqual (5, data.Max ());
  207. }
  208. [Test]
  209. public void TestMaxNullableInt32 ()
  210. {
  211. int? [] data = { null, null, null };
  212. Assert.IsNull (data.Max (x => -x));
  213. data = new int? [] { null, 1, 2 };
  214. Assert.AreEqual (-1, data.Max (x => -x));
  215. }
  216. [Test]
  217. public void TestMin ()
  218. {
  219. int [] data = {3, 5, 2, 6, 1, 7};
  220. Assert.AreEqual (1, data.Min ());
  221. }
  222. [Test]
  223. public void TestMinNullableInt32 ()
  224. {
  225. int? [] data = { null, null, null };
  226. Assert.IsNull (data.Min(x => -x));
  227. data = new int? [] { null, 1, 2 };
  228. Assert.AreEqual (-2, data.Min (x => -x));
  229. }
  230. [Test]
  231. public void TestMinStringEmpty ()
  232. {
  233. Assert.IsNull ((new string [0]).Min ());
  234. }
  235. [Test]
  236. public void TestMaxStringEmpty ()
  237. {
  238. Assert.IsNull ((new string [0]).Max ());
  239. }
  240. [Test]
  241. public void TestToList ()
  242. {
  243. int [] data = {3, 5, 2};
  244. var list = data.ToList ();
  245. AssertAreSame (data, list);
  246. Assert.AreEqual (typeof (List<int>), list.GetType ());
  247. }
  248. [Test]
  249. public void TestToArray ()
  250. {
  251. ICollection<int> coll = new List<int> ();
  252. coll.Add (0);
  253. coll.Add (1);
  254. coll.Add (2);
  255. int [] result = {0, 1, 2};
  256. var array = coll.ToArray ();
  257. AssertAreSame (result, array);
  258. Assert.AreEqual (typeof (int []), array.GetType ());
  259. }
  260. [Test]
  261. public void TestIntersect ()
  262. {
  263. int [] left = { 1, 1 }, right = { 1, 1 };
  264. int [] result = { 1 };
  265. AssertAreSame (result, left.Intersect (right));
  266. }
  267. [Test]
  268. public void TestAverageOnInt32 ()
  269. {
  270. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  271. }
  272. [Test]
  273. public void TestAverageOnInt64 ()
  274. {
  275. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  276. }
  277. [Test]
  278. public void TestAverageInt32 ()
  279. {
  280. // This does not overflow, computation is done with longs
  281. var x = new int [] { Int32.MaxValue, Int32.MaxValue };
  282. Assert.AreEqual ((double) Int32.MaxValue, x.Average ());
  283. }
  284. [Test]
  285. public void TestAverageOverflowOnInt64 ()
  286. {
  287. try {
  288. var x = new long [] { Int64.MaxValue, Int64.MaxValue };
  289. x.Average ();
  290. Assert.Fail ("#1");
  291. } catch (OverflowException) {
  292. }
  293. }
  294. [Test]
  295. public void TestAverageOnLongNullable ()
  296. {
  297. List<long?> list = new List<long?> ();
  298. list.Add (2);
  299. list.Add (3);
  300. Assert.AreEqual (2.5d, list.Average ());
  301. }
  302. [Test]
  303. public void TestRange ()
  304. {
  305. AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
  306. AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
  307. }
  308. [Test]
  309. public void SingleValueOfMaxInt32 ()
  310. {
  311. AssertAreSame (new [] { int.MaxValue }, Enumerable.Range(int.MaxValue, 1));
  312. }
  313. [Test]
  314. public void EmptyRangeStartingAtMinInt32 ()
  315. {
  316. AssertAreSame (new int [0], Enumerable.Range(int.MinValue, 0));
  317. }
  318. static void AssertThrows<T> (Action action) where T : Exception
  319. {
  320. try {
  321. action ();
  322. Assert.Fail ();
  323. } catch (T) {
  324. } catch {
  325. Assert.Fail ();
  326. }
  327. }
  328. [Test]
  329. public void TestTakeTakesProperNumberOfItems ()
  330. {
  331. var stream = new MemoryStream (new byte [] { 1, 2, 3, 4, 0 });
  332. Assert.AreEqual (0, stream.Position);
  333. foreach (byte b in AsEnumerable (stream).Take (2))
  334. ;
  335. Assert.AreEqual (2, stream.Position);
  336. }
  337. static IEnumerable<byte> AsEnumerable (Stream stream)
  338. {
  339. byte b;
  340. while ((b = (byte) stream.ReadByte ()) >= 0)
  341. yield return b;
  342. }
  343. [Test]
  344. public void TestOrderBy ()
  345. {
  346. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  347. var q = from i in array
  348. orderby i
  349. select i;
  350. AssertIsOrdered (q);
  351. }
  352. class Baz {
  353. string name;
  354. int age;
  355. public string Name
  356. {
  357. get {
  358. if (string.IsNullOrEmpty (name))
  359. return Age.ToString ();
  360. return name + " (" + Age + ")";
  361. }
  362. }
  363. public int Age
  364. {
  365. get { return age + 1; }
  366. }
  367. public Baz (string name, int age)
  368. {
  369. this.name = name;
  370. this.age = age;
  371. }
  372. public override int GetHashCode ()
  373. {
  374. return this.Age ^ this.Name.GetHashCode ();
  375. }
  376. public override bool Equals (object obj)
  377. {
  378. Baz b = obj as Baz;
  379. if (b == null)
  380. return false;
  381. return b.Age == this.Age && b.Name == this.Name;
  382. }
  383. public override string ToString ()
  384. {
  385. return this.Name;
  386. }
  387. }
  388. static IEnumerable<Baz> CreateBazCollection ()
  389. {
  390. return new [] {
  391. new Baz ("jb", 25),
  392. new Baz ("ana", 20),
  393. new Baz ("reg", 28),
  394. new Baz ("ro", 25),
  395. new Baz ("jb", 7),
  396. };
  397. }
  398. [Test]
  399. public void TestOrderByAgeAscendingTheByNameDescending ()
  400. {
  401. var q = from b in CreateBazCollection ()
  402. orderby b.Age ascending, b.Name descending
  403. select b;
  404. var expected = new [] {
  405. new Baz ("jb", 7),
  406. new Baz ("ana", 20),
  407. new Baz ("ro", 25),
  408. new Baz ("jb", 25),
  409. new Baz ("reg", 28),
  410. };
  411. AssertAreSame (expected, q);
  412. }
  413. class Data {
  414. public int ID { get; set; }
  415. public string Name { get; set; }
  416. public override string ToString ()
  417. {
  418. return ID + " " + Name;
  419. }
  420. }
  421. IEnumerable<Data> CreateData ()
  422. {
  423. return new [] {
  424. new Data { ID = 10, Name = "bcd" },
  425. new Data { ID = 20, Name = "Abcd" },
  426. new Data { ID = 20, Name = "Ab" },
  427. new Data { ID = 10, Name = "Zyx" },
  428. };
  429. }
  430. [Test]
  431. public void TestOrderByIdDescendingThenByNameAscending ()
  432. {
  433. var q = from d in CreateData ()
  434. orderby d.ID descending, d.Name ascending
  435. select d;
  436. var list = new List<Data> (q);
  437. Assert.AreEqual ("Ab", list [0].Name);
  438. Assert.AreEqual ("Abcd", list [1].Name);
  439. Assert.AreEqual ("bcd", list [2].Name);
  440. Assert.AreEqual ("Zyx", list [3].Name);
  441. }
  442. [Test]
  443. public void TestOrderByDescendingStability ()
  444. {
  445. var data = new [] {
  446. new { Key = true, Value = 1 },
  447. new { Key = false, Value = 2},
  448. new { Key = true, Value = 3},
  449. new { Key = false, Value = 4},
  450. new { Key = true, Value = 5},
  451. new { Key = false, Value = 6},
  452. new { Key = true, Value = 7},
  453. new { Key = false, Value = 8},
  454. new { Key = true, Value = 9},
  455. new { Key = false, Value = 10},
  456. };
  457. var expected = new [] {
  458. new { Key = true, Value = 1 },
  459. new { Key = true, Value = 3},
  460. new { Key = true, Value = 5},
  461. new { Key = true, Value = 7},
  462. new { Key = true, Value = 9},
  463. new { Key = false, Value = 2},
  464. new { Key = false, Value = 4},
  465. new { Key = false, Value = 6},
  466. new { Key = false, Value = 8},
  467. new { Key = false, Value = 10},
  468. };
  469. AssertAreSame (expected, data.OrderByDescending (x => x.Key));
  470. }
  471. static void AssertIsOrdered (IEnumerable<int> e)
  472. {
  473. int f = int.MinValue;
  474. foreach(int i in e) {
  475. Assert.IsTrue (f <= i);
  476. f = i;
  477. }
  478. }
  479. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  480. {
  481. if (expected == null) {
  482. Assert.IsNull (actual);
  483. return;
  484. }
  485. Assert.IsNotNull (actual);
  486. IEnumerator<T> ee = expected.GetEnumerator ();
  487. IEnumerator<T> ea = actual.GetEnumerator ();
  488. while (ee.MoveNext ()) {
  489. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  490. Assert.AreEqual (ee.Current, ea.Current);
  491. }
  492. if (ea.MoveNext ())
  493. Assert.Fail ("Unexpected element: " + ea.Current);
  494. }
  495. }
  496. }