/////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test member and friend functions // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // // // Files needed: // // utstr2.cpp, strs.hpp, strs.cpp // // // // Build command: // // bc utstr2.cpp // // - sets up test driver for debugger walkthrough // // inside integrated development environment // // - single step through tests, tracing into each // // invoked function // // // // Note: utstr2.cpp includes the other files so // // no project file is needed // // // /////////////////////////////////////////////////////////////// // // // Maintenance History: // // v1.0 - 16 Oct 94 // // . tests created // // // /////////////////////////////////////////////////////////////// // // // Signature indicates tests were all run and verified // // all expected results. // // // // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// // /////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test #2a // // append(char ch) append a char to str // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // // // Preconditions: // // - object exists on which to append character // // Derived requirements: // // - add one character to end of str object // // - change physical size only if logical size grows // // bigger // // . if allocated memory size, smax, is big enough to // // hold new logical size then simply concatenate char // // to str end and increment slen by one // // . if allocated memory size, smax, is not big enough // // then allocate new memory, copy char values into // // buffer, concatenate new char, and increment slen // // by one // // // /////////////////////////////////////////////////////////////// // // // Test Procedure - test by debugger walkthrough // // - use null, filled, and partially filled strs as inputs // // - watch slen, smax, (char*)shead // // // // Expected test results: Results verified // // - smax changes only if new slen > smax - // // - in that case new memory is allocated - // // - char is added to end of str values - // // - slen is incremented by one - // // // /////////////////////////////////////////////////////////////// // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// #include "strs.hpp" #include "strs.cpp" void main() { // append to null str str tst2a1; tst2a1.append('a'); // append to filled str str tst2a2 = "this is a string"; tst2a2.append('a'); // append to partially filled str str tst2a3('x',20); tst2a3 = "this is a string"; tst2a3.append('a'); // /////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test #2b // // resize(int n) change logical str size // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // // // Preconditions: // // - object exists to resize // // Derived requirements: // // - change logical size of string // // only change physical size if logical size grows // // bigger // // . if allocated memory size, smax, is big enough to // // hold new logical size then simply fill area beyond // // slen with blanks then change slen to new value // // . if allocated memory size, smax, is not big enough // // then allocate new memory, store blank chars beyond // // slen, delete old allocation, set shead to point to // // new memory, and set slen to new value // // . result is returned // // // /////////////////////////////////////////////////////////////// // // // Test Procedure - test by debugger walkthrough // // - use null, filled, and partially filled sts as inputs // // - resize smaller, bigger but less than physical memory, // // and then bigger than physical memory // // - watch slen, smax, (char*)shead // // // // Expected test results: Results verified // // - smax changes only if new slen > smax - // // - in that case new memory is allocated - // // - added length is padded with blanks - // // - (char*)shead is truncated if slen is - // // decreased // // - slen is set to new size - // // // /////////////////////////////////////////////////////////////// // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// // str tst2b1 = "012345678901234567890"; // truncate tst2b1.resize(10); // expand to size less than physical memory tst2b1.resize(15); // expand beyond physical memory tst2b1.resize(25); str tst2b2; // expand null str tst2b2.resize(10); str tst2b3 = "012345678901234567890"; tst2b3 = "01234567890"; // expand partially filled str within physical memory tst2b3.resize(15); // expand partially filled str beyond physical memory tst2b3.resize(30); // /////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test #2c // // ltoa(const long int i, int radix) // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // Preconditions: // // - input is an int or promotable to int without // // truncation // // Derived requirements: // // - return object which contains string representation // // of the integer input // // . enough memory is allocated to hold the largest // // possible integer value // // . run-time library function ltoa() is used to convert // // the representation, no error return is provided // // . slen is set to number of resulting characters // // . smax is set to the memory allocated // // . result is returned // // // /////////////////////////////////////////////////////////////// // // // Test Procedure - test by debugger walkthrough // // - use large negative, zero, and large positive inputs // // in decimal radix, typical values in binary radix // // - watch temp.slen, temp.smax, (char*)temp.shead, return // // // /////////////////////////////////////////////////////////////// // // // Expected test results: Results verified // // - temp.smax set to 33 - // // - temp.slen set to number of characters - // // - (char*)temp.shead contains accurate set - // // of conversion characters // // - returned str object has slen=temp.slen, - // // smax=slen, (char*)shead=(char*)temp.shead // // // /////////////////////////////////////////////////////////////// // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// // construct decimal str rep from specification of decimal long str tst2c1 = ltos(0); // construct decimal str rep from large negative decimal long str tst2c2 = ltos(-2147483648L); // construct decimal str rep from large positive decimal long str tst2c3 = ltos(2147483647L); // construct binary str rep from positive decimal integer str tst2c4 = ltos(31,2); // construct binary str rep from negative decimal integer str tst2c5 = ltos(-31,2); // /////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test #2d // // dtoa(const long int i, int radix) // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // Preconditions: // // - input is a double or promotable to double without // // truncation // // Derived requirements: // // - return object which contains string representation // // of the double input // // . enough memory is allocated to hold the largest // // possible double value // // . run-time library function dtoa() is used to convert // // the representation, no error return is provided // // . slen is set to number of resulting characters // // . smax is set to the memory allocated // // . result is returned // // // /////////////////////////////////////////////////////////////// // // // Test Procedure - test by debugger walkthrough // // - use negative, zero, and large postive inputs with // // default and specified precision // // - watch temp.slen, temp.smax, (char*)temp.shead, return // // // /////////////////////////////////////////////////////////////// // // // Expected test results: Results verified // // - temp.smax set to 26 - // // - temp.slen set to number of characters - // // - (char*)temp.shead contains accurate set - // // of conversion characters // // - number of digits after decimal is either - // // default (4) or as specified by prec // // - returned str object has slen=temp.slen, - // // smax=slen, (char*)shead=(char*)temp.shead // // // /////////////////////////////////////////////////////////////// // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// // test construction of str from specification of zero str tst2d1 = dtos(0); // test construction of str from large positive number str tst2d2 = dtos(1.2345678E+15); // test construction of str from negative number, set precision str tst2d3 = dtos(-1234567.8,3); // /////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test #2e // // overlay(str& s, int n) // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // Preconditions: // // - overlayed and overlaying objects exist // // Derived requirements: // // - return object which consists of overlayed object // // with overlaying object written over its char values // // starting at the nth character // // - create temporary object which is long enough to hold // // the original and overlaying objects, copy values of // // overlayed into temporary and then copy values of // // overlaying into temporary starting at the nth char // // - return the temporary by value // // // /////////////////////////////////////////////////////////////// // // // Test Procedure - test by debugger walkthrough // // - use null, filled, and partially filled overlayed strs // // - overlay with str which fits inside, hits boundary, // // and extends beyond boundary // // - watch temp.slen, temp.smax, (char*)temp.shead, return // // // // Expected test results: Results verified // // - new memory allocation if needed // // - temp.smax set to allocation size minus one - // // - temp.slen set to number of chars in result - // // - (char*)temp.shead contains overlayed str - // // - return is identical to temp - // // // /////////////////////////////////////////////////////////////// // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// str tst2e1 = "pqrstuvw"; str tst2e2; // overlay null str str tst2e3 = tst2e2.overlay(tst2e1,3); // internal overlay str tst2e4 = "012345678901234567890"; str tst2e5 = tst2e4.overlay(tst2e1,3); // overlay past end of overlayed str str tst2e6 = "012345678901234567890"; str tst2e7 = tst2e6.overlay(tst2e1,15); // overlay partially filled str tst2e6.resize(10); str tst2e8 = tst2e6.overlay(tst2e1,8); // /////////////////////////////////////////////////////////////// // // // utstr2.cpp - unit test #2f // // insert(str& s, int n) // // // // Source: strs.hpp, strs.cpp v4.4 // // Author: Jim Fawcett, x2654, jfawcett@neptune // // // /////////////////////////////////////////////////////////////// // Preconditions: // // - receiver and inserted objects exist // // Derived requirements: // // - return object which consists of copy of original // // str object with inserted object squeezed between the // // original nth and (n+1)th characters of the receiver // // - create temporary object which is long enough to hold // // the original and inserted objects, copy values of // // original, up to the (n-1)th char, copy values of the // // inserted after that, and copy the remaining values of // // the original after that // // // /////////////////////////////////////////////////////////////// // // // Test Procedure - test by debugger walkthrough // // - use null, filled, and partially filled strs for // // insertion // // - insert at 0, in middle, and at slen // // - watch temp.slen, temp.smax, (char*)temp.shead, return // // // // Expected test results: Results verified // // - new memory allocation if needed // // - temp.smax set to allocation size minus one - // // - temp.slen set to number of chars in result - // // - (char*)temp.shead contains inserted str - // // - return is identical to temp - // // // /////////////////////////////////////////////////////////////// // // // Signature: Date: // // -------------------------- ----------- // // // /////////////////////////////////////////////////////////////// str tst2f1 = "pqrstuvw"; str tst2f2; // overlay null str str tst2f3 = tst2f2.insert(tst2f1,3); // internal overlay str tst2f4 = "012345678901234567890"; str tst2f5 = tst2f4.insert(tst2f1,3); // overlay past end of overlayed str str tst2f6 = "012345678901234567890"; str tst2f7 = tst2f6.insert(tst2f1,0); // overlay partially filled str tst2f6.resize(10); str tst2f8 = tst2f6.overlay(tst2f1,6); // dummy statement to keep debugger active str temp; }