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
|
module li_std_vector_runme;
import std.algorithm;
import std.array;
import std.conv;
import std.exception;
import std.stdio;
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();
foreach (int i; 0 .. SIZE) {
vector ~= i * 10;
}
enforce(vector.length == SIZE, "length test failed.");
vector[0] = 200;
enforce(vector[0] == 200, "indexing test failed");
vector[0] = 0 * 10;
enforceThrows((){ vector[vector.length] = 777; }, "out of range test failed" );
foreach (i, value; vector) {
enforce(value == (i * 10), "foreach test failed, i: " ~ to!string(i));
}
enforce(canFind!`a == 0 * 10`(vector[]), "canFind test 1 failed");
enforce(canFind!`a == 10 * 10`(vector[]), "canFind test 2 failed");
enforce(canFind!`a == 19 * 10`(vector[]), "canFind test 3 failed");
enforce(!canFind!`a == 20 * 10`(vector[]), "canFind test 4 failed");
foreach (i, _; vector) {
enforce(countUntil(vector[], i * 10) == i, "indexOf test failed, i: " ~ to!string(i));
}
enforce(countUntil(vector[], 42) == -1, "non-existant item indexOf test failed");
vector.clear();
enforce(vector.length == 0, "clear test failed");
}
// To array conversion tests.
{
auto dVector = new DoubleVector();
foreach (i; 0 .. SIZE) {
dVector ~= i * 10.1f;
}
double[] dArray = array(dVector[]);
foreach (i, value; dArray) {
enforce(dVector[i] == value, "slice test 1 failed, i: " ~ to!string(i));
}
auto sVector = new StructVector();
foreach (i; 0 .. SIZE) {
sVector ~= new Struct(i / 10.0);
}
Struct[] sArray = array(sVector[]);
foreach (i; 0 .. SIZE) {
// Make sure that a shallow copy has been made.
void* aPtr = Struct.swigGetCPtr(sArray[i]);
void* vPtr = Struct.swigGetCPtr(sVector[i]);
enforce(aPtr == vPtr, "slice test 2 failed, i: " ~ to!string(i));
}
}
// remove() tests.
{
auto iVector = new IntVector();
foreach (int i; 0 .. SIZE) {
iVector ~= i;
}
iVector.remove(iVector.length - 1);
iVector.remove(SIZE / 2);
iVector.remove(0);
enforceThrows((){ iVector.remove(iVector.length); }, "remove test failed");
}
// Capacity tests.
{
auto dv = new DoubleVector(10);
enforce(dv.capacity == 10, "constructor setting capacity test failed (1)");
enforce(dv.length == 0, "constructor setting capacity test failed (1)");
dv.reserve(20);
enforce(dv.capacity == 20, "capacity test failed");
}
// Test the methods being wrapped.
{
auto iv = new IntVector();
foreach (int i; 0 .. 4) {
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();
foreach (i; 0 .. SIZE) {
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();
foreach (i; 0 .. SIZE) {
vector ~= new Struct(i / 10.0);
}
Struct[] array = array(vector[]);
foreach (i; 0 .. SIZE) {
// Make sure that a shallow copy has been made.
void* aPtr = Struct.swigGetCPtr(array[i]);
void* vPtr = Struct.swigGetCPtr(vector[i]);
enforce(aPtr == vPtr, "StructConstPtrVector test 1 failed, i: " ~ to!string(i));
}
}
// Test vectors of const pointers.
{
auto vector = new StructConstPtrVector();
foreach (i; 0 .. SIZE) {
vector ~= new Struct(i / 10.0);
}
Struct[] array = array(vector[]);
foreach (i; 0 .. SIZE) {
// Make sure that a shallow copy has been made.
void* aPtr = Struct.swigGetCPtr(array[i]);
void* vPtr = Struct.swigGetCPtr(vector[i]);
enforce(aPtr == vPtr, "StructConstPtrVector test 1 failed, i: " ~ to!string(i));
}
}
// Test vectors destroyed via scope.
{
{
scope vector = new StructVector();
vector ~= new Struct(0.0);
vector ~= new Struct(11.1);
}
{
scope vector = new DoubleVector();
vector ~= 0.0;
vector ~= 11.1;
}
}
}
private void enforceThrows(void delegate() dg, string errorMessage) {
bool hasThrown;
try {
dg();
} catch (Exception) {
hasThrown = true;
} finally {
if (!hasThrown) {
throw new Exception(errorMessage);
}
}
}
|