PageRenderTime 67ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/mcs/class/corlib/Test/System/ArrayTest.cs

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