summaryrefslogtreecommitdiff
path: root/Examples/test-suite/d/li_std_vector_runme.1.d
blob: 895fb450f853fa15ab5b785dd607801a7c74100d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
module li_std_vector_runme;

import tango.core.Exception;
import tango.io.Stdout;
import Integer = tango.text.convert.Integer;
import li_std_vector.li_std_vector;
import li_std_vector.DoubleVector;
import li_std_vector.IntVector;
import li_std_vector.IntPtrVector;
import li_std_vector.IntConstPtrVector;
import li_std_vector.RealVector;
import li_std_vector.Struct;
import li_std_vector.StructVector;
import li_std_vector.StructPtrVector;
import li_std_vector.StructConstPtrVector;

const size_t SIZE = 20;

void main() {
  // Basic functionality tests.
  {
    auto vector = new IntVector();
    for (size_t i = 0; i < SIZE; ++i) {
      vector ~= i * 10;
    }

    if (vector.length != SIZE) {
      throw new Exception("length test failed.");
    }

    vector[0] = 200;
    if (vector[0] != 200) {
      throw new Exception("indexing test failed");
    }
    vector[0] = 0 * 10;

    try {
      vector[vector.length] = 777;
      throw new Exception("out of range test failed");
    } catch (NoSuchElementException) {
    }

    foreach (i, value; vector) {
      if (value != (i * 10)) {
	throw new Exception("foreach test failed, i: " ~ Integer.toString(i));
      }
    }

    vector.clear();
    if (vector.size != 0) {
      throw new Exception("clear test failed");
    }
  }

  // Slice tests.
  {
    auto dVector = new DoubleVector();
    for (size_t i = 0; i < SIZE; ++i) {
      dVector ~= i * 10.1f;
    }

    double[] dArray = dVector[];
    foreach (i, value; dArray) {
      if (dVector[i] != value) {
	throw new Exception("slice test 1 failed, i: " ~ Integer.toString(i));
      }
    }


    auto sVector = new StructVector();
    for (size_t i = 0; i < SIZE; i++) {
      sVector ~= new Struct(i / 10.0);
    }

    Struct[] array = sVector[];

    for (size_t i = 0; i < SIZE; i++) {
      // Make sure that a shallow copy has been made.
      void* aPtr = Struct.swigGetCPtr(array[i]);
      void* vPtr = Struct.swigGetCPtr(sVector[i]);
      if (aPtr != vPtr) {
	throw new Exception("slice test 2 failed, i: " ~
	  Integer.toString(i));
      }
    }
  }

  // remove() tests.
  {
    auto iVector = new IntVector();
    for (int i = 0; i < SIZE; i++) {
      iVector ~= i;
    }

    iVector.remove(iVector.length - 1);
    iVector.remove(SIZE / 2);
    iVector.remove(0);

    try {
      iVector.remove(iVector.size);
      throw new Exception("remove test failed");
    } catch (NoSuchElementException) {
    }
  }

  // Capacity tests.
  {
    auto dv = new DoubleVector(10);
    if ((dv.capacity != 10) || (dv.length != 0)) {
      throw new Exception("constructor setting capacity test failed");
    }

    // TODO: Is this really required (and spec'ed) behavior?
    dv.capacity = 20;
    if (dv.capacity != 20) {
      throw new Exception("capacity test 1 failed");
    }

    dv ~= 1.11;
    try {
      dv.capacity = dv.length - 1;
      throw new Exception("capacity test 2 failed");
    } catch (IllegalArgumentException) {
    }
  }

  // Test the methods being wrapped.
  {
    auto iv = new IntVector();
    for (int i=0; i<4; i++) {
      iv ~= i;
    }

    double x = average(iv);
    x += average(new IntVector([1, 2, 3, 4]));
    RealVector rv = half(new RealVector([10.0f, 10.5f, 11.0f, 11.5f]));

    auto dv = new DoubleVector();
    for (size_t i = 0; i < SIZE; i++) {
      dv ~= i / 2.0;
    }
    halve_in_place(dv);

    RealVector v0 = vecreal(new RealVector());
    float flo = 123.456f;
    v0 ~= flo;
    flo = v0[0];

    IntVector v1 = vecintptr(new IntVector());
    IntPtrVector v2 = vecintptr(new IntPtrVector());
    IntConstPtrVector v3 = vecintconstptr(new IntConstPtrVector());

    v1 ~= 123;
    v2.clear();
    v3.clear();

    StructVector v4 = vecstruct(new StructVector());
    StructPtrVector v5 = vecstructptr(new StructPtrVector());
    StructConstPtrVector v6 = vecstructconstptr(new StructConstPtrVector());

    v4 ~= new Struct(123);
    v5 ~= new Struct(123);
    v6 ~= new Struct(123);
  }

  // Test vectors of pointers.
  {
    auto vector = new StructPtrVector();
    for (size_t i = 0; i < SIZE; i++) {
      vector ~= new Struct(i / 10.0);
    }

    Struct[] array = vector[];

    for (size_t i = 0; i < SIZE; i++) {
      // Make sure that a shallow copy has been made.
      void* aPtr = Struct.swigGetCPtr(array[i]);
      void* vPtr = Struct.swigGetCPtr(vector[i]);
      if (aPtr != vPtr) {
	throw new Exception("StructPtrVector test 1 failed, i: " ~
	  Integer.toString(i));
      }
    }
  }

  // Test vectors of const pointers.
  {
    auto vector = new StructConstPtrVector();
    for (size_t i = 0; i < SIZE; i++) {
      vector ~= new Struct(i / 10.0);
    }

    Struct[] array = vector[];

    for (size_t i = 0; i < SIZE; i++) {
      // Make sure that a shallow copy has been made.
      void* aPtr = Struct.swigGetCPtr(array[i]);
      void* vPtr = Struct.swigGetCPtr(vector[i]);
      if (aPtr != vPtr) {
	throw new Exception("StructConstPtrVector test 1 failed, i: " ~
	  Integer.toString(i));
      }
    }
  }

  // Test vectors destroyed via dispose().
  {
    {
      scope vector = new StructVector();
      vector ~= new Struct(0.0);
      vector ~= new Struct(11.1);
    }
    {
      scope vector = new DoubleVector();
      vector ~= 0.0;
      vector ~= 11.1;
    }
  }
}