EnumerableTest.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 TestOrderBy ()
  188. {
  189. int [] array = { 14, 53, 3, 9, 11, 14, 5, 32, 2 };
  190. var q = from i in array
  191. orderby i
  192. select i;
  193. AssertIsOrdered (q);
  194. }
  195. static void AssertIsOrdered (IEnumerable<int> e)
  196. {
  197. int f = int.MinValue;
  198. foreach(int i in e) {
  199. Assert.IsTrue (f <= i);
  200. f = i;
  201. }
  202. }
  203. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  204. {
  205. if (expected == null) {
  206. Assert.IsNull (actual);
  207. return;
  208. }
  209. Assert.IsNotNull (actual);
  210. IEnumerator<T> ee = expected.GetEnumerator ();
  211. IEnumerator<T> ea = actual.GetEnumerator ();
  212. while (ee.MoveNext ()) {
  213. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  214. Assert.AreEqual (ee.Current, ea.Current);
  215. }
  216. if (ea.MoveNext ())
  217. Assert.Fail ("Unexpected element: " + ea.Current);
  218. }
  219. }
  220. }