/////////////////////////////////////////////////////////////// // // // sptrs.cpp - test stub for smart pointer class // // v3.6 // // // /////////////////////////////////////////////////////////////// // // // Language: Borland C, ver 3.1; // // Platform: Gateway 386/25, win95 // // Application: CSE784, pr1 // // Author: Jim Fawcett, 123 Link Hall, Syracuse Univ // // Syracuse, N.Y., 13244, (315)443-2654 // // // // copyright (c) 1995, 1996 // // all rights granted to users except publication // // // /////////////////////////////////////////////////////////////// #include // setw() #include "sptrs.hpp" #ifdef TEST_SPTRS // structure for testing struct ms { int x; double y; char z; ms(int u, double v, char w) : x(u), y(v), z(w) { } ms() : x(0), y(0), z(0) { } }; // defining quick and dirty shallow str class just for this test struct str { char *text; str(char *t=0) { text = t; } operator const char* () { return text; } }; void main() { cout << "\n" << "=======================\n" << " testing pointer class \n" << "=======================\n\n"; // cout << "\n" << "///////////////////////////////////////\n" << " test #1 - sized and copy construction \n" << "///////////////////////////////////////\n\n"; ptr ip(10); ip.showState("ip - sized for 10 ints"); ptr save = ip; save.showState("save - copied from ip"); ptr nullptr(0); if(!nullptr) cout << "null pointer detected\n"; nullptr.showState("nullptr"); cout << "\n" << "//////////////////////////////////////////////////////\n" << " test #2 - adding, dereferencing ptr to array of ints \n" << "//////////////////////////////////////////////////////\n\n"; int i; for(i=0; i<10; i++) *(ip++) = 2*i-7; ip = save; // reset starting address for(i=0; i<10; i++) cout << " " << *(ip+i); cout << endl; ip.showState("ip - points to array of ints"); cout << "\f\n" << "////////////////////////////////////////////////////\n" << " test #3 - indexing, increm'g, adding out of bounds \n" << "////////////////////////////////////////////////////\n\n"; cout << "indexing too high and too low\n"; int index; ip.first(); index = -1; ip[index]; // index too low index = 10; ip[index]; // index too high cout << "incrementing too high and too low\n"; index = 9; ip += index; *(++ip); // increment too high ip -= index+1; *(--ip); // decrement too low cout << "adding offset too high and too low\n"; ip.first(); int offset = 10; *(ip+offset); // offset too high *(ip-offset-1); // offset too low cout << "\n" << "////////////////////////////////\n" << " test #4 - indexing ptr to ints \n" << "////////////////////////////////\n\n"; for(i=0; i<10; i++) cout << " " << ip[i]; cout << endl; ip.showState("ip - still points to array of ints"); // cout << "\n" << "//////////////////////////////////////////////////\n" << " test #5 - subtracting, dereferencing ptr to ints \n" << "//////////////////////////////////////////////////\n\n"; ip = save+9; for(i=0; i<10; i++) cout << " " << *(ip-i); cout << endl; ip.showState("ip - modified array values through deref"); cout << "\n" << "/////////////////////////////////////////\n" << " test #6 - selections from ptr to struct \n" << "/////////////////////////////////////////\n\n"; sptr ps(1); ps->x = 1; ps->y = 0.5; ps->z = 'a'; cout << " " << setw(2) << ps->x << " " << setw(4) << ps->y << " " << setw(2) << ps->z << " - components of structure\n"; ps.showState("ps - pointer to structure"); // cout << "\f\n" << "////////////////////////////////////////////////\n" << " test #7 - selection, indexing array of structs \n" << "////////////////////////////////////////////////\n\n"; sptr msp(15); for(i=0; i<15; i++) { // alternate method: // (sptr(msp+i))->x = i; // (sptr(msp+i))->y = 0.5*i; // (sptr(msp+i))->z = char(int('a')+i); // note the cast - it's necessary because msp+i is a temp msp->x = i; msp->y = 0.5*i; msp->z = char(int('a')+i); msp++; } msp.first(); for(i=0; i<15; i++) cout << " " << setw(2) << msp[i].x << " " << setw(4) << msp[i].y << " " << setw(2) << msp[i].z << "\n"; cout << endl; msp.showState("msp - points to an array of structures"); // cout << "\n" << "/////////////////////////////////////////////\n" << " test #8 - indexing array of ptrs to structs \n" << "/////////////////////////////////////////////\n\n"; ptr msap(15); for(i=0; i<15; i++) msap[i] = new ms(i,0.5*i,char(int('a')+i)); for(i=0; i<15; i++) cout << " " << setw(2) << msap[i]->x << " " << setw(4) << msap[i]->y << " " << setw(2) << msap[i]->z << "\n"; cout << endl; msap.showState("msap:\n - points to array of structure pointers"); cout << "\f\n" << "////////////////////////////////////////////\n" << " test #9 - smart pointer to smart pointers \n" << "////////////////////////////////////////////\n\n"; int imax = 5; ptr< ptr > ptp(imax); ptp.showState("\n ptp[i] is a smart pointer"); int j, jmax=3; for(i=0; i0) cout << "\n "; else cout << " "; for(j=0; j darray(test,darrSize); cout << " "; for(i=0; i sarray(stest,sarrSize); cout << " "; for(i=0; i newdarray; newdarray = darray; cout << " "; for(i=0; i newsarray; newsarray = sarray; cout << " "; for(i=0; i::showAlloc(1); sptr::showAlloc(1); RCobj::showAlloc(1); ptr::showAlloc(1); } #endif