PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/rel-1-3-29/SWIG/Examples/test-suite/csharp/li_std_vector_runme.cs

#
C# | 606 lines | 529 code | 48 blank | 29 comment | 164 complexity | c6c31e7dc7baaf8b4d825a53f8774f9a MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. // This test tests all the methods in the C# collection wrapper
  2. using System;
  3. using li_std_vectorNamespace;
  4. public class li_std_vector_runme {
  5. private static readonly int collectionSize = 20;
  6. private static readonly int midCollection = collectionSize/2;
  7. public static DoubleVector myDoubleVector;
  8. public static RealVector myRealVector;
  9. public static void Main() {
  10. // Setup collection
  11. DoubleVector vect = new DoubleVector();
  12. for (int i=0; i<collectionSize; i++) {
  13. double num = i*10.1;
  14. vect.Add(num);
  15. }
  16. // Count property test
  17. if (vect.Count != collectionSize)
  18. throw new Exception("Count test failed");
  19. // IsFixedSize property test
  20. if (vect.IsFixedSize)
  21. throw new Exception("IsFixedSize test failed");
  22. // IsReadOnly property test
  23. if (vect.IsReadOnly)
  24. throw new Exception("IsReadOnly test failed");
  25. // Item indexing
  26. vect[0] = 200.1;
  27. if (vect[0] != 200.1)
  28. throw new Exception("Item property test failed");
  29. vect[0] = 0*10.1;
  30. try {
  31. vect[-1] = 777.1;
  32. throw new Exception("Item out of range (1) test failed");
  33. } catch (ArgumentOutOfRangeException) {
  34. }
  35. try {
  36. vect[vect.Count] = 777.1;
  37. throw new Exception("Item out of range (2) test failed");
  38. } catch (ArgumentOutOfRangeException) {
  39. }
  40. // CopyTo() test
  41. {
  42. double[] outputarray = new double[collectionSize];
  43. vect.CopyTo(outputarray);
  44. int index = 0;
  45. foreach(double val in outputarray) {
  46. if (vect[index] != val)
  47. throw new Exception("CopyTo (1) test failed, index:" + index);
  48. index++;
  49. }
  50. }
  51. {
  52. double[] outputarray = new double[midCollection+collectionSize];
  53. vect.CopyTo(outputarray, midCollection);
  54. int index = midCollection;
  55. foreach(double val in vect) {
  56. if (outputarray[index] != val)
  57. throw new Exception("CopyTo (2) test failed, index:" + index);
  58. index++;
  59. }
  60. }
  61. {
  62. double[] outputarray = new double[3];
  63. vect.CopyTo(10, outputarray, 1, 2);
  64. if (outputarray[0] != 0.0 || outputarray[1] != vect[10] || outputarray[2] != vect[11])
  65. throw new Exception("CopyTo (3) test failed");
  66. }
  67. {
  68. double[] outputarray = new double[collectionSize-1];
  69. try {
  70. vect.CopyTo(outputarray);
  71. throw new Exception("CopyTo (4) test failed");
  72. } catch (ArgumentException) {
  73. }
  74. }
  75. {
  76. double[,] outputarray = new double[collectionSize,collectionSize];
  77. try {
  78. vect.CopyTo(outputarray);
  79. throw new Exception("CopyTo (5) test failed");
  80. } catch (ArgumentException) {
  81. }
  82. }
  83. {
  84. StructVector inputvector = new StructVector();
  85. int arrayLen = 10;
  86. for (int i=0; i<arrayLen; i++) {
  87. inputvector.Add(new Struct(i/10.0));
  88. }
  89. Struct[] outputarray = new Struct[arrayLen];
  90. inputvector.CopyTo(outputarray);
  91. for(int i=0; i<arrayLen; i++) {
  92. if (outputarray[i].num != inputvector[i].num)
  93. throw new Exception("CopyTo (6) test failed, i:" + i);
  94. }
  95. foreach (Struct s in inputvector) {
  96. s.num += 20.0;
  97. }
  98. for(int i=0; i<arrayLen; i++) {
  99. if (outputarray[i].num + 20.0 != inputvector[i].num )
  100. throw new Exception("CopyTo (7) test failed (only a shallow copy was made), i:" + i);
  101. }
  102. }
  103. {
  104. try {
  105. vect.CopyTo(null);
  106. throw new Exception("CopyTo (8) test failed");
  107. } catch (ArgumentNullException) {
  108. }
  109. }
  110. // Contains() test
  111. if (!vect.Contains(0*10.1))
  112. throw new Exception("Contains test 1 failed");
  113. if (!vect.Contains(10*10.1))
  114. throw new Exception("Contains test 2 failed");
  115. if (!vect.Contains(19*10.1))
  116. throw new Exception("Contains test 3 failed");
  117. if (vect.Contains(20*10.1))
  118. throw new Exception("Contains test 4 failed");
  119. {
  120. // ICollection constructor
  121. double[] doubleArray = new double[] { 0.0, 11.1, 22.2, 33.3, 44.4, 55.5, 33.3 };
  122. DoubleVector dv = new DoubleVector(doubleArray);
  123. if (doubleArray.Length != dv.Count)
  124. throw new Exception("ICollection constructor length check failed: " + doubleArray.Length + "-" + dv.Count);
  125. for (int i=0; i<doubleArray.Length; i++) {
  126. if (doubleArray[i] != dv[i])
  127. throw new Exception("ICollection constructor failed, index:" + i);
  128. }
  129. {
  130. Struct[] structArray = new Struct[] { new Struct(0.0), new Struct(11.1), new Struct(22.2), new Struct(33.3) };
  131. StructVector sv = new StructVector(structArray);
  132. for (int i=0; i<structArray.Length; i++) {
  133. structArray[i].num += 200.0;
  134. }
  135. for (int i=0; i<structArray.Length; i++) {
  136. if (structArray[i].num != sv[i].num + 200.0)
  137. throw new Exception("ICollection constructor not a deep copy, index:" + i);
  138. }
  139. }
  140. try {
  141. new DoubleVector(null);
  142. throw new Exception("ICollection constructor null test failed");
  143. } catch (ArgumentNullException) {
  144. }
  145. // IndexOf() test
  146. for (int i=0; i<collectionSize; i++) {
  147. if (vect.IndexOf(i*10.1) != i)
  148. throw new Exception("IndexOf test " + i + " failed");
  149. }
  150. if (vect.IndexOf(200.1) != -1)
  151. throw new Exception("IndexOf non-existent test failed");
  152. if (dv.IndexOf(33.3) != 3)
  153. throw new Exception("IndexOf position test failed");
  154. // LastIndexOf() test
  155. for (int i=0; i<collectionSize; i++) {
  156. if (vect.LastIndexOf(i*10.1) != i)
  157. throw new Exception("LastIndexOf test " + i + " failed");
  158. }
  159. if (vect.LastIndexOf(200.1) != -1)
  160. throw new Exception("LastIndexOf non-existent test failed");
  161. if (dv.LastIndexOf(33.3) != 6)
  162. throw new Exception("LastIndexOf position test failed");
  163. }
  164. {
  165. // Repeat() test
  166. try {
  167. myDoubleVector = DoubleVector.Repeat(77.7, -1);
  168. throw new Exception("Repeat negative count test failed");
  169. } catch (ArgumentOutOfRangeException) {
  170. }
  171. DoubleVector dv = DoubleVector.Repeat(77.7, 5);
  172. if (dv.Count != 5)
  173. throw new Exception("Repeat count test failed");
  174. // Also tests enumerator
  175. System.Collections.IEnumerator myEnumerator = dv.GetEnumerator();
  176. while ( myEnumerator.MoveNext() ) {
  177. if ((double)myEnumerator.Current != 77.7)
  178. throw new Exception("Repeat test failed");
  179. }
  180. }
  181. {
  182. // InsertRange() test
  183. DoubleVector dvect = new DoubleVector();
  184. for (int i=0; i<5; i++) {
  185. dvect.Add(1000.0*i);
  186. }
  187. vect.InsertRange(midCollection, dvect);
  188. if (vect.Count != collectionSize+dvect.Count)
  189. throw new Exception("InsertRange test size failed");
  190. for (int i=0; i<midCollection; i++) {
  191. if (vect.IndexOf(i*10.1) != i)
  192. throw new Exception("InsertRange (1) test " + i + " failed");
  193. }
  194. for (int i=0; i<dvect.Count; i++) {
  195. if (vect[i+midCollection] != dvect[i])
  196. throw new Exception("InsertRange (2) test " + i + " failed");
  197. }
  198. for (int i=midCollection; i<collectionSize; i++) {
  199. if (vect.IndexOf(i*10.1) != i+dvect.Count)
  200. throw new Exception("InsertRange (3) test " + i + " failed");
  201. }
  202. try {
  203. vect.InsertRange(0, null);
  204. throw new Exception("InsertRange (4) test failed");
  205. } catch (ArgumentNullException) {
  206. }
  207. // RemoveRange() test
  208. vect.RemoveRange(0, 0);
  209. vect.RemoveRange(midCollection, dvect.Count);
  210. if (vect.Count != collectionSize)
  211. throw new Exception("RemoveRange test size failed");
  212. for (int i=0; i<collectionSize; i++) {
  213. if (vect.IndexOf(i*10.1) != i)
  214. throw new Exception("RemoveRange test " + i + " failed");
  215. }
  216. try {
  217. vect.RemoveRange(-1, 0);
  218. throw new Exception("RemoveRange index out of range (1) test failed");
  219. } catch (ArgumentOutOfRangeException) {
  220. }
  221. try {
  222. vect.RemoveRange(0, -1);
  223. throw new Exception("RemoveRange count out of range (2) test failed");
  224. } catch (ArgumentOutOfRangeException) {
  225. }
  226. try {
  227. vect.RemoveRange(collectionSize+1, 0);
  228. throw new Exception("RemoveRange index and count out of range (1) test failed");
  229. } catch (ArgumentException) {
  230. }
  231. try {
  232. vect.RemoveRange(0, collectionSize+1);
  233. throw new Exception("RemoveRange index and count out of range (2) test failed");
  234. } catch (ArgumentException) {
  235. }
  236. // AddRange() test
  237. vect.AddRange(dvect);
  238. if (vect.Count != collectionSize+dvect.Count)
  239. throw new Exception("AddRange test size failed");
  240. for (int i=0; i<collectionSize; i++) {
  241. if (vect.IndexOf(i*10.1) != i)
  242. throw new Exception("AddRange (1) test " + i + " failed");
  243. }
  244. for (int i=0; i<dvect.Count; i++) {
  245. if (vect[i+collectionSize] != dvect[i])
  246. throw new Exception("AddRange (2) test " + i + " failed");
  247. }
  248. try {
  249. vect.AddRange(null);
  250. throw new Exception("AddRange (3) test failed");
  251. } catch (ArgumentNullException) {
  252. }
  253. vect.RemoveRange(collectionSize, dvect.Count);
  254. // GetRange() test
  255. int rangeSize = 5;
  256. DoubleVector returnedVec = vect.GetRange(0, 0);
  257. returnedVec = vect.GetRange(midCollection, rangeSize);
  258. if (returnedVec.Count != rangeSize)
  259. throw new Exception("GetRange test size failed");
  260. for (int i=0; i<rangeSize; i++) {
  261. if (returnedVec.IndexOf((i+midCollection)*10.1) != i)
  262. throw new Exception("GetRange test " + i + " failed");
  263. }
  264. try {
  265. vect.GetRange(-1, 0);
  266. throw new Exception("GetRange index out of range (1) test failed");
  267. } catch (ArgumentOutOfRangeException) {
  268. }
  269. try {
  270. vect.GetRange(0, -1);
  271. throw new Exception("GetRange count out of range (2) test failed");
  272. } catch (ArgumentOutOfRangeException) {
  273. }
  274. try {
  275. vect.GetRange(collectionSize+1, 0);
  276. throw new Exception("GetRange index and count out of range (1) test failed");
  277. } catch (ArgumentException) {
  278. }
  279. try {
  280. vect.GetRange(0, collectionSize+1);
  281. throw new Exception("GetRange index and count out of range (2) test failed");
  282. } catch (ArgumentException) {
  283. }
  284. {
  285. StructVector inputvector = new StructVector();
  286. int arrayLen = 10;
  287. for (int i=0; i<arrayLen; i++) {
  288. inputvector.Add(new Struct(i/10.0));
  289. }
  290. StructVector outputvector = inputvector.GetRange(0,arrayLen);
  291. for(int i=0; i<arrayLen; i++) {
  292. if (outputvector[i].num != inputvector[i].num)
  293. throw new Exception("GetRange (1) test failed, i:" + i);
  294. }
  295. foreach (Struct s in inputvector) {
  296. s.num += 20.0;
  297. }
  298. for(int i=0; i<arrayLen; i++) {
  299. if (outputvector[i].num + 20.0 != inputvector[i].num )
  300. throw new Exception("GetRange (2) test failed (only a shallow copy was made), i:" + i);
  301. }
  302. }
  303. }
  304. // Insert() test
  305. int pos = 0;
  306. int count = vect.Count;
  307. vect.Insert(pos, -5.1);
  308. count++;
  309. if (vect.Count != count || vect[pos] != -5.1)
  310. throw new Exception("Insert at beginning test failed");
  311. pos = midCollection;
  312. vect.Insert(pos, 85.1);
  313. count++;
  314. if (vect.Count != count || vect[pos] != 85.1)
  315. throw new Exception("Insert at " + pos + " test failed");
  316. pos = vect.Count;
  317. vect.Insert(pos, 195.1);
  318. count++;
  319. if (vect.Count != count || vect[pos] != 195.1)
  320. throw new Exception("Insert at end test failed");
  321. pos = vect.Count+1;
  322. try {
  323. vect.Insert(pos, 222.1); // should throw
  324. throw new Exception("Insert after end (1) test failed");
  325. } catch (ArgumentOutOfRangeException) {
  326. }
  327. if (vect.Count != count)
  328. throw new Exception("Insert after end (2) test failed");
  329. pos = -1;
  330. try {
  331. vect.Insert(pos, 333.1); // should throw
  332. throw new Exception("Insert before start (1) test failed");
  333. } catch (ArgumentOutOfRangeException) {
  334. }
  335. if (vect.Count != count)
  336. throw new Exception("Insert before start (2) test failed");
  337. // Remove() test
  338. vect.Remove(195.1);
  339. count--;
  340. vect.Remove(-5.1);
  341. count--;
  342. vect.Remove(85.1);
  343. count--;
  344. vect.Remove(9999.1); // element does not exist, should quietly do nothing
  345. if (vect.Count != count)
  346. throw new Exception("Remove count check test failed");
  347. for (int i=0; i<collectionSize; i++) {
  348. if (vect[i] != i*10.1)
  349. throw new Exception("Remove test failed, index:" + i);
  350. }
  351. // RemoveAt() test
  352. vect.Insert(0, -4.1);
  353. vect.Insert(midCollection, 84.1);
  354. vect.Insert(vect.Count, 194.1);
  355. vect.RemoveAt(vect.Count-1);
  356. vect.RemoveAt(midCollection);
  357. vect.RemoveAt(0);
  358. try {
  359. vect.RemoveAt(-1);
  360. throw new Exception("RemoveAt test (1) failed");
  361. } catch (ArgumentOutOfRangeException) {
  362. }
  363. try {
  364. vect.RemoveAt(vect.Count);
  365. throw new Exception("RemoveAt test (2) failed");
  366. } catch (ArgumentOutOfRangeException) {
  367. }
  368. for (int i=0; i<collectionSize; i++) {
  369. if (vect[i] != i*10.1)
  370. throw new Exception("RemoveAt test (3) failed, index:" + i);
  371. }
  372. {
  373. // Capacity test
  374. try {
  375. myDoubleVector = new DoubleVector(-1);
  376. throw new Exception("constructor setting capacity (1) test failed");
  377. } catch (ArgumentOutOfRangeException) {
  378. }
  379. DoubleVector dv = new DoubleVector(10);
  380. if (dv.Capacity != 10 || dv.Count != 0)
  381. throw new Exception("constructor setting capacity (2) test failed");
  382. dv.Capacity = 20;
  383. if (dv.Capacity != 20)
  384. throw new Exception("capacity test (1) failed");
  385. dv.Add(1.11);
  386. try {
  387. dv.Capacity = dv.Count-1;
  388. throw new Exception("capacity test (2) failed");
  389. } catch (ArgumentOutOfRangeException) {
  390. }
  391. // SetRange() test
  392. for (int i=dv.Count; i<collectionSize; i++) {
  393. dv.Add(0.0);
  394. }
  395. dv.SetRange(0, vect);
  396. if (dv.Count != collectionSize)
  397. throw new Exception("SetRange count check test failed");
  398. for (int i=0; i<collectionSize; i++) {
  399. if (vect[i] != dv[i])
  400. throw new Exception("SetRange test (1) failed, index:" + i);
  401. }
  402. try {
  403. dv.SetRange(-1, vect);
  404. throw new Exception("SetRange test (2) failed");
  405. } catch (ArgumentOutOfRangeException) {
  406. }
  407. try {
  408. dv.SetRange(1, vect);
  409. throw new Exception("SetRange test (3) failed");
  410. } catch (ArgumentOutOfRangeException) {
  411. }
  412. try {
  413. vect.SetRange(0, null);
  414. throw new Exception("SetRange (4) test failed");
  415. } catch (ArgumentNullException) {
  416. }
  417. // Reverse() test
  418. dv.Reverse();
  419. for (int i=0; i<collectionSize; i++) {
  420. if (vect[i] != dv[collectionSize-i-1])
  421. throw new Exception("Reverse test (1) failed, index:" + i);
  422. }
  423. dv.Reverse(0, collectionSize);
  424. for (int i=0; i<collectionSize; i++) {
  425. if (vect[i] != dv[i])
  426. throw new Exception("Reverse test (2) failed, index:" + i);
  427. }
  428. dv.Reverse(0, 0); // should do nothing!
  429. for (int i=0; i<collectionSize; i++) {
  430. if (vect[i] != dv[i])
  431. throw new Exception("Reverse test (3) failed, index:" + i);
  432. }
  433. try {
  434. dv.Reverse(-1, 0);
  435. throw new Exception("Reverse test (4) failed");
  436. } catch (ArgumentOutOfRangeException) {
  437. }
  438. try {
  439. dv.Reverse(0, -1);
  440. throw new Exception("Reverse test (5) failed");
  441. } catch (ArgumentOutOfRangeException) {
  442. }
  443. try {
  444. dv.Reverse(collectionSize+1, 0);
  445. throw new Exception("Reverse test (6) failed");
  446. } catch (ArgumentException) {
  447. }
  448. try {
  449. dv.Reverse(0, collectionSize+1);
  450. throw new Exception("Reverse test (7) failed");
  451. } catch (ArgumentException) {
  452. }
  453. }
  454. // foreach test
  455. {
  456. int index=0;
  457. foreach (double s in vect) {
  458. if (s != index*10.1)
  459. throw new Exception("foreach test failed, index:" + index);
  460. index++;
  461. }
  462. }
  463. // Clear() test
  464. vect.Clear();
  465. if (vect.Count != 0)
  466. throw new Exception("Clear failed");
  467. // Finally test the methods being wrapped
  468. {
  469. IntVector iv = new IntVector();
  470. for (int i=0; i<4; i++) {
  471. iv.Add(i);
  472. }
  473. double x = li_std_vector.average(iv);
  474. x += li_std_vector.average( new IntVector( new int[] {1, 2, 3, 4} ) );
  475. myRealVector = li_std_vector.half( new RealVector( new float[] {10F, 10.5F, 11F, 11.5F} ) );
  476. DoubleVector dvec = new DoubleVector();
  477. for (int i=0; i<10; i++) {
  478. dvec.Add(i/2.0);
  479. }
  480. li_std_vector.halve_in_place(dvec);
  481. }
  482. // More wrapped methods
  483. {
  484. RealVector v0 = li_std_vector.vecreal(new RealVector());
  485. float flo = 123.456f;
  486. v0.Add(flo);
  487. flo = v0[0];
  488. IntVector v1 = li_std_vector.vecintptr(new IntVector());
  489. IntPtrVector v2 = li_std_vector.vecintptr(new IntPtrVector());
  490. IntConstPtrVector v3 = li_std_vector.vecintconstptr(new IntConstPtrVector());
  491. v1.Add(123);
  492. v2.Clear();
  493. v3.Clear();
  494. StructVector v4 = li_std_vector.vecstruct(new StructVector());
  495. StructPtrVector v5 = li_std_vector.vecstructptr(new StructPtrVector());
  496. StructConstPtrVector v6 = li_std_vector.vecstructconstptr(new StructConstPtrVector());
  497. v4.Add(new Struct(123));
  498. v5.Add(new Struct(123));
  499. v6.Add(new Struct(123));
  500. }
  501. // Test vectors of pointers
  502. {
  503. StructPtrVector inputvector = new StructPtrVector();
  504. int arrayLen = 10;
  505. for (int i=0; i<arrayLen; i++) {
  506. inputvector.Add(new Struct(i/10.0));
  507. }
  508. Struct[] outputarray = new Struct[arrayLen];
  509. inputvector.CopyTo(outputarray);
  510. for(int i=0; i<arrayLen; i++) {
  511. if (outputarray[i].num != inputvector[i].num)
  512. throw new Exception("StructPtrVector test (1) failed, i:" + i);
  513. }
  514. foreach (Struct s in inputvector) {
  515. s.num += 20.0;
  516. }
  517. for(int i=0; i<arrayLen; i++) {
  518. if (outputarray[i].num != 20.0 + i/10.0)
  519. throw new Exception("StructPtrVector test (2) failed (a deep copy was incorrectly made), i:" + i);
  520. }
  521. int rangeSize = 5;
  522. int mid = arrayLen/2;
  523. StructPtrVector returnedVec = inputvector.GetRange(mid, rangeSize);
  524. for (int i=0; i<rangeSize; i++) {
  525. if (inputvector[i+mid].num != returnedVec[i].num)
  526. throw new Exception("StructPtrVector test (3) failed, i:" + i);
  527. }
  528. }
  529. // Test vectors of const pointers
  530. {
  531. StructConstPtrVector inputvector = new StructConstPtrVector();
  532. int arrayLen = 10;
  533. for (int i=0; i<arrayLen; i++) {
  534. inputvector.Add(new Struct(i/10.0));
  535. }
  536. Struct[] outputarray = new Struct[arrayLen];
  537. inputvector.CopyTo(outputarray);
  538. for(int i=0; i<arrayLen; i++) {
  539. if (outputarray[i].num != inputvector[i].num)
  540. throw new Exception("StructConstPtrVector test (1) failed, i:" + i);
  541. }
  542. foreach (Struct s in inputvector) {
  543. s.num += 20.0;
  544. }
  545. for(int i=0; i<arrayLen; i++) {
  546. if (outputarray[i].num != 20.0 + i/10.0)
  547. throw new Exception("StructConstPtrVector test (2) failed (a deep copy was incorrectly made), i:" + i);
  548. }
  549. int rangeSize = 5;
  550. int mid = arrayLen/2;
  551. StructConstPtrVector returnedVec = inputvector.GetRange(mid, rangeSize);
  552. for (int i=0; i<rangeSize; i++) {
  553. if (inputvector[i+mid].num != returnedVec[i].num)
  554. throw new Exception("StructConstPtrVector test (3) failed, i:" + i);
  555. }
  556. }
  557. }
  558. }