EnumerableTest.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. [Test]
  75. public void TestLast ()
  76. {
  77. int [] data = {1, 2, 3};
  78. Assert.AreEqual (3, data.Last ());
  79. }
  80. [Test]
  81. public void TestLastOrDefault ()
  82. {
  83. int [] data = {};
  84. Assert.AreEqual (default (int), data.LastOrDefault ());
  85. }
  86. [Test]
  87. public void TestFirst ()
  88. {
  89. int [] data = {1, 2, 3};
  90. Assert.AreEqual (1, data.First ());
  91. }
  92. [Test]
  93. public void TestFirstOrDefault ()
  94. {
  95. int [] data = {};
  96. Assert.AreEqual (default (int), data.FirstOrDefault ());
  97. }
  98. [Test]
  99. public void TestSequenceEqual ()
  100. {
  101. int [] first = {0, 1, 2, 3, 4, 5};
  102. int [] second = {0, 1, 2};
  103. int [] third = {0, 1, 2, 3, 4, 5};
  104. Assert.IsFalse (first.SequenceEqual (second));
  105. Assert.IsTrue (first.SequenceEqual (third));
  106. }
  107. [Test]
  108. public void TestSkip ()
  109. {
  110. int [] data = {0, 1, 2, 3, 4, 5};
  111. int [] result = {3, 4, 5};
  112. AssertAreSame (result, data.Skip (3));
  113. }
  114. [Test]
  115. public void TestSkipWhile ()
  116. {
  117. int [] data = {0, 1, 2, 3, 4, 5};
  118. int [] result = {3, 4, 5};
  119. AssertAreSame (result, data.SkipWhile (i => i < 3));
  120. }
  121. [Test]
  122. public void TestTake ()
  123. {
  124. int [] data = {0, 1, 2, 3, 4, 5};
  125. int [] result = {0, 1, 2};
  126. AssertAreSame (result, data.Take (3));
  127. }
  128. [Test]
  129. public void TestTakeWhile ()
  130. {
  131. int [] data = {0, 1, 2, 3, 4, 5};
  132. int [] result = {0, 1, 2};
  133. AssertAreSame (result, data.TakeWhile (i => i < 3));
  134. }
  135. [Test]
  136. public void TestSelect ()
  137. {
  138. int [] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  139. int [] result = {1, 3, 5, 7, 9};
  140. AssertAreSame (result, data.Where (i => i % 2 != 0));
  141. }
  142. [Test]
  143. public void TestReverse ()
  144. {
  145. int [] data = {0, 1, 2, 3, 4};
  146. int [] result = {4, 3, 2, 1, 0};
  147. AssertAreSame (result, data.Reverse ());
  148. AssertAreSame (result, Enumerable.Range (0, 5).Reverse ());
  149. }
  150. [Test]
  151. public void TestSum ()
  152. {
  153. int [] data = {1, 2, 3, 4};
  154. Assert.AreEqual (10, data.Sum ());
  155. }
  156. [Test]
  157. public void SumOnEmpty ()
  158. {
  159. int [] data = {};
  160. Assert.AreEqual (0, data.Sum ());
  161. }
  162. [Test]
  163. public void TestMax ()
  164. {
  165. int [] data = {1, 3, 5, 2};
  166. Assert.AreEqual (5, data.Max ());
  167. }
  168. [Test]
  169. public void TestMaxNullableInt32 ()
  170. {
  171. int? [] data = { null, null, null };
  172. Assert.IsNull (data.Max (x => -x));
  173. data = new int? [] { null, 1, 2 };
  174. Assert.AreEqual (-1, data.Max (x => -x));
  175. }
  176. [Test]
  177. public void TestMin ()
  178. {
  179. int [] data = {3, 5, 2, 6, 1, 7};
  180. Assert.AreEqual (1, data.Min ());
  181. }
  182. [Test]
  183. public void TestMinNullableInt32 ()
  184. {
  185. int? [] data = { null, null, null };
  186. Assert.IsNull (data.Min(x => -x));
  187. data = new int? [] { null, 1, 2 };
  188. Assert.AreEqual (-2, data.Min (x => -x));
  189. }
  190. [Test]
  191. public void TestToList ()
  192. {
  193. int [] data = {3, 5, 2};
  194. var list = data.ToList ();
  195. AssertAreSame (data, list);
  196. Assert.AreEqual (typeof (List<int>), list.GetType ());
  197. }
  198. [Test]
  199. public void TestToArray ()
  200. {
  201. ICollection<int> coll = new List<int> ();
  202. coll.Add (0);
  203. coll.Add (1);
  204. coll.Add (2);
  205. int [] result = {0, 1, 2};
  206. var array = coll.ToArray ();
  207. AssertAreSame (result, array);
  208. Assert.AreEqual (typeof (int []), array.GetType ());
  209. }
  210. [Test]
  211. public void TestIntersect ()
  212. {
  213. int [] left = { 1, 1 }, right = { 1, 1 };
  214. int [] result = { 1 };
  215. AssertAreSame (result, left.Intersect (right));
  216. }
  217. [Test]
  218. public void TestAverageOnInt32 ()
  219. {
  220. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  221. }
  222. [Test]
  223. public void TestAverageOnInt64 ()
  224. {
  225. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  226. }
  227. [Test]
  228. public void TestAverageOnLongNullable ()
  229. {
  230. List<long?> list = new List<long?> ();
  231. list.Add (2);
  232. list.Add (3);
  233. Assert.AreEqual (2.5d, list.Average ());
  234. }
  235. [Test]
  236. public void TestRange ()
  237. {
  238. AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
  239. AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
  240. }
  241. [Test]
  242. public void TestTakeTakesProperNumberOfItems ()
  243. {
  244. var stream = new MemoryStream (new byte [] { 1, 2, 3, 4, 0 });
  245. Assert.AreEqual (0, stream.Position);
  246. foreach (byte b in AsEnumerable (stream).Take (2))
  247. ;
  248. Assert.AreEqual (2, stream.Position);
  249. }
  250. static IEnumerable<byte> AsEnumerable (Stream stream)
  251. {
  252. byte b;
  253. while ((b = (byte) stream.ReadByte ()) >= 0)
  254. yield return b;
  255. }
  256. [Test]
  257. public void TestOrderBy ()
  258. {
  259. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  260. var q = from i in array
  261. orderby i
  262. select i;
  263. AssertIsOrdered (q);
  264. }
  265. class Baz {
  266. string name;
  267. int age;
  268. public string Name
  269. {
  270. get {
  271. if (string.IsNullOrEmpty (name))
  272. return Age.ToString ();
  273. return name + " (" + Age + ")";
  274. }
  275. }
  276. public int Age
  277. {
  278. get { return age + 1; }
  279. }
  280. public Baz (string name, int age)
  281. {
  282. this.name = name;
  283. this.age = age;
  284. }
  285. public override int GetHashCode ()
  286. {
  287. return this.Age ^ this.Name.GetHashCode ();
  288. }
  289. public override bool Equals (object obj)
  290. {
  291. Baz b = obj as Baz;
  292. if (b == null)
  293. return false;
  294. return b.Age == this.Age && b.Name == this.Name;
  295. }
  296. public override string ToString ()
  297. {
  298. return this.Name;
  299. }
  300. }
  301. static IEnumerable<Baz> CreateBazCollection ()
  302. {
  303. return new [] {
  304. new Baz ("jb", 25),
  305. new Baz ("ana", 20),
  306. new Baz ("reg", 28),
  307. new Baz ("ro", 25),
  308. new Baz ("jb", 7),
  309. };
  310. }
  311. [Test]
  312. public void TestOrderByAgeAscendingTheByNameDescending ()
  313. {
  314. var q = from b in CreateBazCollection ()
  315. orderby b.Age ascending, b.Name descending
  316. select b;
  317. var expected = new [] {
  318. new Baz ("jb", 7),
  319. new Baz ("ana", 20),
  320. new Baz ("ro", 25),
  321. new Baz ("jb", 25),
  322. new Baz ("reg", 28),
  323. };
  324. AssertAreSame (expected, q);
  325. }
  326. class Data {
  327. public int ID { get; set; }
  328. public string Name { get; set; }
  329. public override string ToString ()
  330. {
  331. return ID + " " + Name;
  332. }
  333. }
  334. IEnumerable<Data> CreateData ()
  335. {
  336. return new [] {
  337. new Data { ID = 10, Name = "bcd" },
  338. new Data { ID = 20, Name = "Abcd" },
  339. new Data { ID = 20, Name = "Ab" },
  340. new Data { ID = 10, Name = "Zyx" },
  341. };
  342. }
  343. [Test]
  344. public void TestOrderByIdDescendingThenByNameAscending ()
  345. {
  346. var q = from d in CreateData ()
  347. orderby d.ID descending, d.Name ascending
  348. select d;
  349. var list = new List<Data> (q);
  350. Assert.AreEqual ("Ab", list [0].Name);
  351. Assert.AreEqual ("Abcd", list [1].Name);
  352. Assert.AreEqual ("bcd", list [2].Name);
  353. Assert.AreEqual ("Zyx", list [3].Name);
  354. }
  355. static void AssertIsOrdered (IEnumerable<int> e)
  356. {
  357. int f = int.MinValue;
  358. foreach(int i in e) {
  359. Assert.IsTrue (f <= i);
  360. f = i;
  361. }
  362. }
  363. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  364. {
  365. if (expected == null) {
  366. Assert.IsNull (actual);
  367. return;
  368. }
  369. Assert.IsNotNull (actual);
  370. IEnumerator<T> ee = expected.GetEnumerator ();
  371. IEnumerator<T> ea = actual.GetEnumerator ();
  372. while (ee.MoveNext ()) {
  373. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  374. Assert.AreEqual (ee.Current, ea.Current);
  375. }
  376. if (ea.MoveNext ())
  377. Assert.Fail ("Unexpected element: " + ea.Current);
  378. }
  379. }
  380. }