ArrayTest.cs 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264
  1. // ArrayTest.cs - NUnit Test Cases for the System.Array class
  2. //
  3. // David Brandt ([email protected])
  4. // Eduardo Garcia ([email protected])
  5. //
  6. // (C) Ximian, Inc. http://www.ximian.com
  7. // Copyright (C) 2004 Novell (http://www.novell.com)
  8. //
  9. using NUnit.Framework;
  10. using System;
  11. using System.Collections;
  12. using System.Globalization;
  13. #if NET_2_0
  14. using System.Collections.Generic;
  15. #endif
  16. namespace MonoTests.System
  17. {
  18. //Auxiliary Things
  19. enum enua {hola,adios,mas,menos};
  20. class AClass
  21. {
  22. public AClass()
  23. {
  24. }
  25. }
  26. class BClass : AClass
  27. {
  28. }
  29. class CClass : AClass
  30. {
  31. }
  32. struct AStruct
  33. {
  34. public string s;
  35. public string a;
  36. }
  37. class DataEqual
  38. {
  39. public override bool Equals (object obj)
  40. {
  41. return true;
  42. }
  43. }
  44. //End Auxiliary Things
  45. [TestFixture]
  46. public class ArrayTest
  47. {
  48. char [] arrsort = {'d', 'b', 'f', 'e', 'a', 'c'};
  49. public ArrayTest() {}
  50. [Test]
  51. public void TestIsFixedSize() {
  52. char[] a1 = {'a'};
  53. Assert.IsTrue (a1.IsFixedSize, "All arrays are fixed");
  54. }
  55. [Test]
  56. public void TestIsReadOnly() {
  57. char[] a1 = {'a'};
  58. Assert.IsTrue (!a1.IsReadOnly, "No array is readonly");
  59. }
  60. [Test]
  61. public void TestIsSynchronized() {
  62. char[] a1 = {'a'};
  63. Assert.IsTrue (!a1.IsSynchronized, "No array is synchronized");
  64. }
  65. [Test]
  66. public void TestLength() {
  67. {
  68. char[] a1 = { };
  69. Assert.AreEqual (0, a1.Length, "Zero length array");
  70. }
  71. {
  72. char[] a1 = {'c'};
  73. Assert.AreEqual (1, a1.Length, "One-length array");
  74. }
  75. {
  76. char[] a1 = {'c', 'c'};
  77. Assert.AreEqual (2, a1.Length, "Two-length array");
  78. }
  79. }
  80. [Test]
  81. public void TestRank() {
  82. char[] a1 = { 'c', 'd', 'e' };
  83. Assert.AreEqual (1, a1.Rank, "Rank one");
  84. char[,] a2 = new Char[3,3];
  85. Assert.AreEqual (2, a2.Rank, "Rank two");
  86. char[,,] a3 = new Char[3,3,3];
  87. Assert.AreEqual (3, a3.Rank, "Rank three");
  88. }
  89. [Test]
  90. public void TestBinarySearch1() {
  91. bool errorThrown = false;
  92. try {
  93. Array.BinarySearch(null, "blue");
  94. } catch (ArgumentNullException) {
  95. errorThrown = true;
  96. }
  97. Assert.IsTrue (errorThrown, "#B01");
  98. errorThrown = false;
  99. try {
  100. char[,] c1 = new Char[2,2];
  101. Array.BinarySearch(c1, "needle");
  102. } catch (RankException) {
  103. errorThrown = true;
  104. }
  105. Assert.IsTrue (errorThrown, "#B02");
  106. {
  107. char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
  108. Assert.IsTrue (Array.BinarySearch(arr, 'c') >= 3, "#B05");
  109. Assert.IsTrue (Array.BinarySearch(arr, 'c') < 6, "#B06");
  110. }
  111. {
  112. char[] arr = {'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
  113. Assert.AreEqual (-4, Array.BinarySearch(arr, 'c'), "#B07");
  114. }
  115. {
  116. char[] arr = {'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
  117. Assert.AreEqual (-9, Array.BinarySearch(arr, 'e'), "#B08");
  118. }
  119. }
  120. [Test]
  121. public void TestBinarySearch2() {
  122. bool errorThrown = false;
  123. try {
  124. Array.BinarySearch(null, 0, 1, "blue");
  125. } catch (ArgumentNullException) {
  126. errorThrown = true;
  127. }
  128. Assert.IsTrue (errorThrown, "#B20");
  129. errorThrown = false;
  130. try {
  131. char[,] c1 = new Char[2,2];
  132. Array.BinarySearch(c1, 0, 1, "needle");
  133. } catch (RankException) {
  134. errorThrown = true;
  135. }
  136. Assert.IsTrue (errorThrown, "#B21");
  137. errorThrown = false;
  138. try {
  139. char[] c1 = {'a'};
  140. Array.BinarySearch(c1, -1, 1, 'a');
  141. } catch (ArgumentOutOfRangeException) {
  142. errorThrown = true;
  143. }
  144. Assert.IsTrue (errorThrown, "#B22");
  145. errorThrown = false;
  146. try {
  147. char[] c1 = {'a'};
  148. Array.BinarySearch(c1, 0, -1, 'a');
  149. } catch (ArgumentOutOfRangeException) {
  150. errorThrown = true;
  151. }
  152. Assert.IsTrue (errorThrown, "#B23");
  153. errorThrown = false;
  154. try {
  155. char[] c1 = {'a'};
  156. Array.BinarySearch(c1, 0, 4, 'a');
  157. } catch (ArgumentException) {
  158. errorThrown = true;
  159. }
  160. Assert.IsTrue (errorThrown, "#B24");
  161. {
  162. char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
  163. Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') >= 5, "#B26");
  164. Assert.IsTrue (Array.BinarySearch(arr, 2, 8, 'c') < 8, "#B27");
  165. }
  166. {
  167. char[] arr = {'z', 'z', 'a', 'b', 'b', 'd', 'd', 'd', 'e', 'e'};
  168. Assert.AreEqual (-6, Array.BinarySearch(arr, 2, 8, 'c'), "#B28");
  169. }
  170. {
  171. char[] arr = {'z', 'z', 'a', 'b', 'b', 'c', 'c', 'c', 'd', 'd'};
  172. Assert.AreEqual (-11, Array.BinarySearch(arr, 2, 8, 'e'), "#B29");
  173. }
  174. }
  175. public void TestBinarySearch3()
  176. {
  177. int[] array = new int[100];
  178. for (int i = 0; i < 100; i++)
  179. array[i] = 10;
  180. Assert.AreEqual (49, Array.BinarySearch(array, 10), "#B30");
  181. }
  182. [Test]
  183. public void BinarySearch_NullValue ()
  184. {
  185. int[] array = new int[1];
  186. Assert.AreEqual (-1, Array.BinarySearch (array, null), "I=a,o");
  187. Assert.AreEqual (-1, Array.BinarySearch (array, null, null), "I=a,o,c");
  188. Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null), "I=a,i,i,o");
  189. Assert.AreEqual (-1, Array.BinarySearch (array, 0, 1, null, null), "I=a,i,i,o,c");
  190. object[] o = new object [3] { this, this, null };
  191. Assert.AreEqual (-1, Array.BinarySearch (o, null), "O=a,o");
  192. Assert.AreEqual (-1, Array.BinarySearch (o, null, null), "O=a,o,c");
  193. Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null), "O=a,i,i,o");
  194. Assert.AreEqual (-1, Array.BinarySearch (o, 0, 3, null, null), "O=a,i,i,o,c");
  195. }
  196. // TODO - testBinarySearch with explicit IComparer args
  197. [Test]
  198. public void TestClear() {
  199. bool errorThrown = false;
  200. try {
  201. Array.Clear(null, 0, 1);
  202. } catch (ArgumentNullException) {
  203. errorThrown = true;
  204. }
  205. Assert.IsTrue (errorThrown, "#C01");
  206. int[] i1 = { 1, 2, 3, 4 };
  207. {
  208. int[] compare = {1,2,3,4};
  209. Assert.AreEqual (compare[0], i1[0], "#C02");
  210. Assert.AreEqual (compare[1], i1[1], "#C03");
  211. Assert.AreEqual (compare[2], i1[2], "#C04");
  212. Assert.AreEqual (compare[3], i1[3], "#C05");
  213. }
  214. Array.Clear(i1, 3, 1);
  215. {
  216. int[] compare = {1,2,3,0};
  217. Assert.AreEqual (compare[0], i1[0], "#C06");
  218. Assert.AreEqual (compare[1], i1[1], "#C07");
  219. Assert.AreEqual (compare[2], i1[2], "#C08");
  220. Assert.AreEqual (compare[3], i1[3], "#C09");
  221. }
  222. Array.Clear(i1, 1, 1);
  223. {
  224. int[] compare = {1,0,3,0};
  225. Assert.AreEqual (compare[0], i1[0], "#C10");
  226. Assert.AreEqual (compare[1], i1[1], "#C11");
  227. Assert.AreEqual (compare[2], i1[2], "#C12");
  228. Assert.AreEqual (compare[3], i1[3], "#C13");
  229. }
  230. Array.Clear(i1, 1, 3);
  231. {
  232. int[] compare = {1,0,0,0};
  233. Assert.AreEqual (compare[0], i1[0], "#C14");
  234. Assert.AreEqual (compare[1], i1[1], "#C15");
  235. Assert.AreEqual (compare[2], i1[2], "#C16");
  236. Assert.AreEqual (compare[3], i1[3], "#C17");
  237. }
  238. string[] s1 = { "red", "green", "blue" };
  239. Array.Clear(s1, 0, 3);
  240. {
  241. string[] compare = {null, null, null};
  242. Assert.AreEqual (compare[0], s1[0], "#C18");
  243. Assert.AreEqual (compare[1], s1[1], "#C19");
  244. Assert.AreEqual (compare[2], s1[2], "#C20");
  245. }
  246. }
  247. [Test]
  248. public void TestClone() {
  249. char[] c1 = {'a', 'b', 'c'};
  250. char[] c2 = (char[])c1.Clone();
  251. Assert.AreEqual (c1[0], c2[0], "#D01");
  252. Assert.AreEqual (c1[1], c2[1], "#D02");
  253. Assert.AreEqual (c1[2], c2[2], "#D03");
  254. char[] d10 = {'a', 'b'};
  255. char[] d11 = {'a', 'c'};
  256. char[] d12 = {'b', 'c'};
  257. char[][] d1 = {d10, d11, d12};
  258. char[][] d2 = (char[][])d1.Clone();
  259. Assert.AreEqual (d1[0], d2[0], "#D04");
  260. Assert.AreEqual (d1[1], d2[1], "#D05");
  261. Assert.AreEqual (d1[2], d2[2], "#D06");
  262. d1[0][0] = 'z';
  263. Assert.AreEqual (d1[0], d2[0], "#D07");
  264. }
  265. [Test] public void TestIndexer ()
  266. {
  267. int [] a = new int [10];
  268. IList b = a;
  269. try {
  270. object c = b [-1];
  271. Assert.Fail ("IList.this [-1] should throw");
  272. } catch (IndexOutOfRangeException) {
  273. // Good
  274. } catch (Exception){
  275. Assert.Fail ("Should have thrown an IndexOutOfRangeException");
  276. }
  277. }
  278. [Test]
  279. public void TestCopy() {
  280. {
  281. bool errorThrown = false;
  282. try {
  283. Char[] c1 = {};
  284. Array.Copy(c1, null, 1);
  285. } catch (ArgumentNullException) {
  286. errorThrown = true;
  287. }
  288. Assert.IsTrue (errorThrown, "#E01");
  289. }
  290. {
  291. bool errorThrown = false;
  292. try {
  293. Char[] c1 = {};
  294. Array.Copy(null, c1, 1);
  295. } catch (ArgumentNullException) {
  296. errorThrown = true;
  297. }
  298. Assert.IsTrue (errorThrown, "#E02");
  299. }
  300. {
  301. bool errorThrown = false;
  302. try {
  303. Char[] c1 = new Char[1];
  304. Char[,] c2 = new Char[1,1];
  305. Array.Copy(c1, c2, 1);
  306. } catch (RankException) {
  307. errorThrown = true;
  308. }
  309. Assert.IsTrue (errorThrown, "#E03");
  310. }
  311. {
  312. bool errorThrown = false;
  313. try {
  314. Char[] c1 = new Char[1];
  315. string[] s1 = new String[1];
  316. Array.Copy(c1, s1, 1);
  317. } catch (ArrayTypeMismatchException) {
  318. errorThrown = true;
  319. }
  320. Assert.IsTrue (errorThrown, "#E04");
  321. }
  322. {
  323. bool errorThrown = false;
  324. try {
  325. Char[] c1 = new Char[1];
  326. Object[] o1 = new Object[1];
  327. o1[0] = "hello";
  328. Array.Copy(o1, c1, 1);
  329. } catch (InvalidCastException) {
  330. errorThrown = true;
  331. }
  332. Assert.IsTrue (errorThrown, "#E05");
  333. }
  334. {
  335. bool errorThrown = false;
  336. try {
  337. Char[] c1 = new Char[1];
  338. Char[] c2 = new Char[1];
  339. Array.Copy(c1, c2, -1);
  340. } catch (ArgumentOutOfRangeException) {
  341. errorThrown = true;
  342. }
  343. Assert.IsTrue (errorThrown, "#E06");
  344. }
  345. {
  346. bool errorThrown = false;
  347. try {
  348. Char[] c1 = new Char[1];
  349. Char[] c2 = new Char[2];
  350. Array.Copy(c1, c2, 2);
  351. } catch (ArgumentException) {
  352. errorThrown = true;
  353. }
  354. Assert.IsTrue (errorThrown, "#E07");
  355. }
  356. {
  357. bool errorThrown = false;
  358. try {
  359. Char[] c1 = new Char[1];
  360. Char[] c2 = new Char[2];
  361. Array.Copy(c2, c1, 2);
  362. } catch (ArgumentException) {
  363. errorThrown = true;
  364. }
  365. Assert.IsTrue (errorThrown, "#E08");
  366. }
  367. char[] orig = {'a', 'b', 'd', 'a'};
  368. char[] copy = new Char[4];
  369. Array.Copy(orig, copy, 4);
  370. for (int i = 0; i < orig.Length; i++) {
  371. Assert.AreEqual (orig[i], copy[i], "#E09(" + i + ")");
  372. }
  373. Array.Clear(copy, 0, copy.Length);
  374. for (int i = 0; i < orig.Length; i++) {
  375. Assert.AreEqual ((char)0, copy[i], "#E10(" + i + ")");
  376. }
  377. Array.Copy(orig, copy, 2);
  378. Assert.AreEqual (orig[0], copy[0], "#E11");
  379. Assert.AreEqual (orig[1], copy[1], "#E12");
  380. Assert.IsTrue (orig[2] != copy[2], "#E13");
  381. Assert.IsTrue (orig[3] != copy[3], "#E14");
  382. }
  383. [Test]
  384. public void TestCopy2() {
  385. {
  386. bool errorThrown = false;
  387. try {
  388. Char[] c1 = new Char[2];
  389. Char[] c2 = new Char[2];
  390. Array.Copy(c2, 1, c1, 0, 2);
  391. } catch (ArgumentException) {
  392. errorThrown = true;
  393. }
  394. Assert.IsTrue (errorThrown, "#E31");
  395. }
  396. {
  397. bool errorThrown = false;
  398. try {
  399. Char[] c1 = new Char[2];
  400. Char[] c2 = new Char[2];
  401. Array.Copy(c2, 0, c1, 1, 2);
  402. } catch (ArgumentException) {
  403. errorThrown = true;
  404. }
  405. Assert.IsTrue (errorThrown, "#E32");
  406. }
  407. char[] orig = {'a', 'b', 'd', 'a'};
  408. char[] copy = new Char[4];
  409. Array.Copy(orig, 1, copy, 1, 3);
  410. Assert.IsTrue (copy[0] != orig[0], "#E33");
  411. for (int i = 1; i < orig.Length; i++) {
  412. Assert.AreEqual (orig[i], copy[i], "#E34(" + i + ")");
  413. }
  414. Array.Clear(copy, 0, copy.Length);
  415. Array.Copy(orig, 1, copy, 0, 2);
  416. Assert.AreEqual (orig[1], copy[0], "#E35");
  417. Assert.AreEqual (orig[2], copy[1], "#E36");
  418. Assert.IsTrue (copy[2] != orig[2], "#E37");
  419. Assert.IsTrue (copy[3] != orig[3], "#E38");
  420. }
  421. [Test]
  422. [ExpectedException (typeof (InvalidCastException))]
  423. public void Copy_InvalidCast () {
  424. object[] arr1 = new object [10];
  425. Type[] arr2 = new Type [10];
  426. arr1 [0] = new object ();
  427. Array.Copy (arr1, 0, arr2, 0, 10);
  428. }
  429. [Test]
  430. public void TestCopyTo() {
  431. {
  432. bool errorThrown = false;
  433. try {
  434. Char[] c1 = new Char[2];
  435. c1.CopyTo(null, 2);
  436. } catch (ArgumentNullException) {
  437. errorThrown = true;
  438. }
  439. Assert.IsTrue (errorThrown, "#E61");
  440. }
  441. {
  442. bool errorThrown = false;
  443. try {
  444. Char[] c1 = new Char[2];
  445. Char[,] c2 = new Char[2,2];
  446. c1.CopyTo(c2, 2);
  447. } catch (ArgumentException) {
  448. errorThrown = true;
  449. }
  450. #if TARGET_JVM // This is really implementation dependent behaviour.
  451. catch (RankException) {
  452. errorThrown = true;
  453. }
  454. #endif // TARGET_JVM
  455. Assert.IsTrue (errorThrown, "#E62");
  456. }
  457. {
  458. bool errorThrown = false;
  459. try {
  460. Char[,] c1 = new Char[2,2];
  461. Char[] c2 = new Char[2];
  462. c1.CopyTo(c2, -1);
  463. } catch (RankException) {
  464. errorThrown = true;
  465. }
  466. Assert.IsTrue (errorThrown, "#E63");
  467. }
  468. {
  469. bool errorThrown = false;
  470. try {
  471. Char[,] c1 = new Char[2,2];
  472. Char[] c2 = new Char[2];
  473. c1.CopyTo(c2, 2);
  474. } catch (RankException) {
  475. errorThrown = true;
  476. }
  477. Assert.IsTrue (errorThrown, "#E64");
  478. }
  479. {
  480. bool errorThrown = false;
  481. try {
  482. Char[] c1 = new Char[2];
  483. Char[] c2 = new Char[2];
  484. c1.CopyTo(c2, -1);
  485. } catch (ArgumentOutOfRangeException) {
  486. errorThrown = true;
  487. }
  488. Assert.IsTrue (errorThrown, "#E65");
  489. }
  490. {
  491. bool errorThrown = false;
  492. try {
  493. Char[] c1 = new Char[2];
  494. Char[] c2 = new Char[2];
  495. c1.CopyTo(c2, 3);
  496. } catch (ArgumentException) {
  497. errorThrown = true;
  498. }
  499. Assert.IsTrue (errorThrown, "#E66");
  500. }
  501. {
  502. bool errorThrown = false;
  503. try {
  504. Char[] c1 = new Char[2];
  505. Char[] c2 = new Char[2];
  506. c1.CopyTo(c2, 1);
  507. } catch (ArgumentException) {
  508. errorThrown = true;
  509. }
  510. Assert.IsTrue (errorThrown, "#E67");
  511. }
  512. {
  513. bool errorThrown = false;
  514. try {
  515. String[] c1 = new String[2];
  516. // TODO: this crashes mono if there are null
  517. // values in the array.
  518. c1[1] = "hey";
  519. c1[0] = "you";
  520. Char[] c2 = new Char[2];
  521. c2[1] = 'a';
  522. c2[0] = 'z';
  523. c1.CopyTo(c2, 0);
  524. } catch (ArrayTypeMismatchException) {
  525. errorThrown = true;
  526. }
  527. Assert.IsTrue (errorThrown, "#E68");
  528. }
  529. Char[] orig = {'a', 'b', 'c', 'd'};
  530. Char[] copy = new Char[10];
  531. Array.Clear(copy, 0, copy.Length);
  532. orig.CopyTo(copy, 3);
  533. Assert.AreEqual ((char)0, copy[0], "#E69");
  534. Assert.AreEqual ((char)0, copy[1], "#E70");
  535. Assert.AreEqual ((char)0, copy[2], "#E71");
  536. Assert.AreEqual (orig[0], copy[3], "#E72");
  537. Assert.AreEqual (orig[1], copy[4], "#E73");
  538. Assert.AreEqual (orig[2], copy[5], "#E74");
  539. Assert.AreEqual (orig[3], copy[6], "#E75");
  540. Assert.AreEqual ((char)0, copy[7], "#E76");
  541. Assert.AreEqual ((char)0, copy[8], "#E77");
  542. Assert.AreEqual ((char)0, copy[9], "#E78");
  543. {
  544. // The following is valid and must not throw an exception.
  545. bool errorThrown = false;
  546. try {
  547. int[] src = new int [0];
  548. int[] dest = new int [0];
  549. src.CopyTo (dest, 0);
  550. } catch (ArgumentException) {
  551. errorThrown = true;
  552. }
  553. Assert.IsTrue (!errorThrown, "#E79");
  554. }
  555. {
  556. // bug #38812
  557. bool errorThrown = false;
  558. try {
  559. CClass[] src = new CClass [] { new CClass () };
  560. BClass[] dest = new BClass [1];
  561. src.CopyTo (dest, 0);
  562. } catch (ArrayTypeMismatchException) {
  563. errorThrown = true;
  564. }
  565. Assert.IsTrue (errorThrown, "#E80");
  566. }
  567. }
  568. [Test]
  569. public void TestCreateInstance() {
  570. {
  571. bool errorThrown = false;
  572. try {
  573. Array.CreateInstance(null, 12);
  574. } catch (ArgumentNullException) {
  575. errorThrown = true;
  576. }
  577. Assert.IsTrue (errorThrown, "#F01");
  578. }
  579. {
  580. bool errorThrown = false;
  581. try {
  582. Array.CreateInstance(Type.GetType("System.Char"), -3);
  583. } catch (ArgumentOutOfRangeException) {
  584. errorThrown = true;
  585. }
  586. Assert.IsTrue (errorThrown, "#F02");
  587. }
  588. {
  589. bool errorThrown = false;
  590. try {
  591. Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
  592. } catch (ArgumentNullException) {
  593. errorThrown = true;
  594. }
  595. Assert.IsTrue (errorThrown, "#F03a");
  596. }
  597. #if NET_1_1
  598. {
  599. bool errorThrown = false;
  600. try {
  601. Array.CreateInstance(Type.GetType("System.Char"), (int [])null);
  602. } catch (ArgumentNullException) {
  603. errorThrown = true;
  604. }
  605. Assert.IsTrue (errorThrown, "#F03b");
  606. }
  607. #endif
  608. #if !TARGET_JVM // Arrays lower bounds are not supported for TARGET_JVM
  609. {
  610. bool errorThrown = false;
  611. try {
  612. Array.CreateInstance(Type.GetType("System.Char"), null, null);
  613. } catch (ArgumentNullException) {
  614. errorThrown = true;
  615. }
  616. Assert.IsTrue (errorThrown, "#F04");
  617. }
  618. #endif // TARGET_JVM
  619. {
  620. bool errorThrown = false;
  621. try {
  622. int[] lengths = new int [0];
  623. Array.CreateInstance(Type.GetType("System.Char"), lengths);
  624. } catch (ArgumentException) {
  625. errorThrown = true;
  626. }
  627. Assert.IsTrue (errorThrown, "#F05");
  628. }
  629. #if !TARGET_JVM // CreateInstance with lower bounds not supported for TARGET_JVM
  630. {
  631. bool errorThrown = false;
  632. try {
  633. int[] lengths = new int [1];
  634. int[] bounds = new int [2];
  635. Array.CreateInstance(Type.GetType("System.Char"), lengths, bounds);
  636. errorThrown = true;
  637. } catch (ArgumentException) {
  638. errorThrown = true;
  639. }
  640. Assert.IsTrue (errorThrown, "#F06");
  641. }
  642. char[] c1 = (char[])Array.CreateInstance(Type.GetType("System.Char"), 12);
  643. Assert.AreEqual (12, c1.Length, "#F07");
  644. Array c2 = Array.CreateInstance(Type.GetType("System.Char"), 12, 5);
  645. Assert.AreEqual (2, c2.Rank, "#F08");
  646. Assert.AreEqual (60, c2.Length, "#F09");
  647. {
  648. int[] lengths = { 3 };
  649. int[] bounds = { 5 };
  650. int[] src = { 512, 718, 912 };
  651. Array array = Array.CreateInstance(typeof(int), lengths, bounds);
  652. Assert.AreEqual (3, array.Length, "#F10");
  653. Assert.AreEqual (5, array.GetLowerBound(0), "#F11");
  654. Assert.AreEqual (7, array.GetUpperBound(0), "#F12");
  655. src.CopyTo (array, 5);
  656. for (int i = 0; i < src.Length; i++)
  657. Assert.AreEqual (src[i], array.GetValue(i+5), "#F13(" + i + ")");
  658. }
  659. // Test that a 1 dimensional array with 0 lower bound is the
  660. // same as an szarray
  661. Type szarrayType = new int [10].GetType ();
  662. Assert.IsTrue (szarrayType == (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {0})).GetType ());
  663. Assert.IsTrue (szarrayType != (Array.CreateInstance (typeof (int), new int[] {1}, new int[] {1})).GetType ());
  664. #endif // TARGET_JVM
  665. }
  666. [Test]
  667. [ExpectedException (typeof (ArgumentNullException))]
  668. public void TestCreateInstance2 ()
  669. {
  670. Array.CreateInstance (typeof (Int32), (int[])null);
  671. }
  672. [Test]
  673. #if NET_2_0
  674. [ExpectedException (typeof (ArgumentNullException))]
  675. #else
  676. [ExpectedException (typeof (NullReferenceException))]
  677. #endif
  678. public void TestCreateInstance2b ()
  679. {
  680. Array.CreateInstance (typeof (Int32), (long[])null);
  681. }
  682. [Test]
  683. public void TestGetEnumerator() {
  684. String[] s1 = {"this", "is", "a", "test"};
  685. IEnumerator en = s1.GetEnumerator ();
  686. Assert.IsNotNull (en, "#G01");
  687. Assert.IsTrue (en.MoveNext (), "#G02");
  688. Assert.AreEqual ("this", en.Current, "#G03");
  689. Assert.IsTrue (en.MoveNext (), "#G04");
  690. Assert.AreEqual ("is", en.Current, "#G05");
  691. Assert.IsTrue (en.MoveNext (), "#G06");
  692. Assert.AreEqual ("a", en.Current, "#G07");
  693. Assert.IsTrue (en.MoveNext (), "#G08");
  694. Assert.AreEqual ("test", en.Current, "#G09");
  695. Assert.IsTrue (!en.MoveNext (), "#G10");
  696. en.Reset ();
  697. Assert.IsTrue (en.MoveNext (), "#G11");
  698. Assert.AreEqual ("this", en.Current, "#G12");
  699. // mutation does not invalidate array enumerator!
  700. s1.SetValue ("change", 1);
  701. Assert.IsTrue (en.MoveNext (), "#G13");
  702. Assert.AreEqual ("change", en.Current, "#G14");
  703. }
  704. [Test]
  705. public void TestGetEnumeratorMultipleDimension() {
  706. String[,] s1 = {{"this", "is"}, {"a", "test"}};
  707. IEnumerator en = s1.GetEnumerator ();
  708. Assert.IsNotNull (en, "#AA01");
  709. Assert.IsTrue (en.MoveNext (), "#AA02");
  710. Assert.AreEqual ("this", en.Current, "#AA03");
  711. Assert.IsTrue (en.MoveNext (), "#AA04");
  712. Assert.AreEqual ("is", en.Current, "#AA05");
  713. Assert.IsTrue (en.MoveNext (), "#AA06");
  714. Assert.AreEqual ("a", en.Current, "#AA07");
  715. Assert.IsTrue (en.MoveNext (), "#AA08");
  716. Assert.AreEqual ("test", en.Current, "#AA09");
  717. Assert.IsTrue (!en.MoveNext (), "#AA10");
  718. en.Reset ();
  719. Assert.IsTrue (en.MoveNext (), "#AA11");
  720. Assert.AreEqual ("this", en.Current, "#AA12");
  721. int[] idxs = {0,1};
  722. // mutation does not invalidate array enumerator!
  723. s1.SetValue ("change", idxs);
  724. Assert.IsTrue (en.MoveNext (), "#AA13");
  725. Assert.AreEqual ("change", en.Current, "#AA14");
  726. }
  727. [Test]
  728. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  729. public void TestGetEnumeratorNonZeroLowerBounds() {
  730. int[] myLengthsArray = new int[2] { 3, 5 };
  731. int[] myBoundsArray = new int[2] { 2, 3 };
  732. Array myArray=Array.CreateInstance( typeof(String), myLengthsArray, myBoundsArray );
  733. for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )
  734. for ( int j = myArray.GetLowerBound(1); j <= myArray.GetUpperBound(1); j++ ) {
  735. int[] myIndicesArray = new int[2] { i, j };
  736. myArray.SetValue( Convert.ToString(i) + j, myIndicesArray );
  737. }
  738. IEnumerator en = myArray.GetEnumerator ();
  739. Assert.IsNotNull (en, "#AB01");
  740. // check the first couple of values
  741. Assert.IsTrue (en.MoveNext (), "#AB02");
  742. Assert.AreEqual ("23", en.Current, "#AB03");
  743. Assert.IsTrue (en.MoveNext (), "#AB04");
  744. Assert.AreEqual ("24", en.Current, "#AB05");
  745. // then check the last element's value
  746. string lastElement;
  747. do {
  748. lastElement = (string)en.Current;
  749. } while (en.MoveNext());
  750. Assert.AreEqual ("47", lastElement, "#AB06");
  751. }
  752. [Test]
  753. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  754. public void TestIList_Add () {
  755. int[] myLengthsArray = new int[2] { 3, 5 };
  756. int[] myBoundsArray = new int[2] { 2, 3 };
  757. Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
  758. try {
  759. ((IList)myArray).Add ("can not");
  760. Assert.Fail ("IList.Add should throw");
  761. }
  762. catch (NotSupportedException) {
  763. return;
  764. }
  765. catch (Exception) {
  766. Assert.Fail ("IList.Add threw wrong exception type");
  767. }
  768. Assert.Fail ("IList.Add shouldn't get this far");
  769. }
  770. [Test]
  771. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  772. public void TestIList_Insert () {
  773. int[] myLengthsArray = new int[2] { 3, 5 };
  774. int[] myBoundsArray = new int[2] { 2, 3 };
  775. Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
  776. try {
  777. ((IList)myArray).Insert (0, "can not");
  778. Assert.Fail ("IList.Insert should throw");
  779. }
  780. catch (NotSupportedException) {
  781. return;
  782. }
  783. catch (Exception) {
  784. Assert.Fail ("IList.Insert threw wrong exception type");
  785. }
  786. Assert.Fail ("IList.Insert shouldn't get this far");
  787. }
  788. [Test]
  789. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  790. public void TestIList_Remove () {
  791. int[] myLengthsArray = new int[2] { 3, 5 };
  792. int[] myBoundsArray = new int[2] { 2, 3 };
  793. Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
  794. try {
  795. ((IList)myArray).Remove ("can not");
  796. Assert.Fail ("IList.Remove should throw");
  797. }
  798. catch (NotSupportedException) {
  799. return;
  800. }
  801. catch (Exception) {
  802. Assert.Fail ("IList.Remove threw wrong exception type");
  803. }
  804. Assert.Fail ("IList.Remove shouldn't get this far");
  805. }
  806. [Test]
  807. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  808. public void TestIList_RemoveAt () {
  809. int[] myLengthsArray = new int[2] { 3, 5 };
  810. int[] myBoundsArray = new int[2] { 2, 3 };
  811. Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
  812. try {
  813. ((IList)myArray).RemoveAt (0);
  814. Assert.Fail ("IList.RemoveAt should throw");
  815. }
  816. catch (NotSupportedException) {
  817. return;
  818. }
  819. catch (Exception) {
  820. Assert.Fail ("IList.RemoveAt threw wrong exception type");
  821. }
  822. Assert.Fail ("IList.RemoveAt shouldn't get this far");
  823. }
  824. [Test]
  825. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  826. public void TestIList_Contains () {
  827. int[] myLengthsArray = new int[2] { 3, 5 };
  828. int[] myBoundsArray = new int[2] { 2, 3 };
  829. Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
  830. try {
  831. bool b = ((IList)myArray).Contains ("23");
  832. Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
  833. }
  834. catch (RankException) {
  835. int[] iArr = new int[3] { 1, 2, 3};
  836. // check the first and last items
  837. Assert.IsTrue (((IList)iArr).Contains (1), "AC01");
  838. Assert.IsTrue (((IList)iArr).Contains (3), "AC02");
  839. // and one that is definately not there
  840. Assert.IsTrue (!((IList)iArr).Contains (42), "AC03");
  841. return;
  842. }
  843. Assert.Fail ("Should not get here");
  844. }
  845. [Test]
  846. [Category ("TargetJvmNotSupported")] // Arrays lower bounds are not supported for TARGET_JVM
  847. public void TestIList_IndexOf () {
  848. int[] myLengthsArray = new int[2] { 3, 5 };
  849. int[] myBoundsArray = new int[2] { 2, 3 };
  850. Array myArray=Array.CreateInstance ( typeof(String), myLengthsArray, myBoundsArray );
  851. try {
  852. bool b = ((IList)myArray).Contains ("23");
  853. Assert.Fail ("IList.Contains should throw with multi-dimensional arrays");
  854. }
  855. catch (RankException) {
  856. int[] iArr = new int[3] { 1, 2, 3};
  857. // check the first and last items
  858. Assert.AreEqual (0, ((IList)iArr).IndexOf (1), "AD01");
  859. Assert.AreEqual (2, ((IList)iArr).IndexOf (3), "AD02");
  860. // and one that is definately not there
  861. Assert.AreEqual (-1, ((IList)iArr).IndexOf (42), "AD03");
  862. }
  863. catch (Exception e) {
  864. Assert.Fail ("Unexpected exception: " + e.ToString());
  865. }
  866. // check that wierd case whem lowerbound is Int32.MinValue,
  867. // so that IndexOf() needs to return Int32.MaxValue when it cannot find the object
  868. int[] myLengthArray = new int[1] { 3 };
  869. int[] myBoundArray = new int[1] { Int32.MinValue };
  870. Array myExtremeArray=Array.CreateInstance ( typeof(String), myLengthArray, myBoundArray );
  871. Assert.AreEqual (Int32.MaxValue, ((IList)myExtremeArray).IndexOf (42), "AD04");
  872. }
  873. [Test]
  874. public void TestGetLength() {
  875. {
  876. bool errorThrown = false;
  877. try {
  878. char[] c1 = {'a', 'b', 'c'};
  879. c1.GetLength(-1);
  880. } catch (IndexOutOfRangeException) {
  881. errorThrown = true;
  882. }
  883. Assert.IsTrue (errorThrown, "#H01");
  884. }
  885. {
  886. bool errorThrown = false;
  887. try {
  888. char[] c1 = {'a', 'b', 'c'};
  889. c1.GetLength(1);
  890. } catch (IndexOutOfRangeException) {
  891. errorThrown = true;
  892. }
  893. Assert.IsTrue (errorThrown, "#H02");
  894. }
  895. char[] c2 = new Char[5];
  896. Assert.AreEqual (5, c2.GetLength(0), "#H03");
  897. char[,] c3 = new Char[6,7];
  898. Assert.AreEqual (6, c3.GetLength(0), "#H04");
  899. Assert.AreEqual (7, c3.GetLength(1), "#H05");
  900. }
  901. [Test]
  902. public void TestGetLowerBound() {
  903. {
  904. bool errorThrown = false;
  905. try {
  906. char[] c = {'a', 'b', 'c'};
  907. c.GetLowerBound(-1);
  908. } catch (IndexOutOfRangeException) {
  909. errorThrown = true;
  910. }
  911. Assert.IsTrue (errorThrown, "#H31");
  912. }
  913. {
  914. bool errorThrown = false;
  915. try {
  916. char[] c = {'a', 'b', 'c'};
  917. c.GetLowerBound(1);
  918. } catch (IndexOutOfRangeException) {
  919. errorThrown = true;
  920. }
  921. Assert.IsTrue (errorThrown, "#H32");
  922. }
  923. char[] c1 = new Char[5];
  924. Assert.AreEqual (0, c1.GetLowerBound(0), "#H33");
  925. char[,] c2 = new Char[4,4];
  926. Assert.AreEqual (0, c2.GetLowerBound(0), "#H34");
  927. Assert.AreEqual (0, c2.GetLowerBound(1), "#H35");
  928. }
  929. [Test]
  930. public void TestGetUpperBound() {
  931. {
  932. bool errorThrown = false;
  933. try {
  934. char[] c = {'a', 'b', 'c'};
  935. c.GetUpperBound(-1);
  936. } catch (IndexOutOfRangeException) {
  937. errorThrown = true;
  938. }
  939. Assert.IsTrue (errorThrown, "#H61");
  940. }
  941. {
  942. bool errorThrown = false;
  943. try {
  944. char[] c = {'a', 'b', 'c'};
  945. c.GetUpperBound(1);
  946. } catch (IndexOutOfRangeException) {
  947. errorThrown = true;
  948. }
  949. Assert.IsTrue (errorThrown, "#H62");
  950. }
  951. char[] c1 = new Char[5];
  952. Assert.AreEqual (4, c1.GetUpperBound(0), "#H63");
  953. char[,] c2 = new Char[4,6];
  954. Assert.AreEqual (3, c2.GetUpperBound(0), "#H64");
  955. Assert.AreEqual (5, c2.GetUpperBound(1), "#H65");
  956. }
  957. [Test]
  958. public void TestGetValue1() {
  959. {
  960. bool errorThrown = false;
  961. try {
  962. char[,] c = new Char[2,2];
  963. c.GetValue(1);
  964. } catch (ArgumentException) {
  965. errorThrown = true;
  966. }
  967. Assert.IsTrue (errorThrown, "#I01");
  968. }
  969. {
  970. bool errorThrown = false;
  971. try {
  972. char[] c = {'a', 'b', 'c'};
  973. c.GetValue(-1);
  974. } catch (IndexOutOfRangeException) {
  975. errorThrown = true;
  976. }
  977. Assert.IsTrue (errorThrown, "#I02");
  978. }
  979. {
  980. bool errorThrown = false;
  981. try {
  982. char[] c = {'a', 'b', 'c'};
  983. c.GetValue(4);
  984. } catch (IndexOutOfRangeException) {
  985. errorThrown = true;
  986. }
  987. Assert.IsTrue (errorThrown, "#I03");
  988. }
  989. char[] c1 = {'a', 'b', 'c', 'd'};
  990. for (int i = 0; i < c1.Length; i++) {
  991. Assert.AreEqual (c1[i], c1.GetValue(i), "#I04(" + i + ")");
  992. }
  993. }
  994. [Test]
  995. public void TestGetValue2() {
  996. {
  997. bool errorThrown = false;
  998. try {
  999. char[] c = new Char[2];
  1000. c.GetValue(1,1);
  1001. } catch (ArgumentException) {
  1002. errorThrown = true;
  1003. }
  1004. Assert.IsTrue (errorThrown, "#I21");
  1005. }
  1006. {
  1007. bool errorThrown = false;
  1008. try {
  1009. char[,] c = new Char[2,2];
  1010. c.GetValue(-1, 1);
  1011. } catch (IndexOutOfRangeException) {
  1012. errorThrown = true;
  1013. }
  1014. Assert.IsTrue (errorThrown, "#I22");
  1015. }
  1016. {
  1017. bool errorThrown = false;
  1018. try {
  1019. char[,] c = new Char[2,2];
  1020. c.GetValue(4,1);
  1021. } catch (IndexOutOfRangeException) {
  1022. errorThrown = true;
  1023. }
  1024. Assert.IsTrue (errorThrown, "#I23");
  1025. }
  1026. char[,] c1 = new Char[4,6];
  1027. for (int i = 0; i < 24; i++) {
  1028. int first = i / 6;
  1029. int second = i % 6;
  1030. c1[first,second] = (char)(((int)'a')+i);
  1031. }
  1032. for (int i = 0; i < c1.GetLength(0); i++) {
  1033. for (int j = 0; j < c1.GetLength(1); j++) {
  1034. Assert.AreEqual (c1[i, j], c1.GetValue(i, j), "#I24(" + i + "," + j + ")");
  1035. }
  1036. }
  1037. }
  1038. [Test]
  1039. public void TestGetValue3() {
  1040. {
  1041. bool errorThrown = false;
  1042. try {
  1043. char[] c = new Char[2];
  1044. c.GetValue(1,1,1);
  1045. } catch (ArgumentException) {
  1046. errorThrown = true;
  1047. }
  1048. Assert.IsTrue (errorThrown, "#I41");
  1049. }
  1050. {
  1051. bool errorThrown = false;
  1052. try {
  1053. char[,,] c = new Char[2,2,2];
  1054. c.GetValue(-1, 1, 1);
  1055. } catch (IndexOutOfRangeException) {
  1056. errorThrown = true;
  1057. }
  1058. Assert.IsTrue (errorThrown, "#I42");
  1059. }
  1060. {
  1061. bool errorThrown = false;
  1062. try {
  1063. char[,,] c = new Char[2,2,2];
  1064. c.GetValue(4,1,1);
  1065. } catch (IndexOutOfRangeException) {
  1066. errorThrown = true;
  1067. }
  1068. Assert.IsTrue (errorThrown, "#I43");
  1069. }
  1070. char[,,] c1 = new Char[4,2,3];
  1071. for (int i = 0; i < 24; i++) {
  1072. int first = i / 6;
  1073. int remains = i % 6;
  1074. int second = remains / 3;
  1075. int third = remains % 3;
  1076. c1[first,second, third] = (char)(((int)'a')+i);
  1077. }
  1078. for (int i = 0; i < c1.GetLength(0); i++) {
  1079. for (int j = 0; j < c1.GetLength(1); j++) {
  1080. for (int k = 0; k < c1.GetLength(2); k++) {
  1081. Assert.AreEqual (c1[i, j, k], c1.GetValue(i, j, k), "#I44(" + i + "," + j + ")");
  1082. }
  1083. }
  1084. }
  1085. }
  1086. [Test]
  1087. #if NET_2_0
  1088. [ExpectedException (typeof (ArgumentNullException))]
  1089. #else
  1090. [ExpectedException (typeof (NullReferenceException))]
  1091. #endif
  1092. public void TestGetValueLongArray ()
  1093. {
  1094. char[] c = new Char[2];
  1095. c.GetValue((long [])null);
  1096. }
  1097. [Test]
  1098. public void TestGetValueN() {
  1099. {
  1100. bool errorThrown = false;
  1101. try {
  1102. char[] c = new Char[2];
  1103. c.GetValue((int [])null);
  1104. } catch (ArgumentNullException) {
  1105. errorThrown = true;
  1106. }
  1107. Assert.IsTrue (errorThrown, "#I61a");
  1108. }
  1109. {
  1110. bool errorThrown = false;
  1111. try {
  1112. char[] c = new Char[2];
  1113. int[] coords = {1, 1};
  1114. c.GetValue(coords);
  1115. } catch (ArgumentException) {
  1116. errorThrown = true;
  1117. }
  1118. Assert.IsTrue (errorThrown, "#I62");
  1119. }
  1120. {
  1121. bool errorThrown = false;
  1122. try {
  1123. char[,] c = new Char[2,2];
  1124. int[] coords = {-1, 1};
  1125. c.GetValue(coords);
  1126. } catch (IndexOutOfRangeException) {
  1127. errorThrown = true;
  1128. }
  1129. Assert.IsTrue (errorThrown, "#I63");
  1130. }
  1131. {
  1132. bool errorThrown = false;
  1133. try {
  1134. char[,] c = new Char[2,2];
  1135. int[] coords = {4, 1};
  1136. c.GetValue(coords);
  1137. } catch (IndexOutOfRangeException) {
  1138. errorThrown = true;
  1139. }
  1140. Assert.IsTrue (errorThrown, "#I64");
  1141. }
  1142. char[,] c1 = new Char[4,6];
  1143. for (int i = 0; i < 24; i++) {
  1144. int first = i / 6;
  1145. int second = i % 6;
  1146. c1[first,second] = (char)(((int)'a')+i);
  1147. }
  1148. for (int i = 0; i < c1.GetLength(0); i++) {
  1149. for (int j = 0; j < c1.GetLength(1); j++) {
  1150. int[] coords = {i, j};
  1151. Assert.AreEqual (c1[i, j], c1.GetValue(coords), "#I65(" + i + "," + j + ")");
  1152. }
  1153. }
  1154. }
  1155. [Test]
  1156. public void TestIndexOf1() {
  1157. {
  1158. bool errorThrown = false;
  1159. try {
  1160. Array.IndexOf(null, "huh?");
  1161. } catch (ArgumentNullException) {
  1162. errorThrown = true;
  1163. }
  1164. Assert.IsTrue (errorThrown, "#J01");
  1165. }
  1166. {
  1167. bool errorThrown = false;
  1168. try {
  1169. char[,] c = new Char[2,2];
  1170. Array.IndexOf(c, "huh?");
  1171. } catch (RankException) {
  1172. errorThrown = true;
  1173. }
  1174. Assert.IsTrue (errorThrown, "#J02");
  1175. }
  1176. String[] s1 = {"this", "is", "a", "test"};
  1177. Assert.AreEqual (-1, Array.IndexOf(s1, null), "#J03");
  1178. Assert.AreEqual (-1, Array.IndexOf(s1, "nothing"), "#J04");
  1179. Assert.AreEqual (0, Array.IndexOf(s1, "this"), "#J05");
  1180. Assert.AreEqual (3, Array.IndexOf(s1, "test"), "#J06");
  1181. }
  1182. [Test]
  1183. public void TestIndexOf2() {
  1184. {
  1185. bool errorThrown = false;
  1186. try {
  1187. Array.IndexOf(null, "huh?", 0);
  1188. } catch (ArgumentNullException) {
  1189. errorThrown = true;
  1190. }
  1191. Assert.IsTrue (errorThrown, "#J21");
  1192. }
  1193. {
  1194. bool errorThrown = false;
  1195. try {
  1196. char[,] c = new Char[2,2];
  1197. Array.IndexOf(c, "huh?", 0);
  1198. } catch (RankException) {
  1199. errorThrown = true;
  1200. }
  1201. Assert.IsTrue (errorThrown, "#J22");
  1202. }
  1203. {
  1204. bool errorThrown = false;
  1205. try {
  1206. char[] c = new Char[2];
  1207. Array.IndexOf(c, "huh?", 3);
  1208. } catch (ArgumentOutOfRangeException) {
  1209. errorThrown = true;
  1210. }
  1211. Assert.IsTrue (errorThrown, "#J23");
  1212. }
  1213. String[] s1 = {"this", "is", "really", "a", "test"};
  1214. Assert.AreEqual (-1, Array.IndexOf(s1, null, 1), "#J24");
  1215. Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1), "#J25");
  1216. Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1), "#J26");
  1217. Assert.AreEqual (1, Array.IndexOf(s1, "is", 1), "#J27");
  1218. Assert.AreEqual (4, Array.IndexOf(s1, "test", 1), "#J28");
  1219. }
  1220. [Test]
  1221. public void TestIndexOf3() {
  1222. {
  1223. bool errorThrown = false;
  1224. try {
  1225. Array.IndexOf(null, "huh?", 0, 1);
  1226. } catch (ArgumentNullException) {
  1227. errorThrown = true;
  1228. }
  1229. Assert.IsTrue (errorThrown, "#J41");
  1230. }
  1231. {
  1232. bool errorThrown = false;
  1233. try {
  1234. char[,] c = new Char[2,2];
  1235. Array.IndexOf(c, "huh?", 0, 1);
  1236. } catch (RankException) {
  1237. errorThrown = true;
  1238. }
  1239. Assert.IsTrue (errorThrown, "#J42");
  1240. }
  1241. {
  1242. bool errorThrown = false;
  1243. try {
  1244. char[] c = new Char[2];
  1245. Array.IndexOf(c, "huh?", 3, 1);
  1246. } catch (ArgumentOutOfRangeException) {
  1247. errorThrown = true;
  1248. }
  1249. Assert.IsTrue (errorThrown, "#J43");
  1250. }
  1251. {
  1252. bool errorThrown = false;
  1253. try {
  1254. char[] c = new Char[2];
  1255. Array.IndexOf(c, "huh?", 0, 5);
  1256. } catch (ArgumentOutOfRangeException) {
  1257. errorThrown = true;
  1258. }
  1259. Assert.IsTrue (errorThrown, "#J44");
  1260. }
  1261. String[] s1 = {"this", "is", "really", "a", "test"};
  1262. Assert.AreEqual (-1, Array.IndexOf(s1, null, 1, 3), "#J45");
  1263. Assert.AreEqual (-1, Array.IndexOf(s1, "nothing", 1, 3), "#J46");
  1264. Assert.AreEqual (-1, Array.IndexOf(s1, "this", 1, 3), "#J47");
  1265. Assert.AreEqual (1, Array.IndexOf(s1, "is", 1, 3), "#J48");
  1266. Assert.AreEqual (-1, Array.IndexOf(s1, "test", 1, 3), "#J49");
  1267. Assert.AreEqual (3, Array.IndexOf(s1, "a", 1, 3), "#J50");
  1268. }
  1269. [Test]
  1270. public void TestIndexOf_CustomEqual ()
  1271. {
  1272. DataEqual[] test = new DataEqual [] { new DataEqual () };
  1273. Assert.AreEqual (0, Array.IndexOf (test, "asdfas", 0));
  1274. IList array = (IList)test;
  1275. Assert.AreEqual (0, array.IndexOf ("asdfas"));
  1276. }
  1277. [Test]
  1278. public void TestLastIndexOf1() {
  1279. {
  1280. bool errorThrown = false;
  1281. try {
  1282. Array.LastIndexOf(null, "huh?");
  1283. } catch (ArgumentNullException) {
  1284. errorThrown = true;
  1285. }
  1286. Assert.IsTrue (errorThrown, "#K01");
  1287. }
  1288. {
  1289. bool errorThrown = false;
  1290. try {
  1291. char[,] c = new Char[2,2];
  1292. Array.LastIndexOf(c, "huh?");
  1293. } catch (RankException) {
  1294. errorThrown = true;
  1295. }
  1296. Assert.IsTrue (errorThrown, "#K02");
  1297. }
  1298. String[] s1 = {"this", "is", "a", "a", "test"};
  1299. Assert.AreEqual (-1, Array.LastIndexOf(s1, null), "#K03");
  1300. Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing"), "#K04");
  1301. Assert.AreEqual (0, Array.LastIndexOf(s1, "this"), "#K05");
  1302. Assert.AreEqual (4, Array.LastIndexOf(s1, "test"), "#K06");
  1303. Assert.AreEqual (3, Array.LastIndexOf(s1, "a"), "#K07");
  1304. Assert.AreEqual (-1, Array.LastIndexOf (new String [0], "foo"));
  1305. }
  1306. [Test]
  1307. public void TestLastIndexOf2() {
  1308. {
  1309. bool errorThrown = false;
  1310. try {
  1311. Array.LastIndexOf(null, "huh?", 0);
  1312. } catch (ArgumentNullException) {
  1313. errorThrown = true;
  1314. }
  1315. Assert.IsTrue (errorThrown, "#K21");
  1316. }
  1317. {
  1318. bool errorThrown = false;
  1319. try {
  1320. char[,] c = new Char[2,2];
  1321. Array.LastIndexOf(c, "huh?", 0);
  1322. } catch (RankException) {
  1323. errorThrown = true;
  1324. }
  1325. Assert.IsTrue (errorThrown, "#K22");
  1326. }
  1327. {
  1328. bool errorThrown = false;
  1329. try {
  1330. char[] c = new Char[2];
  1331. Array.LastIndexOf(c, "huh?", 3);
  1332. } catch (ArgumentOutOfRangeException) {
  1333. errorThrown = true;
  1334. }
  1335. Assert.IsTrue (errorThrown, "#K23");
  1336. }
  1337. String[] s1 = {"this", "is", "really", "a", "test"};
  1338. Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3), "#K24");
  1339. Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3), "#K25");
  1340. Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3), "#K26");
  1341. Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3), "#K27");
  1342. Assert.AreEqual (0, Array.LastIndexOf(s1, "this", 3), "#K28");
  1343. }
  1344. [Test]
  1345. public void TestLastIndexOf3() {
  1346. {
  1347. bool errorThrown = false;
  1348. try {
  1349. Array.LastIndexOf(null, "huh?", 0, 1);
  1350. } catch (ArgumentNullException) {
  1351. errorThrown = true;
  1352. }
  1353. Assert.IsTrue (errorThrown, "#K41");
  1354. }
  1355. {
  1356. bool errorThrown = false;
  1357. try {
  1358. char[,] c = new Char[2,2];
  1359. Array.LastIndexOf(c, "huh?", 0, 1);
  1360. } catch (RankException) {
  1361. errorThrown = true;
  1362. }
  1363. Assert.IsTrue (errorThrown, "#K42");
  1364. }
  1365. {
  1366. bool errorThrown = false;
  1367. try {
  1368. char[] c = new Char[2];
  1369. Array.LastIndexOf(c, "huh?", 3, 1);
  1370. } catch (ArgumentOutOfRangeException) {
  1371. errorThrown = true;
  1372. }
  1373. Assert.IsTrue (errorThrown, "#K43");
  1374. }
  1375. {
  1376. bool errorThrown = false;
  1377. try {
  1378. char[] c = new Char[2];
  1379. Array.LastIndexOf(c, "huh?", 0, 5);
  1380. } catch (ArgumentOutOfRangeException) {
  1381. errorThrown = true;
  1382. }
  1383. Assert.IsTrue (errorThrown, "#K44");
  1384. }
  1385. String[] s1 = {"this", "is", "really", "a", "test"};
  1386. Assert.AreEqual (-1, Array.LastIndexOf(s1, null, 3, 3), "#K45");
  1387. Assert.AreEqual (-1, Array.LastIndexOf(s1, "nothing", 3, 3), "#K46");
  1388. Assert.AreEqual (-1, Array.LastIndexOf(s1, "this", 3, 3), "#K47");
  1389. Assert.AreEqual (1, Array.LastIndexOf(s1, "is", 3, 3), "#K48");
  1390. Assert.AreEqual (-1, Array.LastIndexOf(s1, "test", 3, 3), "#K49");
  1391. Assert.AreEqual (3, Array.LastIndexOf(s1, "a", 3, 3), "#K50");
  1392. }
  1393. [Test]
  1394. public void TestLastIndexOf4 ()
  1395. {
  1396. short [] a = new short [] { 19, 238, 317, 6, 565, 0, -52, 60, -563, 753, 238, 238};
  1397. try {
  1398. Array.LastIndexOf (a, (object)16, -1);
  1399. NUnit.Framework.Assert.Fail ("#1");
  1400. } catch (ArgumentOutOfRangeException) { }
  1401. #if NET_2_0
  1402. try {
  1403. Array.LastIndexOf<short> (a, 16, -1);
  1404. NUnit.Framework.Assert.Fail ("#2");
  1405. } catch (ArgumentOutOfRangeException) { }
  1406. #endif
  1407. }
  1408. [Test]
  1409. public void TestLastIndexOf5 ()
  1410. {
  1411. char [] a = new char [] {'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a', 'j', 'i', 'h'};
  1412. string s;
  1413. int retval;
  1414. bool error = false;
  1415. for (int i = a.Length - 1; i >= 0 ; i--) {
  1416. s = i.ToString ();
  1417. retval = Array.LastIndexOf(a, a [i], i, i + 1);
  1418. if (retval != i)
  1419. error = true;
  1420. }
  1421. Assert.IsTrue (!error);
  1422. }
  1423. [Test]
  1424. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1425. public void LastIndexOf_StartIndexOverflow ()
  1426. {
  1427. // legal - no exception
  1428. byte[] array = new byte [16];
  1429. Array.LastIndexOf (array, this, Int32.MaxValue, 1);
  1430. }
  1431. [Test]
  1432. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  1433. public void LastIndexOf_CountOverflow ()
  1434. {
  1435. // legal - no exception
  1436. byte[] array = new byte [16];
  1437. Array.LastIndexOf (array, this, 1, Int32.MaxValue);
  1438. }
  1439. [Test]
  1440. public void LastIndexOf_0LengthArray ()
  1441. {
  1442. Array array = Array.CreateInstance (typeof (char), 0);
  1443. int idx = Array.LastIndexOf (array, (object) null, -1, 0);
  1444. Assert.IsTrue (idx == -1, "#01");
  1445. idx = Array.LastIndexOf (array, (object) null, -1, 10);
  1446. Assert.IsTrue (idx == -1, "#02");
  1447. idx = Array.LastIndexOf (array, (object) null, -100, 10);
  1448. Assert.IsTrue (idx == -1, "#02");
  1449. array = Array.CreateInstance (typeof (char), 1);
  1450. try {
  1451. Array.LastIndexOf (array, (object) null, -1, 0);
  1452. Assert.Fail ("#04");
  1453. } catch (ArgumentOutOfRangeException e) {
  1454. }
  1455. try {
  1456. Array.LastIndexOf (array, (object) null, -1, 10);
  1457. Assert.Fail ("#05");
  1458. } catch (ArgumentOutOfRangeException e) {
  1459. }
  1460. try {
  1461. Array.LastIndexOf (array, (object) null, -100, 10);
  1462. Assert.Fail ("#06");
  1463. } catch (ArgumentOutOfRangeException e) {
  1464. }
  1465. }
  1466. [Test]
  1467. public void TestReverse() {
  1468. {
  1469. bool errorThrown = false;
  1470. try {
  1471. Array.Reverse(null);
  1472. } catch (ArgumentNullException) {
  1473. errorThrown = true;
  1474. }
  1475. Assert.IsTrue (errorThrown, "#L01");
  1476. }
  1477. {
  1478. bool errorThrown = false;
  1479. try {
  1480. char[,] c = new Char[2,2];
  1481. Array.Reverse(c);
  1482. } catch (RankException) {
  1483. errorThrown = true;
  1484. }
  1485. Assert.IsTrue (errorThrown, "#L02");
  1486. }
  1487. char[] c1 = {'a', 'b', 'c', 'd'};
  1488. Array.Reverse(c1);
  1489. Assert.AreEqual ('d', c1[0], "#L03");
  1490. Assert.AreEqual ('c', c1[1], "#L04");
  1491. Assert.AreEqual ('b', c1[2], "#L05");
  1492. Assert.AreEqual ('a', c1[3], "#L06");
  1493. {
  1494. bool errorThrown = false;
  1495. try {
  1496. Array.Reverse(null, 0, 0);
  1497. } catch (ArgumentNullException) {
  1498. errorThrown = true;
  1499. }
  1500. Assert.IsTrue (errorThrown, "#L07");
  1501. }
  1502. {
  1503. bool errorThrown = false;
  1504. try {
  1505. char[,] c = new Char[2,2];
  1506. Array.Reverse(c, 0, 0);
  1507. } catch (RankException) {
  1508. errorThrown = true;
  1509. }
  1510. Assert.IsTrue (errorThrown, "#L08");
  1511. }
  1512. //{
  1513. //bool errorThrown = false;
  1514. //try {
  1515. // char[] c = new Char[2];
  1516. // Array.Reverse(c, 0, 3);
  1517. //} catch (ArgumentOutOfRangeException) {
  1518. // errorThrown = true;
  1519. //}
  1520. //Assert.IsTrue (errorThrown, "#L09");
  1521. //}
  1522. //{
  1523. //bool errorThrown = false;
  1524. //try {
  1525. // char[] c = new Char[2];
  1526. // Array.Reverse(c, 3, 0);
  1527. //} catch (ArgumentOutOfRangeException) {
  1528. // errorThrown = true;
  1529. //}
  1530. //Assert.IsTrue (errorThrown, "#L10");
  1531. //}
  1532. char[] c2 = { 'a', 'b', 'c', 'd'};
  1533. Array.Reverse(c2, 1, 2);
  1534. Assert.AreEqual ('a', c2[0], "#L11");
  1535. Assert.AreEqual ('c', c2[1], "#L12");
  1536. Assert.AreEqual ('b', c2[2], "#L13");
  1537. Assert.AreEqual ('d', c2[3], "#L14");
  1538. }
  1539. [Test]
  1540. public void TestSetValue1() {
  1541. {
  1542. bool errorThrown = false;
  1543. try {
  1544. char[,] c = new Char[2,2];
  1545. c.SetValue("buh", 1);
  1546. } catch (ArgumentException) {
  1547. errorThrown = true;
  1548. }
  1549. Assert.IsTrue (errorThrown, "#M01");
  1550. }
  1551. {
  1552. bool errorThrown = false;
  1553. try {
  1554. char[] c = {'a', 'b', 'c'};
  1555. c.SetValue("buh", -1);
  1556. } catch (IndexOutOfRangeException) {
  1557. errorThrown = true;
  1558. }
  1559. Assert.IsTrue (errorThrown, "#M02");
  1560. }
  1561. {
  1562. bool errorThrown = false;
  1563. try {
  1564. char[] c = {'a', 'b', 'c'};
  1565. c.SetValue("buh", 4);
  1566. } catch (IndexOutOfRangeException) {
  1567. errorThrown = true;
  1568. }
  1569. Assert.IsTrue (errorThrown, "#M03");
  1570. }
  1571. char[] c1 = {'a', 'b', 'c', 'd'};
  1572. char[] c2 = new char[4];
  1573. for (int i = 0; i < c1.Length; i++) {
  1574. c2.SetValue(c1[i], i);
  1575. }
  1576. for (int i = 0; i < c1.Length; i++) {
  1577. Assert.AreEqual (c1[i], c2[i], "#M04(" + i + ")");
  1578. }
  1579. int[] c3 = { 1, 2, 3 };
  1580. long[] c4 = new long [3];
  1581. for (int i = 0; i < c3.Length; i++)
  1582. c4.SetValue (c3 [i], i);
  1583. try {
  1584. c3.CopyTo (c4, 0);
  1585. } catch (Exception e) {
  1586. Assert.Fail ("c3.CopyTo(): e=" + e);
  1587. }
  1588. for (int i = 0; i < c3.Length; i++)
  1589. Assert.IsTrue (c3[i] == c4[i], "#M05(" + i + ")");
  1590. Object[] c5 = new Object [3];
  1591. long[] c6 = new long [3];
  1592. try {
  1593. c4.CopyTo (c5, 0);
  1594. } catch (Exception e) {
  1595. Assert.Fail ("c4.CopyTo(): e=" + e);
  1596. }
  1597. try {
  1598. c5.CopyTo (c6, 0);
  1599. } catch (Exception e) {
  1600. Assert.Fail ("c5.CopyTo(): e=" + e);
  1601. }
  1602. // for (int i = 0; i < c5.Length; i++)
  1603. // Assert.IsTrue (c5[i] == c6[i], "#M06(" + i + ")");
  1604. }
  1605. [Test]
  1606. public void TestSetValue2() {
  1607. {
  1608. bool errorThrown = false;
  1609. try {
  1610. char[] c = new Char[2];
  1611. c.SetValue("buh", 1,1);
  1612. } catch (ArgumentException) {
  1613. errorThrown = true;
  1614. }
  1615. Assert.IsTrue (errorThrown, "#M21");
  1616. }
  1617. {
  1618. bool errorThrown = false;
  1619. try {
  1620. char[,] c = new Char[2,2];
  1621. c.SetValue("buh", -1, 1);
  1622. } catch (IndexOutOfRangeException) {
  1623. errorThrown = true;
  1624. }
  1625. Assert.IsTrue (errorThrown, "#M22");
  1626. }
  1627. {
  1628. bool errorThrown = false;
  1629. try {
  1630. char[,] c = new Char[2,2];
  1631. c.SetValue("buh", 4,1);
  1632. } catch (IndexOutOfRangeException) {
  1633. errorThrown = true;
  1634. }
  1635. Assert.IsTrue (errorThrown, "#M23");
  1636. }
  1637. char[,] c1 = new Char[4,6];
  1638. char[,] c2 = new Char[4,6];
  1639. for (int i = 0; i < 24; i++) {
  1640. int first = i / 6;
  1641. int second = i % 6;
  1642. c1[first,second] = (char)(((int)'a')+i);
  1643. c2.SetValue(c1[first,second], first, second);
  1644. }
  1645. for (int i = 0; i < c1.GetLength(0); i++) {
  1646. for (int j = 0; j < c1.GetLength(1); j++) {
  1647. Assert.AreEqual (c1[i, j], c2[i, j], "#M24(" + i + "," + j + ")");
  1648. }
  1649. }
  1650. }
  1651. [Test]
  1652. public void TestSetValue3() {
  1653. {
  1654. bool errorThrown = false;
  1655. try {
  1656. char[] c = new Char[2];
  1657. c.SetValue("buh", 1,1,1);
  1658. } catch (ArgumentException) {
  1659. errorThrown = true;
  1660. }
  1661. Assert.IsTrue (errorThrown, "#M41");
  1662. }
  1663. {
  1664. bool errorThrown = false;
  1665. try {
  1666. char[,,] c = new Char[2,2,2];
  1667. c.SetValue("buh", -1, 1, 1);
  1668. } catch (IndexOutOfRangeException) {
  1669. errorThrown = true;
  1670. }
  1671. Assert.IsTrue (errorThrown, "#M42");
  1672. }
  1673. {
  1674. bool errorThrown = false;
  1675. try {
  1676. char[,,] c = new Char[2,2,2];
  1677. c.SetValue("buh", 4,1,1);
  1678. } catch (IndexOutOfRangeException) {
  1679. errorThrown = true;
  1680. }
  1681. Assert.IsTrue (errorThrown, "#M43");
  1682. }
  1683. char[,,] c1 = new Char[4,2,3];
  1684. char[,,] c2 = new Char[4,2,3];
  1685. for (int i = 0; i < 24; i++) {
  1686. int first = i / 6;
  1687. int remains = i % 6;
  1688. int second = remains / 3;
  1689. int third = remains % 3;
  1690. c1[first,second, third] = (char)(((int)'a')+i);
  1691. c2.SetValue(c1[first, second, third], first, second, third);
  1692. }
  1693. for (int i = 0; i < c1.GetLength(0); i++) {
  1694. for (int j = 0; j < c1.GetLength(1); j++) {
  1695. for (int k = 0; k < c1.GetLength(2); k++) {
  1696. Assert.AreEqual (c1[i, j, k], c2[i, j, k], "#M44(" + i + "," + j + " )");
  1697. }
  1698. }
  1699. }
  1700. }
  1701. [Test]
  1702. #if NET_2_0
  1703. [ExpectedException (typeof (ArgumentNullException))]
  1704. #else
  1705. [ExpectedException (typeof (NullReferenceException))]
  1706. #endif
  1707. public void TestSetValueLongArray ()
  1708. {
  1709. char[] c = new Char[2];
  1710. c.SetValue("buh", (long [])null);
  1711. }
  1712. [Test]
  1713. public void TestSetValueN() {
  1714. {
  1715. bool errorThrown = false;
  1716. try {
  1717. char[] c = new Char[2];
  1718. c.SetValue("buh", (int [])null);
  1719. } catch (ArgumentNullException) {
  1720. errorThrown = true;
  1721. }
  1722. Assert.IsTrue (errorThrown, "#M61a");
  1723. }
  1724. {
  1725. bool errorThrown = false;
  1726. try {
  1727. char[] c = new Char[2];
  1728. int[] coords = {1, 1};
  1729. c.SetValue("buh", coords);
  1730. } catch (ArgumentException) {
  1731. errorThrown = true;
  1732. }
  1733. Assert.IsTrue (errorThrown, "#M62");
  1734. }
  1735. {
  1736. bool errorThrown = false;
  1737. try {
  1738. char[,] c = new Char[2,2];
  1739. int[] coords = {-1, 1};
  1740. c.SetValue("buh", coords);
  1741. } catch (IndexOutOfRangeException) {
  1742. errorThrown = true;
  1743. }
  1744. Assert.IsTrue (errorThrown, "#M63");
  1745. }
  1746. {
  1747. bool errorThrown = false;
  1748. try {
  1749. char[,] c = new Char[2,2];
  1750. int[] coords = {4, 1};
  1751. c.SetValue("buh", coords);
  1752. } catch (IndexOutOfRangeException) {
  1753. errorThrown = true;
  1754. }
  1755. Assert.IsTrue (errorThrown, "#M64");
  1756. }
  1757. char[,] c1 = new Char[4,6];
  1758. char[,] c2 = new Char[4,6];
  1759. for (int i = 0; i < 24; i++) {
  1760. int first = i / 6;
  1761. int second = i % 6;
  1762. c1[first,second] = (char)(((int)'a')+i);
  1763. int[] coords = {first, second};
  1764. c2.SetValue(c1[first,second], coords);
  1765. }
  1766. for (int i = 0; i < c1.GetLength(0); i++) {
  1767. for (int j = 0; j < c1.GetLength(1); j++) {
  1768. Assert.AreEqual (c1[i, j], c2[i, j], "#M65(" + i + "," + j + ")");
  1769. }
  1770. }
  1771. }
  1772. [Test]
  1773. public void TestSetValue4() {
  1774. {
  1775. int[] c1 = { 1, 2, 3 };
  1776. long[] c2 = new long [3];
  1777. for (int i = 0; i < c1.Length; i++)
  1778. c2.SetValue (c1 [i], i);
  1779. for (int i = 0; i < c1.Length; i++) {
  1780. Assert.IsTrue (c1[i] == c2[i], "#M81(" + i + ")");
  1781. Assert.AreEqual (typeof (long), c2[i].GetType (), "#M82(" + i + ")");
  1782. }
  1783. }
  1784. {
  1785. long[] c1 = { 1, 2, 3 };
  1786. int[] c2 = new int [3];
  1787. bool errorThrown = false;
  1788. try {
  1789. c2.SetValue (c1 [0], 0);
  1790. } catch (ArgumentException) {
  1791. errorThrown = true;
  1792. }
  1793. Assert.IsTrue (errorThrown, "#M83");
  1794. }
  1795. {
  1796. int[] c1 = { 1, 2, 3 };
  1797. Object[] c2 = new Object [3];
  1798. for (int i = 0; i < c1.Length; i++)
  1799. c2.SetValue (c1 [i], i);
  1800. for (int i = 0; i < c1.Length; i++)
  1801. Assert.AreEqual (c1[i], Convert.ToInt32 (c2[i]), "#M84(" + i + ")");
  1802. }
  1803. {
  1804. Object[] c1 = new Object [3];
  1805. Object[] c2 = new Object [3];
  1806. c1[0] = new Object ();
  1807. for (int i = 0; i < c1.Length; i++)
  1808. c2.SetValue (c1 [i], i);
  1809. for (int i = 0; i < c1.Length; i++)
  1810. Assert.AreEqual (c1[i], c2[i], "#M85(" + i + ")");
  1811. }
  1812. {
  1813. Object[] c1 = new Object [3];
  1814. string[] c2 = new String [3];
  1815. string test = "hello";
  1816. c1[0] = test;
  1817. c2.SetValue (c1 [0], 0);
  1818. Assert.AreEqual (c1[0], c2[0], "#M86");
  1819. Assert.AreEqual ("hello", c2[0], "#M87");
  1820. }
  1821. {
  1822. char[] c1 = { 'a', 'b', 'c' };
  1823. string[] c2 = new string [3];
  1824. try {
  1825. c2.SetValue (c1 [0], 0);
  1826. Assert.Fail ("#M88");
  1827. } catch (InvalidCastException) {}
  1828. }
  1829. {
  1830. Single[] c1 = { 1.2F, 2.3F, 3.4F, 4.5F };
  1831. long[] c2 = new long [3];
  1832. try {
  1833. c2.SetValue (c1 [0], 0);
  1834. Assert.Fail ("#M89");
  1835. } catch (ArgumentException) {}
  1836. }
  1837. {
  1838. Type[] types = {
  1839. typeof (Boolean),
  1840. typeof (Byte),
  1841. typeof (Char),
  1842. typeof (Double),
  1843. typeof (Int16),
  1844. typeof (Int32),
  1845. typeof (Int64),
  1846. typeof (SByte),
  1847. typeof (Single),
  1848. typeof (UInt16),
  1849. typeof (UInt32),
  1850. typeof (UInt64)
  1851. };
  1852. bool v1 = true;
  1853. Byte v2 = 1;
  1854. Char v3 = 'a';
  1855. Double v4 = -1.2;
  1856. Int16 v5 = -32;
  1857. Int32 v6 = -234;
  1858. Int64 v7 = -34523;
  1859. SByte v8 = -1;
  1860. Single v9 = -4.8F;
  1861. UInt16 v10 = 24234;
  1862. UInt32 v11 = 235354;
  1863. UInt64 v12 = 234552;
  1864. Object[] va1 = { v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12 };
  1865. Object[] va2 = { "true", "1", "a", "-1.2", "-32", "-234", "-34523", "-1",
  1866. "-4.8F", "24234", "235354", "234552" };
  1867. Object[][] vt = { va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1, va1 };
  1868. int[] arg_ex = {
  1869. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1870. 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
  1871. 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
  1872. 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1,
  1873. 1, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 1,
  1874. 1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 1, 1,
  1875. 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1,
  1876. 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1,
  1877. 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 1,
  1878. 1, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0,
  1879. 1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0,
  1880. 1, 1, 1, 0, 1, 1, 1, 1, 0, 1, 1, 0
  1881. };
  1882. // SetValue
  1883. for (int i = 0; i < types.Length; i++) {
  1884. for (int j = 0; j < types.Length; j++) {
  1885. Array array = Array.CreateInstance (types [j], 2);
  1886. Object value = vt[j][i];
  1887. bool errorThrown = false;
  1888. try {
  1889. array.SetValue (value, 0);
  1890. } catch (ArgumentException) {
  1891. errorThrown = true;
  1892. }
  1893. int ex_index = (i * types.Length) + j;
  1894. Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M90(" + types [i] + "," + types [j] + ")");
  1895. }
  1896. }
  1897. for (int i = 0; i < types.Length; i++) {
  1898. String[] array = new String [2];
  1899. Object value = va1 [i];
  1900. bool errorThrown = false;
  1901. try {
  1902. array.SetValue (value, 0);
  1903. } catch (InvalidCastException) {
  1904. errorThrown = true;
  1905. }
  1906. Assert.IsTrue (errorThrown, "#M91(" + types [i] + ")");
  1907. }
  1908. for (int i = 0; i < types.Length; i++) {
  1909. Array array = Array.CreateInstance (types [i], 2);
  1910. Object value = va2 [i];
  1911. bool errorThrown = false;
  1912. try {
  1913. array.SetValue (value, 0);
  1914. } catch (InvalidCastException) {
  1915. errorThrown = true;
  1916. }
  1917. Assert.IsTrue (errorThrown, "#M92(" + types [i] + ")");
  1918. }
  1919. for (int i = 0; i < types.Length; i++) {
  1920. Array array = Array.CreateInstance (types [i], 2);
  1921. Object value = null;
  1922. bool errorThrown = false;
  1923. try {
  1924. array.SetValue (value, 0);
  1925. } catch (InvalidCastException) {
  1926. errorThrown = true;
  1927. }
  1928. Assert.IsTrue (!errorThrown, "#M93(" + types [i] + ")");
  1929. }
  1930. // Copy
  1931. for (int i = 0; i < types.Length; i++) {
  1932. for (int j = 0; j < types.Length; j++) {
  1933. Array source = Array.CreateInstance (types [i], 2);
  1934. Array array = Array.CreateInstance (types [j], 2);
  1935. source.SetValue (vt[j][i], 0);
  1936. source.SetValue (vt[j][i], 1);
  1937. bool errorThrown = false;
  1938. try {
  1939. Array.Copy (source, array, 2);
  1940. } catch (ArrayTypeMismatchException) {
  1941. errorThrown = true;
  1942. }
  1943. int ex_index = (i * types.Length) + j;
  1944. Assert.AreEqual (errorThrown, arg_ex [ex_index] == 1, "#M94(" + types [i] + "," + types [j] + ")");
  1945. }
  1946. }
  1947. for (int i = 0; i < types.Length; i++) {
  1948. Array source = Array.CreateInstance (types [i], 2);
  1949. String[] array = new String [2];
  1950. source.SetValue (va1 [i], 0);
  1951. source.SetValue (va1 [i], 1);
  1952. bool errorThrown = false;
  1953. try {
  1954. Array.Copy (source, array, 2);
  1955. } catch (ArrayTypeMismatchException) {
  1956. errorThrown = true;
  1957. }
  1958. Assert.IsTrue (errorThrown, "#M95(" + types [i] + ")");
  1959. }
  1960. for (int i = 0; i < types.Length; i++) {
  1961. String[] source = new String [2];
  1962. Array array = Array.CreateInstance (types [i], 2);
  1963. source.SetValue (va2 [i], 0);
  1964. source.SetValue (va2 [i], 1);
  1965. bool errorThrown = false;
  1966. try {
  1967. Array.Copy (source, array, 2);
  1968. } catch (ArrayTypeMismatchException) {
  1969. errorThrown = true;
  1970. }
  1971. Assert.IsTrue (errorThrown, "#M96(" + types [i] + ")");
  1972. }
  1973. }
  1974. }
  1975. [Test]
  1976. public void TestSort() {
  1977. {
  1978. bool errorThrown = false;
  1979. try {
  1980. Array.Sort(null);
  1981. } catch (ArgumentNullException) {
  1982. errorThrown = true;
  1983. }
  1984. Assert.IsTrue (errorThrown, "#N01");
  1985. }
  1986. {
  1987. bool errorThrown = false;
  1988. try {
  1989. Array.Sort(null, 0, 1);
  1990. } catch (ArgumentNullException) {
  1991. errorThrown = true;
  1992. }
  1993. Assert.IsTrue (errorThrown, "#N02");
  1994. }
  1995. {
  1996. bool errorThrown = false;
  1997. try {
  1998. char[] c1 = new Char[2];
  1999. Array.Sort(null, c1);
  2000. } catch (ArgumentNullException) {
  2001. errorThrown = true;
  2002. }
  2003. Assert.IsTrue (errorThrown, "#N03");
  2004. }
  2005. {
  2006. bool errorThrown = false;
  2007. try {
  2008. char[] c1 = new Char[2];
  2009. Array.Sort(null, c1, 0, 1);
  2010. } catch (ArgumentNullException) {
  2011. errorThrown = true;
  2012. }
  2013. Assert.IsTrue (errorThrown, "#N04");
  2014. }
  2015. {
  2016. int tc = 5;
  2017. char[] arr = {'d', 'b', 'f', 'e', 'a', 'c'};
  2018. try {
  2019. Array.Sort (null, 0, 1);
  2020. Assert.Fail ("#N" + tc.ToString ());
  2021. }
  2022. catch (ArgumentException) {}
  2023. catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
  2024. tc++;
  2025. try {
  2026. Array.Sort (arr, -1, 3);
  2027. Assert.Fail ("#N" + tc.ToString ());
  2028. }
  2029. catch (ArgumentException) {}
  2030. catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
  2031. tc++;
  2032. try {
  2033. Array.Sort (arr, 1, -3);
  2034. Assert.Fail ("#N" + tc.ToString ());
  2035. }
  2036. catch (ArgumentException) {}
  2037. catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
  2038. tc++;
  2039. try {
  2040. Array.Sort (arr, arr.Length, arr.Length + 2);
  2041. Assert.Fail ("#N" + tc.ToString ());
  2042. }
  2043. catch (ArgumentException) {}
  2044. catch (Exception) { Assert.Fail ("#N" + tc.ToString ()); }
  2045. }
  2046. // note: null second array => just sort first array
  2047. char[] starter = {'d', 'b', 'f', 'e', 'a', 'c'};
  2048. int[] starter1 = {1,2,3,4,5,6};
  2049. {
  2050. char[] c1 = (char[])starter.Clone();
  2051. Array.Sort(c1);
  2052. Assert.AreEqual ('a', c1[0], "#N21");
  2053. Assert.AreEqual ('b', c1[1], "#N22");
  2054. Assert.AreEqual ('c', c1[2], "#N23");
  2055. Assert.AreEqual ('d', c1[3], "#N24");
  2056. Assert.AreEqual ('e', c1[4], "#N25");
  2057. Assert.AreEqual ('f', c1[5], "#N26");
  2058. }
  2059. {
  2060. char[] c1 = (char[])starter.Clone();
  2061. int[] i1 = (int[])starter1.Clone();
  2062. Array.Sort(c1, i1);
  2063. Assert.AreEqual ('a', c1[0], "#N41");
  2064. Assert.AreEqual ('b', c1[1], "#N42");
  2065. Assert.AreEqual ('c', c1[2], "#N43");
  2066. Assert.AreEqual ('d', c1[3], "#N44");
  2067. Assert.AreEqual ('e', c1[4], "#N45");
  2068. Assert.AreEqual ('f', c1[5], "#N46");
  2069. Assert.AreEqual (5, i1[0], "#N47");
  2070. Assert.AreEqual (2, i1[1], "#N48");
  2071. Assert.AreEqual (6, i1[2], "#N49");
  2072. Assert.AreEqual (1, i1[3], "#N50");
  2073. Assert.AreEqual (4, i1[4], "#N51");
  2074. Assert.AreEqual (3, i1[5], "#N52");
  2075. }
  2076. {
  2077. char[] c1 = (char[])starter.Clone();
  2078. Array.Sort(c1, 1, 4);
  2079. Assert.AreEqual ('d', c1[0], "#N61");
  2080. Assert.AreEqual ('a', c1[1], "#N62");
  2081. Assert.AreEqual ('b', c1[2], "#N63");
  2082. Assert.AreEqual ('e', c1[3], "#N64");
  2083. Assert.AreEqual ('f', c1[4], "#N65");
  2084. Assert.AreEqual ('c', c1[5], "#N66");
  2085. }
  2086. {
  2087. char[] c1 = (char[])starter.Clone();
  2088. int[] i1 = (int[])starter1.Clone();
  2089. Array.Sort(c1, i1, 1, 4);
  2090. Assert.AreEqual ('d', c1[0], "#N81");
  2091. Assert.AreEqual ('a', c1[1], "#N82");
  2092. Assert.AreEqual ('b', c1[2], "#N83");
  2093. Assert.AreEqual ('e', c1[3], "#N84");
  2094. Assert.AreEqual ('f', c1[4], "#N85");
  2095. Assert.AreEqual ('c', c1[5], "#N86");
  2096. Assert.AreEqual (1, i1[0], "#N87");
  2097. Assert.AreEqual (5, i1[1], "#N88");
  2098. Assert.AreEqual (2, i1[2], "#N89");
  2099. Assert.AreEqual (4, i1[3], "#N90");
  2100. Assert.AreEqual (3, i1[4], "#N91");
  2101. Assert.AreEqual (6, i1[5], "#N92");
  2102. }
  2103. }
  2104. [Test]
  2105. public void TestInitializeEmpty()
  2106. {
  2107. bool catched=false;
  2108. int[] a = {};
  2109. try
  2110. {
  2111. a.Initialize();
  2112. }
  2113. catch(Exception)
  2114. {
  2115. catched=true;
  2116. }
  2117. Assert.IsTrue (!catched, "#TI01");
  2118. }
  2119. [Test]
  2120. public void TestInitializeInt()
  2121. {
  2122. int[] a = {1,2,0};
  2123. a.Initialize();
  2124. int[] b = {1,2,0};
  2125. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2126. {
  2127. Assert.AreEqual (a[i], b[i], "#TI02 " + i);
  2128. }
  2129. }
  2130. [Test]
  2131. public void TestInitializeDouble()
  2132. {
  2133. double[] a = {1.0,2.0,0.0};
  2134. a.Initialize();
  2135. double[] b = {1.0,2.0,0.0};
  2136. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2137. {
  2138. Assert.AreEqual (a[i], b[i], "#TI03 " + i);
  2139. }
  2140. }
  2141. [Test]
  2142. public void TestInitializeFloat()
  2143. {
  2144. float[] a = {1.0F,2.0F,0.0F};
  2145. a.Initialize();
  2146. float[] b = {1.0F,2.0F,0.0F};
  2147. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2148. {
  2149. Assert.AreEqual (a[i], b[i], "#TI04 " + i);
  2150. }
  2151. }
  2152. [Test]
  2153. public void TestInitializeChar()
  2154. {
  2155. char[] a = {'1','.','0','F','2','.','0','F'};
  2156. a.Initialize();
  2157. char[] b = {'1','.','0','F','2','.','0','F'};
  2158. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2159. {
  2160. Assert.AreEqual (a[i], b[i], "#TI05 " + i);
  2161. }
  2162. }
  2163. [Test]
  2164. public void TestInitializeString()
  2165. {
  2166. string[] a = {"hola","adios","menos","mas"};
  2167. a.Initialize();
  2168. string[] b = {"hola","adios","menos","mas"};
  2169. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2170. {
  2171. Assert.AreEqual (a[i], b[i], "#TI06 " + i);
  2172. }
  2173. }
  2174. [Test]
  2175. public void TestInitializeEnum()
  2176. {
  2177. enua[] a = {enua.hola,enua.adios,enua.menos,enua.mas};
  2178. a.Initialize();
  2179. enua[] b = {enua.hola,enua.adios,enua.menos,enua.mas};
  2180. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2181. {
  2182. Assert.AreEqual (a[i], b[i], "#TI07 " + i);
  2183. }
  2184. }
  2185. [Test]
  2186. public void TestInitializeIntNI()
  2187. {
  2188. int[] a = new int[20];
  2189. a.Initialize();
  2190. foreach(int b in a)
  2191. {
  2192. Assert.AreEqual (b, 0, "#TI08");
  2193. }
  2194. }
  2195. [Test]
  2196. public void TestInitializeCharNI()
  2197. {
  2198. char[] a = new char[20];
  2199. a.Initialize();
  2200. foreach(char b in a)
  2201. {
  2202. Assert.AreEqual (b, 0, "#TI09");
  2203. }
  2204. }
  2205. [Test]
  2206. public void TestInitializeDoubleNI()
  2207. {
  2208. double[] a = new double[20];
  2209. a.Initialize();
  2210. foreach(double b in a)
  2211. {
  2212. Assert.AreEqual (b, 0.0, "#TI09");
  2213. }
  2214. }
  2215. [Test]
  2216. public void TestInitializeStringNI()
  2217. {
  2218. string[] a = new string[20];
  2219. a.Initialize();
  2220. foreach(string b in a)
  2221. {
  2222. Assert.AreEqual (b, null, "#TI10");
  2223. }
  2224. }
  2225. [Test]
  2226. public void TestInitializeObjectNI()
  2227. {
  2228. object[] a = new object[20];
  2229. a.Initialize();
  2230. foreach(object b in a)
  2231. {
  2232. Assert.AreEqual (b, null, "#TI11");
  2233. }
  2234. }
  2235. [Test]
  2236. public void TestInitializeAClassNI()
  2237. {
  2238. AClass[] a = new AClass[20];
  2239. a.Initialize();
  2240. foreach(AClass b in a)
  2241. {
  2242. Assert.AreEqual (b, null, "#TI12");
  2243. }
  2244. }
  2245. [Test]
  2246. public void TestInitializeAStructNI()
  2247. {
  2248. AStruct[] a = new AStruct[20];
  2249. a.Initialize();
  2250. foreach(AStruct b in a)
  2251. {
  2252. Assert.AreEqual (b, new AStruct(), "#TI14");
  2253. }
  2254. }
  2255. [Test]
  2256. public void TestInitializeAStruct()
  2257. {
  2258. AStruct[] a = new AStruct[3];
  2259. a[1].a = "ADIOS";
  2260. a[1].s = "HOLA";
  2261. a.Initialize();
  2262. AStruct[] b = new AStruct[3];
  2263. b[1].a = "ADIOS";
  2264. b[1].s = "HOLA";
  2265. for(int i=a.GetLowerBound(0);i<=a.GetUpperBound(0);i++)
  2266. {
  2267. Assert.AreEqual (a[i], b[i], "#TI15 " + i);
  2268. }
  2269. }
  2270. [Test]
  2271. public void TestInitializeDateTimeNI()
  2272. {
  2273. DateTime[] a = new DateTime[20];
  2274. a.Initialize();
  2275. foreach(DateTime b in a)
  2276. {
  2277. Assert.AreEqual (b, new DateTime(), "#TI16");
  2278. }
  2279. }
  2280. [Test]
  2281. [ExpectedException (typeof (ArgumentNullException))]
  2282. public void MoreSort1 ()
  2283. {
  2284. Array.Sort (null, 0, 1);
  2285. }
  2286. [Test]
  2287. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  2288. public void MoreSort2 ()
  2289. {
  2290. Array.Sort (arrsort, -1, 3);
  2291. }
  2292. [Test]
  2293. [ExpectedException (typeof (ArgumentOutOfRangeException))]
  2294. public void MoreSort3 ()
  2295. {
  2296. Array.Sort (arrsort, 1, -3);
  2297. }
  2298. [Test]
  2299. [ExpectedException (typeof (ArgumentException))]
  2300. public void MoreSort4 ()
  2301. {
  2302. Array.Sort (arrsort, arrsort.Length, arrsort.Length + 2);
  2303. }
  2304. [Test]
  2305. [ExpectedException (typeof (RankException))]
  2306. public void MoreSort5 ()
  2307. {
  2308. char [,] arr = new char [,] {{'a'}, {'b'}};
  2309. Array.Sort (arr, 0, 1);
  2310. }
  2311. [Test]
  2312. public void MoreSort6 ()
  2313. {
  2314. Array.Sort (arrsort, 0, 0);
  2315. }
  2316. [Test]
  2317. [ExpectedException (typeof (ArgumentException))]
  2318. public void MoreSort7 ()
  2319. {
  2320. Array.Sort (arrsort, arrsort.Length - 1, 2);
  2321. }
  2322. [Test]
  2323. [ExpectedException (typeof (ArgumentException))]
  2324. public void MoreSort8 ()
  2325. {
  2326. Array.Sort (arrsort, 0, arrsort.Length + 1);
  2327. }
  2328. [Test]
  2329. public void MoreSort9 ()
  2330. {
  2331. Array.Sort (arrsort, null, 0, arrsort.Length, null);
  2332. }
  2333. [Test]
  2334. [ExpectedException (typeof (InvalidOperationException))]
  2335. public void MoreSort10 ()
  2336. {
  2337. object [] array = {true, 'k', SByte.MinValue, Byte.MinValue, (short) 2, 634, (long) 436, (float) 1.1, 1.23, "Hello World"};
  2338. Array.Sort (array, (IComparer) null);
  2339. }
  2340. [Test] // bug #81941
  2341. public void Sort ()
  2342. {
  2343. double [] a = new double [2] { 0.9, 0.3 };
  2344. uint [] b = new uint [2] { 4, 7 };
  2345. Array.Sort (a, b);
  2346. Assert.AreEqual (0.3, a [0], "#1");
  2347. Assert.AreEqual (0.9, a [1], "#2");
  2348. Assert.AreEqual (7, b [0], "#3");
  2349. Assert.AreEqual (4, b [1], "#4");
  2350. }
  2351. [Test]
  2352. public void ClearJaggedArray ()
  2353. {
  2354. byte[][] matrix = new byte [8][];
  2355. for (int i=0; i < 8; i++) {
  2356. matrix [i] = new byte [8];
  2357. for (int j=0; j < 8; j++) {
  2358. matrix [i][j] = 1;
  2359. }
  2360. }
  2361. Array.Clear (matrix, 0, 8);
  2362. for (int i=0; i < 8; i++) {
  2363. Assert.IsNull (matrix [i], i.ToString ());
  2364. }
  2365. }
  2366. [Test]
  2367. public void ClearMultidimentionalArray ()
  2368. {
  2369. byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
  2370. Array.Clear (matrix, 0, 2);
  2371. Assert.AreEqual (0, matrix [0, 0], "0,0");
  2372. Assert.AreEqual (0, matrix [0, 1], "0,1");
  2373. Assert.AreEqual (2, matrix [1, 0], "1,0");
  2374. Assert.AreEqual (2, matrix [1, 1], "1,1");
  2375. }
  2376. [Test]
  2377. [ExpectedException (typeof (IndexOutOfRangeException))]
  2378. public void ClearOutsideMultidimentionalArray ()
  2379. {
  2380. byte[,] matrix = new byte [2,2] { {1, 1}, {2, 2} };
  2381. Array.Clear (matrix, 0, 5);
  2382. }
  2383. [Test]
  2384. [ExpectedException (typeof (IndexOutOfRangeException))]
  2385. public void Clear_IndexOverflow ()
  2386. {
  2387. byte[] array = new byte [16];
  2388. Array.Clear (array, 4, Int32.MaxValue);
  2389. }
  2390. [Test]
  2391. [ExpectedException (typeof (IndexOutOfRangeException))]
  2392. public void Clear_LengthOverflow ()
  2393. {
  2394. byte[] array = new byte [16];
  2395. Array.Clear (array, Int32.MaxValue, 4);
  2396. }
  2397. [Test]
  2398. [ExpectedException (typeof (ArgumentException))]
  2399. public void Copy_SourceIndexOverflow ()
  2400. {
  2401. byte[] array = new byte [16];
  2402. Array.Copy (array, Int32.MaxValue, array, 8, 8);
  2403. }
  2404. [Test]
  2405. [ExpectedException (typeof (ArgumentException))]
  2406. public void Copy_DestinationIndexOverflow ()
  2407. {
  2408. byte[] array = new byte [16];
  2409. Array.Copy (array, 8, array, Int32.MaxValue, 8);
  2410. }
  2411. [Test]
  2412. [ExpectedException (typeof (ArgumentException))]
  2413. public void Copy_LengthOverflow ()
  2414. {
  2415. byte[] array = new byte [16];
  2416. Array.Copy (array, 8, array, 8, Int32.MaxValue);
  2417. }
  2418. [Test]
  2419. [ExpectedException (typeof (ArgumentException))]
  2420. public void Reverse_IndexOverflow ()
  2421. {
  2422. byte[] array = new byte [16];
  2423. Array.Reverse (array, Int32.MaxValue, 8);
  2424. }
  2425. [Test]
  2426. [ExpectedException (typeof (ArgumentException))]
  2427. public void Reverse_LengthOverflow ()
  2428. {
  2429. byte[] array = new byte [16];
  2430. Array.Reverse (array, 8, Int32.MaxValue);
  2431. }
  2432. public struct CharX : IComparable {
  2433. public char c;
  2434. public CharX (char c)
  2435. {
  2436. this.c = c;
  2437. }
  2438. public int CompareTo (object obj)
  2439. {
  2440. if (obj is CharX)
  2441. return c.CompareTo (((CharX) obj).c);
  2442. else
  2443. return c.CompareTo (obj);
  2444. }
  2445. }
  2446. [Test]
  2447. public void BinarySearch_ArgPassingOrder ()
  2448. {
  2449. //
  2450. // This tests that arguments are passed to the comprer in the correct
  2451. // order. The IComparable of the *array* elements must get called, not
  2452. // that of the search object.
  2453. //
  2454. CharX [] x = { new CharX ('a'), new CharX ('b'), new CharX ('c') };
  2455. Assert.AreEqual (1, Array.BinarySearch (x, 'b'));
  2456. }
  2457. class Comparer: IComparer {
  2458. private bool called = false;
  2459. public bool Called {
  2460. get {
  2461. bool result = called;
  2462. called = false;
  2463. return called;
  2464. }
  2465. }
  2466. public int Compare (object x, object y)
  2467. {
  2468. called = true;
  2469. return 0;
  2470. }
  2471. }
  2472. [Test]
  2473. public void BinarySearch1_EmptyList ()
  2474. {
  2475. int[] array = new int[0];
  2476. Assert.AreEqual (- 1, Array.BinarySearch (array, 0), "BinarySearch");
  2477. }
  2478. [Test]
  2479. public void BinarySearch2_EmptyList ()
  2480. {
  2481. int[] array = new int[0];
  2482. Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, 0), "BinarySearch");
  2483. }
  2484. [Test]
  2485. public void BinarySearch3_EmptyList ()
  2486. {
  2487. Comparer comparer = new Comparer ();
  2488. int[] array = new int[0];
  2489. Assert.AreEqual (-1, Array.BinarySearch (array, 0, comparer), "BinarySearch");
  2490. // bug 77030 - the comparer isn't called for an empty array/list
  2491. Assert.IsTrue (!comparer.Called, "Called");
  2492. }
  2493. [Test]
  2494. public void BinarySearch4_EmptyList ()
  2495. {
  2496. Comparer comparer = new Comparer ();
  2497. int[] array = new int[0];
  2498. Assert.AreEqual (-1, Array.BinarySearch (array, 0, 0, comparer), "BinarySearch");
  2499. // bug 77030 - the comparer isn't called for an empty array/list
  2500. Assert.IsTrue (!comparer.Called, "Called");
  2501. }
  2502. #if NET_2_0
  2503. [Test]
  2504. [ExpectedException (typeof (ArgumentNullException))]
  2505. public void AsReadOnly_NullArray ()
  2506. {
  2507. Array.AsReadOnly <int> (null);
  2508. }
  2509. [Test]
  2510. public void ReadOnly_Count ()
  2511. {
  2512. Assert.AreEqual (10, Array.AsReadOnly (new int [10]).Count);
  2513. }
  2514. [Test]
  2515. public void ReadOnly_Contains ()
  2516. {
  2517. int[] arr = new int [2];
  2518. arr [0] = 3;
  2519. arr [1] = 5;
  2520. IList<int> a = Array.AsReadOnly (arr);
  2521. Assert.IsTrue (a.Contains (3));
  2522. Assert.IsTrue (!a.Contains (6));
  2523. }
  2524. [Test]
  2525. public void ReadOnly_IndexOf ()
  2526. {
  2527. int[] arr = new int [2];
  2528. arr [0] = 3;
  2529. arr [1] = 5;
  2530. IList<int> a = Array.AsReadOnly (arr);
  2531. Assert.AreEqual (0, a.IndexOf (3));
  2532. Assert.AreEqual (1, a.IndexOf (5));
  2533. Assert.AreEqual (-1, a.IndexOf (6));
  2534. }
  2535. [Test]
  2536. public void ReadOnly_Indexer ()
  2537. {
  2538. int[] arr = new int [2];
  2539. arr [0] = 3;
  2540. arr [1] = 5;
  2541. IList<int> a = Array.AsReadOnly (arr);
  2542. Assert.AreEqual (3, a [0]);
  2543. Assert.AreEqual (5, a [1]);
  2544. /* Check that modifications to the original array are visible */
  2545. arr [0] = 6;
  2546. Assert.AreEqual (6, a [0]);
  2547. }
  2548. [Test]
  2549. public void ReadOnly_Enumerator ()
  2550. {
  2551. int[] arr = new int [10];
  2552. for (int i = 0; i < 10; ++i)
  2553. arr [i] = i;
  2554. int sum = 0;
  2555. foreach (int i in Array.AsReadOnly (arr))
  2556. sum += i;
  2557. Assert.AreEqual (45, sum);
  2558. }
  2559. [Test]
  2560. public void ReadOnly_CopyTo ()
  2561. {
  2562. int[] arr = new int [2];
  2563. arr [0] = 3;
  2564. arr [1] = 5;
  2565. IList<int> a = Array.AsReadOnly (arr);
  2566. int[] arr2 = new int [3];
  2567. a.CopyTo (arr2, 1);
  2568. Assert.AreEqual (0, arr2 [0]);
  2569. Assert.AreEqual (3, arr2 [1]);
  2570. Assert.AreEqual (5, arr2 [2]);
  2571. }
  2572. [Test]
  2573. public void Resize ()
  2574. {
  2575. int [] arr = new int [] { 1, 3, 5 };
  2576. Array.Resize <int> (ref arr, 3);
  2577. Assert.AreEqual (3, arr.Length, "#A1");
  2578. Assert.AreEqual (1, arr [0], "#A2");
  2579. Assert.AreEqual (3, arr [1], "#A3");
  2580. Assert.AreEqual (5, arr [2], "#A4");
  2581. Array.Resize <int> (ref arr, 2);
  2582. Assert.AreEqual (2, arr.Length, "#B1");
  2583. Assert.AreEqual (1, arr [0], "#B2");
  2584. Assert.AreEqual (3, arr [1], "#B3");
  2585. Array.Resize <int> (ref arr, 4);
  2586. Assert.AreEqual (4, arr.Length, "#C1");
  2587. Assert.AreEqual (1, arr [0], "#C2");
  2588. Assert.AreEqual (3, arr [1], "#C3");
  2589. Assert.AreEqual (0, arr [2], "#C4");
  2590. Assert.AreEqual (0, arr [3], "#C5");
  2591. }
  2592. [Test]
  2593. public void Resize_null ()
  2594. {
  2595. int [] arr = null;
  2596. Array.Resize (ref arr, 10);
  2597. Assert.AreEqual (arr.Length, 10);
  2598. }
  2599. [Test]
  2600. public void Test_ContainsAndIndexOf_EquatableItem ()
  2601. {
  2602. EquatableClass[] list = new EquatableClass[] {new EquatableClass (0), new EquatableClass (1), new EquatableClass (0)};
  2603. Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, list[0]), "#0");
  2604. Assert.AreEqual (0, Array.IndexOf<EquatableClass> (list, new EquatableClass (0)), "#1");
  2605. Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, list[0]), "#2");
  2606. Assert.AreEqual (2, Array.LastIndexOf<EquatableClass> (list, new EquatableClass (0)), "#3");
  2607. }
  2608. public class EquatableClass : IEquatable<EquatableClass>
  2609. {
  2610. int _x;
  2611. public EquatableClass (int x)
  2612. {
  2613. _x = x;
  2614. }
  2615. public bool Equals (EquatableClass other)
  2616. {
  2617. return this._x == other._x;
  2618. }
  2619. }
  2620. [Test]
  2621. public void AsIList ()
  2622. {
  2623. IList<int> arr = new int [10];
  2624. arr [0] = 5;
  2625. Assert.AreEqual (5, arr [0]);
  2626. IList<FooStruct> arr2 = new FooStruct [10];
  2627. FooStruct s = new FooStruct ();
  2628. s.i = 11;
  2629. s.j = 22;
  2630. arr2 [5] = s;
  2631. s = arr2 [5];
  2632. Assert.AreEqual (11, s.i);
  2633. Assert.AreEqual (22, s.j);
  2634. IList<string> arr3 = new string [10];
  2635. arr3 [5] = "ABC";
  2636. Assert.AreEqual ("ABC", arr3 [5]);
  2637. }
  2638. struct FooStruct {
  2639. public int i, j;
  2640. }
  2641. #if !TARGET_JVM // BugBUG: T[] is not yet ICollection<T> under TARGET_JVM
  2642. [Test]
  2643. // From bug #80563
  2644. public void ICollectionNull ()
  2645. {
  2646. ICollection<object> test;
  2647. test = new List<object>();
  2648. Assert.AreEqual (test.Contains (null), false, "list<o>");
  2649. test = new object[] {};
  2650. Assert.AreEqual (test.Contains (null), false, "empty array");
  2651. test = new object[] {null};
  2652. Assert.AreEqual (test.Contains (null), true, "array with null");
  2653. test = new List<object>(test);
  2654. Assert.AreEqual (test.Contains (null), true, "List<object> with test");
  2655. test = new object[] {new object()};
  2656. Assert.AreEqual (test.Contains (null), false, "array with object");
  2657. test = new List<object>(test);
  2658. Assert.AreEqual (test.Contains (null), false, "array with test");
  2659. }
  2660. #endif // TARGET_JVM
  2661. #endif
  2662. #region Bug 80299
  2663. enum ByteEnum : byte {}
  2664. enum IntEnum : int {}
  2665. [Test]
  2666. public void TestByteEnumArrayToByteArray ()
  2667. {
  2668. ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
  2669. byte[] b = new byte[a.Length];
  2670. a.CopyTo (b, 0);
  2671. }
  2672. [Test]
  2673. public void TestByteEnumArrayToIntArray ()
  2674. {
  2675. ByteEnum[] a = new ByteEnum[] {(ByteEnum) 1, (ByteEnum) 2};
  2676. int[] b = new int[a.Length];
  2677. a.CopyTo (b, 0);
  2678. }
  2679. [Test]
  2680. [ExpectedException (typeof (ArrayTypeMismatchException))]
  2681. public void TestIntEnumArrayToByteArray ()
  2682. {
  2683. IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
  2684. byte[] b = new byte[a.Length];
  2685. a.CopyTo (b, 0);
  2686. }
  2687. [Test]
  2688. public void TestIntEnumArrayToIntArray ()
  2689. {
  2690. IntEnum[] a = new IntEnum[] {(IntEnum) 1, (IntEnum) 2};
  2691. int[] b = new int[a.Length];
  2692. a.CopyTo (b, 0);
  2693. }
  2694. #endregion
  2695. #if NET_2_0
  2696. [Test] // bug #322248
  2697. public void IEnumerator_Reset ()
  2698. {
  2699. int[] array = new int[] { 1, 2, 3};
  2700. IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
  2701. Assert.IsTrue (e.MoveNext (), "#A1");
  2702. Assert.AreEqual (1, e.Current, "#A2");
  2703. Assert.IsTrue (e.MoveNext (), "#A3");
  2704. Assert.AreEqual (2, e.Current, "#A4");
  2705. e.Reset ();
  2706. Assert.IsTrue (e.MoveNext (), "#C1");
  2707. Assert.AreEqual (1, e.Current, "#C2");
  2708. }
  2709. [Test]
  2710. public void IEnumerator_Current_Finished ()
  2711. {
  2712. int[] array = new int[] { 1, 2, 3 };
  2713. IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
  2714. Assert.IsTrue (e.MoveNext (), "#A1");
  2715. Assert.AreEqual (1, e.Current, "#A2");
  2716. Assert.IsTrue (e.MoveNext (), "#A3");
  2717. Assert.AreEqual (2, e.Current, "#A4");
  2718. Assert.IsTrue (e.MoveNext (), "#A5");
  2719. Assert.AreEqual (3, e.Current, "#A6");
  2720. Assert.IsTrue (!e.MoveNext (), "#A6");
  2721. try {
  2722. Assert.Fail ("#B1:" + e.Current);
  2723. } catch (InvalidOperationException ex) {
  2724. // Enumeration already finished
  2725. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  2726. Assert.IsNull (ex.InnerException, "#B3");
  2727. Assert.IsNotNull (ex.Message, "#B4");
  2728. }
  2729. }
  2730. [Test]
  2731. public void IEnumerator_Current_NotStarted ()
  2732. {
  2733. int[] array = new int[] { 1, 2, 3 };
  2734. IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
  2735. try {
  2736. Assert.Fail ("#A1:" + e.Current);
  2737. } catch (InvalidOperationException ex) {
  2738. // Enumeration has not started. Call MoveNext
  2739. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#A2");
  2740. Assert.IsNull (ex.InnerException, "#A3");
  2741. Assert.IsNotNull (ex.Message, "#A4");
  2742. }
  2743. }
  2744. [Test]
  2745. public void IEnumerator_Current_Reset ()
  2746. {
  2747. int[] array = new int[] { 1, 2, 3 };
  2748. IEnumerator<int> e = ((IEnumerable<int>)array).GetEnumerator ();
  2749. e.MoveNext ();
  2750. e.Reset ();
  2751. try {
  2752. Assert.Fail ("#B1:" + e.Current);
  2753. } catch (InvalidOperationException ex) {
  2754. // Enumeration has not started. Call MoveNext
  2755. Assert.AreEqual (typeof (InvalidOperationException), ex.GetType (), "#B2");
  2756. Assert.IsNull (ex.InnerException, "#B3");
  2757. Assert.IsNotNull (ex.Message, "#B4");
  2758. }
  2759. }
  2760. public void ICollection_IsReadOnly() {
  2761. ICollection<string> arr = new string [10];
  2762. Assert.IsTrue (arr.IsReadOnly);
  2763. }
  2764. #endif
  2765. [Test]
  2766. [ExpectedException (typeof (NotSupportedException))]
  2767. public void ArrayCreateInstanceOfVoid ()
  2768. {
  2769. Array.CreateInstance (typeof (void), 42);
  2770. }
  2771. class Foo<T> {}
  2772. [Test]
  2773. [ExpectedException (typeof (NotSupportedException))]
  2774. public void ArrayCreateInstanceOfOpenGenericType ()
  2775. {
  2776. Array.CreateInstance (typeof (Foo<>), 42);
  2777. }
  2778. [Test]
  2779. [ExpectedException (typeof (IndexOutOfRangeException))]
  2780. public void ClearNegativeLength ()
  2781. {
  2782. Array.Clear (new int [] { 1, 2 }, 0, -1);
  2783. }
  2784. [Test]
  2785. [ExpectedException (typeof (ArgumentException))]
  2786. public void MultiDimension_IList_setItem ()
  2787. {
  2788. IList array = new int [1, 1];
  2789. array [0] = 2;
  2790. }
  2791. [Test]
  2792. [ExpectedException (typeof (ArgumentException))]
  2793. public void MultiDimension_IList_getItem ()
  2794. {
  2795. IList array = new int [1, 1];
  2796. int a = (int) array [0];
  2797. }
  2798. [Test]
  2799. public void SetValue_Nullable () {
  2800. Array array = Array.CreateInstance (typeof (int?), 7);
  2801. object o = 42;
  2802. array.SetValue (o, 0);
  2803. Assert.AreEqual (42, array.GetValue (0));
  2804. array.SetValue (null, 0);
  2805. Assert.AreEqual (null, array.GetValue (0));
  2806. }
  2807. #if NET_4_0
  2808. [Test]
  2809. [ExpectedException (typeof (ArgumentException))]
  2810. public void CompareToWithJaggedArray () {
  2811. IStructuralComparable a = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
  2812. IStructuralComparable b = new int[][] { new int [] { 1,2 }, new int [] { 3,4 }};
  2813. a.CompareTo (b, Comparer<object>.Default);
  2814. }
  2815. [Test]
  2816. [ExpectedException (typeof (ArgumentException))]
  2817. public void CompareToWithArrayOfTheWrongKind () {
  2818. IStructuralComparable a = new int[] { 1, 2 };
  2819. IStructuralComparable b = new double[] { 1, 2 };
  2820. a.CompareTo (b, Comparer<object>.Default);
  2821. }
  2822. [Test]
  2823. [ExpectedException (typeof (ArgumentException))]
  2824. public void CompareToWithNonArrayType () {
  2825. IStructuralComparable a = new int[] { 1, 2 };
  2826. a.CompareTo (99, Comparer<object>.Default);
  2827. }
  2828. [Test]
  2829. [ExpectedException (typeof (ArgumentException))]
  2830. public void CompareToWithNonArrayOfDifferentSize () {
  2831. IStructuralComparable a = new int[] { 1, 2 };
  2832. IStructuralComparable b = new int[] { 1, 2, 3 };
  2833. a.CompareTo (b, Comparer<object>.Default);
  2834. }
  2835. [Test]
  2836. [ExpectedException (typeof (ArgumentException))]
  2837. public void CompareToWithMultiDimArray1 () {
  2838. IStructuralComparable a = new int [2,2] { {10, 10 }, { 10, 10 } };
  2839. IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
  2840. a.CompareTo (b, Comparer<object>.Default);
  2841. }
  2842. [Test]
  2843. [ExpectedException (typeof (ArgumentException))]
  2844. public void CompareToWithMultiDimArray2 () {
  2845. IStructuralComparable a = new int [2] { 10, 10 };
  2846. IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
  2847. a.CompareTo (b, Comparer<object>.Default);
  2848. }
  2849. [Test]
  2850. [ExpectedException (typeof (ArgumentException))]
  2851. public void CompareToWithMultiDimArray3 () {
  2852. IStructuralComparable a = new int [4] { 10, 10, 10, 10 };
  2853. IStructuralComparable b = new int [2,2] { {10, 10 }, { 10, 10 } };
  2854. a.CompareTo (b, Comparer<object>.Default);
  2855. }
  2856. [Test]
  2857. [ExpectedException (typeof (IndexOutOfRangeException))]
  2858. public void CompareToWithBoundedArray1 () {
  2859. IStructuralComparable a = new int [2] { 10, 10 };
  2860. Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
  2861. IStructuralComparable b = ab;
  2862. ab.SetValue (10, 5);
  2863. ab.SetValue (10, 6);
  2864. a.CompareTo (b, Comparer<object>.Default);
  2865. }
  2866. [Test]
  2867. [ExpectedException (typeof (IndexOutOfRangeException))]
  2868. public void CompareToWithBoundedArray2 () {
  2869. IStructuralComparable a = new int [2] { 10, 10 };
  2870. Array ab = Array.CreateInstance (typeof (int), new int[] { 2 }, new int [] { 5 });
  2871. IStructuralComparable b = ab;
  2872. ab.SetValue (10, 5);
  2873. ab.SetValue (10, 6);
  2874. //Yes, CompareTo simply doesn't work with bounded arrays!
  2875. b.CompareTo (b, Comparer<object>.Default);
  2876. }
  2877. [Test]
  2878. [ExpectedException (typeof (NullReferenceException))]
  2879. public void CompareToWithNullComparer () {
  2880. IStructuralComparable a = new int[] { 1, 2 };
  2881. IStructuralComparable b = new int[] { 1, 2 };
  2882. a.CompareTo (b, null);
  2883. }
  2884. [Test]
  2885. public void CompareToWithNullArray () {
  2886. IStructuralComparable a = new int[] { 1, 2 };
  2887. Assert.AreEqual (1, a.CompareTo (null, Comparer<object>.Default));
  2888. }
  2889. [Test]
  2890. public void CompareToWithGoodArrays () {
  2891. IStructuralComparable a = new int[] { 10, 20 };
  2892. Assert.AreEqual (0, a.CompareTo (a, Comparer<object>.Default));
  2893. Assert.AreEqual (0, a.CompareTo (new int [] { 10, 20 }, Comparer<object>.Default));
  2894. Assert.AreEqual (-1, a.CompareTo (new int [] { 11, 20 }, Comparer<object>.Default));
  2895. Assert.AreEqual (-1, a.CompareTo (new int [] { 10, 21 }, Comparer<object>.Default));
  2896. Assert.AreEqual (1, a.CompareTo (new int [] { 9, 20 }, Comparer<object>.Default));
  2897. Assert.AreEqual (1, a.CompareTo (new int [] { 10, 19 }, Comparer<object>.Default));
  2898. }
  2899. [Test]
  2900. public void IStructuralEquatable_Equals ()
  2901. {
  2902. IStructuralEquatable array = new int[] {1, 2, 3};
  2903. IStructuralEquatable array2 = new int[] {1, 2, 3};
  2904. Assert.AreEqual (false, array.Equals (null, null));
  2905. Assert.AreEqual (true, array.Equals (array, null));
  2906. Assert.AreEqual (true, array.Equals (array2, EqualityComparer<int>.Default));
  2907. }
  2908. [Test]
  2909. [ExpectedException (typeof (NullReferenceException))]
  2910. public void IStructuralEquatable_Equals_NoComparer ()
  2911. {
  2912. IStructuralEquatable array = new int[] {1, 2, 3};
  2913. IStructuralComparable array2 = new int[] {1, 2, 3};
  2914. array.Equals (array2, null);
  2915. }
  2916. [Test]
  2917. [ExpectedException (typeof (ArgumentException))]
  2918. public void IStructuralEquatable_Equals_ComparerThrows ()
  2919. {
  2920. IStructuralEquatable array = new int[] {1, 2, 3};
  2921. IStructuralComparable array2 = new int[] {1, 2, 3};
  2922. array.Equals (array2, EqualityComparer<long>.Default);
  2923. }
  2924. #endif
  2925. }
  2926. }