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