PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mcs/class/System.Core/Test/System.Linq/EnumerableTest.cs

https://bitbucket.org/steenlund/mono-2.6.7-for-amiga
C# | 523 lines | 388 code | 108 blank | 27 comment | 12 complexity | f50ee77f2bed1706ff95b4fe86af742b MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0, LGPL-2.1
  1. //
  2. // EnumerableTest.cs
  3. //
  4. // Author:
  5. // Jb Evain (jbevain@novell.com)
  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 TestSum ()
  177. {
  178. int [] data = {1, 2, 3, 4};
  179. Assert.AreEqual (10, data.Sum ());
  180. }
  181. [Test]
  182. public void SumOnEmpty ()
  183. {
  184. int [] data = {};
  185. Assert.AreEqual (0, data.Sum ());
  186. }
  187. [Test]
  188. public void TestMax ()
  189. {
  190. int [] data = {1, 3, 5, 2};
  191. Assert.AreEqual (5, data.Max ());
  192. }
  193. [Test]
  194. public void TestMaxNullableInt32 ()
  195. {
  196. int? [] data = { null, null, null };
  197. Assert.IsNull (data.Max (x => -x));
  198. data = new int? [] { null, 1, 2 };
  199. Assert.AreEqual (-1, data.Max (x => -x));
  200. }
  201. [Test]
  202. public void TestMin ()
  203. {
  204. int [] data = {3, 5, 2, 6, 1, 7};
  205. Assert.AreEqual (1, data.Min ());
  206. }
  207. [Test]
  208. public void TestMinNullableInt32 ()
  209. {
  210. int? [] data = { null, null, null };
  211. Assert.IsNull (data.Min(x => -x));
  212. data = new int? [] { null, 1, 2 };
  213. Assert.AreEqual (-2, data.Min (x => -x));
  214. }
  215. [Test]
  216. public void TestMinStringEmpty ()
  217. {
  218. Assert.IsNull ((new string [0]).Min ());
  219. }
  220. [Test]
  221. public void TestMaxStringEmpty ()
  222. {
  223. Assert.IsNull ((new string [0]).Max ());
  224. }
  225. [Test]
  226. public void TestToList ()
  227. {
  228. int [] data = {3, 5, 2};
  229. var list = data.ToList ();
  230. AssertAreSame (data, list);
  231. Assert.AreEqual (typeof (List<int>), list.GetType ());
  232. }
  233. [Test]
  234. public void TestToArray ()
  235. {
  236. ICollection<int> coll = new List<int> ();
  237. coll.Add (0);
  238. coll.Add (1);
  239. coll.Add (2);
  240. int [] result = {0, 1, 2};
  241. var array = coll.ToArray ();
  242. AssertAreSame (result, array);
  243. Assert.AreEqual (typeof (int []), array.GetType ());
  244. }
  245. [Test]
  246. public void TestIntersect ()
  247. {
  248. int [] left = { 1, 1 }, right = { 1, 1 };
  249. int [] result = { 1 };
  250. AssertAreSame (result, left.Intersect (right));
  251. }
  252. [Test]
  253. public void TestAverageOnInt32 ()
  254. {
  255. Assert.AreEqual (23.25, (new int [] { 24, 7, 28, 34 }).Average ());
  256. }
  257. [Test]
  258. public void TestAverageOnInt64 ()
  259. {
  260. Assert.AreEqual (23.25, (new long [] { 24, 7, 28, 34 }).Average ());
  261. }
  262. [Test]
  263. public void TestAverageOnLongNullable ()
  264. {
  265. List<long?> list = new List<long?> ();
  266. list.Add (2);
  267. list.Add (3);
  268. Assert.AreEqual (2.5d, list.Average ());
  269. }
  270. [Test]
  271. public void TestRange ()
  272. {
  273. AssertAreSame (new [] {1, 2, 3, 4}, Enumerable.Range (1, 4));
  274. AssertAreSame (new [] {0, 1, 2, 3}, Enumerable.Range (0, 4));
  275. }
  276. [Test]
  277. public void TestTakeTakesProperNumberOfItems ()
  278. {
  279. var stream = new MemoryStream (new byte [] { 1, 2, 3, 4, 0 });
  280. Assert.AreEqual (0, stream.Position);
  281. foreach (byte b in AsEnumerable (stream).Take (2))
  282. ;
  283. Assert.AreEqual (2, stream.Position);
  284. }
  285. static IEnumerable<byte> AsEnumerable (Stream stream)
  286. {
  287. byte b;
  288. while ((b = (byte) stream.ReadByte ()) >= 0)
  289. yield return b;
  290. }
  291. [Test]
  292. public void TestOrderBy ()
  293. {
  294. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  295. var q = from i in array
  296. orderby i
  297. select i;
  298. AssertIsOrdered (q);
  299. }
  300. class Baz {
  301. string name;
  302. int age;
  303. public string Name
  304. {
  305. get {
  306. if (string.IsNullOrEmpty (name))
  307. return Age.ToString ();
  308. return name + " (" + Age + ")";
  309. }
  310. }
  311. public int Age
  312. {
  313. get { return age + 1; }
  314. }
  315. public Baz (string name, int age)
  316. {
  317. this.name = name;
  318. this.age = age;
  319. }
  320. public override int GetHashCode ()
  321. {
  322. return this.Age ^ this.Name.GetHashCode ();
  323. }
  324. public override bool Equals (object obj)
  325. {
  326. Baz b = obj as Baz;
  327. if (b == null)
  328. return false;
  329. return b.Age == this.Age && b.Name == this.Name;
  330. }
  331. public override string ToString ()
  332. {
  333. return this.Name;
  334. }
  335. }
  336. static IEnumerable<Baz> CreateBazCollection ()
  337. {
  338. return new [] {
  339. new Baz ("jb", 25),
  340. new Baz ("ana", 20),
  341. new Baz ("reg", 28),
  342. new Baz ("ro", 25),
  343. new Baz ("jb", 7),
  344. };
  345. }
  346. [Test]
  347. public void TestOrderByAgeAscendingTheByNameDescending ()
  348. {
  349. var q = from b in CreateBazCollection ()
  350. orderby b.Age ascending, b.Name descending
  351. select b;
  352. var expected = new [] {
  353. new Baz ("jb", 7),
  354. new Baz ("ana", 20),
  355. new Baz ("ro", 25),
  356. new Baz ("jb", 25),
  357. new Baz ("reg", 28),
  358. };
  359. AssertAreSame (expected, q);
  360. }
  361. class Data {
  362. public int ID { get; set; }
  363. public string Name { get; set; }
  364. public override string ToString ()
  365. {
  366. return ID + " " + Name;
  367. }
  368. }
  369. IEnumerable<Data> CreateData ()
  370. {
  371. return new [] {
  372. new Data { ID = 10, Name = "bcd" },
  373. new Data { ID = 20, Name = "Abcd" },
  374. new Data { ID = 20, Name = "Ab" },
  375. new Data { ID = 10, Name = "Zyx" },
  376. };
  377. }
  378. [Test]
  379. public void TestOrderByIdDescendingThenByNameAscending ()
  380. {
  381. var q = from d in CreateData ()
  382. orderby d.ID descending, d.Name ascending
  383. select d;
  384. var list = new List<Data> (q);
  385. Assert.AreEqual ("Ab", list [0].Name);
  386. Assert.AreEqual ("Abcd", list [1].Name);
  387. Assert.AreEqual ("bcd", list [2].Name);
  388. Assert.AreEqual ("Zyx", list [3].Name);
  389. }
  390. static void AssertIsOrdered (IEnumerable<int> e)
  391. {
  392. int f = int.MinValue;
  393. foreach(int i in e) {
  394. Assert.IsTrue (f <= i);
  395. f = i;
  396. }
  397. }
  398. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  399. {
  400. if (expected == null) {
  401. Assert.IsNull (actual);
  402. return;
  403. }
  404. Assert.IsNotNull (actual);
  405. IEnumerator<T> ee = expected.GetEnumerator ();
  406. IEnumerator<T> ea = actual.GetEnumerator ();
  407. while (ee.MoveNext ()) {
  408. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  409. Assert.AreEqual (ee.Current, ea.Current);
  410. }
  411. if (ea.MoveNext ())
  412. Assert.Fail ("Unexpected element: " + ea.Current);
  413. }
  414. }
  415. }