ArrayTest.cs 65 KB

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