EnumerableTest.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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.Collections;
  30. using System.Collections.Generic;
  31. using System.Linq;
  32. using System.Linq.Expressions;
  33. using NUnit.Framework;
  34. namespace MonoTests.System.Linq {
  35. [TestFixture]
  36. public class EnumerableTest {
  37. [Test]
  38. public void TestSimpleExcept ()
  39. {
  40. int [] first = {0, 1, 2, 3, 4, 5};
  41. int [] second = {2, 4, 6};
  42. int [] result = {0, 1, 3, 5};
  43. AssertAreSame (result, first.Except (second));
  44. }
  45. [Test]
  46. public void TestSimpleIntersect ()
  47. {
  48. int [] first = {0, 1, 2, 3, 4, 5};
  49. int [] second = {2, 4, 6};
  50. int [] result = {2, 4};
  51. AssertAreSame (result, first.Intersect (second));
  52. }
  53. [Test]
  54. public void TestSimpleUnion ()
  55. {
  56. int [] first = {0, 1, 2, 3, 4, 5};
  57. int [] second = {2, 4, 6};
  58. int [] result = {0, 1, 2, 3, 4, 5, 6};
  59. AssertAreSame (result, first.Union (second));
  60. }
  61. class Foo {}
  62. class Bar : Foo {}
  63. [Test]
  64. public void TestCast ()
  65. {
  66. Bar a = new Bar ();
  67. Bar b = new Bar ();
  68. Bar c = new Bar ();
  69. Foo [] foos = new Foo [] {a, b, c};
  70. Bar [] result = new Bar [] {a, b, c};
  71. AssertAreSame (result, foos.Cast<Bar> ());
  72. }
  73. [Test]
  74. public void TestLast ()
  75. {
  76. int [] data = {1, 2, 3};
  77. Assert.AreEqual (3, data.Last ());
  78. }
  79. [Test]
  80. public void TestLastOrDefault ()
  81. {
  82. int [] data = {};
  83. Assert.AreEqual (default (int), data.LastOrDefault ());
  84. }
  85. [Test]
  86. public void TestFirst ()
  87. {
  88. int [] data = {1, 2, 3};
  89. Assert.AreEqual (1, data.First ());
  90. }
  91. [Test]
  92. public void TestFirstOrDefault ()
  93. {
  94. int [] data = {};
  95. Assert.AreEqual (default (int), data.FirstOrDefault ());
  96. }
  97. [Test]
  98. public void TestSequenceEqual ()
  99. {
  100. int [] first = {0, 1, 2, 3, 4, 5};
  101. int [] second = {0, 1, 2};
  102. int [] third = {0, 1, 2, 3, 4, 5};
  103. Assert.IsFalse (first.SequenceEqual (second));
  104. Assert.IsTrue (first.SequenceEqual (third));
  105. }
  106. [Test]
  107. public void TestSkip ()
  108. {
  109. int [] data = {0, 1, 2, 3, 4, 5};
  110. int [] result = {3, 4, 5};
  111. AssertAreSame (result, data.Skip (3));
  112. }
  113. [Test]
  114. public void TestSkipWhile ()
  115. {
  116. int [] data = {0, 1, 2, 3, 4, 5};
  117. int [] result = {3, 4, 5};
  118. AssertAreSame (result, data.SkipWhile (i => i < 3));
  119. }
  120. [Test]
  121. public void TestTake ()
  122. {
  123. int [] data = {0, 1, 2, 3, 4, 5};
  124. int [] result = {0, 1, 2};
  125. AssertAreSame (result, data.Take (3));
  126. }
  127. [Test]
  128. public void TestTakeWhile ()
  129. {
  130. int [] data = {0, 1, 2, 3, 4, 5};
  131. int [] result = {0, 1, 2};
  132. AssertAreSame (result, data.TakeWhile (i => i < 3));
  133. }
  134. [Test]
  135. public void TestSelect ()
  136. {
  137. int [] data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  138. int [] result = {1, 3, 5, 7, 9};
  139. AssertAreSame (result, data.Where (i => i % 2 != 0));
  140. }
  141. [Test]
  142. public void TestReverse ()
  143. {
  144. int [] data = {0, 1, 2, 3, 4};
  145. int [] result = {4, 3, 2, 1, 0};
  146. AssertAreSame (result, data.Reverse ());
  147. AssertAreSame (result, Enumerable.Range (0, 5).Reverse ());
  148. }
  149. [Test]
  150. public void TestSum ()
  151. {
  152. int [] data = {1, 2, 3, 4};
  153. Assert.AreEqual (10, data.Sum ());
  154. }
  155. [Test]
  156. public void SumOnEmpty ()
  157. {
  158. int [] data = {};
  159. Assert.AreEqual (0, data.Sum ());
  160. }
  161. [Test]
  162. public void TestMax ()
  163. {
  164. int [] data = {1, 3, 5, 2};
  165. Assert.AreEqual (5, data.Max ());
  166. }
  167. [Test]
  168. public void TestMin ()
  169. {
  170. int [] data = {3, 5, 2, 6, 1, 7};
  171. Assert.AreEqual (1, data.Min ());
  172. }
  173. [Test]
  174. public void TestToList ()
  175. {
  176. int [] data = {3, 5, 2};
  177. var list = data.ToList ();
  178. AssertAreSame (data, list);
  179. Assert.AreEqual (typeof (List<int>), list.GetType ());
  180. }
  181. [Test]
  182. public void TestToArray ()
  183. {
  184. ICollection<int> coll = new List<int> ();
  185. coll.Add (0);
  186. coll.Add (1);
  187. coll.Add (2);
  188. int [] result = {0, 1, 2};
  189. var array = coll.ToArray ();
  190. AssertAreSame (result, array);
  191. Assert.AreEqual (typeof (int []), array.GetType ());
  192. }
  193. [Test]
  194. public void TestAverageOnInt32 ()
  195. {
  196. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  197. }
  198. [Test]
  199. public void TestAverageOnInt64 ()
  200. {
  201. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  202. }
  203. [Test]
  204. public void TestRange ()
  205. {
  206. AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
  207. AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
  208. }
  209. [Test]
  210. public void TestOrderBy ()
  211. {
  212. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  213. var q = from i in array
  214. orderby i
  215. select i;
  216. AssertIsOrdered (q);
  217. }
  218. class Baz {
  219. string name;
  220. int age;
  221. public string Name
  222. {
  223. get {
  224. if (string.IsNullOrEmpty (name))
  225. return Age.ToString ();
  226. return name + " (" + Age + ")";
  227. }
  228. }
  229. public int Age
  230. {
  231. get { return age + 1; }
  232. }
  233. public Baz (string name, int age)
  234. {
  235. this.name = name;
  236. this.age = age;
  237. }
  238. public override int GetHashCode ()
  239. {
  240. return this.Age ^ this.Name.GetHashCode ();
  241. }
  242. public override bool Equals (object obj)
  243. {
  244. Baz b = obj as Baz;
  245. if (b == null)
  246. return false;
  247. return b.Age == this.Age && b.Name == this.Name;
  248. }
  249. public override string ToString ()
  250. {
  251. return this.Name;
  252. }
  253. }
  254. static IEnumerable<Baz> CreateBazCollection ()
  255. {
  256. return new [] {
  257. new Baz ("jb", 25),
  258. new Baz ("ana", 20),
  259. new Baz ("reg", 28),
  260. new Baz ("ro", 25),
  261. new Baz ("jb", 7),
  262. };
  263. }
  264. [Test]
  265. public void TestOrderByAgeAscendingTheByNameDescending ()
  266. {
  267. var q = from b in CreateBazCollection ()
  268. orderby b.Age ascending, b.Name descending
  269. select b;
  270. var expected = new [] {
  271. new Baz ("jb", 7),
  272. new Baz ("ana", 20),
  273. new Baz ("ro", 25),
  274. new Baz ("jb", 25),
  275. new Baz ("reg", 28),
  276. };
  277. AssertAreSame (expected, q);
  278. }
  279. class Data {
  280. public int ID { get; set; }
  281. public string Name { get; set; }
  282. public override string ToString ()
  283. {
  284. return ID + " " + Name;
  285. }
  286. }
  287. IEnumerable<Data> CreateData ()
  288. {
  289. return new [] {
  290. new Data { ID = 10, Name = "bcd" },
  291. new Data { ID = 20, Name = "Abcd" },
  292. new Data { ID = 20, Name = "Ab" },
  293. new Data { ID = 10, Name = "Zyx" },
  294. };
  295. }
  296. [Test]
  297. public void TestOrderByIdDescendingThenByNameAscending ()
  298. {
  299. var q = from d in CreateData ()
  300. orderby d.ID descending, d.Name ascending
  301. select d;
  302. var list = new List<Data> (q);
  303. Assert.AreEqual ("Ab", list [0].Name);
  304. Assert.AreEqual ("Abcd", list [1].Name);
  305. Assert.AreEqual ("bcd", list [2].Name);
  306. Assert.AreEqual ("Zyx", list [3].Name);
  307. }
  308. static void AssertIsOrdered (IEnumerable<int> e)
  309. {
  310. int f = int.MinValue;
  311. foreach(int i in e) {
  312. Assert.IsTrue (f <= i);
  313. f = i;
  314. }
  315. }
  316. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  317. {
  318. if (expected == null) {
  319. Assert.IsNull (actual);
  320. return;
  321. }
  322. Assert.IsNotNull (actual);
  323. IEnumerator<T> ee = expected.GetEnumerator ();
  324. IEnumerator<T> ea = actual.GetEnumerator ();
  325. while (ee.MoveNext ()) {
  326. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  327. Assert.AreEqual (ee.Current, ea.Current);
  328. }
  329. if (ea.MoveNext ())
  330. Assert.Fail ("Unexpected element: " + ea.Current);
  331. }
  332. }
  333. }