00001
00002
00003 #if defined(_MSC_VER)
00004
00005 # pragma warning(disable:4786)
00006 #endif
00007
00008 #include <cassert>
00009
00010 #include "CoinIndexedVector.hpp"
00011 #include "CoinShallowPackedVector.hpp"
00012
00013 #ifdef NDEBUG
00014 #undef NDEBUG
00015 #endif
00016
00017
00018 void
00019 CoinIndexedVectorUnitTest()
00020 {
00021
00022 int i;
00023
00024 {
00025 CoinIndexedVector r;
00026 assert( r.indices_==NULL );
00027 assert( r.elements_==NULL );
00028 assert( r.getNumElements()==0 );
00029 assert( r.capacity_==0);
00030 }
00031
00032
00033 const int ne = 4;
00034 int inx[ne] = { 1, 3, 4, 7 };
00035 double el[ne] = { 1.2, 3.4, 5.6, 7.8 };
00036 {
00037 CoinIndexedVector r;
00038 assert( r.getNumElements()==0 );
00039
00040
00041 r.setVector( ne, inx, el );
00042 assert( r.getNumElements()==ne );
00043 for ( i=0; i<ne; i++ ) {
00044 assert( r.getIndices()[i] == inx[i] );
00045 assert( r[inx[i]] == el[i] );
00046 }
00047
00048
00049 const int ne2 = 5;
00050 int inx2[ne2] = { 2, 4, 8, 14, 3 };
00051 double el2[ne2] = { 2.2, 4.4, 6.6, 8.8, 3.3 };
00052
00053 r.setVector(ne2,inx2,el2);
00054
00055 assert( r.getNumElements()==ne2 );
00056
00057 assert( r.getIndices()[0]==inx2[0] );
00058
00059 assert( r.getIndices()[1]==inx2[1] );
00060
00061 assert( r.getIndices()[2]==inx2[2] );
00062
00063 assert( r.getIndices()[3]==inx2[3] );
00064
00065 assert( r.getIndices()[4]==inx2[4] );
00066
00067
00068 CoinIndexedVector r1(ne2,inx2,el2);
00069 assert( r == r1 );
00070 }
00071 CoinIndexedVector r;
00072
00073
00074 {
00075 CoinIndexedVector r;
00076 const int ne = 3;
00077 int inx[ne] = { 1, 2, 3 };
00078 double el[ne] = { 2.2, 4.4, 8.8};
00079 r.setVector(ne,inx,el);
00080 int c = r.capacity();
00081
00082 r.swap(0,2);
00083 assert( r.getIndices()[0]==3 );
00084 assert( r.getIndices()[1]==2 );
00085 assert( r.getIndices()[2]==1 );
00086 assert( r.capacity() == c );
00087
00088
00089 CoinIndexedVector s;
00090 const int nes = 4;
00091 int inxs[nes] = { 11, 12, 13, 14 };
00092 double els[nes] = { .122, 14.4, 18.8, 19.9};
00093 s.setVector(nes,inxs,els);
00094 r.append(s);
00095 assert( r.getNumElements()==7 );
00096 assert( r.getIndices()[0]==3 );
00097 assert( r.getIndices()[1]==2 );
00098 assert( r.getIndices()[2]==1 );
00099 assert( r.getIndices()[3]==11 );
00100 assert( r.getIndices()[4]==12 );
00101 assert( r.getIndices()[5]==13 );
00102 assert( r.getIndices()[6]==14 );
00103
00104
00105 c = r.capacity();
00106 r.truncate(4);
00107
00108 assert( r.getNumElements()==3 );
00109 assert( r.getIndices()[0]==3 );
00110 assert( r.getIndices()[1]==2 );
00111 assert( r.getIndices()[2]==1 );
00112 assert( r.getMaxIndex() == 3 );
00113 assert( r.getMinIndex() == 1 );
00114 assert( r.capacity() == c );
00115 }
00116
00117
00118 {
00119 CoinIndexedVector r,r2;
00120 const int ne = 3;
00121 int inx[ne] = { 1, 2, 3 };
00122 double el[ne] = { 2.2, 4.4, 8.8};
00123 double els[4] = { 0.0,2.2, 4.4, 8.8};
00124 r.setVector(ne,inx,el);
00125 r2.borrowVector(4,ne,inx,els);
00126 assert (r==r2);
00127 r2.returnVector();
00128 assert (!r2.capacity());
00129 assert (!r2.getNumElements());
00130 assert (!r2.denseVector());
00131 assert (!r2.getIndices());
00132 }
00133
00134
00135 {
00136 CoinIndexedVector rhs;
00137 {
00138 CoinIndexedVector r;
00139 {
00140 CoinIndexedVector rC1(r);
00141 assert( 0==r.getNumElements() );
00142 assert( 0==rC1.getNumElements() );
00143
00144
00145 r.setVector( ne, inx, el );
00146
00147 assert( ne==r.getNumElements() );
00148 assert( 0==rC1.getNumElements() );
00149 }
00150
00151 CoinIndexedVector rC2(r);
00152
00153 assert( ne==r.getNumElements() );
00154 assert( ne==rC2.getNumElements() );
00155
00156 for ( i=0; i<ne; i++ ) {
00157 assert( r.getIndices()[i] == rC2.getIndices()[i] );
00158 }
00159
00160 rhs=rC2;
00161 }
00162
00163 assert( rhs.getNumElements()==ne );
00164
00165 for ( i=0; i<ne; i++ ) {
00166 assert( inx[i] == rhs.getIndices()[i] );
00167 }
00168 }
00169
00170
00171 {
00172 CoinIndexedVector v1,v2;
00173 assert( v1==v2 );
00174 assert( v2==v1 );
00175 assert( v1==v1 );
00176 assert( !(v1!=v2) );
00177
00178 v1.setVector( ne, inx, el );
00179 assert ( !(v1==v2) );
00180 assert ( v1!=v2 );
00181
00182 CoinIndexedVector v3(v1);
00183 assert( v3==v1 );
00184 assert( v3!=v2 );
00185
00186 CoinIndexedVector v4(v2);
00187 assert( v4!=v1 );
00188 assert( v4==v2 );
00189 }
00190
00191 {
00192
00193 const int ne = 4;
00194 int inx[ne] = { 1, 4, 0, 2 };
00195 double el[ne] = { 10., 40., 1., 20. };
00196 CoinIndexedVector r;
00197 r.setVector(ne,inx,el);
00198
00199
00200 r.sort();
00201 for ( i=1; i<ne; i++ ) assert( r.getIndices()[i-1] < r.getIndices()[i] );
00202
00203 }
00204 {
00205
00206 const int ne = 4;
00207 int inx[ne] = { 1, 4, 0, 2 };
00208 double el[ne] = { 10., 40., 1., 50. };
00209 CoinIndexedVector r;
00210 bool errorThrown = false;
00211 try {
00212 assert( r[1]==0. );
00213 }
00214 catch (CoinError e) {
00215 errorThrown = true;
00216 }
00217 assert( errorThrown );
00218
00219 r.setVector(ne,inx,el);
00220
00221 errorThrown = false;
00222 try {
00223 assert( r[-1]==0. );
00224 }
00225 catch (CoinError e) {
00226 errorThrown = true;
00227 }
00228 assert( errorThrown );
00229
00230 assert( r[ 0]==1. );
00231 assert( r[ 1]==10.);
00232 assert( r[ 2]==50.);
00233 assert( r[ 3]==0. );
00234 assert( r[ 4]==40.);
00235 errorThrown = false;
00236 try {
00237 assert( r[5]==0. );
00238 }
00239 catch (CoinError e) {
00240 errorThrown = true;
00241 }
00242 assert( errorThrown );
00243
00244 assert ( r.getMaxIndex()==4 );
00245 assert ( r.getMinIndex()==0 );
00246 }
00247
00248
00249
00250 {
00251 CoinIndexedVector nullVec;
00252 assert( nullVec.getMaxIndex() ==
00253 INT_MIN );
00254 assert( nullVec.getMinIndex() ==
00255 INT_MAX );
00256 }
00257
00258
00259 {
00260 const int ne = 4;
00261 int inx1[ne] = { 1, 3, 4, 7 };
00262 double el1[ne] = { 1.2, 3.4, 5.6, 7.8 };
00263 int inx2[ne] = { 7, 4, 3, 1 };
00264 double el2[ne] = { 7.8+.5, 5.6+.5, 3.4+.5, 1.2+.5 };
00265 CoinIndexedVector v1,v2;
00266 v1.setVector(ne,inx1,el1);
00267 v2.setVector(ne,inx2,el2);
00268 }
00269
00270 {
00271
00272 CoinIndexedVector v1,v2;
00273 assert( v1.capacity()==0 );
00274 v1.reserve(6);
00275 assert( v1.capacity()==6 );
00276 assert( v1.getNumElements()==0 );
00277 v2=v1;
00278 assert( v2.capacity() == 6 );
00279 assert( v2.getNumElements()==0 );
00280 assert( v2==v1 );
00281 v1.setVector(0,NULL,NULL);
00282 assert( v1.capacity()==6 );
00283 assert( v1.getNumElements()==0 );
00284 assert( v2==v1 );
00285 v2=v1;
00286 assert( v2.capacity() == 6 );
00287 assert( v2.getNumElements()==0 );
00288 assert( v2==v1 );
00289
00290 const int ne = 2;
00291 int inx[ne] = { 1, 3 };
00292 double el[ne] = { 1.2, 3.4 };
00293 v1.setVector(ne,inx,el);
00294 assert( v1.capacity()==6 );
00295 assert( v1.getNumElements()==2 );
00296 v2=v1;
00297 assert( v2.capacity()==6 );
00298 assert( v2.getNumElements()==2 );
00299 assert( v2==v1 );
00300
00301 const int ne1 = 5;
00302 int inx1[ne1] = { 1, 3, 4, 5, 6 };
00303 double el1[ne1] = { 1.2, 3.4, 5., 6., 7. };
00304 v1.setVector(ne1,inx1,el1);
00305 assert( v1.capacity()==7 );
00306 assert( v1.getNumElements()==5 );
00307 v2=v1;
00308 assert( v2.capacity()==7 );
00309 assert( v2.getNumElements()==5 );
00310 assert( v2==v1 );
00311
00312 const int ne2 = 8;
00313 int inx2[ne2] = { 1, 3, 4, 5, 6, 7, 8, 9 };
00314 double el2[ne2] = { 1.2, 3.4, 5., 6., 7., 8., 9., 10. };
00315 v1.setVector(ne2,inx2,el2);
00316 assert( v1.capacity()==10 );
00317 assert( v1.getNumElements()==8 );
00318 v2=v1;
00319 assert( v2.getNumElements()==8 );
00320 assert( v2==v1 );
00321
00322 v1.setVector(ne1,inx1,el1);
00323 assert( v1.capacity()==10 );
00324 assert( v1.getNumElements()==5 );
00325 v2=v1;
00326 assert( v2.capacity()==10 );
00327 assert( v2.getNumElements()==5 );
00328 assert( v2==v1 );
00329
00330 v1.reserve(7);
00331 assert( v1.capacity()==10 );
00332 assert( v1.getNumElements()==5 );
00333 v2=v1;
00334 assert( v2.capacity()==10 );
00335 assert( v2.getNumElements()==5 );
00336 assert( v2==v1 );
00337
00338 }
00339
00340
00341 {
00342 CoinIndexedVector v1;
00343 assert( v1.getNumElements()==0 );
00344 assert( v1.capacity()==0 );
00345
00346 v1.insert(1,1.);
00347 assert( v1.getNumElements()==1 );
00348 assert( v1.capacity()==2 );
00349 assert( v1.getIndices()[0] == 1 );
00350
00351 v1.insert(10,10.);
00352 assert( v1.getNumElements()==2 );
00353 assert( v1.capacity()==11 );
00354 assert( v1.getIndices()[1] == 10 );
00355
00356 v1.insert(20,20.);
00357 assert( v1.getNumElements()==3 );
00358 assert( v1.capacity()==21 );
00359 assert( v1.getIndices()[2] == 20 );
00360
00361 v1.insert(30,30.);
00362 assert( v1.getNumElements()==4 );
00363 assert( v1.capacity()==31 );
00364 assert( v1.getIndices()[3] == 30 );
00365
00366 v1.insert(40,40.);
00367 assert( v1.getNumElements()==5 );
00368 assert( v1.capacity()==41 );
00369 assert( v1.getIndices()[4] == 40 );
00370
00371 v1.insert(50,50.);
00372 assert( v1.getNumElements()==6 );
00373 assert( v1.capacity()==51 );
00374 assert( v1.getIndices()[5] == 50 );
00375
00376 CoinIndexedVector v2;
00377 const int ne1 = 3;
00378 int inx1[ne1] = { 1, 3, 4 };
00379 double el1[ne1] = { 1.2, 3.4, 5. };
00380 v2.setVector(ne1,inx1,el1);
00381 assert( v2.getNumElements()==3 );
00382 assert( v2.capacity()==5 );
00383
00384
00385 assert(v2.clean(3.0)==2);
00386 assert(v2.denseVector()[1]==0.0);
00387
00388
00389
00390
00391 v2.checkClean();
00392 assert( v2.getNumElements()==2 );
00393
00394
00395 assert(v2.clean(10.0)==0);
00396 v2.checkClear();
00397
00398 }
00399
00400 {
00401
00402 CoinIndexedVector v2;
00403 const int ne1 = 3;
00404 int inx1[ne1] = { 1, 3, 4 };
00405 v2.setConstant(ne1,inx1,3.14);
00406 assert( v2.getNumElements()==3 );
00407 assert( v2.capacity()==5 );
00408 assert( v2.getIndices()[0]==1 );
00409 assert( v2.getIndices()[1]==3 );
00410 assert( v2.getIndices()[2]==4 );
00411
00412 assert( v2[3] == 3.14 );
00413
00414 CoinIndexedVector v2X(ne1,inx1,3.14);
00415 assert( v2 == v2X );
00416
00417 }
00418
00419 {
00420
00421 CoinIndexedVector v2;
00422 const int ne2 = 3;
00423 double el2[ne2] = { 1., 3., 4. };
00424 v2.setFull(ne2,el2);
00425 assert( v2.getNumElements()==3 );
00426 assert( v2.capacity()==3 );
00427 assert( v2.getIndices()[0]==0 );
00428 assert( v2.getIndices()[1]==1 );
00429 assert( v2.getIndices()[2]==2 );
00430
00431 assert( v2[1] == 3. );
00432
00433 CoinIndexedVector v2X(ne2,el2);
00434 assert( v2 == v2X );
00435
00436 v2.setFull(0,el2);
00437 assert( v2[2] == 0. );
00438 }
00439
00440
00441 #if 0
00442
00443
00444 {
00445 const int ne = 4;
00446 int inx1[ne] = { 1, 3, 4, 7 };
00447 double el1[ne] = { 1.2, 3.4, 5.6, 7.8 };
00448 CoinIndexedVector v1;
00449 v1.set(-ne,inx1,el1);
00450 }
00451 #endif
00452
00453
00454
00455 {
00456 const int ne = 4;
00457 int inx[ne] = { 1, 4, 0, 2 };
00458 double el[ne] = { 10., 40., 1., 50. };
00459 CoinIndexedVector std(ne,inx,el);
00460 CoinShallowPackedVector * spvP = new CoinShallowPackedVector(ne,inx,el);
00461 CoinIndexedVector pv(*spvP);
00462 assert( pv == std );
00463 delete spvP;
00464 assert( pv == std );
00465 }
00466
00467
00468 {
00469 const int ne = 4;
00470 int inx[ne] = { 1, 4, 0, 2 };
00471 double el[ne] = { 10., 40., 1., 50. };
00472 CoinIndexedVector std(ne,inx,el);
00473 CoinShallowPackedVector * spvP = new CoinShallowPackedVector(ne,inx,el);
00474 CoinIndexedVector pv;
00475 pv = *spvP;
00476 assert( pv == std );
00477 delete spvP;
00478 assert( pv == std );
00479 }
00480
00481 {
00482
00483
00484 const int ne = 4;
00485 int inx[ne] = { 1, 4, 0, 2 };
00486 double el[ne] = { 10., 40., 1., 50. };
00487 CoinIndexedVector r(ne,inx,el);
00488
00489 assert( r.getIndices()[0]== 1 );
00490 assert( r.getIndices()[1]== 4 );
00491 assert( r.getIndices()[2]== 0 );
00492 assert( r.getIndices()[3]== 2 );
00493
00494 assert( r[ 0]==1. );
00495 assert( r[ 1]==10.);
00496 assert( r[ 2]==50.);
00497 assert( r[ 3]==0. );
00498 assert( r[ 4]==40.);
00499
00500 r.sortIncrElement();
00501
00502 assert( r.getIndices()[0]== 0 );
00503 assert( r.getIndices()[1]== 1 );
00504 assert( r.getIndices()[2]== 4 );
00505 assert( r.getIndices()[3]== 2 );
00506
00507 assert( r[ 0]==1. );
00508 assert( r[ 1]==10.);
00509 assert( r[ 2]==50.);
00510 assert( r[ 3]==0. );
00511 assert( r[ 4]==40.);
00512
00513 CoinIndexedVector r1;
00514 r1=r;
00515 assert( r==r1 );
00516
00517 CoinIndexedVector add = r + r1;
00518 assert( add[0] == 1.+ 1. );
00519 assert( add[1] == 10.+10. );
00520 assert( add[2] == 50.+50. );
00521 assert( add[3] == 0.+ 0. );
00522 assert( add[4] == 40.+40. );
00523
00524 }
00525
00526 }
00527
00528
00529