EnumerableTest.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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 TestMax ()
  157. {
  158. int [] data = {1, 3, 5, 2};
  159. Assert.AreEqual (5, data.Max ());
  160. }
  161. [Test]
  162. public void TestMin ()
  163. {
  164. int [] data = {3, 5, 2, 6, 1, 7};
  165. Assert.AreEqual (1, data.Min ());
  166. }
  167. [Test]
  168. public void TestToList ()
  169. {
  170. int [] data = {3, 5, 2};
  171. var list = data.ToList ();
  172. AssertAreSame (data, list);
  173. Assert.AreEqual (typeof (List<int>), list.GetType ());
  174. }
  175. [Test]
  176. public void TestToArray ()
  177. {
  178. ICollection<int> coll = new List<int> ();
  179. coll.Add (0);
  180. coll.Add (1);
  181. coll.Add (2);
  182. int [] result = {0, 1, 2};
  183. var array = coll.ToArray ();
  184. AssertAreSame (result, array);
  185. Assert.AreEqual (typeof (int []), array.GetType ());
  186. }
  187. [Test]
  188. public void TestAverageOnInt32 ()
  189. {
  190. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  191. }
  192. [Test]
  193. public void TestAverageOnInt64 ()
  194. {
  195. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  196. }
  197. [Test]
  198. public void TestRange ()
  199. {
  200. AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
  201. AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
  202. }
  203. [Test]
  204. public void TestOrderBy ()
  205. {
  206. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  207. var q = from i in array
  208. orderby i
  209. select i;
  210. AssertIsOrdered (q);
  211. }
  212. class Baz {
  213. string name;
  214. int age;
  215. public string Name
  216. {
  217. get {
  218. if (string.IsNullOrEmpty (name))
  219. return Age.ToString ();
  220. return name + " (" + Age + ")";
  221. }
  222. }
  223. public int Age
  224. {
  225. get { return age + 1; }
  226. }
  227. public Baz (string name, int age)
  228. {
  229. this.name = name;
  230. this.age = age;
  231. }
  232. public override int GetHashCode ()
  233. {
  234. return this.Age ^ this.Name.GetHashCode ();
  235. }
  236. public override bool Equals (object obj)
  237. {
  238. Baz b = obj as Baz;
  239. if (b == null)
  240. return false;
  241. return b.Age == this.Age && b.Name == this.Name;
  242. }
  243. public override string ToString ()
  244. {
  245. return this.Name;
  246. }
  247. }
  248. static IEnumerable<Baz> CreateBazCollection ()
  249. {
  250. return new [] {
  251. new Baz ("jb", 25),
  252. new Baz ("ana", 20),
  253. new Baz ("reg", 28),
  254. new Baz ("ro", 25),
  255. new Baz ("jb", 7),
  256. };
  257. }
  258. [Test]
  259. public void TestOrderByAgeAscendingTheByNameDescending ()
  260. {
  261. var q = from b in CreateBazCollection ()
  262. orderby b.Age ascending, b.Name descending
  263. select b;
  264. var expected = new [] {
  265. new Baz ("jb", 7),
  266. new Baz ("ana", 20),
  267. new Baz ("ro", 25),
  268. new Baz ("jb", 25),
  269. new Baz ("reg", 28),
  270. };
  271. AssertAreSame (expected, q);
  272. }
  273. class Data {
  274. public int ID { get; set; }
  275. public string Name { get; set; }
  276. public override string ToString ()
  277. {
  278. return ID + " " + Name;
  279. }
  280. }
  281. IEnumerable<Data> CreateData ()
  282. {
  283. return new [] {
  284. new Data { ID = 10, Name = "bcd" },
  285. new Data { ID = 20, Name = "Abcd" },
  286. new Data { ID = 20, Name = "Ab" },
  287. new Data { ID = 10, Name = "Zyx" },
  288. };
  289. }
  290. [Test]
  291. public void TestOrderByIdDescendingThenByNameAscending ()
  292. {
  293. var q = from d in CreateData ()
  294. orderby d.ID descending, d.Name ascending
  295. select d;
  296. var list = new List<Data> (q);
  297. Assert.AreEqual ("Ab", list [0].Name);
  298. Assert.AreEqual ("Abcd", list [1].Name);
  299. Assert.AreEqual ("bcd", list [2].Name);
  300. Assert.AreEqual ("Zyx", list [3].Name);
  301. }
  302. static void AssertIsOrdered (IEnumerable<int> e)
  303. {
  304. int f = int.MinValue;
  305. foreach(int i in e) {
  306. Assert.IsTrue (f <= i);
  307. f = i;
  308. }
  309. }
  310. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  311. {
  312. if (expected == null) {
  313. Assert.IsNull (actual);
  314. return;
  315. }
  316. Assert.IsNotNull (actual);
  317. IEnumerator<T> ee = expected.GetEnumerator ();
  318. IEnumerator<T> ea = actual.GetEnumerator ();
  319. while (ee.MoveNext ()) {
  320. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  321. Assert.AreEqual (ee.Current, ea.Current);
  322. }
  323. if (ea.MoveNext ())
  324. Assert.Fail ("Unexpected element: " + ea.Current);
  325. }
  326. }
  327. }