PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/csharp/li_std_vector_runme.cs

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