// Test driver for exception handling of IntArray class #include #include "IntArray.h" // Print the first few and last few values in an array void print(const IntArray & a) { int i = 0; while (i < 3 && i < a.length()) cout << a[i++] << " "; cout << " . . . "; while (i < a.length() - 3) ++i; while (i < a.length()) cout << a[i++] << " "; } int main(int argc, char * argv[]) { // Set heap limit to 100K bytes to trigger memory alloc failures struct rlimit rl; rl.rlim_cur = rl.rlim_max = 100000; setrlimit(RLIMIT_DATA, &rl); if (argc != 3) { cerr << "Usage: " << argv[0] << "size index" << endl; return -1; } int size = atoi(argv[1]); int index = atoi(argv[2]); try { IntArray a(size); for (int i = 0; i < a.length(); ++i) a[i] = i; cout << "a has size " << a.length() << " and values: "; print(a); cout << endl; IntArray b(2*size); for (int i = 0; i < b.length(); ++i) b[i] = 2*i + 1; cout << "b has size " << b.length() << " and values: "; print(b); cout << endl; a = b; cout << "a has size " << a.length() << " and values: "; print(a); cout << endl; cout << "Setting a[" << index << "] to -1" << endl; a[index] = -1; cout << "a[" << index << "] is now " << a[index] << endl; } catch (const IntArray::Error & err) { // Report the caught exception cout << endl << " ***** Caught array error: " << err << endl; } return 0; }