Main Page | Namespace List | Class Hierarchy | Compound List | File List | Compound Members | File Members

DblVecTest.C

Go to the documentation of this file.
00001 
00061 #include <stdio.h>
00062 #include "DoubleVec.h"
00063 
00064 
00065 bool CheckConstructor()
00066 {
00067   bool rslt = true;
00068   size_t i;
00069 
00070   DoubleVec empty;
00071   if (empty.length() != 0) {
00072     printf("CheckConstructor: 1. bad length.  Length = %d\n", empty.length());
00073     rslt = false;
00074   }
00075   
00076   if (empty.getRefCnt() != 1) {
00077     printf("CheckConstructor: 2. bad refCnt.  RefCnt = %d\n", 
00078            empty.getRefCnt() );
00079     rslt = false;
00080   }
00081 
00082   const size_t len = 10;
00083   DoubleVec vec( len, 0.0 );
00084   if (vec.length() != len) {
00085     printf("CheckConstructor: 3. bad length.  Length = %d, should be = %d\n",
00086            vec.length(), len );
00087     rslt = false;
00088   }
00089 
00090   bool isZero = true;
00091   for (i = 0; i < len; i++) {
00092     if (vec[i] != 0.0) {
00093       isZero = false;
00094       break;
00095     }
00096   }
00097   if (!isZero) {
00098     printf("CheckConstructor: 4. Array should be initialized to zero\n");
00099     rslt = false;
00100   }
00101 
00102   size_t answer = 42;
00103   DoubleVec TheAnswer( len, answer );
00104   bool initOK = true;
00105 
00106   for (i = 0; i < len; i++) {
00107     if (TheAnswer[i] != answer) {
00108       initOK = false;
00109       break;
00110     }
00111   }
00112   if (!initOK) {
00113     printf("CheckConstructor: 5. Array should be initialized to %d\n",
00114            answer );
00115     rslt = false;
00116   }
00117 
00118   const double avacadoNumber = 6.02e23;
00119   const size_t bigLen = 4000;
00120   DoubleVec MrBig( bigLen );
00121   MrBig[ bigLen - 1 ] = avacadoNumber;
00122   if ((double)MrBig[ bigLen-1 ] != avacadoNumber) {
00123     printf("CheckConstructor: 6. Assignment to end of a largish array failed");
00124     printf("(double)MrBig[bigLen-1] = %7.4e\n", (double)MrBig[ bigLen-1 ] );
00125     rslt = false;
00126   }
00127   
00128   return rslt;
00129 } // CheckConstructor
00130 
00131 
00132 bool equals( DoubleVec &vec, const double *test)
00133 {
00134   const size_t len = vec.length();
00135   bool rslt = true;
00136   size_t i;
00137 
00138   for (i = 0; i < len; i++) {
00139     if (vec[i] != test[i]) {
00140       rslt = false;
00141       break;
00142     }
00143   }
00144 
00145   return rslt;
00146 } // equals
00147 
00148 bool AppendNewValues()
00149 {
00150   bool rslt = true;
00151 
00152   DoubleVec vec;
00153   
00154   const size_t len = 16;
00155   double *testVec = new double[len];
00156   size_t i;
00157   for (i = 0; i < len; i++) {
00158     vec.append( i+1 );
00159     testVec[i] = i+1;
00160   }
00161 
00162   if (vec.length() != len) {
00163     printf("AppendNewValues: 1. length = %d, should be %d\n",
00164            vec.length(), len);
00165     rslt = false;
00166   }
00167 
00168   if (! equals( vec, testVec )) {
00169     printf("AppendNewValues: 2. short test failed\n");
00170     rslt = false;
00171   }
00172   delete [] testVec;
00173 
00174   const size_t bigger = 6000;
00175   testVec = new double[bigger];  
00176   DoubleVec bigVec;
00177   for (i = 0; i < len; i++) {
00178     bigVec.append( i+1 );
00179     testVec[i] = i+1;
00180   }
00181   if (! equals( vec, testVec )) {
00182     printf("AppendNewValues: 3. long test failed\n");
00183     rslt = false;
00184   }
00185   delete [] testVec;
00186 
00187   DoubleVec a( len );
00188   testVec = new double[len*2];  
00189   for (i = 0; i < len; i++) {
00190     a[i] = i;
00191     testVec[i] = i;
00192   }
00193   for (i = len; i < len*2; i++) {
00194     a.append( i );
00195     testVec[i] = i;
00196   }
00197   if (! equals( a, testVec )) {
00198     printf("AppendNewValues: 4. append to existing array failed\n");
00199     rslt = false;
00200   }
00201   delete [] testVec;
00202   
00203   return rslt;
00204 } // AppendNewValues
00205 
00206 
00207 
00208 bool Assignment()
00209 {
00210   bool rslt = true;
00211 
00212   const size_t len = 20;
00213   DoubleVec vec(len);
00214   size_t i;
00215 
00216   for (i = 0; i < len; i++) {
00217     vec[i] = i * 2;
00218   }
00219   
00220   DoubleVec a = vec;
00221   if (a.getRefCnt() != 2) {
00222     printf("Assignment: 1. refCnt = %d, should be 2\n", a.getRefCnt() );
00223     rslt = false;
00224   }
00225 
00226   DoubleVec b(len/2);
00227   b = a;
00228   if (b.getRefCnt() != 3) {
00229     printf("Assignment: 2. refCnt = %d, should be 3\n", b.getRefCnt() );
00230     rslt = false;
00231   }
00232 
00233   if (b.length() != len) {
00234     printf("Assignment: 3. b.length() = %d, should be %d\n", 
00235            b.length(), len );
00236     rslt = false;
00237   }
00238 
00239   bool contentOK = true;
00240   for (i = 0; i < len; i++) {
00241     if (b[i] != i * 2) {
00242       contentOK = false;
00243       break;
00244     }
00245   }
00246   if (!contentOK) {
00247     printf("Assignment: 3. Array content is wrong\n");
00248     rslt = false;
00249   }
00250 
00251   // Assignment to "self" should not change the reference count
00252   size_t beforeRefCnt = b.getRefCnt();
00253   b = b;
00254   
00255   if (b.getRefCnt() != beforeRefCnt) {
00256     printf("Assignment: 4. refCnt = %d, should be %d\n",
00257            b.getRefCnt(), beforeRefCnt );
00258     rslt = false;
00259   }
00260 
00261   contentOK = true;
00262   for (i = 0; i < len; i++) {
00263     if (b[i] != i * 2) {
00264       contentOK = false;
00265       break;
00266     }
00267   }
00268   if (!contentOK) {
00269     printf("Assignment: 5. After self assignment, array content is wrong\n");
00270     rslt = false;
00271   }
00272 
00273   return rslt;
00274 } // Assignment
00275 
00276 
00277 DoubleVec globalVec1;
00278 
00279 bool checkRef(DoubleVec &vec, size_t len)
00280 {
00281   bool rslt = true;
00282   if (vec.getRefCnt() != 1) {
00283     printf("PassByReference: 1. refCnt = %d, should be 1\n", vec.getRefCnt() );
00284     rslt = false;
00285   }
00286 
00287   if (vec.length() != len) {
00288     printf("PassByReference: 2. length = %d, should be %d\n", 
00289            vec.length(), len );
00290     rslt = false;
00291   }
00292 
00293   DoubleVec a = vec;
00294   if (vec.getRefCnt() != 2) {
00295     printf("PassByReference: 3. refCnt = %d, should be 2\n", vec.getRefCnt() );
00296     rslt = false;
00297   }
00298 
00299   bool contentOK = true;
00300   size_t i;
00301   for (i = 0; i < len; i++) {
00302     if (a[i] != i) {
00303       printf("PassByReference: 4. Content is wrong" );
00304       rslt = false;
00305       break;
00306     }
00307   }
00308 
00309   globalVec1 = a;
00310   if (vec.getRefCnt() != 3) {
00311     printf("PassByReference: 5. refCnt = %d, should be 3\n", vec.getRefCnt() );
00312     rslt = false;
00313   }
00314 
00315   return rslt;
00316 } // checkRef
00317 
00318 bool PassByReference()
00319 {
00320   bool rslt = true;
00321   
00322   size_t len = 17;
00323   DoubleVec a(len);
00324   size_t i;
00325 
00326   for (i = 0; i < len; i++) {
00327     a[i] = i;
00328   }
00329   if (! checkRef(a, len)) {
00330     rslt = false;
00331   }
00332 
00333   // remember, we assigned to a global variable in checkRef()
00334   if (a.getRefCnt() != 2) {
00335     printf("PassByReference: 6. refCnt = %d, should be 2\n", a.getRefCnt() );
00336     rslt = false;
00337   }
00338 
00339   // remember, we assigned to a global variable in checkRef()
00340   if (globalVec1.length() != len) {
00341     printf("PassByReference: 7. length = %d, should be %d\n", 
00342            globalVec1.getRefCnt(), len );
00343     rslt = false;
00344   }
00345 
00346   bool contentOK = true;
00347   for (i = 0; i < len; i++) {
00348     if (globalVec1[i] != i) {
00349       printf("PassByReference: 8. Content is wrong" );
00350       rslt = false;
00351       break;
00352     }
00353   }
00354 
00355   return rslt;
00356 } // PassByReference
00357 
00358 
00359 DoubleVec globalVec2;
00360 
00361 bool checkPassByValue( DoubleVec a, size_t len )
00362 {
00363   bool rslt = true;
00364   if (a.getRefCnt() != 2) {
00365     printf("PassByValue: 1. refCnt = %d, should be 2\n", a.getRefCnt());
00366     rslt = false;
00367   }
00368 
00369   if (a.length() != len) {
00370     printf("PassByValue: 2. length = %d, should be %d\n", 
00371            a.length(), len);
00372     rslt = false;
00373   }
00374 
00375   DoubleVec b = a;
00376   if (a.getRefCnt() != 3) {
00377     printf("PassByValue: 3. refCnt = %d, should be 3\n", a.getRefCnt());
00378     rslt = false;
00379   }
00380 
00381   bool contentOK = true;
00382   size_t i;
00383   for (i = 0; i < len; i++) {
00384     if (b[i] != i) {
00385       printf("PassByValue: 4. Content is wrong" );
00386       rslt = false;
00387       break;
00388     }
00389   }
00390 
00391   globalVec2 = a;
00392   if (b.getRefCnt() != 4) {
00393     printf("PassByValue: 5. refCnt = %d, should be 4\n", b.getRefCnt());
00394     rslt = false;
00395   }
00396 
00397   return rslt;
00398 } // checkPassByValue
00399 
00400 
00401 bool PassByValue()
00402 {
00403   bool rslt = true;
00404 
00405   const size_t len = 21;
00406   DoubleVec a(len);
00407   size_t i;
00408 
00409   for (i = 0; i < len; i++) {
00410     a[i] = i;
00411   }
00412 
00413   if (!checkPassByValue( a, len )) {
00414     rslt = false;
00415   }
00416 
00417   // note: copy to globalVec2
00418   if (a.getRefCnt() != 2) {
00419     printf("PassByValue: 6. a.getRefCnt() = %d, should be 2\n", 
00420            a.getRefCnt());
00421     rslt = false;
00422   }
00423 
00424   if (globalVec2.getRefCnt() != 2) {
00425     printf("PassByValue: 7. globalVec2.getRefCnt() = %d, should be 2\n", 
00426            globalVec2.getRefCnt());
00427     rslt = false;
00428   }
00429 
00430   bool contentOK = true;
00431   for (i = 0; i < len; i++) {
00432     if (a[i] != i) {
00433       printf("PassByValue: 8. Content is wrong" );
00434       rslt = false;
00435       break;
00436     }
00437   }
00438 
00439   contentOK = true;
00440   for (i = 0; i < len; i++) {
00441     if (globalVec2[i] != i) {
00442       printf("PassByValue: 9. Content is wrong" );
00443       rslt = false;
00444       break;
00445     }
00446   }
00447 
00448   return rslt;
00449 } // PassByValue
00450 
00451 
00452 DoubleVec DoubleVecFactory(const size_t size)
00453 {
00454   DoubleVec v(size);
00455   return v;
00456 } // DoubleVecFactory
00457 
00458 
00459 DoubleVec &passThrough( DoubleVec &v )
00460 {
00461   return v;
00462 }
00463 
00464 
00465 DoubleVec makeCopy(DoubleVec &v )
00466 {
00467   size_t size = v.length();
00468   DoubleVec newVec(size);
00469 
00470   for (size_t i = 0; i < size; i++) {
00471     newVec[i] = (double)v[i];
00472   }
00473 
00474   return newVec;
00475 } // makeCopy
00476 
00477 
00482 bool CheckFuncReturn()
00483 {
00484   bool rslt = true;
00485   const size_t size = 20;
00486   DoubleVec v(size);
00487 
00488   size_t i;
00489   for (i = 0; i < size; i++) {
00490     v[i] = i + 1;
00491   }
00492 
00493   DoubleVec a = makeCopy( v );
00494   for (i = 0; i < size; i++) {
00495     if (a[i] != v[i]) {
00496       printf("CheckFuncReturn: 1. bad values\n");
00497       rslt = false;
00498       break;
00499     }
00500   }
00501 
00502   if (a.getRefCnt() != 1) {
00503     printf("CheckFuncReturn: 2. unexpected reference count\n");
00504     rslt = false;
00505   }
00506 
00507   if (v.getRefCnt() != 1) {
00508     printf("CheckFuncReturn: 3. unexpected reference count\n");
00509     rslt = false;
00510   }
00511 
00512   DoubleVec b = passThrough( v );
00513 
00514   if (b.getRefCnt() != 2) {
00515     printf("CheckFuncReturn: 4. unexpected reference count\n");
00516     rslt = false;
00517   }
00518 
00519   if (v.getRefCnt() != 2) {
00520     printf("CheckFuncReturn: 5. unexpected reference count\n");
00521     rslt = false;
00522   }
00523 
00524   const size_t s = 21;
00525   DoubleVec c = DoubleVecFactory( 21 );
00526   if (c.getRefCnt() != 1) {
00527     printf("CheckFuncReturn: 6. unexpected reference count\n");
00528     rslt = false;
00529   }
00530 
00531   if (c.length() != s) {
00532     printf("CheckFuncReturn: 7. size is not correct\n");
00533     rslt = false;
00534   }
00535 
00536   return rslt;
00537 } // CheckFuncReturn
00538 
00539 
00545 bool Index()
00546 {
00547   bool rslt = true;
00548   const size_t len = 31;
00549   size_t i;
00550   DoubleVec a;
00551 
00552   for (i = 0; i < len; i++) {
00553     a.append( i );
00554   }
00555 
00556   DoubleVec b = a;
00557   if (b.getRefCnt() != 2) {
00558     printf("Index: 1. reference count is wrong\n");
00559     rslt = false;
00560   }
00561 
00562   a[len/2] = 0;
00563 
00564   if (b.getRefCnt() != 1) {
00565     printf("Index: 2. reference count is wrong\n");
00566     rslt = false;
00567   }
00568 
00569   // b should be unchanged
00570   bool contentOK = true;
00571   for (i = 0; i < len; i++) {
00572     if (b[i] != i) {
00573       printf("Index: 3. Content is wrong" );
00574       rslt = false;
00575       break;
00576     }
00577   }
00578 
00579   // a should be changed: a[len/2] == 0
00580   contentOK = true;
00581   for (i = 0; i < len; i++) {
00582     if (i == len/2) {
00583       if (a[i] != 0) {
00584         printf("Index: 4. a[%d] = %f, should be 0.0\n",
00585                i, (double)a[i] );
00586         rslt = false;
00587         break;
00588       }
00589     }
00590     else if (a[i] != i) {
00591       printf("Index: 5. Content is wrong" );
00592       rslt = false;
00593       break;
00594     }
00595   }
00596 
00597   DoubleVec c(20, 42.0);
00598   DoubleVec d = c;
00599   if (c.getRefCnt() != 2) {
00600     printf("Index: 6. reference count is wrong\n");
00601     rslt = false;
00602   }
00603 
00604   return rslt;
00605 } // Index
00606 
00607 
00608 main()
00609 {
00610   bool passed = true;
00611 
00612   printf("Check constructors\n");
00613   if (! CheckConstructor()) {
00614     passed = false;
00615   }
00616 
00617   printf("Check append new values\n");
00618   if (! AppendNewValues()) {
00619     passed = false;
00620   }
00621 
00622   printf("Check assignment\n");
00623   if (! Assignment()) {
00624     passed = false;
00625   }
00626 
00627   printf("Check pass by reference\n");
00628   if (! PassByReference()) {
00629     passed = false;
00630   }
00631 
00632   printf("Check function return values\n");
00633   if (!CheckFuncReturn()) {
00634     passed = false;
00635   }
00636 
00637   printf("Check pass by value\n");
00638   if (! PassByValue()) {
00639     passed = false;
00640   }
00641 
00642   printf("Check index operation\n");
00643   if (! Index()) {
00644     passed = false;
00645   }
00646 
00647   if (passed)
00648     printf("test passed\n");
00649   else
00650     printf("test failed\n");
00651 }

Generated on Mon Sep 22 20:22:58 2003 by doxygen 1.3.3