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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
|
// This test tests all the methods in the C# collection wrapper
using System;
using li_std_vectorNamespace;
public class li_std_vector_runme {
private static readonly int collectionSize = 20;
private static readonly int midCollection = collectionSize/2;
public static void Main() {
// Setup collection
DoubleVector vect = new DoubleVector();
for (int i=0; i<collectionSize; i++) {
double num = i*10.1;
vect.Add(num);
}
// Count property test
if (vect.Count != collectionSize)
throw new Exception("Count test failed");
// IsFixedSize property test
if (vect.IsFixedSize)
throw new Exception("IsFixedSize test failed");
// IsReadOnly property test
if (vect.IsReadOnly)
throw new Exception("IsReadOnly test failed");
// Item indexing
vect[0] = 200.1;
if (vect[0] != 200.1)
throw new Exception("Item property test failed");
vect[0] = 0*10.1;
try {
vect[-1] = 777.1;
throw new Exception("Item out of range (1) test failed");
} catch (ArgumentOutOfRangeException) {
}
try {
vect[vect.Count] = 777.1;
throw new Exception("Item out of range (2) test failed");
} catch (ArgumentOutOfRangeException) {
}
// CopyTo() test
{
double[] outputarray = new double[collectionSize];
vect.CopyTo(outputarray);
int index = 0;
foreach(double val in outputarray) {
if (vect[index] != val)
throw new Exception("CopyTo (1) test failed, index:" + index);
index++;
}
}
{
double[] outputarray = new double[midCollection+collectionSize];
vect.CopyTo(outputarray, midCollection);
int index = midCollection;
foreach(double val in vect) {
if (outputarray[index] != val)
throw new Exception("CopyTo (2) test failed, index:" + index);
index++;
}
}
{
double[] outputarray = new double[3];
vect.CopyTo(10, outputarray, 1, 2);
if (outputarray[0] != 0.0 || outputarray[1] != vect[10] || outputarray[2] != vect[11])
throw new Exception("CopyTo (3) test failed");
}
{
double[] outputarray = new double[collectionSize-1];
try {
vect.CopyTo(outputarray);
throw new Exception("CopyTo (4) test failed");
} catch (ArgumentException) {
}
}
{
double[,] outputarray = new double[collectionSize,collectionSize];
try {
vect.CopyTo(outputarray);
throw new Exception("CopyTo (5) test failed");
} catch (ArgumentException) {
}
}
{
StructVector inputvector = new StructVector();
int arrayLen = 10;
for (int i=0; i<arrayLen; i++) {
inputvector.Add(new Struct(i/10.0));
}
Struct[] outputarray = new Struct[arrayLen];
inputvector.CopyTo(outputarray);
for(int i=0; i<arrayLen; i++) {
if (outputarray[i].num != inputvector[i].num)
throw new Exception("CopyTo (6) test failed, i:" + i);
}
foreach (Struct s in inputvector) {
s.num += 20.0;
}
for(int i=0; i<arrayLen; i++) {
if (outputarray[i].num + 20.0 != inputvector[i].num )
throw new Exception("CopyTo (7) test failed (only a shallow copy was made), i:" + i);
}
}
// Contains() test
if (!vect.Contains(0*10.1))
throw new Exception("Contains test 1 failed");
if (!vect.Contains(10*10.1))
throw new Exception("Contains test 2 failed");
if (!vect.Contains(19*10.1))
throw new Exception("Contains test 3 failed");
if (vect.Contains(20*10.1))
throw new Exception("Contains test 4 failed");
{
// ICollection constructor
double[] doubleArray = new double[] { 0.0, 11.1, 22.2, 33.3, 44.4, 55.5, 33.3 };
DoubleVector dv = new DoubleVector(doubleArray);
if (doubleArray.Length != dv.Count)
throw new Exception("ICollection constructor length check failed: " + doubleArray.Length + "-" + dv.Count);
for (int i=0; i<doubleArray.Length; i++) {
if (doubleArray[i] != dv[i])
throw new Exception("ICollection constructor failed, index:" + i);
}
{
Struct[] structArray = new Struct[] { new Struct(0.0), new Struct(11.1), new Struct(22.2), new Struct(33.3) };
StructVector sv = new StructVector(structArray);
for (int i=0; i<structArray.Length; i++) {
structArray[i].num += 200.0;
}
for (int i=0; i<structArray.Length; i++) {
if (structArray[i].num != sv[i].num + 200.0)
throw new Exception("ICollection constructor not a deep copy, index:" + i);
}
}
// IndexOf() test
for (int i=0; i<collectionSize; i++) {
if (vect.IndexOf(i*10.1) != i)
throw new Exception("IndexOf test " + i + " failed");
}
if (vect.IndexOf(200.1) != -1)
throw new Exception("IndexOf non-existent test failed");
if (dv.IndexOf(33.3) != 3)
throw new Exception("IndexOf position test failed");
// LastIndexOf() test
for (int i=0; i<collectionSize; i++) {
if (vect.LastIndexOf(i*10.1) != i)
throw new Exception("LastIndexOf test " + i + " failed");
}
if (vect.LastIndexOf(200.1) != -1)
throw new Exception("LastIndexOf non-existent test failed");
if (dv.LastIndexOf(33.3) != 6)
throw new Exception("LastIndexOf position test failed");
}
{
// Repeat() test
try {
DoubleVector d = DoubleVector.Repeat(77.7, -1);
throw new Exception("Repeat negative count test failed");
} catch (ArgumentOutOfRangeException) {
}
DoubleVector dv = DoubleVector.Repeat(77.7, 5);
if (dv.Count != 5)
throw new Exception("Repeat count test failed");
// Also tests enumerator
System.Collections.IEnumerator myEnumerator = dv.GetEnumerator();
while ( myEnumerator.MoveNext() ) {
if ((double)myEnumerator.Current != 77.7)
throw new Exception("Repeat test failed");
}
}
{
// InsertRange() test
DoubleVector dvect = new DoubleVector();
for (int i=0; i<5; i++) {
dvect.Add(1000.0*i);
}
vect.InsertRange(midCollection, dvect);
if (vect.Count != collectionSize+dvect.Count)
throw new Exception("InsertRange test size failed");
for (int i=0; i<midCollection; i++) {
if (vect.IndexOf(i*10.1) != i)
throw new Exception("InsertRange (1) test " + i + " failed");
}
for (int i=0; i<dvect.Count; i++) {
if (vect[i+midCollection] != dvect[i])
throw new Exception("InsertRange (2) test " + i + " failed");
}
for (int i=midCollection; i<collectionSize; i++) {
if (vect.IndexOf(i*10.1) != i+dvect.Count)
throw new Exception("InsertRange (3) test " + i + " failed");
}
// RemoveRange() test
vect.RemoveRange(midCollection, dvect.Count);
if (vect.Count != collectionSize)
throw new Exception("RemoveRange test size failed");
for (int i=0; i<collectionSize; i++) {
if (vect.IndexOf(i*10.1) != i)
throw new Exception("RemoveRange test " + i + " failed");
}
try {
vect.RemoveRange(-1, 0);
throw new Exception("RemoveRange index out of range (1) test failed");
} catch (ArgumentOutOfRangeException) {
}
try {
vect.RemoveRange(collectionSize+1, 0);
throw new Exception("RemoveRange index out of range (2) test failed");
} catch (ArgumentOutOfRangeException) {
}
// AddRange() test
vect.AddRange(dvect);
if (vect.Count != collectionSize+dvect.Count)
throw new Exception("AddRange test size failed");
for (int i=0; i<collectionSize; i++) {
if (vect.IndexOf(i*10.1) != i)
throw new Exception("AddRange (1) test " + i + " failed");
}
for (int i=0; i<dvect.Count; i++) {
if (vect[i+collectionSize] != dvect[i])
throw new Exception("AddRange (2) test " + i + " failed");
}
vect.RemoveRange(collectionSize, dvect.Count);
// GetRange() test
int rangeSize = 5;
DoubleVector returnedVec = vect.GetRange(midCollection, rangeSize);
if (returnedVec.Count != rangeSize)
throw new Exception("GetRange test size failed");
for (int i=0; i<rangeSize; i++) {
if (returnedVec.IndexOf((i+midCollection)*10.1) != i)
throw new Exception("GetRange test " + i + " failed");
}
try {
vect.GetRange(-1, 0);
throw new Exception("GetRange index out of range (1) test failed");
} catch (ArgumentOutOfRangeException) {
}
try {
vect.GetRange(collectionSize+1, 0);
throw new Exception("GetRange index out of range (2) test failed");
} catch (ArgumentOutOfRangeException) {
}
{
StructVector inputvector = new StructVector();
int arrayLen = 10;
for (int i=0; i<arrayLen; i++) {
inputvector.Add(new Struct(i/10.0));
}
StructVector outputvector = inputvector.GetRange(0,arrayLen);
for(int i=0; i<arrayLen; i++) {
if (outputvector[i].num != inputvector[i].num)
throw new Exception("GetRange (1) test failed, i:" + i);
}
foreach (Struct s in inputvector) {
s.num += 20.0;
}
for(int i=0; i<arrayLen; i++) {
if (outputvector[i].num + 20.0 != inputvector[i].num )
throw new Exception("GetRange (2) test failed (only a shallow copy was made), i:" + i);
}
}
}
// Insert() test
int pos = 0;
int count = vect.Count;
vect.Insert(pos, -5.1);
count++;
if (vect.Count != count || vect[pos] != -5.1)
throw new Exception("Insert at beginning test failed");
pos = midCollection;
vect.Insert(pos, 85.1);
count++;
if (vect.Count != count || vect[pos] != 85.1)
throw new Exception("Insert at " + pos + " test failed");
pos = vect.Count;
vect.Insert(pos, 195.1);
count++;
if (vect.Count != count || vect[pos] != 195.1)
throw new Exception("Insert at end test failed");
pos = vect.Count+1;
try {
vect.Insert(pos, 222.1); // should throw
throw new Exception("Insert after end (1) test failed");
} catch (ArgumentOutOfRangeException) {
}
if (vect.Count != count)
throw new Exception("Insert after end (2) test failed");
pos = -1;
try {
vect.Insert(pos, 333.1); // should throw
throw new Exception("Insert before start (1) test failed");
} catch (ArgumentOutOfRangeException) {
}
if (vect.Count != count)
throw new Exception("Insert before start (2) test failed");
// Remove() test
vect.Remove(195.1);
count--;
vect.Remove(-5.1);
count--;
vect.Remove(85.1);
count--;
vect.Remove(9999.1); // element does not exist, should quietly do nothing
if (vect.Count != count)
throw new Exception("Remove count check test failed");
for (int i=0; i<collectionSize; i++) {
if (vect[i] != i*10.1)
throw new Exception("Remove test failed, index:" + i);
}
// RemoveAt() test
vect.Insert(0, -4.1);
vect.Insert(midCollection, 84.1);
vect.Insert(vect.Count, 194.1);
vect.RemoveAt(vect.Count-1);
vect.RemoveAt(midCollection);
vect.RemoveAt(0);
try {
vect.RemoveAt(-1);
throw new Exception("RemoveAt test (1) failed");
} catch (ArgumentOutOfRangeException) {
}
try {
vect.RemoveAt(vect.Count);
throw new Exception("RemoveAt test (2) failed");
} catch (ArgumentOutOfRangeException) {
}
for (int i=0; i<collectionSize; i++) {
if (vect[i] != i*10.1)
throw new Exception("RemoveAt test (3) failed, index:" + i);
}
{
// Capacity test
try {
DoubleVector dvv = new DoubleVector(-1);
throw new Exception("constructor setting capacity (1) test failed");
} catch (ArgumentOutOfRangeException) {
}
DoubleVector dv = new DoubleVector(10);
if (dv.Capacity != 10 || dv.Count != 0)
throw new Exception("constructor setting capacity (2) test failed");
dv.Capacity = 20;
if (dv.Capacity != 20)
throw new Exception("capacity test (1) failed");
dv.Add(1.11);
try {
dv.Capacity = dv.Count-1;
throw new Exception("capacity test (2) failed");
} catch (ArgumentOutOfRangeException) {
}
// SetRange() test
for (int i=dv.Count; i<collectionSize; i++) {
dv.Add(0.0);
}
dv.SetRange(0, vect);
if (dv.Count != collectionSize)
throw new Exception("SetRange count check test failed");
for (int i=0; i<collectionSize; i++) {
if (vect[i] != dv[i])
throw new Exception("SetRange test failed, index:" + i);
}
// Reverse() test
dv.Reverse();
for (int i=0; i<collectionSize; i++) {
if (vect[i] != dv[collectionSize-i-1])
throw new Exception("Reverse test (1) failed, index:" + i);
}
dv.Reverse(0, collectionSize);
for (int i=0; i<collectionSize; i++) {
if (vect[i] != dv[i])
throw new Exception("Reverse test (2) failed, index:" + i);
}
dv.Reverse(0, 0); // should do nothing!
for (int i=0; i<collectionSize; i++) {
if (vect[i] != dv[i])
throw new Exception("Reverse test (2) failed, index:" + i);
}
try {
dv.Reverse(-1, 0);
throw new Exception("Reverse test (3) failed");
} catch (ArgumentOutOfRangeException) {
}
try {
dv.Reverse(collectionSize+1, 0);
throw new Exception("Reverse test (4) failed");
} catch (ArgumentOutOfRangeException) {
}
}
// foreach test
{
int index=0;
foreach (double s in vect) {
if (s != index*10.1)
throw new Exception("foreach test failed, index:" + index);
index++;
}
}
// Clear() test
vect.Clear();
if (vect.Count != 0)
throw new Exception("Clear failed");
// Finally test the methods being wrapped
{
IntVector iv = new IntVector();
for (int i=0; i<4; i++) {
iv.Add(i);
}
double x = li_std_vector.average(iv);
double y = li_std_vector.average( new IntVector( new int[] {1, 2, 3, 4} ) );
RealVector a = li_std_vector.half( new RealVector( new float[] {10F, 10.5F, 11F, 11.5F} ) );
DoubleVector dvec = new DoubleVector();
for (int i=0; i<10; i++) {
dvec.Add(i/2.0);
}
li_std_vector.halve_in_place(dvec);
}
}
}
|