EnumerableTest.cs 7.1 KB

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