/////////////////////////////////////////////////////////////// // testDrv.cpp -- test driver for Assert Module // // ver 1.0 // // Language: Visual C++, ver 6.0 // // Platform: Micron Dual Pentium Pro 200, Win NT 4.0 // // Application: CSE784 Pr2, Fall 1998 // // Author: Jim Fawcett, CST 2-187, Syracuse Univ. // // (315) 443-3948, fawcett@ECS.syr.edu // /////////////////////////////////////////////////////////////// #define ALL_ASSERTIONS #include "assert.h" #include #include using namespace std; class str { public: str(char *s); str(const str &s); ~str(); char& operator[](int n); int length() const; const char* getArray() const; private: char *array; int len; str(); INVARIANT ASSERT(len >= 0); ASSERT(array[len] == '\0'); END_INVARIANT }; //----< promotion constructor >-------------------------------- str::str(char *s) { len = strlen(s); array = new char[len+1]; CHECK(array != NULL); strcpy(array,s); CHECK_INVARIANT; } // //----< copy constructor >------------------------------------- str::str(const str &s) : len(s.len) { #ifdef ERR1 const_cast(&s)->len = -1; #endif REQUIRE(s.len >= 0 && s.array[s.len] == '\0'); array = new char[len+1]; #ifdef ERR2 array=NULL; #endif CHECK(array != NULL); strcpy(array,const_cast(s.getArray())); #ifdef ERR3 array[len] = 'z'; #endif CHECK_INVARIANT; } //----< void constructor should not be called >---------------- str::str() { CHECK(false); } //----< destructor >------------------------------------------- str::~str() { delete [] array; len = 0; } //----< return length of str >--------------------------------- int str::length() const { return len; } //----< index operator >--------------------------------------- char& str::operator[](int n) { REQUIRE(0<=n && n---------------------------- const char* str::getArray() const { return array; } // //----< test stub >-------------------------------------------- void main() { try { str myStr = "this is my string"; str yourStr = myStr; cout << myStr.getArray() << endl; cout << myStr[0] << myStr[1] << myStr[2] << endl; #ifdef ERR4 myStr[-1]; #endif } catch(Assertion &a) { cout << "failed: " << a.getReason() << endl; cout << "in source: " << a.getFile() << endl; cout << "at line no. " << a.getLine() << endl; cout << "error type: " << a.getType() << endl; cout << endl; } }