EnumerableMoreTest.cs 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361
  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. public void CountOverflowTest ()
  425. {
  426. //BigEnumerable data = new BigEnumerable ((ulong) int.MaxValue + 1);
  427. // Count<TSource> ()
  428. //AssertException<OverflowException> (delegate () { data.Count (); });
  429. // Count<TSource> (Func<TSource, bool>)
  430. //AssertException<OverflowException> (delegate () { data.Count (x => 3 == x); });
  431. // Documentation error: http://msdn2.microsoft.com/en-us/library/bb535181.aspx
  432. // An exception is only rasied if count > int.MaxValue. Not if source contains more than int.MaxValue elements.
  433. // AssertException<OverflowException> (delegate () { data.Count (x => 5 == x); });
  434. }
  435. [Test]
  436. public void LongCountArgumentNullTest ()
  437. {
  438. string [] data = { "2", "1", "5", "3", "4" };
  439. // LongCount<TSource> ()
  440. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (); });
  441. // LongCount<TSource> (Func<TSource, bool>)
  442. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).LongCount (x => true); });
  443. AssertException<ArgumentNullException> (delegate () { data.LongCount ((Func<string, bool>) null); });
  444. }
  445. [Test]
  446. public void LongCountTest ()
  447. {
  448. int [] data = { 5, 2, 3, 1, 6 };
  449. //TODO: Overflow test...
  450. // LongCount<TSource> ()
  451. Assert.AreEqual (5, data.LongCount ());
  452. Assert.AreEqual (5, Enumerable.Range (0, 5).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. static void IsFalse(bool b, int[] data) {
  466. if (b) {
  467. Console.WriteLine (data.Contains (0));
  468. object o = null;
  469. o.ToString ();
  470. Assert.IsFalse (true);
  471. }
  472. //Console.WriteLine ("HIT!");
  473. }
  474. [Test]
  475. public void ContainsTest ()
  476. {
  477. int [] data = { 5, 2, 3, 1, 6 };
  478. ICollection<int> icoll = data;
  479. // Contains<TSource> (TSource)
  480. Assert.IsTrue (data.Contains (2));
  481. for (int i = 0; i < 50; ++i)
  482. icoll.Contains (i);//Console.WriteLine (data.Contains (0));
  483. IsFalse (data.Contains (0), data);
  484. // Contains<TSource> (TSource, IEqualityComparer<TSource>)
  485. Assert.IsTrue (data.Contains (2, EqualityComparer<int>.Default));
  486. Assert.IsFalse (data.Contains (0, EqualityComparer<int>.Default));
  487. }
  488. [Test]
  489. public void AggregateArgumentNullTest ()
  490. {
  491. string [] data = { "2", "1", "5", "3", "4" };
  492. // Aggregate<TSource> (Func<TSource, TSource, TSource>)
  493. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ((x, y) => "test"); });
  494. AssertException<ArgumentNullException> (delegate () { data.Aggregate ((Func<string, string, string>) null); });
  495. // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
  496. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test"); });
  497. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null); });
  498. // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
  499. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Aggregate ("initial", (x, y) => "test", x => "test"); });
  500. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (Func<string, string, string>) null, x => "test"); });
  501. AssertException<ArgumentNullException> (delegate () { data.Aggregate ("initial", (x, y) => "test", (Func<string, string>) null); });
  502. }
  503. [Test]
  504. public void AggregateTest ()
  505. {
  506. string [] data = { "2", "1", "5", "3", "4" };
  507. string [] empty = { };
  508. // Aggregate<TSource> (Func<TSource, TSource, TSource>)
  509. Assert.AreEqual ("21534", data.Aggregate ((x, y) => x + y));
  510. AssertException<InvalidOperationException> (delegate () { empty.Aggregate ((x, y) => x + y); }); //only this overload throws
  511. // Aggregate<TSource,TAccumulate> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>)
  512. Assert.AreEqual ("initial21534", (data.Aggregate ("initial", (x, y) => x + y)));
  513. // Aggregate<TSource,TAccumulate,TResult> (TAccumulate, Func<TAccumulate, TSource, TAccumulate>, Func<TAccumulate, TResult>)
  514. Assert.AreEqual ("INITIAL21534", data.Aggregate ("initial", (x, y) => x + y, (x => x.ToUpper ())));
  515. }
  516. [Test]
  517. public void SumArgumentNullTest ()
  518. {
  519. string [] data = { "2", "1", "5", "3", "4" };
  520. // Sum<TSource> (Func<TSource, int>)
  521. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, int>) (x => 0)); });
  522. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, int>) null); });
  523. // Sum<TSource> (Func<TSource, Nullable<int>>)
  524. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  525. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<int>>) null); });
  526. // Sum<TSource> (Func<TSource, Int64>)
  527. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Int64>) (x => 0L)); });
  528. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Int64>) null); });
  529. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  530. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  531. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Int64>>) null); });
  532. // Sum<TSource> (Func<TSource, Single>)
  533. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Single>) (x => 0f)); });
  534. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Single>) null); });
  535. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  536. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  537. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Single>>) null); });
  538. // Sum<TSource> (Func<TSource, Double>)
  539. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Double>) (x => 0d)); });
  540. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Double>) null); });
  541. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  542. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  543. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Double>>) null); });
  544. // Sum<TSource> (Func<TSource, Decimal>)
  545. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Decimal>) (x => 0m)); });
  546. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Decimal>) null); });
  547. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  548. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  549. AssertException<ArgumentNullException> (delegate () { data.Sum<string> ((Func<string, Nullable<Decimal>>) null); });
  550. // Sum (IEnumerable<int>)
  551. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Sum (); });
  552. // Sum (IEnumerable<int?>)
  553. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Sum (); });
  554. // Sum (IEnumerable<long>)
  555. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Sum (); });
  556. // Sum (IEnumerable<long?>)
  557. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Sum (); });
  558. // Sum (IEnumerable<float>)
  559. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Sum (); });
  560. // Sum (IEnumerable<float?>)
  561. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Sum (); });
  562. // Sum (IEnumerable<double>)
  563. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Sum (); });
  564. // Sum (IEnumerable<double?>)
  565. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Sum (); });
  566. // Sum (IEnumerable<decimal>)
  567. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Sum (); });
  568. // Sum (IEnumerable<decimal?>)
  569. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Sum (); });
  570. }
  571. [Test]
  572. public void SumTest ()
  573. {
  574. string [] data = { "2", "3", "5", "5" };
  575. //TODO: OverflowException
  576. // Sum<TSource> (Func<TSource, int>)
  577. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  578. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, int>) (x => int.Parse (x))));
  579. // Sum<TSource> (Func<TSource, Nullable<int>>)
  580. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  581. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  582. // Sum<TSource> (Func<TSource, Int64>)
  583. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  584. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Int64>) (x => int.Parse (x))));
  585. // Sum<TSource> (Func<TSource, Nullable<Int64>>)
  586. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  587. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  588. // Sum<TSource> (Func<TSource, Single>)
  589. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  590. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Single>) (x => int.Parse (x))));
  591. // Sum<TSource> (Func<TSource, Nullable<Single>>)
  592. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  593. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  594. // Sum<TSource> (Func<TSource, Double>)
  595. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  596. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Double>) (x => int.Parse (x))));
  597. // Sum<TSource> (Func<TSource, Nullable<Double>>)
  598. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  599. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  600. // Sum<TSource> (Func<TSource, Decimal>)
  601. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  602. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  603. // Sum<TSource> (Func<TSource, Nullable<Decimal>>)
  604. Assert.AreEqual (15, ((IEnumerable<string>) data).Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  605. Assert.AreEqual (0, Enumerable.Empty<string> ().Sum<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  606. // Sum<> ()
  607. Assert.AreEqual (6, ((IEnumerable<int>) new int [] { 1, 2, 3 }).Sum ());
  608. Assert.AreEqual (0, Enumerable.Empty<int> ().Sum ());
  609. // Sum<> ()
  610. Assert.AreEqual (6, ((IEnumerable<Nullable<int>>) new int? [] { 1, 2, 3 }).Sum ());
  611. Assert.AreEqual (0, Enumerable.Empty<int?> ().Sum ());
  612. // Sum<> ()
  613. Assert.AreEqual (6, ((IEnumerable<Int64>) new long [] { 1, 2, 3 }).Sum ());
  614. Assert.AreEqual (0, Enumerable.Empty<long> ().Sum ());
  615. // Sum<> ()
  616. Assert.AreEqual (6, ((IEnumerable<Nullable<Int64>>) new long? [] { 1, 2, 3 }).Sum ());
  617. Assert.AreEqual (0, Enumerable.Empty<long?> ().Sum ());
  618. // Sum<> ()
  619. Assert.AreEqual (6, ((IEnumerable<Single>) new float [] { 1, 2, 3 }).Sum ());
  620. Assert.AreEqual (0, Enumerable.Empty<float> ().Sum ());
  621. // Sum<> ()
  622. Assert.AreEqual (6, ((IEnumerable<Nullable<Single>>) new float? [] { 1, 2, 3 }).Sum ());
  623. Assert.AreEqual (0, Enumerable.Empty<float?> ().Sum ());
  624. // Sum<> ()
  625. Assert.AreEqual (6, ((IEnumerable<Double>) new double [] { 1, 2, 3 }).Sum ());
  626. Assert.AreEqual (0, Enumerable.Empty<double> ().Sum ());
  627. // Sum<> ()
  628. Assert.AreEqual (6, ((IEnumerable<Nullable<Double>>) new double? [] { 1, 2, 3 }).Sum ());
  629. Assert.AreEqual (0, Enumerable.Empty<double?> ().Sum ());
  630. // Sum<> ()
  631. Assert.AreEqual (6, ((IEnumerable<Decimal>) new decimal [] { 1, 2, 3 }).Sum ());
  632. Assert.AreEqual (0, Enumerable.Empty<decimal> ().Sum ());
  633. // Sum<> ()
  634. Assert.AreEqual (6, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 1, 2, 3 }).Sum ());
  635. Assert.AreEqual (0, Enumerable.Empty<decimal?> ().Sum ());
  636. }
  637. [Test]
  638. public void MinArgumentNullTest ()
  639. {
  640. string [] data = { "2", "1", "5", "3", "4" };
  641. // Min<TSource> ()
  642. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> (); });
  643. // Min<TSource> (Func<TSource, int>)
  644. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, int>) (x => 0)); });
  645. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, int>) null); });
  646. // Min<TSource> (Func<TSource, Nullable<int>>)
  647. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  648. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<int>>) null); });
  649. // Min<TSource> (Func<TSource, Int64>)
  650. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Int64>) (x => 0L)); });
  651. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Int64>) null); });
  652. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  653. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  654. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Int64>>) null); });
  655. // Min<TSource> (Func<TSource, Single>)
  656. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Single>) (x => 0f)); });
  657. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Single>) null); });
  658. // Min<TSource> (Func<TSource, Nullable<Single>>)
  659. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  660. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Single>>) null); });
  661. // Min<TSource> (Func<TSource, Double>)
  662. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Double>) (x => 0d)); });
  663. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Double>) null); });
  664. // Min<TSource> (Func<TSource, Nullable<Double>>)
  665. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  666. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Double>>) null); });
  667. // Min<TSource> (Func<TSource, Decimal>)
  668. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Decimal>) (x => 0m)); });
  669. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Decimal>) null); });
  670. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  671. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  672. AssertException<ArgumentNullException> (delegate () { data.Min<string> ((Func<string, Nullable<Decimal>>) null); });
  673. // Min<TSource,TSource> (Func<TSource, string>)
  674. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Min<string, string> ((Func<string, string>) (x => "test")); });
  675. AssertException<ArgumentNullException> (delegate () { data.Min<string, string> ((Func<string, string>) null); });
  676. // Min<> ()
  677. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Min (); });
  678. // Min<> ()
  679. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Min (); });
  680. // Min<> ()
  681. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Min (); });
  682. // Min<> ()
  683. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Min (); });
  684. // Min<> ()
  685. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Min (); });
  686. // Min<> ()
  687. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Min (); });
  688. // Min<> ()
  689. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Min (); });
  690. // Min<> ()
  691. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Min (); });
  692. // Min<> ()
  693. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Min (); });
  694. // Min<> ()
  695. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Min (); });
  696. }
  697. [Test]
  698. public void MinTest ()
  699. {
  700. string [] data = { "2", "1", "5", "3", "4" };
  701. // Min<TSource> ()
  702. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string> ());
  703. // Min<TSource> (Func<TSource, int>)
  704. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, int>) (x => int.Parse (x))));
  705. // Min<TSource> (Func<TSource, Nullable<int>>)
  706. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  707. // Min<TSource> (Func<TSource, Int64>)
  708. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Int64>) (x => int.Parse (x))));
  709. // Min<TSource> (Func<TSource, Nullable<Int64>>)
  710. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  711. // Min<TSource> (Func<TSource, Single>)
  712. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Single>) (x => int.Parse (x))));
  713. // Min<TSource> (Func<TSource, Nullable<Single>>)
  714. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  715. // Min<TSource> (Func<TSource, Double>)
  716. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Double>) (x => int.Parse (x))));
  717. // Min<TSource> (Func<TSource, Nullable<Double>>)
  718. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  719. // Min<TSource> (Func<TSource, Decimal>)
  720. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  721. // Min<TSource> (Func<TSource, Nullable<Decimal>>)
  722. Assert.AreEqual (1, ((IEnumerable<string>) data).Min<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  723. // Min<TSource,TSource> (Func<TSource, TSource>)
  724. Assert.AreEqual ("1", ((IEnumerable<string>) data).Min<string, string> ((Func<string, string>) (x => x)));
  725. // Min<> ()
  726. Assert.AreEqual (2, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Min ());
  727. // Min<> ()
  728. Assert.AreEqual (2, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Min ());
  729. // Min<> ()
  730. Assert.AreEqual (2, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Min ());
  731. // Min<> ()
  732. Assert.AreEqual (2, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Min ());
  733. // Min<> ()
  734. Assert.AreEqual (2, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Min ());
  735. // Min<> ()
  736. Assert.AreEqual (2, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Min ());
  737. // Min<> ()
  738. Assert.AreEqual (2, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Min ());
  739. // Min<> ()
  740. Assert.AreEqual (2, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Min ());
  741. // Min<> ()
  742. Assert.AreEqual (2, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Min ());
  743. // Min<> ()
  744. Assert.AreEqual (2, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Min ());
  745. }
  746. [Test]
  747. public void MaxArgumentNullTest ()
  748. {
  749. string [] data = { "2", "1", "5", "3", "4" };
  750. // Max<TSource> ()
  751. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> (); });
  752. // Max<TSource> (Func<TSource, int>)
  753. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, int>) (x => 0)); });
  754. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, int>) null); });
  755. // Max<TSource> (Func<TSource, Nullable<int>>)
  756. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  757. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<int>>) null); });
  758. // Max<TSource> (Func<TSource, Int64>)
  759. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Int64>) (x => 0L)); });
  760. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Int64>) null); });
  761. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  762. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  763. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Int64>>) null); });
  764. // Max<TSource> (Func<TSource, Single>)
  765. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Single>) (x => 0f)); });
  766. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Single>) null); });
  767. // Max<TSource> (Func<TSource, Nullable<Single>>)
  768. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  769. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Single>>) null); });
  770. // Max<TSource> (Func<TSource, Double>)
  771. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Double>) (x => 0d)); });
  772. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Double>) null); });
  773. // Max<TSource> (Func<TSource, Nullable<Double>>)
  774. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  775. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Double>>) null); });
  776. // Max<TSource> (Func<TSource, Decimal>)
  777. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Decimal>) (x => 0m)); });
  778. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Decimal>) null); });
  779. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  780. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  781. AssertException<ArgumentNullException> (delegate () { data.Max<string> ((Func<string, Nullable<Decimal>>) null); });
  782. // Max<TSource,TSource> (Func<TSource, TSource>)
  783. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Max<string, string> ((Func<string, string>) (x => "test")); });
  784. AssertException<ArgumentNullException> (delegate () { data.Max<string, string> ((Func<string, string>) null); });
  785. // Max<> ()
  786. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Max (); });
  787. // Max<> ()
  788. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Max (); });
  789. // Max<> ()
  790. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Max (); });
  791. // Max<> ()
  792. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Max (); });
  793. // Max<> ()
  794. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Max (); });
  795. // Max<> ()
  796. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Max (); });
  797. // Max<> ()
  798. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Max (); });
  799. // Max<> ()
  800. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Max (); });
  801. // Max<> ()
  802. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Max (); });
  803. // Max<> ()
  804. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Max (); });
  805. }
  806. [Test]
  807. public void MaxTest ()
  808. {
  809. string [] data = { "2", "1", "5", "3", "4" };
  810. // Max<string> ()
  811. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string> ());
  812. // Max<TSource> (Func<TSource, int>)
  813. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, int>) (x => int.Parse (x))));
  814. // Max<TSource> (Func<TSource, Nullable<int>>)
  815. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  816. // Max<TSource> (Func<TSource, Int64>)
  817. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Int64>) (x => int.Parse (x))));
  818. // Max<TSource> (Func<TSource, Nullable<Int64>>)
  819. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Int64>>) (x => (int?) int.Parse (x))));
  820. // Max<TSource> (Func<TSource, Single>)
  821. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Single>) (x => int.Parse (x))));
  822. // Max<TSource> (Func<TSource, Nullable<Single>>)
  823. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Single>>) (x => (int?) int.Parse (x))));
  824. // Max<TSource> (Func<TSource, Double>)
  825. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Double>) (x => int.Parse (x))));
  826. // Max<TSource> (Func<TSource, Nullable<Double>>)
  827. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Double>>) (x => (int?) int.Parse (x))));
  828. // Max<TSource> (Func<TSource, Decimal>)
  829. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Decimal>) (x => int.Parse (x))));
  830. // Max<TSource> (Func<TSource, Nullable<Decimal>>)
  831. Assert.AreEqual (5, ((IEnumerable<string>) data).Max<string> ((Func<string, Nullable<Decimal>>) (x => (int?) int.Parse (x))));
  832. // Max<TSource,TSource> (Func<TSource, TSource>)
  833. Assert.AreEqual ("5", ((IEnumerable<string>) data).Max<string, string> ((Func<string, string>) (x => x)));
  834. // Max<> ()
  835. Assert.AreEqual (4, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Max ());
  836. // Max<> ()
  837. Assert.AreEqual (4, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Max ());
  838. // Max<> ()
  839. Assert.AreEqual (4, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Max ());
  840. // Max<> ()
  841. Assert.AreEqual (4, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Max ());
  842. // Max<> ()
  843. Assert.AreEqual (4, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Max ());
  844. // Max<> ()
  845. Assert.AreEqual (4, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Max ());
  846. // Max<> ()
  847. Assert.AreEqual (4, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Max ());
  848. // Max<> ()
  849. Assert.AreEqual (4, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Max ());
  850. // Max<> ()
  851. Assert.AreEqual (4, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Max ());
  852. // Max<> ()
  853. Assert.AreEqual (4, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Max ());
  854. }
  855. [Test]
  856. public void AverageArgumentNullTest ()
  857. {
  858. string [] data = { "2", "1", "5", "3", "4" };
  859. // Average<TSource> (Func<TSource, int>)
  860. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, int>) (x => 0)); });
  861. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, int>) null); });
  862. // Average<TSource> (Func<TSource, Nullable<int>>)
  863. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<int>>) (x => (int?) 0)); });
  864. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<int>>) null); });
  865. // Average<TSource> (Func<TSource, Int64>)
  866. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Int64>) (x => 0L)); });
  867. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Int64>) null); });
  868. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  869. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Int64>>) (x => (int?) 0L)); });
  870. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Int64>>) null); });
  871. // Average<TSource> (Func<TSource, Single>)
  872. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Single>) (x => 0f)); });
  873. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Single>) null); });
  874. // Average<TSource> (Func<TSource, Nullable<Single>>)
  875. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Single>>) (x => (int?) 0f)); });
  876. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Single>>) null); });
  877. // Average<TSource> (Func<TSource, Double>)
  878. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Double>) (x => 0d)); });
  879. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Double>) null); });
  880. // Average<TSource> (Func<TSource, Nullable<Double>>)
  881. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Double>>) (x => (int?) 0d)); });
  882. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Double>>) null); });
  883. // Average<TSource> (Func<TSource, Decimal>)
  884. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Decimal>) (x => 0m)); });
  885. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Decimal>) null); });
  886. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  887. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Average<string> ((Func<string, Nullable<Decimal>>) (x => (int?) 0m)); });
  888. AssertException<ArgumentNullException> (delegate () { data.Average<string> ((Func<string, Nullable<Decimal>>) null); });
  889. // Average<> ()
  890. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<int>) null).Average (); });
  891. // Average<> ()
  892. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<int>>) null).Average (); });
  893. // Average<> ()
  894. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Int64>) null).Average (); });
  895. // Average<> ()
  896. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Int64>>) null).Average (); });
  897. // Average<> ()
  898. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Single>) null).Average (); });
  899. // Average<> ()
  900. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Single>>) null).Average (); });
  901. // Average<> ()
  902. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Double>) null).Average (); });
  903. // Average<> ()
  904. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Double>>) null).Average (); });
  905. // Average<> ()
  906. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Decimal>) null).Average (); });
  907. // Average<> ()
  908. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<Nullable<Decimal>>) null).Average (); });
  909. }
  910. [Test]
  911. public void AverageTest ()
  912. {
  913. string [] data = { "2", "1", "5", "3", "4" };
  914. string [] empty = { };
  915. // Average<string> (Func<string, int>)
  916. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, int>) (x => int.Parse (x))));
  917. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, int>) (x => int.Parse (x))); });
  918. // Average<TSource> (Func<TSource, Nullable<int>>)
  919. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, Nullable<int>>) (x => (int?) int.Parse (x))));
  920. // Average<TSource> (Func<TSource, Int64>)
  921. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long>) (x => int.Parse (x))));
  922. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, long>) (x => int.Parse (x))); });
  923. // Average<TSource> (Func<TSource, Nullable<Int64>>)
  924. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, long?>) (x => (int?) int.Parse (x))));
  925. // Average<TSource> (Func<TSource, Single>)
  926. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float>) (x => int.Parse (x))));
  927. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, float>) (x => int.Parse (x))); });
  928. // Average<TSource> (Func<TSource, Nullable<Single>>)
  929. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, float?>) (x => (int?) int.Parse (x))));
  930. // Average<TSource> (Func<TSource, Double>)
  931. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double>) (x => int.Parse (x))));
  932. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, double>) (x => int.Parse (x))); });
  933. // Average<TSource> (Func<TSource, Nullable<Double>>)
  934. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, double?>) (x => (int?) int.Parse (x))));
  935. // Average<TSource> (Func<TSource, Decimal>)
  936. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal>) (x => int.Parse (x))));
  937. AssertException<InvalidOperationException> (delegate () { empty.Average ((Func<string, decimal>) (x => int.Parse (x))); });
  938. // Average<TSource> (Func<TSource, Nullable<Decimal>>)
  939. Assert.AreEqual (3, ((IEnumerable<string>) data).Average<string> ((Func<string, decimal?>) (x => (int?) int.Parse (x))));
  940. // Average<> ()
  941. Assert.AreEqual (3, ((IEnumerable<int>) new int [] { 2, 3, 4 }).Average ());
  942. AssertException<InvalidOperationException> (delegate () { new int [0].Average (); });
  943. // Average<> ()
  944. Assert.AreEqual (3, ((IEnumerable<Nullable<int>>) new int? [] { 2, 3, 4 }).Average ());
  945. // Average<> ()
  946. Assert.AreEqual (3, ((IEnumerable<Int64>) new long [] { 2, 3, 4 }).Average ());
  947. AssertException<InvalidOperationException> (delegate () { new long [0].Average (); });
  948. // Average<> ()
  949. Assert.AreEqual (3, ((IEnumerable<Nullable<Int64>>) new long? [] { 2, 3, 4 }).Average ());
  950. // Average<> ()
  951. Assert.AreEqual (3, ((IEnumerable<Single>) new float [] { 2, 3, 4 }).Average ());
  952. AssertException<InvalidOperationException> (delegate () { new float [0].Average (); });
  953. // Average<> ()
  954. Assert.AreEqual (3, ((IEnumerable<Nullable<Single>>) new float? [] { 2, 3, 4 }).Average ());
  955. // Average<> ()
  956. Assert.AreEqual (3, ((IEnumerable<Double>) new double [] { 2, 3, 4 }).Average ());
  957. AssertException<InvalidOperationException> (delegate () { new double [0].Average (); });
  958. // Average<> ()
  959. Assert.AreEqual (3, ((IEnumerable<Nullable<Double>>) new double? [] { 2, 3, 4 }).Average ());
  960. // Average<> ()
  961. Assert.AreEqual (3, ((IEnumerable<Decimal>) new decimal [] { 2, 3, 4 }).Average ());
  962. AssertException<InvalidOperationException> (delegate () { new decimal [0].Average (); });
  963. // Average<> ()
  964. Assert.AreEqual (3, ((IEnumerable<Nullable<Decimal>>) new decimal? [] { 2, 3, 4 }).Average ());
  965. }
  966. [Test]
  967. public void WhereArgumentNullTest ()
  968. {
  969. string [] data = { "2", "1", "5", "3", "4" };
  970. // Where<TSource> (Func<TSource, bool>)
  971. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where (x => true); });
  972. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, bool>) null); });
  973. // Where<TSource> (Func<TSource, int, bool>)
  974. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Where ((x, y) => true); });
  975. AssertException<ArgumentNullException> (delegate () { data.Where ((Func<string, int, bool>) null); });
  976. }
  977. [Test]
  978. public void WhereTest ()
  979. {
  980. int [] data = { 2, 1, 5, 3, 4 };
  981. int [] expected1 = { 2, 1 };
  982. int [] expected2 = { 2 };
  983. // Where<TSource> (Func<TSource, bool>)
  984. AssertAreSame (expected1, data.Where (x => x < 3));
  985. // Where<TSource> (Func<TSource, int, bool>)
  986. AssertAreSame (expected2, data.Where ((x, y) => x < 3 && y != 1));
  987. }
  988. [Test]
  989. public void SelectArgumentNullTest ()
  990. {
  991. string [] data = { "2", "1", "5", "3", "4" };
  992. // Select<TSource,TResult> (Func<TSource, TResult>)
  993. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select (x => "test"); });
  994. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, string>) null); });
  995. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  996. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Select ((x, y) => "test"); });
  997. AssertException<ArgumentNullException> (delegate () { data.Select ((Func<string, int, string>) null); });
  998. }
  999. [Test]
  1000. public void SelectTest ()
  1001. {
  1002. string [] data = { "2", "1", "5", "3", "4" };
  1003. string [] expected1 = { "2x", "1x", "5x", "3x", "4x" };
  1004. string [] expected2 = { "2x0", "1x1", "5x2", "3x3", "4x4" };
  1005. // Select<TSource,TResult> (Func<TSource, TResult>)
  1006. AssertAreSame (expected1, data.Select<string, string> (x => x + "x"));
  1007. // Select<TSource,TResult> (Func<TSource, int, TResult>)
  1008. AssertAreSame (expected2, data.Select<string, string> ((x, y) => x + "x" + y));
  1009. }
  1010. [Test]
  1011. public void SelectManyArgumentNullTest ()
  1012. {
  1013. string [] data = { "2", "1", "5", "3", "4" };
  1014. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1015. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data); });
  1016. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null); });
  1017. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1018. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data); });
  1019. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null); });
  1020. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1021. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany ((x, y) => data, (x, y) => "test"); });
  1022. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, int, IEnumerable<string>>) null, (x, y) => "test"); });
  1023. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((x, y) => data, (Func<string, string, string>) null); });
  1024. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1025. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SelectMany (x => data, (x, y) => "test"); });
  1026. AssertException<ArgumentNullException> (delegate () { data.SelectMany ((Func<string, IEnumerable<string>>) null, (x, y) => "test"); });
  1027. AssertException<ArgumentNullException> (delegate () { data.SelectMany (x => data, (Func<string, string, string>) null); });
  1028. }
  1029. [Test]
  1030. public void SelectManyTest ()
  1031. {
  1032. string [] data = { "0", "1" };
  1033. string [] expected = { "0", "00", "1", "11" };
  1034. // SelectMany<TSource,TResult> (Func<TSource, IEnumerable<TResult>>)
  1035. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }));
  1036. // SelectMany<TSource,TResult> (Func<TSource, int, IEnumerable<TResult>>)
  1037. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }));
  1038. // SelectMany<TSource,TCollection,TResult> (Func<string, int, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1039. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany ((x, y) => new string [] { x, x + y }, (x, y) => y));
  1040. // SelectMany<TSource,TCollection,TResult> (Func<TSource, IEnumerable<TCollection>>, Func<TSource, TCollection, TResult>)
  1041. AssertAreSame (expected, ((IEnumerable<string>) data).SelectMany (x => new string [] { x, x + x }, (x, y) => y));
  1042. }
  1043. [Test]
  1044. public void TakeArgumentNullTest ()
  1045. {
  1046. //string [] data = { "2", "1", "5", "3", "4" };
  1047. // Take<TSource> (int)
  1048. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Take (0); });
  1049. }
  1050. [Test]
  1051. public void TakeTest ()
  1052. {
  1053. int [] data = { 2, 1, 5, 3, 1 };
  1054. int [] expected = { 2, 1 };
  1055. int [] empty = { };
  1056. // Take<TSource> (int)
  1057. AssertAreSame (expected, data.Take (2));
  1058. AssertAreSame (empty, data.Take (-2));
  1059. }
  1060. [Test]
  1061. public void TakeWhileArgumentNullTest ()
  1062. {
  1063. string [] data = { "2", "1", "5", "3", "4" };
  1064. // TakeWhile<TSource> (Func<TSource, bool>)
  1065. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile (x => true); });
  1066. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, bool>) null); });
  1067. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1068. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).TakeWhile ((x, y) => true); });
  1069. AssertException<ArgumentNullException> (delegate () { data.TakeWhile ((Func<string, int, bool>) null); });
  1070. }
  1071. [Test]
  1072. public void TakeWhileTest ()
  1073. {
  1074. int [] data = { 2, 1, 5, 3, 1 };
  1075. int [] expected = { 2, 1 };
  1076. // TakeWhile<TSource> (Func<TSource, bool>)
  1077. AssertAreSame (expected, data.TakeWhile (x => x != 5));
  1078. // TakeWhile<TSource> (Func<TSource, int, bool>)
  1079. AssertAreSame (expected, data.TakeWhile ((x, y) => y != 2));
  1080. }
  1081. [Test]
  1082. public void SkipArgumentNullTest ()
  1083. {
  1084. //string [] data = { "2", "1", "5", "3", "4" };
  1085. // Skip<TSource> (int)
  1086. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Skip (0); });
  1087. }
  1088. [Test]
  1089. public void SkipTest ()
  1090. {
  1091. int [] data = { 2, 1, 5, 3, 1 };
  1092. int [] expected = { 5, 3, 1 };
  1093. // Skip<string> (TSource)
  1094. AssertAreSame (expected, data.Skip (2));
  1095. AssertAreSame (data, data.Skip (-2));
  1096. }
  1097. [Test]
  1098. public void SkipWhileArgumentNullTest ()
  1099. {
  1100. string [] data = { "2", "1", "5", "3", "4" };
  1101. // SkipWhile<TSource> (Func<TSource, bool>)
  1102. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile (x => true); });
  1103. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, bool>) null); });
  1104. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1105. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SkipWhile ((x, y) => true); });
  1106. AssertException<ArgumentNullException> (delegate () { data.SkipWhile ((Func<string, int, bool>) null); });
  1107. }
  1108. [Test]
  1109. public void SkipWhileTest ()
  1110. {
  1111. int [] data = { 2, 1, 5, 3, 1 };
  1112. int [] expected = { 5, 3, 1 };
  1113. // SkipWhile<TSource> (Func<TSource, bool>)
  1114. AssertAreSame (expected, data.SkipWhile (x => x != 5));
  1115. // SkipWhile<TSource> (Func<TSource, int, bool>)
  1116. AssertAreSame (expected, data.SkipWhile ((x, y) => y != 2));
  1117. }
  1118. [Test]
  1119. public void JoinArgumentNullTest ()
  1120. {
  1121. string [] data = { "2", "1", "5", "3", "4" };
  1122. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1123. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test"); });
  1124. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1125. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1126. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1127. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null); });
  1128. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1129. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Join (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1130. AssertException<ArgumentNullException> (delegate () { data.Join ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1131. AssertException<ArgumentNullException> (delegate () { data.Join (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1132. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1133. AssertException<ArgumentNullException> (delegate () { data.Join (data, x => "test", x => "test", (Func<string, string, string>) null, EqualityComparer<string>.Default); });
  1134. }
  1135. [Test]
  1136. public void JoinTest ()
  1137. {
  1138. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1139. string [] dataInner1 = { "7", "3", "5", "8", "9" };
  1140. string [] expected1 = { "55", "33" };
  1141. string [] dataOuter2 = { "2", "1", "3", "4" };
  1142. string [] dataInner2 = { "7", "5", "8", "9" };
  1143. string [] expected2 = { };
  1144. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>)
  1145. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y));
  1146. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y));
  1147. // Join<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, TInner, TResult>, IEqualityComparer<string>)
  1148. AssertAreSame (expected1, dataOuter1.Join (dataInner1, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1149. AssertAreSame (expected2, dataOuter2.Join (dataInner2, x => x, x => x, (x, y) => x + y, EqualityComparer<string>.Default));
  1150. }
  1151. [Test]
  1152. public void JoinTestNullKeys ()
  1153. {
  1154. var l1 = new [] {
  1155. new { Name = "name1", Nullable = (int?) null },
  1156. new { Name = "name2", Nullable = (int?) null }
  1157. };
  1158. var count = l1.Join (l1, i => i.Nullable, i => i.Nullable, (x, y) => x.Name).Count ();
  1159. Assert.AreEqual (0, count);
  1160. }
  1161. [Test]
  1162. public void GroupJoinArgumentNullTest ()
  1163. {
  1164. string [] data = { "2", "1", "5", "3", "4" };
  1165. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1166. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test"); });
  1167. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test"); });
  1168. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test"); });
  1169. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test"); });
  1170. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null); });
  1171. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1172. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupJoin (data, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1173. AssertException<ArgumentNullException> (delegate () { data.GroupJoin ((IEnumerable<string>) null, x => "test", x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1174. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, (Func<string, string>) null, x => "test", (x, y) => "test", EqualityComparer<string>.Default); });
  1175. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", (Func<string, string>) null, (x, y) => "test", EqualityComparer<string>.Default); });
  1176. AssertException<ArgumentNullException> (delegate () { data.GroupJoin (data, x => "test", x => "test", (Func<string, IEnumerable<string>, string>) null, EqualityComparer<string>.Default); });
  1177. }
  1178. [Test]
  1179. public void GroupJoinTest ()
  1180. {
  1181. string [] dataOuter1 = { "2", "1", "5", "3", "4" };
  1182. string [] dataInner1 = { "7", "3", "5", "3", "9" };
  1183. string [] expected1 = { "2", "1", "55", "333", "4" };
  1184. string [] dataOuter2 = { "2", "1", "5", "8", "4" };
  1185. string [] dataInner2 = { "7", "3", "6", "3", "9" };
  1186. string [] expected2 = { "2", "1", "5", "8", "4" };
  1187. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult>)
  1188. AssertAreSame (expected1, (dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1189. AssertAreSame (expected2, (dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; })));
  1190. // GroupJoin<TOuter,TInner,TKey,TResult> (IEnumerable<TInner>, Func<TOuter, TKey>, Func<TInner, TKey>, Func<TOuter, IEnumerable<TInner>, TResult, IEqualityComparer<TKey>>)
  1191. AssertAreSame (expected1, dataOuter1.GroupJoin (dataInner1, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1192. AssertAreSame (expected2, dataOuter2.GroupJoin (dataInner2, x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1193. }
  1194. [Test]
  1195. public void GroupJoinWithNullKeys ()
  1196. {
  1197. string[] l1 = { null };
  1198. string[] l2 = { null, null };
  1199. var res = l1.GroupJoin (l2, x => x, y => y, (a, b) => new { Key = a, Count = b.Count () }).ToArray ();
  1200. Assert.AreEqual (1, res.Length);
  1201. Assert.AreEqual (0, res [0].Count);
  1202. }
  1203. [Test]
  1204. public void OrderByArgumentNullTest ()
  1205. {
  1206. string [] data = { "2", "1", "5", "3", "4" };
  1207. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1208. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test"); });
  1209. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null); });
  1210. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1211. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderBy (x => "test", Comparer<string>.Default); });
  1212. AssertException<ArgumentNullException> (delegate () { data.OrderBy ((Func<string, string>) null, Comparer<string>.Default); });
  1213. }
  1214. [Test]
  1215. public void OrderByTest ()
  1216. {
  1217. int [] data = { 2, 1, 5, 3, 4 };
  1218. int [] expected = { 1, 2, 3, 4, 5 };
  1219. // OrderBy<TSource,TKey> (Func<TSource, TKey>)
  1220. AssertAreSame (expected, data.OrderBy (x => x));
  1221. // OrderBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1222. AssertAreSame (expected, data.OrderBy (x => x, Comparer<int>.Default));
  1223. }
  1224. [Test]
  1225. public void OrderByDescendingArgumentNullTest ()
  1226. {
  1227. string [] data = { "2", "1", "5", "3", "4" };
  1228. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1229. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test"); });
  1230. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null); });
  1231. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1232. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).OrderByDescending (x => "test", Comparer<string>.Default); });
  1233. AssertException<ArgumentNullException> (delegate () { data.OrderByDescending ((Func<string, string>) null, Comparer<string>.Default); });
  1234. }
  1235. [Test]
  1236. public void OrderByDescendingTest ()
  1237. {
  1238. int [] data = { 2, 1, 5, 3, 4 };
  1239. int [] expected = { 5, 4, 3, 2, 1 };
  1240. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>)
  1241. AssertAreSame (expected, data.OrderByDescending (x => x));
  1242. // OrderByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1243. AssertAreSame (expected, data.OrderByDescending (x => x, Comparer<int>.Default));
  1244. }
  1245. [Test]
  1246. public void ThenByArgumentNullTest ()
  1247. {
  1248. string [] data = { "2", "1", "5", "3", "4" };
  1249. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1250. AssertException<ArgumentNullException> (delegate () {
  1251. ((IOrderedEnumerable<string>) null).ThenBy (x => "test");
  1252. });
  1253. AssertException<ArgumentNullException> (delegate () {
  1254. data.OrderBy (x => x).ThenBy ((Func<string, string>) null);
  1255. });
  1256. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1257. AssertException<ArgumentNullException> (delegate () {
  1258. ((IOrderedEnumerable<string>) null).ThenBy (x => "test", Comparer<string>.Default);
  1259. });
  1260. AssertException<ArgumentNullException> (delegate () {
  1261. data.OrderBy (x => x).ThenBy ((Func<string, string>) null, Comparer<string>.Default);
  1262. });
  1263. }
  1264. [Test]
  1265. public void ThenByTest ()
  1266. {
  1267. int [] data = { 2, 1, 5, 3, 4 };
  1268. int [] expected = { 1, 2, 3, 4, 5 };
  1269. // ThenBy<TSource,TKey> (Func<TSource, TKey>)
  1270. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x));
  1271. // ThenBy<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1272. AssertAreSame (expected, data.OrderBy (x => x).ThenBy (x => x, Comparer<int>.Default));
  1273. }
  1274. [Test]
  1275. public void ThenByDescendingArgumentNullTest ()
  1276. {
  1277. string [] data = { "2", "1", "5", "3", "4" };
  1278. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1279. AssertException<ArgumentNullException> (delegate () {
  1280. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test");
  1281. });
  1282. AssertException<ArgumentNullException> (delegate () {
  1283. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null);
  1284. });
  1285. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1286. AssertException<ArgumentNullException> (delegate () {
  1287. ((IOrderedEnumerable<string>) null).ThenByDescending (x => "test", Comparer<string>.Default);
  1288. });
  1289. AssertException<ArgumentNullException> (delegate () {
  1290. data.OrderBy (x => x).ThenByDescending ((Func<string, string>) null, Comparer<string>.Default);
  1291. });
  1292. }
  1293. [Test]
  1294. public void ThenByDescendingTest ()
  1295. {
  1296. int [] data = { 2, 1, 5, 3, 4 };
  1297. int [] expected = { 5, 4, 3, 2, 1 };
  1298. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>)
  1299. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x));
  1300. // ThenByDescending<TSource,TKey> (Func<TSource, TKey>, IComparer<string>)
  1301. AssertAreSame (expected, data.OrderBy (x => 0).ThenByDescending (x => x, Comparer<int>.Default));
  1302. }
  1303. [Test]
  1304. public void GroupByArgumentNullTest ()
  1305. {
  1306. string [] data = { "2", "1", "5", "3", "4" };
  1307. // GroupBy<string,string> (Func<string, string>)
  1308. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test")); });
  1309. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null); });
  1310. // GroupBy<string,string> (Func<string, string>, IEqualityComparer<string>)
  1311. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1312. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1313. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>)
  1314. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1315. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1316. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1317. // GroupBy<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1318. 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); });
  1319. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1320. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1321. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>)
  1322. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1323. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) null, (Func<string, IEnumerable<string>, string>) ((x, y) => "test")); });
  1324. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null); });
  1325. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>)
  1326. 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")); });
  1327. 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")); });
  1328. 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")); });
  1329. 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); });
  1330. // GroupBy<string,string,string> (Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1331. 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); });
  1332. 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); });
  1333. AssertException<ArgumentNullException> (delegate () { data.GroupBy<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, IEnumerable<string>, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1334. // GroupBy<string,string,string,string> (Func<string, string>, Func<string, string>, Func<string, IEnumerable<string>, string>, IEqualityComparer<string>)
  1335. 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); });
  1336. 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); });
  1337. 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); });
  1338. 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); });
  1339. }
  1340. [Test]
  1341. public void GroupByTest ()
  1342. {
  1343. string [] data = { "2", "1", "5", "3", "4", "3" };
  1344. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1345. expected.Add ("2", new List<string> () { "2" });
  1346. expected.Add ("1", new List<string> () { "1" });
  1347. expected.Add ("5", new List<string> () { "5" });
  1348. expected.Add ("3", new List<string> () { "3", "3" });
  1349. expected.Add ("4", new List<string> () { "4" });
  1350. Dictionary<string, IEnumerable<string>> expected2 = new Dictionary<string, IEnumerable<string>> ();
  1351. expected2.Add ("2", new List<string> () { "22" });
  1352. expected2.Add ("1", new List<string> () { "11" });
  1353. expected2.Add ("5", new List<string> () { "55" });
  1354. expected2.Add ("3", new List<string> () { "33", "33" });
  1355. expected2.Add ("4", new List<string> () { "44" });
  1356. string [] expected3 = new string [] { "22", "11", "55", "333", "44" };
  1357. // GroupBy<int,int> (Func<int, int>)
  1358. AssertAreSame (expected, data.GroupBy (x => x));
  1359. // GroupBy<int,int> (Func<int, int>, IEqualityComparer<int>)
  1360. AssertAreSame (expected, data.GroupBy (x => x, EqualityComparer<string>.Default));
  1361. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>)
  1362. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x));
  1363. // GroupBy<int,int,int> (Func<int, int>, Func<int, int>, IEqualityComparer<int>)
  1364. AssertAreSame (expected2, data.GroupBy (x => x, x => x + x, EqualityComparer<string>.Default));
  1365. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>)
  1366. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1367. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>)
  1368. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }));
  1369. // GroupBy<int,int,int> (Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1370. AssertAreSame (expected3, data.GroupBy (x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1371. // GroupBy<int,int,int,int> (Func<int, int>, Func<int, int>, Func<int, IEnumerable<int>, int>, IEqualityComparer<int>)
  1372. AssertAreSame (expected3, data.GroupBy (x => x, x => x, (x, y) => { foreach (var s in y) x += s; return x; }, EqualityComparer<string>.Default));
  1373. }
  1374. class Data {
  1375. public int Number;
  1376. public string String;
  1377. public Data (int number, string str)
  1378. {
  1379. Number = number;
  1380. String = str;
  1381. }
  1382. }
  1383. [Test]
  1384. public void GroupByLastNullGroup ()
  1385. {
  1386. var values = new List<Data> ();
  1387. values.Add (new Data (0, "a"));
  1388. values.Add (new Data (1, "a"));
  1389. values.Add (new Data (2, "b"));
  1390. values.Add (new Data (3, "b"));
  1391. values.Add (new Data (4, null));
  1392. var groups = values.GroupBy (d => d.String);
  1393. Assert.AreEqual (3, groups.Count ());
  1394. }
  1395. [Test]
  1396. public void ConcatArgumentNullTest ()
  1397. {
  1398. string [] data = { "2", "1", "5", "3", "4" };
  1399. // Concat<TSource> (IEnumerable<TSource>)
  1400. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Concat (data); });
  1401. AssertException<ArgumentNullException> (delegate () { data.Concat ((IEnumerable<string>) null); });
  1402. }
  1403. [Test]
  1404. public void ConcatTest ()
  1405. {
  1406. int [] data1 = { 2, 1, 5, 3, 4 };
  1407. int [] data2 = { 1, 2, 3, 4, 5 };
  1408. int [] expected = { 2, 1, 5, 3, 4, 1, 2, 3, 4, 5 };
  1409. // Concat<TSource> (IEnumerable<TSource>)
  1410. AssertAreSame (expected, data1.Concat (data2));
  1411. }
  1412. [Test]
  1413. public void DistinctArgumentNullTest ()
  1414. {
  1415. //string [] data = { "2", "1", "5", "3", "4" };
  1416. // Distinct<TSource> ()
  1417. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (); });
  1418. // Distinct<TSource> (IEqualityComparer<TSource>)
  1419. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Distinct (EqualityComparer<string>.Default); });
  1420. }
  1421. [Test]
  1422. public void DistinctTest ()
  1423. {
  1424. int [] data = { 2, 1, 5, 3, 4, 2, 5, 3, 1, 8 };
  1425. int [] expected = { 2, 1, 5, 3, 4, 8 };
  1426. // Distinct<TSource> ()
  1427. AssertAreSame (expected, data.Distinct ());
  1428. // Distinct<iTSourcent> (IEqualityComparer<TSource>)
  1429. AssertAreSame (expected, data.Distinct (EqualityComparer<int>.Default));
  1430. }
  1431. [Test]
  1432. public void UnionArgumentNullTest ()
  1433. {
  1434. string [] data = { "2", "1", "5", "3", "4" };
  1435. // Union<TSource> (IEnumerable<TSource>)
  1436. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data); });
  1437. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null); });
  1438. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1439. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Union (data, EqualityComparer<string>.Default); });
  1440. AssertException<ArgumentNullException> (delegate () { data.Union ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1441. }
  1442. [Test]
  1443. public void UnionTest ()
  1444. {
  1445. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1446. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1447. int [] expected = { 2, 1, 5, 7, 3, 4, 8 };
  1448. // Union<TSource> (IEnumerable<TSource>)
  1449. AssertAreSame (expected, data1.Union (data2));
  1450. // Union<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1451. AssertAreSame (expected, data1.Union (data2, EqualityComparer<int>.Default));
  1452. }
  1453. [Test]
  1454. public void IntersectArgumentNullTest ()
  1455. {
  1456. string [] data = { "2", "1", "5", "3", "4" };
  1457. // Intersect<TSource> (IEnumerable<TSource>)
  1458. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data); });
  1459. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null); });
  1460. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1461. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Intersect (data, EqualityComparer<string>.Default); });
  1462. AssertException<ArgumentNullException> (delegate () { data.Intersect ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1463. }
  1464. [Test]
  1465. public void IntersectTest ()
  1466. {
  1467. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1468. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1469. int [] expected = { 2, 1, 5, 3, 4 };
  1470. // Intersect<TSource> (IEnumerable<TSource>)
  1471. AssertAreSame (expected, data1.Intersect (data2));
  1472. // Intersect<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1473. AssertAreSame (expected, data1.Intersect (data2, EqualityComparer<int>.Default));
  1474. }
  1475. [Test]
  1476. public void ExceptArgumentNullTest ()
  1477. {
  1478. string [] data = { "2", "1", "5", "3", "4" };
  1479. // Except<TSource> (IEnumerable<TSource>)
  1480. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data); });
  1481. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null); });
  1482. // Except<TSource> (IEnumerable<string>, IEqualityComparer<TSource>)
  1483. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Except (data, EqualityComparer<string>.Default); });
  1484. AssertException<ArgumentNullException> (delegate () { data.Except ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1485. }
  1486. [Test]
  1487. public void ExceptTest ()
  1488. {
  1489. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1490. int [] data2 = { 1, 2, 3, 8, 4, 5 };
  1491. int [] expected = { 7 };
  1492. // Except<TSource> (IEnumerable<TSource>)
  1493. AssertAreSame (expected, data1.Except (data2));
  1494. // Except<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1495. AssertAreSame (expected, data1.Except (data2, EqualityComparer<int>.Default));
  1496. }
  1497. [Test]
  1498. public void ReverseArgumentNullTest ()
  1499. {
  1500. //string [] data = { "2", "1", "5", "3", "4" };
  1501. // Reverse<TSource> ()
  1502. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).Reverse (); });
  1503. }
  1504. [Test]
  1505. public void ReverseTest ()
  1506. {
  1507. int [] data = { 2, 1, 5, 7, 3, 4 };
  1508. int [] expected = { 4, 3, 7, 5, 1, 2 };
  1509. // Reverse<TSource> ()
  1510. AssertAreSame (expected, data.Reverse ());
  1511. }
  1512. [Test]
  1513. public void SequenceEqualArgumentNullTest ()
  1514. {
  1515. string [] data = { "2", "1", "5", "3", "4" };
  1516. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1517. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data); });
  1518. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null); });
  1519. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1520. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).SequenceEqual (data, EqualityComparer<string>.Default); });
  1521. AssertException<ArgumentNullException> (delegate () { data.SequenceEqual ((IEnumerable<string>) null, EqualityComparer<string>.Default); });
  1522. }
  1523. [Test]
  1524. public void SequenceEqualTest ()
  1525. {
  1526. int [] data1 = { 2, 1, 5, 7, 3, 4 };
  1527. int [] data2 = { 2, 1, 5, 7, 3, 4 };
  1528. int [] data3 = { 2, 1, 5, 7, 3, 4, 5 };
  1529. int [] data4 = { 2, 1, 5, 7, 3 };
  1530. int [] data5 = { 2, 1, 5, 8, 3, 4 };
  1531. // SequenceEqual<TSource> (IEnumerable<TSource>)
  1532. Assert.IsTrue (data1.SequenceEqual (data2));
  1533. Assert.IsFalse (data1.SequenceEqual (data3));
  1534. Assert.IsFalse (data1.SequenceEqual (data4));
  1535. Assert.IsFalse (data1.SequenceEqual (data5));
  1536. // SequenceEqual<TSource> (IEnumerable<TSource>, IEqualityComparer<TSource>)
  1537. Assert.IsTrue (data1.SequenceEqual (data2, EqualityComparer<int>.Default));
  1538. Assert.IsFalse (data1.SequenceEqual (data3, EqualityComparer<int>.Default));
  1539. Assert.IsFalse (data1.SequenceEqual (data4, EqualityComparer<int>.Default));
  1540. Assert.IsFalse (data1.SequenceEqual (data5, EqualityComparer<int>.Default));
  1541. }
  1542. [Test]
  1543. public void AsEnumerableArgumentNullTest ()
  1544. {
  1545. //string [] data = { "2", "1", "5", "3", "4" };
  1546. }
  1547. [Test]
  1548. public void AsEnumerableTest ()
  1549. {
  1550. int [] data = { 2, 1, 5, 7, 3, 4 };
  1551. // AsEnumerable<TSource> ()
  1552. Assert.AreSame (data, data.AsEnumerable ());
  1553. }
  1554. [Test]
  1555. public void ToArrayArgumentNullTest ()
  1556. {
  1557. //string [] data = { "2", "1", "5", "3", "4" };
  1558. // ToArray<TSource> ()
  1559. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToArray (); });
  1560. }
  1561. [Test]
  1562. public void ToArrayTest ()
  1563. {
  1564. int [] data = { 2, 3, 4, 5 };
  1565. int [] expected = { 2, 3, 4, 5 };
  1566. // ToArray<TSource> ()
  1567. AssertAreSame (expected, data.ToArray ());
  1568. }
  1569. [Test]
  1570. public void ToListArgumentNullTest ()
  1571. {
  1572. //string [] data = { "2", "1", "5", "3", "4" };
  1573. // ToList<string> ()
  1574. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToList (); });
  1575. }
  1576. [Test]
  1577. public void ToListTest ()
  1578. {
  1579. int [] data = { 2, 4, 5, 1 };
  1580. int [] expected = { 2, 4, 5, 1 };
  1581. // ToList<int> ()
  1582. AssertAreSame (expected, data.ToList ());
  1583. }
  1584. [Test]
  1585. public void ToDictionaryArgumentNullTest ()
  1586. {
  1587. string [] data = { "2", "1", "5", "3", "4" };
  1588. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1589. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test"); });
  1590. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null); });
  1591. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1592. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", EqualityComparer<string>.Default); });
  1593. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, EqualityComparer<string>.Default); });
  1594. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1595. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test"); });
  1596. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test"); });
  1597. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null); });
  1598. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1599. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToDictionary (x => "test", x => "test", EqualityComparer<string>.Default); });
  1600. AssertException<ArgumentNullException> (delegate () { data.ToDictionary ((Func<string, string>) null, x => "test", EqualityComparer<string>.Default); });
  1601. AssertException<ArgumentNullException> (delegate () { data.ToDictionary (x => "test", (Func<string, string>) null, EqualityComparer<string>.Default); });
  1602. }
  1603. [Test]
  1604. public void ToDictionaryTest ()
  1605. {
  1606. string [] data = { "2", "1", "5", "3", "4" };
  1607. Dictionary<string, string> expected = new Dictionary<string, string> ();
  1608. expected.Add ("k2", "2");
  1609. expected.Add ("k1", "1");
  1610. expected.Add ("k5", "5");
  1611. expected.Add ("k3", "3");
  1612. expected.Add ("k4", "4");
  1613. // ToDictionary<TSource,TKey> (Func<TSource, TKey>)
  1614. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x));
  1615. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key"); });
  1616. // ToDictionary<TSource,TKey> (Func<TSource, TKey>, IEqualityComparer<TKey>)
  1617. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, EqualityComparer<string>.Default));
  1618. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", EqualityComparer<string>.Default); });
  1619. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>)
  1620. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x));
  1621. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x); });
  1622. // ToDictionary<TSource,TKey,TElement> (Func<TSource, TKey>, Func<TSource, TElement>, IEqualityComparer<TKey>)
  1623. AssertAreSame (expected, ((IEnumerable<string>) data).ToDictionary (x => "k" + x, x => x, EqualityComparer<string>.Default));
  1624. AssertException<ArgumentException> (delegate () { data.ToDictionary (x => "key", x => x, EqualityComparer<string>.Default); });
  1625. }
  1626. [Test]
  1627. public void ToLookupArgumentNullTest ()
  1628. {
  1629. string [] data = { "2", "1", "5", "3", "4" };
  1630. // ToLookup<string,string> (Func<string, string>)
  1631. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test")); });
  1632. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null); });
  1633. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1634. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string> ((Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1635. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string> ((Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1636. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1637. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) (x => "test")); });
  1638. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test")); });
  1639. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null); });
  1640. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1641. 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); });
  1642. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) null, (Func<string, string>) (x => "test"), (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1643. AssertException<ArgumentNullException> (delegate () { data.ToLookup<string, string, string> ((Func<string, string>) (x => "test"), (Func<string, string>) null, (IEqualityComparer<string>) EqualityComparer<string>.Default); });
  1644. }
  1645. [Test]
  1646. public void ToLookupTest ()
  1647. {
  1648. string [] data = { "23", "12", "55", "42", "41" };
  1649. Dictionary<string, IEnumerable<string>> expected = new Dictionary<string, IEnumerable<string>> ();
  1650. expected.Add ("2", new List<string> () { "23" });
  1651. expected.Add ("1", new List<string> () { "12" });
  1652. expected.Add ("5", new List<string> () { "55" });
  1653. expected.Add ("4", new List<string> () { "42", "41" });
  1654. Assert.AreEqual (expected.Count, ((IEnumerable<string>)data).ToLookup ((x => x [0].ToString ())).Count);
  1655. // ToLookup<string,string> (Func<string, string>)
  1656. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup ((x => x [0].ToString ())));
  1657. // ToLookup<string,string> (Func<string, string>, IEqualityComparer<string>)
  1658. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), EqualityComparer<string>.Default));
  1659. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>)
  1660. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x));
  1661. // ToLookup<string,string,string> (Func<string, string>, Func<string, string>, IEqualityComparer<string>)
  1662. AssertAreSame (expected, ((IEnumerable<string>) data).ToLookup (x => x [0].ToString (), x => x, EqualityComparer<string>.Default));
  1663. }
  1664. [Test]
  1665. public void ToLookupNullKeyTest ()
  1666. {
  1667. string[] strs = new string[] { "one", null, "two", null, "three" };
  1668. int i = 0;
  1669. var l = strs.ToLookup (s => (s == null) ? null : "numbers", s => (s == null) ? (++i).ToString() : s);
  1670. Assert.AreEqual (2, l.Count);
  1671. Assert.AreEqual (2, l [null].Count());
  1672. Assert.IsTrue (l [null].Contains ("1"));
  1673. Assert.IsTrue (l [null].Contains ("2"));
  1674. Assert.AreEqual (3, l ["numbers"].Count());
  1675. Assert.IsTrue (l ["numbers"].Contains ("one"));
  1676. Assert.IsTrue (l ["numbers"].Contains ("two"));
  1677. Assert.IsTrue (l ["numbers"].Contains ("three"));
  1678. }
  1679. [Test]
  1680. public void DefaultIfEmptyArgumentNullTest ()
  1681. {
  1682. //string [] data = { "2", "1", "5", "3", "4" };
  1683. // DefaultIfEmpty<string> ()
  1684. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> (); });
  1685. // DefaultIfEmpty<string> (string)
  1686. AssertException<ArgumentNullException> (delegate () { ((IEnumerable<string>) null).DefaultIfEmpty<string> ((string) "default"); });
  1687. }
  1688. [Test]
  1689. public void DefaultIfEmptyTest ()
  1690. {
  1691. string [] data = { "2", "1", "5", "3", "4" };
  1692. string [] empty = { };
  1693. string [] default1 = { null };
  1694. string [] default2 = { "default" };
  1695. // DefaultIfEmpty<string> ()
  1696. AssertAreSame (data, data.DefaultIfEmpty ());
  1697. AssertAreSame (default1, empty.DefaultIfEmpty ());
  1698. // DefaultIfEmpty<string> (string)
  1699. AssertAreSame (data, data.DefaultIfEmpty ("default"));
  1700. AssertAreSame (default2, empty.DefaultIfEmpty ("default"));
  1701. }
  1702. [Test]
  1703. public void OfTypeArgumentNullTest ()
  1704. {
  1705. //string [] data = { "2", "1", "5", "3", "4" };
  1706. // OfType<string> ()
  1707. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).OfType<string> (); });
  1708. }
  1709. [Test]
  1710. public void OfTypeTest ()
  1711. {
  1712. object [] data = { "2", 2, "1", "5", "3", "4" };
  1713. string [] expected = { "2", "1", "5", "3", "4" };
  1714. // OfType<string> ()
  1715. AssertAreSame (expected, data.OfType<string> ());
  1716. }
  1717. [Test]
  1718. public void CastArgumentNullTest ()
  1719. {
  1720. //string [] data = { "2", "1", "5", "3", "4" };
  1721. // Cast<string> ()
  1722. AssertException<ArgumentNullException> (delegate () { ((IEnumerable) null).Cast<string> (); });
  1723. }
  1724. [Test]
  1725. public void CastTest ()
  1726. {
  1727. object [] data = { 1, 2, 3 };
  1728. int [] expected = { 1, 2, 3 };
  1729. // Cast<string> ()
  1730. AssertAreSame (expected, data.Cast<int> ());
  1731. AssertException<InvalidCastException> (delegate () { data.Cast<IEnumerable> ().GetEnumerator ().MoveNext (); });
  1732. data.Cast<IEnumerable> ();
  1733. }
  1734. [Test]
  1735. public void RangeArgumentNullTest ()
  1736. {
  1737. //string [] data = { "2", "1", "5", "3", "4" };
  1738. }
  1739. [Test]
  1740. public void RangeTest ()
  1741. {
  1742. int [] expected = { 2, 3, 4, 5 };
  1743. // Range<> (int)
  1744. AssertAreSame (expected, Enumerable.Range (2, 4));
  1745. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (2, -3); });
  1746. AssertException<ArgumentOutOfRangeException> (delegate () { Enumerable.Range (int.MaxValue - 5, 7); });
  1747. Enumerable.Range (int.MaxValue - 5, 6);
  1748. }
  1749. [Test]
  1750. public void ExceptMultipleItems ()
  1751. {
  1752. var data = new [] { 1, 2, 2, 2, 3, 4, 5, 5, 6, 7, 8, 8, 9, 10 };
  1753. var expected = new [] { 2, 4, 6, 8, 10 };
  1754. AssertAreSame (expected, data.Except (new [] { 1, 3, 5, 7, 9 }));
  1755. }
  1756. }
  1757. }