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

DblVecTest.C File Reference

Regression tests for the DoubleVec reference counted class. More...

#include <stdio.h>
#include "DoubleVec.h"

Include dependency graph for DblVecTest.C:

Include dependency graph

Go to the source code of this file.

Functions

bool CheckConstructor ()
bool equals (DoubleVec &vec, const double *test)
bool AppendNewValues ()
bool Assignment ()
bool checkRef (DoubleVec &vec, size_t len)
bool PassByReference ()
bool checkPassByValue (DoubleVec a, size_t len)
bool PassByValue ()
DoubleVec DoubleVecFactory (const size_t size)
DoubleVecpassThrough (DoubleVec &v)
DoubleVec makeCopy (DoubleVec &v)
bool CheckFuncReturn ()
 Check that reference counted arrays work properly as function results.

bool Index ()
 At this point the RHS index is pretty well tested, since the tests above would have failed otherwise.

 main ()

Variables

DoubleVec globalVec1
DoubleVec globalVec2


Detailed Description

Regression tests for the DoubleVec reference counted class.

The DoubleVec class is an instantiation of the RCArray template, which supports reference counted arrays. As a result, this code serves as a regression test for RCArray.

Note that the existence of these regression tests does not change that statement this code is provided without waranty. You use this code at your own risk.

Areas to test:

Copyright and Use

You may use this source code without limitation and without fee as long as you include:

This software was written and is copyrighted by Ian Kaplan, Bear Products International, www.bearcave.com, 2001, 2002, 2003

This software is provided "as is", without any warrenty or claim as to its usefulness. Anyone who uses this source code uses it at their own risk. Nor is any support provided by Ian Kaplan and Bear Products International.

Please send any bug fixes or suggested source changes to:

iank@bearcave.com

Definition in file DblVecTest.C.


Function Documentation

bool AppendNewValues  ) 
 

Definition at line 148 of file DblVecTest.C.

References RCArray< double >::append(), equals(), and DoubleVec::length().

Referenced by main().

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

bool Assignment  ) 
 

Definition at line 208 of file DblVecTest.C.

References DoubleVec::getRefCnt(), and DoubleVec::length().

Referenced by main().

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

bool CheckConstructor  ) 
 

Definition at line 65 of file DblVecTest.C.

References DoubleVec::getRefCnt(), and DoubleVec::length().

Referenced by main().

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

bool CheckFuncReturn  ) 
 

Check that reference counted arrays work properly as function results.

Definition at line 482 of file DblVecTest.C.

References DoubleVecFactory(), DoubleVec::getRefCnt(), DoubleVec::length(), makeCopy(), and passThrough().

Referenced by main().

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

bool checkPassByValue DoubleVec  a,
size_t  len
 

Definition at line 361 of file DblVecTest.C.

References DoubleVec::getRefCnt(), globalVec2, and DoubleVec::length().

Referenced by PassByValue().

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

bool checkRef DoubleVec vec,
size_t  len
 

Definition at line 279 of file DblVecTest.C.

References DoubleVec::getRefCnt(), globalVec1, and DoubleVec::length().

Referenced by PassByReference().

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

DoubleVec DoubleVecFactory const size_t  size  ) 
 

Definition at line 452 of file DblVecTest.C.

Referenced by CheckFuncReturn().

00453 {
00454   DoubleVec v(size);
00455   return v;
00456 } // DoubleVecFactory

bool equals DoubleVec vec,
const double *  test
 

Definition at line 132 of file DblVecTest.C.

References DoubleVec::length().

Referenced by AppendNewValues().

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

bool Index  ) 
 

At this point the RHS index is pretty well tested, since the tests above would have failed otherwise.

So this function concentrates on testing the LHS index (e.g., address).

Definition at line 545 of file DblVecTest.C.

References RCArray< double >::append(), and DoubleVec::getRefCnt().

Referenced by main().

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

main  ) 
 

Definition at line 608 of file DblVecTest.C.

References AppendNewValues(), Assignment(), CheckConstructor(), CheckFuncReturn(), Index(), PassByReference(), and PassByValue().

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 }

DoubleVec makeCopy DoubleVec v  ) 
 

Definition at line 465 of file DblVecTest.C.

References DoubleVec::length().

Referenced by CheckFuncReturn().

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

bool PassByReference  ) 
 

Definition at line 318 of file DblVecTest.C.

References checkRef(), DoubleVec::getRefCnt(), globalVec1, and DoubleVec::length().

Referenced by main().

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

bool PassByValue  ) 
 

Definition at line 401 of file DblVecTest.C.

References checkPassByValue(), DoubleVec::getRefCnt(), and globalVec2.

Referenced by main().

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

DoubleVec& passThrough DoubleVec v  ) 
 

Definition at line 459 of file DblVecTest.C.

Referenced by CheckFuncReturn().

00460 {
00461   return v;
00462 }


Variable Documentation

DoubleVec globalVec1
 

Definition at line 277 of file DblVecTest.C.

Referenced by checkRef(), and PassByReference().

DoubleVec globalVec2
 

Definition at line 359 of file DblVecTest.C.

Referenced by checkPassByValue(), and PassByValue().


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