EnumerableMoreTest.cs 97 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280
  1. //
  2. // EnumerableMoreTest.cs
  3. //
  4. // Author:
  5. // Andreas Noever <[email protected]>
  6. //
  7. // (C) 2007 Andreas Noever
  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 NUnit.Framework;
  33. namespace MonoTests.System.Linq {
  34. [TestFixture]
  35. public class EnumerableMoreTest {
  36. class BigEnumerable : IEnumerable<int> {
  37. public readonly ulong Count;
  38. public BigEnumerable (ulong Count)
  39. {
  40. this.Count = Count;
  41. }
  42. #region IEnumerable<int> Members
  43. public IEnumerator<int> GetEnumerator ()
  44. {
  45. return new BigEnumerator (this);
  46. }
  47. #endregion
  48. #region IEnumerable Members
  49. IEnumerator IEnumerable.GetEnumerator ()
  50. {
  51. throw new NotImplementedException ();
  52. }
  53. #endregion
  54. }
  55. class BigEnumerator : IEnumerator<int> {
  56. BigEnumerable Parent;
  57. private ulong current;
  58. public BigEnumerator (BigEnumerable parent)
  59. {
  60. Parent = parent;
  61. }
  62. public int Current
  63. {
  64. get { return 3; }
  65. }
  66. public void Dispose ()
  67. {
  68. }
  69. object IEnumerator.Current
  70. {
  71. get { throw new NotImplementedException (); }
  72. }
  73. public bool MoveNext ()
  74. {
  75. if (current == Parent.Count)
  76. return false;
  77. current++;
  78. return true;
  79. }
  80. public void Reset ()
  81. {
  82. throw new NotImplementedException ();
  83. }
  84. }
  85. public static void AssertException<T> (Action action) where T : Exception
  86. {
  87. try {
  88. action ();
  89. }
  90. catch (T) {
  91. return;
  92. }
  93. Assert.Fail ("Expected: " + typeof (T).Name);
  94. }
  95. static void AssertAreSame<K, V> (K expectedKey, IEnumerable<V> expectedValues, IGrouping<K, V> actual)
  96. {
  97. if (expectedValues == null) {
  98. Assert.IsNull (actual);
  99. return;
  100. }
  101. Assert.IsNotNull (actual);
  102. Assert.AreEqual (expectedKey, actual.Key);
  103. var ee = expectedValues.GetEnumerator ();
  104. var ea = actual.GetEnumerator ();
  105. while (ee.MoveNext ()) {
  106. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  107. Assert.AreEqual (ee.Current, ea.Current);
  108. }
  109. if (ea.MoveNext ())
  110. Assert.Fail ("Unexpected element: " + ee.Current);
  111. }
  112. static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, IEnumerable<IGrouping<K, V>> actual)
  113. {
  114. if (expected == null) {
  115. Assert.IsNull (actual);
  116. return;
  117. }
  118. Assert.IsNotNull (actual);
  119. var ee = expected.GetEnumerator ();
  120. var ea = actual.GetEnumerator ();
  121. while (ee.MoveNext ()) {
  122. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
  123. AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
  124. }
  125. if (ea.MoveNext ())
  126. Assert.Fail ("Unexpected element: " + ee.Current.Key);
  127. }
  128. static void AssertAreSame<K, V> (IDictionary<K, IEnumerable<V>> expected, ILookup<K, V> actual)
  129. {
  130. if (expected == null) {
  131. Assert.IsNull (actual);
  132. return;
  133. }
  134. Assert.IsNotNull (actual);
  135. var ee = expected.GetEnumerator ();
  136. var ea = actual.GetEnumerator ();
  137. while (ee.MoveNext ()) {
  138. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + "' expected.");
  139. AssertAreSame (ee.Current.Key, ee.Current.Value, ea.Current);
  140. }
  141. if (ea.MoveNext ())
  142. Assert.Fail ("Unexpected element: " + ee.Current.Key);
  143. }
  144. static void AssertAreSame<K, V> (IDictionary<K, V> expected, IDictionary<K, V> actual)
  145. {
  146. if (expected == null) {
  147. Assert.IsNull (actual);
  148. return;
  149. }
  150. Assert.IsNotNull (actual);
  151. var ee = expected.GetEnumerator ();
  152. var ea = actual.GetEnumerator ();
  153. while (ee.MoveNext ()) {
  154. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current.Key + ", " + ee.Current.Value + "' expected.");
  155. Assert.AreEqual (ee.Current.Key, ea.Current.Key);
  156. Assert.AreEqual (ee.Current.Value, ea.Current.Value);
  157. }
  158. if (ea.MoveNext ())
  159. Assert.Fail ("Unexpected element: " + ee.Current.Key + ", " + ee.Current.Value);
  160. }
  161. static void AssertAreSame<T> (IEnumerable<T> expected, IEnumerable<T> actual)
  162. {
  163. if (expected == null) {
  164. Assert.IsNull (actual);
  165. return;
  166. }
  167. Assert.IsNotNull (actual);
  168. IEnumerator<T> ee = expected.GetEnumerator ();
  169. IEnumerator<T> ea = actual.GetEnumerator ();
  170. while (ee.MoveNext ()) {
  171. Assert.IsTrue (ea.MoveNext (), "'" + ee.Current + "' expected.");
  172. Assert.AreEqual (ee.Current, ea.Current);
  173. }
  174. if (ea.MoveNext ())
  175. Assert.Fail ("Unexpected element: " + ea.Current);
  176. }
  177. [Test]
  178. public void FirstArgumentNullTest ()
  179. {
  180. string [] data = { "2", "1", "5", "3", "4" };
  181. // First<TSource> ()
  182. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).First (); });
  183. // First<TSource> (Func<TSource, bool>)
  184. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).First ((x => true)); });
  185. AssertException<ArgumentNullException> (delegate () { data.First ((Func<string, bool>) null); });
  186. }
  187. [Test]
  188. public void FirstTest ()
  189. {
  190. int [] data = { 2, 1, 5, 3, 4 };
  191. int [] empty = { };
  192. // First<TSource> ()
  193. Assert.AreEqual (2, data.First ());
  194. AssertException<InvalidOperationException> (delegate () { empty.First (); });
  195. // First<TSource> (Func<TSource, bool>)
  196. Assert.AreEqual (5, data.First (x => x == 5));
  197. AssertException<InvalidOperationException> (delegate () { empty.First (x => x == 5); });
  198. AssertException<InvalidOperationException> (delegate () { data.First (x => x == 6); });
  199. }
  200. [Test]
  201. public void FirstOrDefaultArgumentNullTest ()
  202. {
  203. string [] data = { "2", "1", "5", "3", "4" };
  204. // FirstOrDefault<TSource> ()
  205. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).FirstOrDefault (); });
  206. // FirstOrDefault<TSource> (Func<string, bool>)
  207. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).FirstOrDefault ((x => true)); });
  208. AssertException<ArgumentNullException> (delegate () { data.FirstOrDefault ((Func<string, bool>) null); });
  209. }
  210. [Test]
  211. public void FirstOrDefaultTest ()
  212. {
  213. int [] data = { 2, 1, 5, 3, 4 };
  214. int [] empty = { };
  215. // FirstOrDefault<TSource> ()
  216. Assert.AreEqual (2, data.FirstOrDefault ());
  217. Assert.AreEqual (0, empty.FirstOrDefault ());
  218. // FirstOrDefault<TSource> (Func<TSource, bool>)
  219. Assert.AreEqual (5, data.FirstOrDefault (x => x == 5));
  220. Assert.AreEqual (0, empty.FirstOrDefault (x => x == 5));
  221. Assert.AreEqual (0, data.FirstOrDefault (x => x == 6));
  222. }
  223. [Test]
  224. public void LastArgumentNullTest ()
  225. {
  226. string [] data = { "2", "1", "5", "3", "4" };
  227. // Last<TSource> ()
  228. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Last (); });
  229. // Last<TSource> (Func<TSource, bool>)
  230. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Last (x => true); });
  231. AssertException<ArgumentNullException> (delegate () { data.Last ((Func<string, bool>) null); });
  232. }
  233. [Test]
  234. public void LastTest ()
  235. {
  236. int [] data = { 2, 1, 1, 3, 4, 5 };
  237. int [] empty = { };
  238. // Last<TSource> ()
  239. Assert.AreEqual (5, data.Last ());
  240. AssertException<InvalidOperationException> (delegate () { empty.Last (); });
  241. // Last<TSource> (Func<TSource, bool>)
  242. Assert.AreEqual (4, data.Last (x => x < 5));
  243. AssertException<InvalidOperationException> (delegate () { empty.Last (x => x == 5); });
  244. AssertException<InvalidOperationException> (delegate () { data.Last (x => x == 6); });
  245. }
  246. [Test]
  247. public void LastOrDefaultArgumentNullTest ()
  248. {
  249. string [] data = { "2", "1", "5", "3", "4" };
  250. // LastOrDefault<TSource> ()
  251. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LastOrDefault (); });
  252. // LastOrDefault<TSource> (Func<TSource, bool>)
  253. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LastOrDefault (x => true); });
  254. AssertException<ArgumentNullException> (delegate () { data.LastOrDefault ((Func<string, bool>) null); });
  255. }
  256. [Test]
  257. public void LastOrDefaultTest ()
  258. {
  259. int [] data = { 2, 1, 5, 3, 4 };
  260. int [] empty = { };
  261. // LastOrDefault<TSource> ()
  262. Assert.AreEqual (4, data.LastOrDefault ());
  263. Assert.AreEqual (0, empty.LastOrDefault ());
  264. // LastOrDefault<TSource> (Func<TSource, bool>)
  265. Assert.AreEqual (3, data.LastOrDefault (x => x < 4));
  266. Assert.AreEqual (0, empty.LastOrDefault (x => x == 5));
  267. Assert.AreEqual (0, data.LastOrDefault (x => x == 6));
  268. }
  269. [Test]
  270. public void SingleArgumentNullTest ()
  271. {
  272. string [] data = { "2", "1", "5", "3", "4" };
  273. // Single<TSource> ()
  274. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Single (); });
  275. // Single<TSource> (Func<TSource, bool>)
  276. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Single ((x => true)); });
  277. AssertException<ArgumentNullException> (delegate () { data.Single ((Func<string, bool>) null); });
  278. }
  279. [Test]
  280. public void SingleTest ()
  281. {
  282. int [] data = { 2 };
  283. int [] data2 = { 2, 3, 5 };
  284. int [] empty = { };
  285. // Single<TSource> ()
  286. Assert.AreEqual (2, data.Single ());
  287. AssertException<InvalidOperationException> (delegate () { data2.Single (); });
  288. AssertException<InvalidOperationException> (delegate () { empty.Single (); });
  289. // Single<TSource> (Func<TSource, bool>)
  290. Assert.AreEqual (5, data2.Single (x => x == 5));
  291. AssertException<InvalidOperationException> (delegate () { data2.Single (x => false); });
  292. AssertException<InvalidOperationException> (delegate () { data2.Single (x => true); });
  293. AssertException<InvalidOperationException> (delegate () { empty.Single (x => true); });
  294. }
  295. [Test]
  296. public void SingleOrDefaultArgumentNullTest ()
  297. {
  298. string [] data = { "2", "1", "5", "3", "4" };
  299. // SingleOrDefault<TSource> ()
  300. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SingleOrDefault (); });
  301. // SingleOrDefault<TSource> (Func<TSource, bool>)
  302. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SingleOrDefault (x => true); });
  303. AssertException<ArgumentNullException> (delegate () { data.SingleOrDefault ((Func<string, bool>) null); });
  304. }
  305. [Test]
  306. public void SingleOrDefaultTest ()
  307. {
  308. int [] data = { 2 };
  309. int [] data2 = { 2, 3, 5 };
  310. int [] empty = { };
  311. // SingleOrDefault<TSource> ()
  312. Assert.AreEqual (2, data.SingleOrDefault ());
  313. Assert.AreEqual (0, empty.SingleOrDefault ());
  314. AssertException<InvalidOperationException> (delegate () { data2.SingleOrDefault (); });
  315. // SingleOrDefault<TSource> (Func<TSource, bool>)
  316. Assert.AreEqual (3, data2.SingleOrDefault (x => x == 3));
  317. Assert.AreEqual (0, data2.SingleOrDefault (x => false));
  318. AssertException<InvalidOperationException> (delegate () { data2.SingleOrDefault (x => true); });
  319. }
  320. [Test]
  321. public void ElementAtArgumentNullTest ()
  322. {
  323. string [] data = { "2", "1", "5", "3", "4" };
  324. // ElementAt<TSource> (int)
  325. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ElementAt (0); });
  326. }
  327. [Test]
  328. public void ElementAtTest ()
  329. {
  330. int [] data = { 2, 3, 4, 5 };
  331. // ElementAt<string> (int)
  332. Assert.AreEqual (2, data.ElementAt (0));
  333. Assert.AreEqual (4, data.ElementAt (2));
  334. AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (-1); });
  335. AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (4); });
  336. AssertException<ArgumentOutOfRangeException> (delegate () { data.ElementAt (6); });
  337. }
  338. [Test]
  339. public void ElementAtOrDefaultArgumentNullTest ()
  340. {
  341. string [] data = { "2", "1", "5", "3", "4" };
  342. // ElementAtOrDefault<TSource> (int)
  343. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ElementAtOrDefault (0); });
  344. }
  345. [Test]
  346. public void ElementAtOrDefaultTest ()
  347. {
  348. int [] data = { 2, 3, 4, 5 };
  349. int [] empty = { };
  350. // ElementAtOrDefault<TSource> (int)
  351. Assert.AreEqual (2, data.ElementAtOrDefault (0));
  352. Assert.AreEqual (4, data.ElementAtOrDefault (2));
  353. Assert.AreEqual (0, data.ElementAtOrDefault (-1));
  354. Assert.AreEqual (0, data.ElementAtOrDefault (4));
  355. Assert.AreEqual (0, empty.ElementAtOrDefault (4));
  356. }
  357. [Test]
  358. public void EmptyTest ()
  359. {
  360. IEnumerable<string> empty = Enumerable.Empty<string> ();
  361. Assert.IsFalse (empty.GetEnumerator ().MoveNext ());
  362. }
  363. [Test]
  364. public void AnyArgumentNullTest ()
  365. {
  366. string [] data = { "2", "1", "5", "3", "4" };
  367. // Any<TSource> ()
  368. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Any (); });
  369. // Any<TSource> (Func<TSource, bool>)
  370. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Any (x => true); });
  371. AssertException<ArgumentNullException> (delegate () { data.Any ((Func<string, bool>) null); });
  372. }
  373. [Test]
  374. public void AnyTest ()
  375. {
  376. int [] data = { 5, 2, 3, 1, 6 };
  377. int [] empty = { };
  378. // Any<TSource> ()
  379. Assert.IsTrue (data.Any ());
  380. Assert.IsFalse (empty.Any ());
  381. // Any<TSource> (Func<TSource, bool>)
  382. Assert.IsTrue (data.Any (x => x == 5));
  383. Assert.IsFalse (data.Any (x => x == 9));
  384. Assert.IsFalse (empty.Any (x => true));
  385. }
  386. [Test]
  387. public void AllArgumentNullTest ()
  388. {
  389. string [] data = { "2", "1", "5", "3", "4" };
  390. // All<TSource> (Func<TSource, bool>)
  391. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).All (x => true); });
  392. AssertException<ArgumentNullException> (delegate () { data.All ((Func<string, bool>) null); });
  393. }
  394. [Test]
  395. public void AllTest ()
  396. {
  397. int [] data = { 5, 2, 3, 1, 6 };
  398. int [] empty = { };
  399. // All<TSource> (Func<TSource, bool>)
  400. Assert.IsTrue (data.All (x => true));
  401. Assert.IsFalse (data.All (x => x != 1));
  402. Assert.IsTrue (empty.All (x => false));
  403. }
  404. [Test]
  405. public void CountArgumentNullTest ()
  406. {
  407. string [] data = { "2", "1", "5", "3", "4" };
  408. // Count<TSource> ()
  409. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Count (); });
  410. // Count<TSource> (Func<TSource, bool>)
  411. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Count (x => true); });
  412. AssertException<ArgumentNullException> (delegate () { data.Count ((Func<string, bool>) null); });
  413. }
  414. [Test]
  415. public void CountTest ()
  416. {
  417. int [] data = { 5, 2, 3, 1, 6 };
  418. // Count<TSource> ()
  419. Assert.AreEqual (5, data.Count ());
  420. // Count<TSource> (Func<TSource, bool>)
  421. Assert.AreEqual (3, data.Count (x => x < 5));
  422. }
  423. [Test]
  424. [Ignore ("Takes some time.")]
  425. public void CountOverflowTest ()
  426. {
  427. BigEnumerable data = new BigEnumerable ((ulong) int.MaxValue + 1);
  428. // Count<TSource> ()
  429. //AssertException<OverflowException> (delegate () { data.Count (); });
  430. // Count<TSource> (Func<TSource, bool>)
  431. //AssertException<OverflowException> (delegate () { data.Count (x => 3 == x); });
  432. // Documentation error: http://msdn2.microsoft.com/en-us/library/bb535181.aspx
  433. // An exception is only rasied if count > int.MaxValue. Not if source contains more than int.MaxValue elements.
  434. // AssertException<OverflowException> (delegate () { data.Count (x => 5 == x); });
  435. }
  436. [Test]
  437. public void LongCountArgumentNullTest ()
  438. {
  439. string [] data = { "2", "1", "5", "3", "4" };
  440. // LongCount<TSource> ()
  441. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (); });
  442. // LongCount<TSource> (Func<TSource, bool>)
  443. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (x => true); });
  444. AssertException<ArgumentNullException> (delegate () { data.LongCount ((Func<string, bool>) null); });
  445. }
  446. [Test]
  447. public void LongCountTest ()
  448. {
  449. int [] data = { 5, 2, 3, 1, 6 };
  450. //TODO: Overflow test...
  451. // LongCount<TSource> ()
  452. Assert.AreEqual (5, data.LongCount ());
  453. // LongCount<TSource> (Func<TSource, bool>)
  454. Assert.AreEqual (3, data.LongCount (x => x < 5));
  455. }
  456. [Test]
  457. public void ContainsArgumentNullTest ()
  458. {
  459. string [] data = { "2", "1", "5", "3", "4" };
  460. // Contains<TSource> (TSource)
  461. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Contains ("2"); });
  462. // Contains<TSource> (TSource, IEqualityComparer<TSource>)
  463. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Contains ("2", (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  464. }
  465. [Test]
  466. public void ContainsTest ()
  467. {
  468. int [] data = { 5, 2, 3, 1, 6 };
  469. // Contains<TSource> (TSource)
  470. Assert.IsTrue (data.Contains (2));
  471. Assert.IsFalse (data.Contains (0));
  472. // Contains<TSource> (TSource, IEqualityComparer<TSource>)
  473. Assert.IsTrue (data.Contains (2, EqualityComparer<int>.Default));
  474. Assert.IsFalse (data.Contains (0, EqualityComparer<int>.Default));
  475. }
  476. [Test]
  477. public void AggregateArgumentNullTest ()
  478. {
  479. string [] data = { "2", "1", "5", "3", "4" };
  480. // Aggregate<TSource> (Func<TSource, TSource, TSource>)
  481. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ((x, y) => "test"); });
  482. AssertException<ArgumentNullException> (delegate () { data.Aggregate ((Func<string, string, string>) null); });
  483. // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
  484. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test"); });
  485. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null); });
  486. // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
  487. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test", x => "test"); });
  488. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null, x => "test"); });
  489. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (x, y) => "test", (Func<string, string>) null); });
  490. }
  491. [Test]
  492. public void AggregateTest ()
  493. {
  494. string [] data = { "2", "1", "5", "3", "4" };
  495. string [] empty = { };
  496. // Aggregate<TSource> (Func<TSource, TSource, TSource>)
  497. Assert.AreEqual ("21534", data.Aggregate ((x, y) => x + y));
  498. AssertException<InvalidOperationException> (delegate () { empty.Aggregate ((x, y) => x + y); }); //only this overload throws
  499. // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
  500. Assert.AreEqual ("initial21534", (data.Aggregate ("initial", (x, y) => x + y)));
  501. // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
  502. Assert.AreEqual ("INITIAL21534", data.Aggregate ("initial", (x, y) => x + y, (x => x.ToUpper ())));
  503. }
  504. [Test]
  505. [Category ("NotWorking")]
  506. public void SumArgumentNullTest ()
  507. {
  508. string [] data = { "2", "1", "5", "3", "4" };
  509. // Sum<TSource> (Func<TSource, int>)
  510. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, int>) (x => 0)); });
  511. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, int>) null); });
  512. // Sum<TSource> (Func<TSource, Nullable<int>>)
  513. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  514. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<int>>) null); });
  515. // Sum<TSource> (Func<TSource, Int64>)
  516. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Int64>) (x => 0L)); });
  517. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Int64>) null); });
  518. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  519. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  520. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Int64>>) null); });
  521. // Sum<TSource> (Func<TSource, Single>)
  522. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Single>) (x => 0f)); });
  523. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Single>) null); });
  524. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  525. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  526. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Single>>) null); });
  527. // Sum<TSource> (Func<TSource, Double>)
  528. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Double>) (x => 0d)); });
  529. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Double>) null); });
  530. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  531. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  532. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Double>>) null); });
  533. // Sum<TSource> (Func<TSource, Decimal>)
  534. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Decimal>) (x => 0m)); });
  535. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Decimal>) null); });
  536. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  537. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  538. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Decimal>>) null); });
  539. // Sum (IEnumerable<int>)
  540. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Sum (); });
  541. // Sum (IEnumerable<int?>)
  542. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Sum (); });
  543. // Sum (IEnumerable<long>)
  544. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Sum (); });
  545. // Sum (IEnumerable<long?>)
  546. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Sum (); });
  547. // Sum (IEnumerable<float>)
  548. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Sum (); });
  549. // Sum (IEnumerable<float?>)
  550. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Sum (); });
  551. // Sum (IEnumerable<double>)
  552. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Sum (); });
  553. // Sum (IEnumerable<double?>)
  554. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Sum (); });
  555. // Sum (IEnumerable<decimal>)
  556. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Sum (); });
  557. // Sum (IEnumerable<decimal?>)
  558. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Sum (); });
  559. }
  560. [Test]
  561. [Category ("NotWorking")]
  562. public void SumTest ()
  563. {
  564. string [] data = { "2", "3", "5", "5" };
  565. //TODO: OverflowException
  566. // Sum<TSource> (Func<TSource, int>)
  567. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  568. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  569. // Sum<TSource> (Func<TSource, Nullable<int>>)
  570. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  571. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  572. // Sum<TSource> (Func<TSource, Int64>)
  573. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  574. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  575. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  576. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  577. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  578. // Sum<TSource> (Func<TSource, Single>)
  579. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  580. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  581. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  582. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  583. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  584. // Sum<TSource> (Func<TSource, Double>)
  585. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  586. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  587. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  588. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  589. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  590. // Sum<TSource> (Func<TSource, Decimal>)
  591. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  592. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  593. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  594. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  595. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  596. // Sum<> ()
  597. Assert.AreEqual (6, ((IEnumerable<int>) new int [] { 1, 2, 3 }).Sum ());
  598. Assert.AreEqual (0, Enumerable.Empty<int> ().Sum ());
  599. // Sum<> ()
  600. Assert.AreEqual (6, ((IEnumerable<Nullable<int>>) new int? [] { 1, 2, 3 }).Sum ());
  601. Assert.AreEqual (0, Enumerable.Empty<int?> ().Sum ());
  602. // Sum<> ()
  603. Assert.AreEqual (6, ((IEnumerable<Int64>) new long [] { 1, 2, 3 }).Sum ());
  604. Assert.AreEqual (0, Enumerable.Empty<long> ().Sum ());
  605. // Sum<> ()
  606. Assert.AreEqual (6, ((IEnumerable<Nullable<Int64>>) new long? [] { 1, 2, 3 }).Sum ());
  607. Assert.AreEqual (0, Enumerable.Empty<long?> ().Sum ());
  608. // Sum<> ()
  609. Assert.AreEqual (6, ((IEnumerable<Single>) new float [] { 1, 2, 3 }).Sum ());
  610. Assert.AreEqual (0, Enumerable.Empty<float> ().Sum ());
  611. // Sum<> ()
  612. Assert.AreEqual (6, ((IEnumerable<Nullable<Single>>) new float? [] { 1, 2, 3 }).Sum ());
  613. Assert.AreEqual (0, Enumerable.Empty<float?> ().Sum ());
  614. // Sum<> ()
  615. Assert.AreEqual (6, ((IEnumerable<Double>) new double [] { 1, 2, 3 }).Sum ());
  616. Assert.AreEqual (0, Enumerable.Empty<double> ().Sum ());
  617. // Sum<> ()
  618. Assert.AreEqual (6, ((IEnumerable<Nullable<Double>>) new double? [] { 1, 2, 3 }).Sum ());
  619. Assert.AreEqual (0, Enumerable.Empty<double?> ().Sum ());
  620. // Sum<> ()
  621. Assert.AreEqual (6, ((IEnumerable<Decimal>) new decimal [] { 1, 2, 3 }).Sum ());
  622. Assert.AreEqual (0, Enumerable.Empty<decimal> ().Sum ());
  623. // Sum<> ()
  624. Assert.AreEqual (6, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 1, 2, 3 }).Sum ());
  625. Assert.AreEqual (0, Enumerable.Empty<decimal?> ().Sum ());
  626. }
  627. [Test]
  628. [Category ("NotWorking")]
  629. public void MinArgumentNullTest ()
  630. {
  631. string [] data = { "2", "1", "5", "3", "4" };
  632. // Min<TSource> ()
  633. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> (); });
  634. // Min<TSource> (Func<TSource, int>)
  635. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, int>) (x => 0)); });
  636. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, int>) null); });
  637. // Min<TSource> (Func<TSource, Nullable<int>>)
  638. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  639. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<int>>) null); });
  640. // Min<TSource> (Func<TSource, Int64>)
  641. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Int64>) (x => 0L)); });
  642. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Int64>) null); });
  643. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  644. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  645. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Int64>>) null); });
  646. // Min<TSource> (Func<TSource, Single>)
  647. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Single>) (x => 0f)); });
  648. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Single>) null); });
  649. // Min<TSource> (Func<TSource, Nullable<Single>>)
  650. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  651. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Single>>) null); });
  652. // Min<TSource> (Func<TSource, Double>)
  653. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Double>) (x => 0d)); });
  654. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Double>) null); });
  655. // Min<TSource> (Func<TSource, Nullable<Double>>)
  656. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  657. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Double>>) null); });
  658. // Min<TSource> (Func<TSource, Decimal>)
  659. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Decimal>) (x => 0m)); });
  660. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Decimal>) null); });
  661. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  662. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  663. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Decimal>>) null); });
  664. // Min<TSource,TSource> (Func<TSource, string>)
  665. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string, string> ((Func<string, string>) (x => "test")); });
  666. AssertException<ArgumentNullException> (delegate () { data.Min<string, string> ((Func<string, string>) null); });
  667. // Min<> ()
  668. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Min (); });
  669. // Min<> ()
  670. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Min (); });
  671. // Min<> ()
  672. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Min (); });
  673. // Min<> ()
  674. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Min (); });
  675. // Min<> ()
  676. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Min (); });
  677. // Min<> ()
  678. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Min (); });
  679. // Min<> ()
  680. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Min (); });
  681. // Min<> ()
  682. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Min (); });
  683. // Min<> ()
  684. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Min (); });
  685. // Min<> ()
  686. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Min (); });
  687. }
  688. [Test]
  689. [Category ("NotWorking")]
  690. public void MinTest ()
  691. {
  692. string [] data = { "2", "1", "5", "3", "4" };
  693. // Min<TSource> ()
  694. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string> ());
  695. // Min<TSource> (Func<TSource, int>)
  696. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, int>) (x => int.Parse (x))));
  697. // Min<TSource> (Func<TSource, Nullable<int>>)
  698. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  699. // Min<TSource> (Func<TSource, Int64>)
  700. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Int64>) (x => int.Parse (x))));
  701. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  702. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  703. // Min<TSource> (Func<TSource, Single>)
  704. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Single>) (x => int.Parse (x))));
  705. // Min<TSource> (Func<TSource, Nullable<Single>>)
  706. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  707. // Min<TSource> (Func<TSource, Double>)
  708. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Double>) (x => int.Parse (x))));
  709. // Min<TSource> (Func<TSource, Nullable<Double>>)
  710. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  711. // Min<TSource> (Func<TSource, Decimal>)
  712. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  713. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  714. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  715. // Min<TSource,TSource> (Func<TSource, TSource>)
  716. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string, string> ((Func<string, string>) (x => x)));
  717. // Min<> ()
  718. Assert.AreEqual (2, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Min ());
  719. // Min<> ()
  720. Assert.AreEqual (2, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Min ());
  721. // Min<> ()
  722. Assert.AreEqual (2, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Min ());
  723. // Min<> ()
  724. Assert.AreEqual (2, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Min ());
  725. // Min<> ()
  726. Assert.AreEqual (2, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Min ());
  727. // Min<> ()
  728. Assert.AreEqual (2, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Min ());
  729. // Min<> ()
  730. Assert.AreEqual (2, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Min ());
  731. // Min<> ()
  732. Assert.AreEqual (2, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Min ());
  733. // Min<> ()
  734. Assert.AreEqual (2, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Min ());
  735. // Min<> ()
  736. Assert.AreEqual (2, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Min ());
  737. }
  738. [Test]
  739. [Category ("NotWorking")]
  740. public void MaxArgumentNullTest ()
  741. {
  742. string [] data = { "2", "1", "5", "3", "4" };
  743. // Max<TSource> ()
  744. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> (); });
  745. // Max<TSource> (Func<TSource, int>)
  746. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, int>) (x => 0)); });
  747. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, int>) null); });
  748. // Max<TSource> (Func<TSource, Nullable<int>>)
  749. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  750. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<int>>) null); });
  751. // Max<TSource> (Func<TSource, Int64>)
  752. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Int64>) (x => 0L)); });
  753. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Int64>) null); });
  754. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  755. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  756. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Int64>>) null); });
  757. // Max<TSource> (Func<TSource, Single>)
  758. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Single>) (x => 0f)); });
  759. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Single>) null); });
  760. // Max<TSource> (Func<TSource, Nullable<Single>>)
  761. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  762. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Single>>) null); });
  763. // Max<TSource> (Func<TSource, Double>)
  764. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Double>) (x => 0d)); });
  765. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Double>) null); });
  766. // Max<TSource> (Func<TSource, Nullable<Double>>)
  767. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  768. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Double>>) null); });
  769. // Max<TSource> (Func<TSource, Decimal>)
  770. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Decimal>) (x => 0m)); });
  771. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Decimal>) null); });
  772. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  773. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  774. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Decimal>>) null); });
  775. // Max<TSource,TSource> (Func<TSource, TSource>)
  776. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string, string> ((Func<string, string>) (x => "test")); });
  777. AssertException<ArgumentNullException> (delegate () { data.Max<string, string> ((Func<string, string>) null); });
  778. // Max<> ()
  779. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Max (); });
  780. // Max<> ()
  781. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Max (); });
  782. // Max<> ()
  783. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Max (); });
  784. // Max<> ()
  785. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Max (); });
  786. // Max<> ()
  787. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Max (); });
  788. // Max<> ()
  789. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Max (); });
  790. // Max<> ()
  791. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Max (); });
  792. // Max<> ()
  793. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Max (); });
  794. // Max<> ()
  795. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Max (); });
  796. // Max<> ()
  797. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Max (); });
  798. }
  799. [Test]
  800. public void MaxTest ()
  801. {
  802. string [] data = { "2", "1", "5", "3", "4" };
  803. // Max<string> ()
  804. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string> ());
  805. // Max<TSource> (Func<TSource, int>)
  806. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, int>) (x => int.Parse (x))));
  807. // Max<TSource> (Func<TSource, Nullable<int>>)
  808. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  809. // Max<TSource> (Func<TSource, Int64>)
  810. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Int64>) (x => int.Parse (x))));
  811. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  812. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  813. // Max<TSource> (Func<TSource, Single>)
  814. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Single>) (x => int.Parse (x))));
  815. // Max<TSource> (Func<TSource, Nullable<Single>>)
  816. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  817. // Max<TSource> (Func<TSource, Double>)
  818. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Double>) (x => int.Parse (x))));
  819. // Max<TSource> (Func<TSource, Nullable<Double>>)
  820. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  821. // Max<TSource> (Func<TSource, Decimal>)
  822. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  823. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  824. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  825. // Max<TSource,TSource> (Func<TSource, TSource>)
  826. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string, string> ((Func<string, string>) (x => x)));
  827. // Max<> ()
  828. Assert.AreEqual (4, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Max ());
  829. // Max<> ()
  830. Assert.AreEqual (4, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Max ());
  831. // Max<> ()
  832. Assert.AreEqual (4, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Max ());
  833. // Max<> ()
  834. Assert.AreEqual (4, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Max ());
  835. // Max<> ()
  836. Assert.AreEqual (4, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Max ());
  837. // Max<> ()
  838. Assert.AreEqual (4, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Max ());
  839. // Max<> ()
  840. Assert.AreEqual (4, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Max ());
  841. // Max<> ()
  842. Assert.AreEqual (4, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Max ());
  843. // Max<> ()
  844. Assert.AreEqual (4, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Max ());
  845. // Max<> ()
  846. Assert.AreEqual (4, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Max ());
  847. }
  848. [Test]
  849. public void AverageArgumentNullTest ()
  850. {
  851. string [] data = { "2", "1", "5", "3", "4" };
  852. // Average<TSource> (Func<TSource, int>)
  853. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, int>) (x => 0)); });
  854. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, int>) null); });
  855. // Average<TSource> (Func<TSource, Nullable<int>>)
  856. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  857. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<int>>) null); });
  858. // Average<TSource> (Func<TSource, Int64>)
  859. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Int64>) (x => 0L)); });
  860. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Int64>) null); });
  861. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  862. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  863. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Int64>>) null); });
  864. // Average<TSource> (Func<TSource, Single>)
  865. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Single>) (x => 0f)); });
  866. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Single>) null); });
  867. // Average<TSource> (Func<TSource, Nullable<Single>>)
  868. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  869. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Single>>) null); });
  870. // Average<TSource> (Func<TSource, Double>)
  871. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Double>) (x => 0d)); });
  872. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Double>) null); });
  873. // Average<TSource> (Func<TSource, Nullable<Double>>)
  874. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  875. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Double>>) null); });
  876. // Average<TSource> (Func<TSource, Decimal>)
  877. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Decimal>) (x => 0m)); });
  878. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Decimal>) null); });
  879. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  880. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  881. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Decimal>>) null); });
  882. // Average<> ()
  883. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Average (); });
  884. // Average<> ()
  885. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Average (); });
  886. // Average<> ()
  887. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Average (); });
  888. // Average<> ()
  889. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Average (); });
  890. // Average<> ()
  891. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Average (); });
  892. // Average<> ()
  893. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Average (); });
  894. // Average<> ()
  895. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Average (); });
  896. // Average<> ()
  897. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Average (); });
  898. // Average<> ()
  899. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Average (); });
  900. // Average<> ()
  901. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Average (); });
  902. }
  903. [Test]
  904. public void AverageTest ()
  905. {
  906. string [] data = { "2", "1", "5", "3", "4" };
  907. string [] empty = { };
  908. // Average<string> (Func<string, int>)
  909. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, int>) (x => int.Parse (x))));
  910. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, int>) (x => int.Parse (x))); });
  911. // Average<TSource> (Func<TSource, Nullable<int>>)
  912. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  913. // Average<TSource> (Func<TSource, Int64>)
  914. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long>) (x => int.Parse (x))));
  915. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, long>) (x => int.Parse (x))); });
  916. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  917. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long?>) (x => (int?) int.Parse (x))));
  918. // Average<TSource> (Func<TSource, Single>)
  919. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float>) (x => int.Parse (x))));
  920. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, float>) (x => int.Parse (x))); });
  921. // Average<TSource> (Func<TSource, Nullable<Single>>)
  922. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float?>) (x => (int?) int.Parse (x))));
  923. // Average<TSource> (Func<TSource, Double>)
  924. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double>) (x => int.Parse (x))));
  925. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, double>) (x => int.Parse (x))); });
  926. // Average<TSource> (Func<TSource, Nullable<Double>>)
  927. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double?>) (x => (int?) int.Parse (x))));
  928. // Average<TSource> (Func<TSource, Decimal>)
  929. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal>) (x => int.Parse (x))));
  930. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, decimal>) (x => int.Parse (x))); });
  931. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  932. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal?>) (x => (int?) int.Parse (x))));
  933. // Average<> ()
  934. Assert.AreEqual (3, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Average ());
  935. AssertException<InvalidOperationException> (delegate () { new int [0].Average (); });
  936. // Average<> ()
  937. Assert.AreEqual (3, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Average ());
  938. // Average<> ()
  939. Assert.AreEqual (3, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Average ());
  940. AssertException<InvalidOperationException> (delegate () { new long [0].Average (); });
  941. // Average<> ()
  942. Assert.AreEqual (3, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Average ());
  943. // Average<> ()
  944. Assert.AreEqual (3, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Average ());
  945. AssertException<InvalidOperationException> (delegate () { new float [0].Average (); });
  946. // Average<> ()
  947. Assert.AreEqual (3, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Average ());
  948. // Average<> ()
  949. Assert.AreEqual (3, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Average ());
  950. AssertException<InvalidOperationException> (delegate () { new double [0].Average (); });
  951. // Average<> ()
  952. Assert.AreEqual (3, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Average ());
  953. // Average<> ()
  954. Assert.AreEqual (3, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Average ());
  955. AssertException<InvalidOperationException> (delegate () { new decimal [0].Average (); });
  956. // Average<> ()
  957. Assert.AreEqual (3, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Average ());
  958. }
  959. [Test]
  960. public void WhereArgumentNullTest ()
  961. {
  962. string [] data = { "2", "1", "5", "3", "4" };
  963. // Where<TSource> (Func<TSource, bool>)
  964. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where (x => true); });
  965. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, bool>) null); });
  966. // Where<TSource> (Func<TSource, int, bool>)
  967. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where ((x, y) => true); });
  968. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, int, bool>) null); });
  969. }
  970. [Test]
  971. public void WhereTest ()
  972. {
  973. int [] data = { 2, 1, 5, 3, 4 };
  974. int [] expected1 = { 2, 1 };
  975. int [] expected2 = { 2 };
  976. // Where<TSource> (Func<TSource, bool>)
  977. AssertAreSame (expected1, data.Where (x => x < 3));
  978. // Where<TSource> (Func<TSource, int, bool>)
  979. AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1));
  980. }
  981. [Test]
  982. public void SelectArgumentNullTest ()
  983. {
  984. string [] data = { "2", "1", "5", "3", "4" };
  985. // Select<TSource,TResult> (Func<TSource, TResult>)
  986. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select (x => "test"); });
  987. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, string>) null); });
  988. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  989. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select ((x, y) => "test"); });
  990. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, int, string>) null); });
  991. }
  992. [Test]
  993. public void SelectTest ()
  994. {
  995. string [] data = { "2", "1", "5", "3", "4" };
  996. string [] expected1 = { "2x", "1x", "5x", "3x", "4x" };
  997. string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" };
  998. // Select<TSource,TResult> (Func<TSource, TResult>)
  999. AssertAreSame (expected1, data.Select<string, string> (x => x + "x"));
  1000. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  1001. AssertAreSame (expected2, data.Select<string, string> ((x, y) => x + "x" + y));
  1002. }
  1003. [Test]
  1004. public void SelectManyArgumentNullTest ()
  1005. {
  1006. string [] data = { "2", "1", "5", "3", "4" };
  1007. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1008. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data); });
  1009. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null); });
  1010. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1011. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data); });
  1012. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null); });
  1013. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1014. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data, (x, y) => "test"); });
  1015. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null, (x, y) => "test"); });
  1016. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((x, y) => data, (Func<string, string, string>) null); });
  1017. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1018. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data, (x, y) => "test"); });
  1019. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null, (x, y) => "test"); });
  1020. AssertException<ArgumentNullException> (delegate () { data.SelectMany (x => data, (Func<string, string, string>) null); });
  1021. }
  1022. [Test]
  1023. public void SelectManyTest ()
  1024. {
  1025. string [] data = { "0", "1" };
  1026. string [] expected = { "0", "00", "1", "11" };
  1027. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1028. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }));
  1029. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1030. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }));
  1031. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1032. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y));
  1033. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1034. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y));
  1035. }
  1036. [Test]
  1037. public void TakeArgumentNullTest ()
  1038. {
  1039. string [] data = { "2", "1", "5", "3", "4" };
  1040. // Take<TSource> (int)
  1041. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Take (0); });
  1042. }
  1043. [Test]
  1044. public void TakeTest ()
  1045. {
  1046. int [] data = { 2, 1, 5, 3, 1 };
  1047. int [] expected = { 2, 1 };
  1048. int [] empty = { };
  1049. // Take<TSource> (int)
  1050. AssertAreSame (expected, data.Take (2));
  1051. AssertAreSame (empty, data.Take (-2));
  1052. }
  1053. [Test]
  1054. public void TakeWhileArgumentNullTest ()
  1055. {
  1056. string [] data = { "2", "1", "5", "3", "4" };
  1057. // TakeWhile<TSource> (Func<TSource, bool>)
  1058. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile (x => true); });
  1059. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, bool>) null); });
  1060. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1061. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile ((x, y) => true); });
  1062. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, int, bool>) null); });
  1063. }
  1064. [Test]
  1065. public void TakeWhileTest ()
  1066. {
  1067. int [] data = { 2, 1, 5, 3, 1 };
  1068. int [] expected = { 2, 1 };
  1069. // TakeWhile<TSource> (Func<TSource, bool>)
  1070. AssertAreSame (expected, data.TakeWhile (x => x != 5));
  1071. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1072. AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2));
  1073. }
  1074. [Test]
  1075. public void SkipArgumentNullTest ()
  1076. {
  1077. string [] data = { "2", "1", "5", "3", "4" };
  1078. // Skip<TSource> (int)
  1079. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Skip (0); });
  1080. }
  1081. [Test]
  1082. public void SkipTest ()
  1083. {
  1084. int [] data = { 2, 1, 5, 3, 1 };
  1085. int [] expected = { 5, 3, 1 };
  1086. // Skip<string> (TSource)
  1087. AssertAreSame (expected, data.Skip (2));
  1088. AssertAreSame (data, data.Skip (-2));
  1089. }
  1090. [Test]
  1091. public void SkipWhileArgumentNullTest ()
  1092. {
  1093. string [] data = { "2", "1", "5", "3", "4" };
  1094. // SkipWhile<TSource> (Func<TSource, bool>)
  1095. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile (x => true); });
  1096. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, bool>) null); });
  1097. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1098. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile ((x, y) => true); });
  1099. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, int, bool>) null); });
  1100. }
  1101. [Test]
  1102. public void SkipWhileTest ()
  1103. {
  1104. int [] data = { 2, 1, 5, 3, 1 };
  1105. int [] expected = { 5, 3, 1 };
  1106. // SkipWhile<TSource> (Func<TSource, bool>)
  1107. AssertAreSame (expected, data.SkipWhile (x => x != 5));
  1108. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1109. AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2));
  1110. }
  1111. [Test]
  1112. [Category ("NotWorking")]
  1113. public void JoinArgumentNullTest ()
  1114. {
  1115. string [] data = { "2", "1", "5", "3", "4" };
  1116. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1117. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test"); });
  1118. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1119. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1120. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1121. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null); });
  1122. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1123. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1124. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1125. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1126. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1127. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null, EqualityComparer<string>.Default); });
  1128. }
  1129. [Test]
  1130. public void JoinTest ()
  1131. {
  1132. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1133. string [] dataInner1 = { "7", "3", "5", "8", "9" };
  1134. string [] expected1 = { "55", "33" };
  1135. string [] dataOuter2 = { "2", "1", "3", "4" };
  1136. string [] dataInner2 = { "7", "5", "8", "9" };
  1137. string [] expected2 = { };
  1138. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1139. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y));
  1140. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y));
  1141. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1142. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1143. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1144. }
  1145. [Test]
  1146. [Category ("NotWorking")]
  1147. public void GroupJoinArgumentNullTest ()
  1148. {
  1149. string [] data = { "2", "1", "5", "3", "4" };
  1150. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1151. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
  1152. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1153. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1154. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1155. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
  1156. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1157. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1158. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1159. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1160. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1161. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
  1162. }
  1163. [Test]
  1164. public void GroupJoinTest ()
  1165. {
  1166. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1167. string [] dataInner1 = { "7", "3", "5", "3", "9" };
  1168. string [] expected1 = { "2", "1", "55", "333", "4" };
  1169. string [] dataOuter2 = { "2", "1", "5", "8", "4" };
  1170. string [] dataInner2 = { "7", "3", "6", "3", "9" };
  1171. string [] expected2 = { "2", "1", "5", "8", "4" };
  1172. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1173. AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1174. AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1175. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1176. AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1177. AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1178. }
  1179. [Test]
  1180. public void OrderByArgumentNullTest ()
  1181. {
  1182. string [] data = { "2", "1", "5", "3", "4" };
  1183. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1184. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
  1185. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
  1186. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1187. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
  1188. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
  1189. }
  1190. [Test]
  1191. public void OrderByTest ()
  1192. {
  1193. int [] data = { 2, 1, 5, 3, 4 };
  1194. int [] expected = { 1, 2, 3, 4, 5 };
  1195. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1196. AssertAreSame (expected, data.OrderBy (x => x));
  1197. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1198. AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
  1199. }
  1200. [Test]
  1201. public void OrderByDescendingArgumentNullTest ()
  1202. {
  1203. string [] data = { "2", "1", "5", "3", "4" };
  1204. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1205. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
  1206. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
  1207. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1208. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
  1209. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
  1210. }
  1211. [Test]
  1212. public void OrderByDescendingTest ()
  1213. {
  1214. int [] data = { 2, 1, 5, 3, 4 };
  1215. int [] expected = { 5, 4, 3, 2, 1 };
  1216. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1217. AssertAreSame (expected, data.OrderByDescending (x => x));
  1218. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1219. AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
  1220. }
  1221. [Test]
  1222. public void ThenByArgumentNullTest ()
  1223. {
  1224. string [] data = { "2", "1", "5", "3", "4" };
  1225. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1226. AssertException<ArgumentNullException> (delegate () {
  1227. ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
  1228. });
  1229. AssertException<ArgumentNullException> (delegate () {
  1230. data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
  1231. });
  1232. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1233. AssertException<ArgumentNullException> (delegate () {
  1234. ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
  1235. });
  1236. AssertException<ArgumentNullException> (delegate () {
  1237. data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
  1238. });
  1239. }
  1240. [Test]
  1241. public void ThenByTest ()
  1242. {
  1243. int [] data = { 2, 1, 5, 3, 4 };
  1244. int [] expected = { 1, 2, 3, 4, 5 };
  1245. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1246. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
  1247. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1248. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
  1249. }
  1250. [Test]
  1251. public void ThenByDescendingArgumentNullTest ()
  1252. {
  1253. string [] data = { "2", "1", "5", "3", "4" };
  1254. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1255. AssertException<ArgumentNullException> (delegate () {
  1256. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
  1257. });
  1258. AssertException<ArgumentNullException> (delegate () {
  1259. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
  1260. });
  1261. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1262. AssertException<ArgumentNullException> (delegate () {
  1263. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
  1264. });
  1265. AssertException<ArgumentNullException> (delegate () {
  1266. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
  1267. });
  1268. }
  1269. [Test]
  1270. public void ThenByDescendingTest ()
  1271. {
  1272. int [] data = { 2, 1, 5, 3, 4 };
  1273. int [] expected = { 5, 4, 3, 2, 1 };
  1274. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1275. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
  1276. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1277. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
  1278. }
  1279. [Test]
  1280. [Category ("NotWorking")]
  1281. public void GroupByArgumentNullTest ()
  1282. {
  1283. string [] data = { "2", "1", "5", "3", "4" };
  1284. // GroupBy<string,string> (Func<string, string>)
  1285. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
  1286. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
  1287. // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
  1288. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1289. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1290. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
  1291. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1292. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1293. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1294. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1295. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1296. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1297. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1298. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
  1299. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1300. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1301. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
  1302. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
  1303. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1304. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1305. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1306. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
  1307. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1308. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1309. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1310. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1311. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1312. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1313. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1314. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1315. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1316. }
  1317. [Test]
  1318. [Category ("NotWorking")]
  1319. public void GroupByTest ()
  1320. {
  1321. string [] data = { "2", "1", "5", "3", "4", "3" };
  1322. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1323. expected.Add ("2", new List<string> () { "2" });
  1324. expected.Add ("1", new List<string> () { "1" });
  1325. expected.Add ("5", new List<string> () { "5" });
  1326. expected.Add ("3", new List<string> () { "3", "3" });
  1327. expected.Add ("4", new List<string> () { "4" });
  1328. Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
  1329. expected2.Add ("2", new List<string> () { "22" });
  1330. expected2.Add ("1", new List<string> () { "11" });
  1331. expected2.Add ("5", new List<string> () { "55" });
  1332. expected2.Add ("3", new List<string> () { "33", "33" });
  1333. expected2.Add ("4", new List<string> () { "44" });
  1334. string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
  1335. // GroupBy<int,int> (Func<int, int>)
  1336. AssertAreSame (expected, data.GroupBy (x => x));
  1337. // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
  1338. AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
  1339. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
  1340. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
  1341. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
  1342. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
  1343. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
  1344. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1345. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
  1346. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1347. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1348. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1349. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1350. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1351. }
  1352. [Test]
  1353. public void ConcatArgumentNullTest ()
  1354. {
  1355. string [] data = { "2", "1", "5", "3", "4" };
  1356. // Concat<TSource> (IEnumerable<TSource>)
  1357. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
  1358. AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
  1359. }
  1360. [Test]
  1361. public void ConcatTest ()
  1362. {
  1363. int [] data1 = { 2, 1, 5, 3, 4 };
  1364. int [] data2 = { 1, 2, 3, 4, 5 };
  1365. int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
  1366. // Concat<TSource> (IEnumerable<TSource>)
  1367. AssertAreSame (expected, data1.Concat (data2));
  1368. }
  1369. [Test]
  1370. public void DistinctArgumentNullTest ()
  1371. {
  1372. string [] data = { "2", "1", "5", "3", "4" };
  1373. // Distinct<TSource> ()
  1374. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
  1375. // Distinct<TSource> (IEqualityComparer<TSource>)
  1376. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
  1377. }
  1378. [Test]
  1379. public void DistinctTest ()
  1380. {
  1381. int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
  1382. int [] expected = { 2, 1, 5, 3, 4, 8 };
  1383. // Distinct<TSource> ()
  1384. AssertAreSame (expected, data.Distinct ());
  1385. // Distinct<iTSourcent> (IEqualityComparer<TSource>)
  1386. AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
  1387. }
  1388. [Test]
  1389. public void UnionArgumentNullTest ()
  1390. {
  1391. string [] data = { "2", "1", "5", "3", "4" };
  1392. // Union<TSource> (IEnumerable<TSource>)
  1393. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
  1394. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
  1395. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1396. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
  1397. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1398. }
  1399. [Test]
  1400. public void UnionTest ()
  1401. {
  1402. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1403. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1404. int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
  1405. // Union<TSource> (IEnumerable<TSource>)
  1406. AssertAreSame (expected, data1.Union (data2));
  1407. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1408. AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
  1409. }
  1410. [Test]
  1411. public void IntersectArgumentNullTest ()
  1412. {
  1413. string [] data = { "2", "1", "5", "3", "4" };
  1414. // Intersect<TSource> (IEnumerable<TSource>)
  1415. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
  1416. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
  1417. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1418. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
  1419. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1420. }
  1421. [Test]
  1422. public void IntersectTest ()
  1423. {
  1424. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1425. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1426. int [] expected = { 2, 1, 5, 3, 4 };
  1427. // Intersect<TSource> (IEnumerable<TSource>)
  1428. AssertAreSame (expected, data1.Intersect (data2));
  1429. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1430. AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
  1431. }
  1432. [Test]
  1433. public void ExceptArgumentNullTest ()
  1434. {
  1435. string [] data = { "2", "1", "5", "3", "4" };
  1436. // Except<TSource> (IEnumerable<TSource>)
  1437. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
  1438. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
  1439. // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
  1440. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
  1441. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1442. }
  1443. [Test]
  1444. public void ExceptTest ()
  1445. {
  1446. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1447. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1448. int [] expected = { 7 };
  1449. // Except<TSource> (IEnumerable<TSource>)
  1450. AssertAreSame (expected, data1.Except (data2));
  1451. // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1452. AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
  1453. }
  1454. [Test]
  1455. public void ReverseArgumentNullTest ()
  1456. {
  1457. string [] data = { "2", "1", "5", "3", "4" };
  1458. // Reverse<TSource> ()
  1459. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
  1460. }
  1461. [Test]
  1462. public void ReverseTest ()
  1463. {
  1464. int [] data = { 2, 1, 5, 7, 3, 4 };
  1465. int [] expected = { 4, 3, 7, 5, 1, 2 };
  1466. // Reverse<TSource> ()
  1467. AssertAreSame (expected, data.Reverse ());
  1468. }
  1469. [Test]
  1470. public void SequenceEqualArgumentNullTest ()
  1471. {
  1472. string [] data = { "2", "1", "5", "3", "4" };
  1473. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1474. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
  1475. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
  1476. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1477. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
  1478. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1479. }
  1480. [Test]
  1481. public void SequenceEqualTest ()
  1482. {
  1483. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1484. int [] data2 = { 2, 1, 5, 7, 3, 4 };
  1485. int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
  1486. int [] data4 = { 2, 1, 5, 7, 3 };
  1487. int [] data5 = { 2, 1, 5, 8, 3, 4 };
  1488. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1489. Assert.IsTrue (data1.SequenceEqual (data2));
  1490. Assert.IsFalse (data1.SequenceEqual (data3));
  1491. Assert.IsFalse (data1.SequenceEqual (data4));
  1492. Assert.IsFalse (data1.SequenceEqual (data5));
  1493. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1494. Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
  1495. Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
  1496. Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
  1497. Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
  1498. }
  1499. [Test]
  1500. public void AsEnumerableArgumentNullTest ()
  1501. {
  1502. string [] data = { "2", "1", "5", "3", "4" };
  1503. }
  1504. [Test]
  1505. public void AsEnumerableTest ()
  1506. {
  1507. int [] data = { 2, 1, 5, 7, 3, 4 };
  1508. // AsEnumerable<TSource> ()
  1509. Assert.AreSame (data, data.AsEnumerable ());
  1510. }
  1511. [Test]
  1512. public void ToArrayArgumentNullTest ()
  1513. {
  1514. string [] data = { "2", "1", "5", "3", "4" };
  1515. // ToArray<TSource> ()
  1516. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
  1517. }
  1518. [Test]
  1519. public void ToArrayTest ()
  1520. {
  1521. int [] data = { 2, 3, 4, 5 };
  1522. int [] expected = { 2, 3, 4, 5 };
  1523. // ToArray<TSource> ()
  1524. AssertAreSame (expected, data.ToArray ());
  1525. }
  1526. [Test]
  1527. public void ToListArgumentNullTest ()
  1528. {
  1529. string [] data = { "2", "1", "5", "3", "4" };
  1530. // ToList<string> ()
  1531. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
  1532. }
  1533. [Test]
  1534. public void ToListTest ()
  1535. {
  1536. int [] data = { 2, 4, 5, 1 };
  1537. int [] expected = { 2, 4, 5, 1 };
  1538. // ToList<int> ()
  1539. AssertAreSame (expected, data.ToList ());
  1540. }
  1541. [Test]
  1542. [Category ("NotWorking")]
  1543. public void ToDictionaryArgumentNullTest ()
  1544. {
  1545. string [] data = { "2", "1", "5", "3", "4" };
  1546. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1547. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
  1548. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
  1549. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1550. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
  1551. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
  1552. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1553. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
  1554. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
  1555. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
  1556. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1557. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
  1558. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
  1559. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
  1560. }
  1561. [Test]
  1562. [Category ("NotWorking")]
  1563. public void ToDictionaryTest ()
  1564. {
  1565. string [] data = { "2", "1", "5", "3", "4" };
  1566. Dictionary<string, string> expected = new Dictionary<string, string> ();
  1567. expected.Add ("k2", "2");
  1568. expected.Add ("k1", "1");
  1569. expected.Add ("k5", "5");
  1570. expected.Add ("k3", "3");
  1571. expected.Add ("k4", "4");
  1572. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1573. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
  1574. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
  1575. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1576. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
  1577. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
  1578. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1579. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
  1580. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
  1581. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1582. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
  1583. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
  1584. }
  1585. [Test]
  1586. public void ToLookupArgumentNullTest ()
  1587. {
  1588. string [] data = { "2", "1", "5", "3", "4" };
  1589. // ToLookup<string,string> (Func<string, string>)
  1590. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
  1591. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
  1592. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1593. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1594. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1595. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1596. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1597. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1598. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1599. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1600. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1601. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1602. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1603. }
  1604. [Test]
  1605. public void ToLookupTest ()
  1606. {
  1607. string [] data = { "23", "12", "55", "42", "41" };
  1608. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1609. expected.Add ("2", new List<string> () { "23" });
  1610. expected.Add ("1", new List<string> () { "12" });
  1611. expected.Add ("5", new List<string> () { "55" });
  1612. expected.Add ("4", new List<string> () { "42", "41" });
  1613. // ToLookup<string,string> (Func<string, string>)
  1614. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
  1615. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1616. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
  1617. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1618. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
  1619. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1620. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
  1621. }
  1622. [Test]
  1623. public void DefaultIfEmptyArgumentNullTest ()
  1624. {
  1625. string [] data = { "2", "1", "5", "3", "4" };
  1626. // DefaultIfEmpty<string> ()
  1627. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
  1628. // DefaultIfEmpty<string> (string)
  1629. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
  1630. }
  1631. [Test]
  1632. public void DefaultIfEmptyTest ()
  1633. {
  1634. string [] data = { "2", "1", "5", "3", "4" };
  1635. string [] empty = { };
  1636. string [] default1 = { null };
  1637. string [] default2 = { "default" };
  1638. // DefaultIfEmpty<string> ()
  1639. AssertAreSame (data, data.DefaultIfEmpty ());
  1640. AssertAreSame (default1, empty.DefaultIfEmpty ());
  1641. // DefaultIfEmpty<string> (string)
  1642. AssertAreSame (data, data.DefaultIfEmpty ("default"));
  1643. AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
  1644. }
  1645. [Test]
  1646. public void OfTypeArgumentNullTest ()
  1647. {
  1648. string [] data = { "2", "1", "5", "3", "4" };
  1649. // OfType<string> ()
  1650. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
  1651. }
  1652. [Test]
  1653. public void OfTypeTest ()
  1654. {
  1655. object [] data = { "2", 2, "1", "5", "3", "4" };
  1656. string [] expected = { "2", "1", "5", "3", "4" };
  1657. // OfType<string> ()
  1658. AssertAreSame (expected, data.OfType<string> ());
  1659. }
  1660. [Test]
  1661. public void CastArgumentNullTest ()
  1662. {
  1663. string [] data = { "2", "1", "5", "3", "4" };
  1664. // Cast<string> ()
  1665. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
  1666. }
  1667. [Test]
  1668. public void CastTest ()
  1669. {
  1670. object [] data = { 1, 2, 3 };
  1671. int [] expected = { 1, 2, 3 };
  1672. // Cast<string> ()
  1673. AssertAreSame (expected, data.Cast<int> ());
  1674. AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
  1675. data.Cast<IEnumerable> ();
  1676. }
  1677. [Test]
  1678. public void RangeArgumentNullTest ()
  1679. {
  1680. string [] data = { "2", "1", "5", "3", "4" };
  1681. }
  1682. [Test]
  1683. public void RangeTest ()
  1684. {
  1685. int [] expected = { 2, 3, 4, 5 };
  1686. // Range<> (int)
  1687. AssertAreSame (expected, Enumerable.Range (2, 4));
  1688. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
  1689. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
  1690. Enumerable.Range (int.MaxValue - 5, 6);
  1691. }
  1692. }
  1693. }