PageRenderTime 52ms CodeModel.GetById 2ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/d/li_std_vector_runme.1.d

#
D | 219 lines | 172 code | 35 blank | 12 comment | 33 complexity | 21dd9d2fecfbf5ab07142283f6790276 MD5 | raw file
  1module li_std_vector_runme;
  2
  3import tango.core.Exception;
  4import tango.io.Stdout;
  5import Integer = tango.text.convert.Integer;
  6import li_std_vector.li_std_vector;
  7import li_std_vector.DoubleVector;
  8import li_std_vector.IntVector;
  9import li_std_vector.IntPtrVector;
 10import li_std_vector.IntConstPtrVector;
 11import li_std_vector.RealVector;
 12import li_std_vector.Struct;
 13import li_std_vector.StructVector;
 14import li_std_vector.StructPtrVector;
 15import li_std_vector.StructConstPtrVector;
 16
 17const size_t SIZE = 20;
 18
 19void main() {
 20  // Basic functionality tests.
 21  {
 22    auto vector = new IntVector();
 23    for (size_t i = 0; i < SIZE; ++i) {
 24      vector ~= i * 10;
 25    }
 26
 27    if (vector.length != SIZE) {
 28      throw new Exception("length test failed.");
 29    }
 30
 31    vector[0] = 200;
 32    if (vector[0] != 200) {
 33      throw new Exception("indexing test failed");
 34    }
 35    vector[0] = 0 * 10;
 36
 37    try {
 38      vector[vector.length] = 777;
 39      throw new Exception("out of range test failed");
 40    } catch (NoSuchElementException) {
 41    }
 42
 43    foreach (i, value; vector) {
 44      if (value != (i * 10)) {
 45	throw new Exception("foreach test failed, i: " ~ Integer.toString(i));
 46      }
 47    }
 48
 49    vector.clear();
 50    if (vector.size != 0) {
 51      throw new Exception("clear test failed");
 52    }
 53  }
 54
 55  // Slice tests.
 56  {
 57    auto dVector = new DoubleVector();
 58    for (size_t i = 0; i < SIZE; ++i) {
 59      dVector ~= i * 10.1f;
 60    }
 61
 62    double[] dArray = dVector[];
 63    foreach (i, value; dArray) {
 64      if (dVector[i] != value) {
 65	throw new Exception("slice test 1 failed, i: " ~ Integer.toString(i));
 66      }
 67    }
 68
 69
 70    auto sVector = new StructVector();
 71    for (size_t i = 0; i < SIZE; i++) {
 72      sVector ~= new Struct(i / 10.0);
 73    }
 74
 75    Struct[] array = sVector[];
 76
 77    for (size_t i = 0; i < SIZE; i++) {
 78      // Make sure that a shallow copy has been made.
 79      void* aPtr = Struct.swigGetCPtr(array[i]);
 80      void* vPtr = Struct.swigGetCPtr(sVector[i]);
 81      if (aPtr != vPtr) {
 82	throw new Exception("slice test 2 failed, i: " ~
 83	  Integer.toString(i));
 84      }
 85    }
 86  }
 87
 88  // remove() tests.
 89  {
 90    auto iVector = new IntVector();
 91    for (int i = 0; i < SIZE; i++) {
 92      iVector ~= i;
 93    }
 94
 95    iVector.remove(iVector.length - 1);
 96    iVector.remove(SIZE / 2);
 97    iVector.remove(0);
 98
 99    try {
100      iVector.remove(iVector.size);
101      throw new Exception("remove test failed");
102    } catch (NoSuchElementException) {
103    }
104  }
105
106  // Capacity tests.
107  {
108    auto dv = new DoubleVector(10);
109    if ((dv.capacity != 10) || (dv.length != 0)) {
110      throw new Exception("constructor setting capacity test failed");
111    }
112
113    // TODO: Is this really required (and spec'ed) behavior?
114    dv.capacity = 20;
115    if (dv.capacity != 20) {
116      throw new Exception("capacity test 1 failed");
117    }
118
119    dv ~= 1.11;
120    try {
121      dv.capacity = dv.length - 1;
122      throw new Exception("capacity test 2 failed");
123    } catch (IllegalArgumentException) {
124    }
125  }
126
127  // Test the methods being wrapped.
128  {
129    auto iv = new IntVector();
130    for (int i=0; i<4; i++) {
131      iv ~= i;
132    }
133
134    double x = average(iv);
135    x += average(new IntVector([1, 2, 3, 4]));
136    RealVector rv = half(new RealVector([10.0f, 10.5f, 11.0f, 11.5f]));
137
138    auto dv = new DoubleVector();
139    for (size_t i = 0; i < SIZE; i++) {
140      dv ~= i / 2.0;
141    }
142    halve_in_place(dv);
143
144    RealVector v0 = vecreal(new RealVector());
145    float flo = 123.456f;
146    v0 ~= flo;
147    flo = v0[0];
148
149    IntVector v1 = vecintptr(new IntVector());
150    IntPtrVector v2 = vecintptr(new IntPtrVector());
151    IntConstPtrVector v3 = vecintconstptr(new IntConstPtrVector());
152
153    v1 ~= 123;
154    v2.clear();
155    v3.clear();
156
157    StructVector v4 = vecstruct(new StructVector());
158    StructPtrVector v5 = vecstructptr(new StructPtrVector());
159    StructConstPtrVector v6 = vecstructconstptr(new StructConstPtrVector());
160
161    v4 ~= new Struct(123);
162    v5 ~= new Struct(123);
163    v6 ~= new Struct(123);
164  }
165
166  // Test vectors of pointers.
167  {
168    auto vector = new StructPtrVector();
169    for (size_t i = 0; i < SIZE; i++) {
170      vector ~= new Struct(i / 10.0);
171    }
172
173    Struct[] array = vector[];
174
175    for (size_t i = 0; i < SIZE; i++) {
176      // Make sure that a shallow copy has been made.
177      void* aPtr = Struct.swigGetCPtr(array[i]);
178      void* vPtr = Struct.swigGetCPtr(vector[i]);
179      if (aPtr != vPtr) {
180	throw new Exception("StructPtrVector test 1 failed, i: " ~
181	  Integer.toString(i));
182      }
183    }
184  }
185
186  // Test vectors of const pointers.
187  {
188    auto vector = new StructConstPtrVector();
189    for (size_t i = 0; i < SIZE; i++) {
190      vector ~= new Struct(i / 10.0);
191    }
192
193    Struct[] array = vector[];
194
195    for (size_t i = 0; i < SIZE; i++) {
196      // Make sure that a shallow copy has been made.
197      void* aPtr = Struct.swigGetCPtr(array[i]);
198      void* vPtr = Struct.swigGetCPtr(vector[i]);
199      if (aPtr != vPtr) {
200	throw new Exception("StructConstPtrVector test 1 failed, i: " ~
201	  Integer.toString(i));
202      }
203    }
204  }
205
206  // Test vectors destroyed via dispose().
207  {
208    {
209      scope vector = new StructVector();
210      vector ~= new Struct(0.0);
211      vector ~= new Struct(11.1);
212    }
213    {
214      scope vector = new DoubleVector();
215      vector ~= 0.0;
216      vector ~= 11.1;
217    }
218  }
219}