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