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