// File: p5main4.cpp // // Tests ZoomTable functionality. // Used for grading. // #include #include #include #include "ZTerror.h" #include "ZoomTable.H" using namespace std ; // Global two dimensional array. // We're using a global variable here because it is statically allocated // and hopefully immune to pointer problems in the student's code. const int AROWS = 11 ; const int ACOLS = 17 ; const string EMPTY_STR="epsilon" ; string ANS[AROWS][ACOLS] ; int errors = 0 ; #define INSERT(r, c, d) Z.insert(r,c,d) ; ANS[r][c] = d void CheckNode(Node* ptr, int r, int c, int lineno) { bool isNULL ; int row, col ; if ( (r < 0) || (r >= AROWS) || (c < 0) || (c >= ACOLS)) { isNULL = true ; } else { isNULL = false ; } if ( ptr == NULL & !isNULL ) { cerr << "ptr should not be NULL in line " << lineno <<" \n" ; errors++ ; return ; } if ( ptr != NULL & isNULL) { cerr << "ptr should be NULL in line " << lineno <<" \n" ; errors++ ; return ; } if (isNULL) return ; row = ptr->row() ; col = ptr->col() ; if ( (row != r) || (col != c) ) { cerr << "Node should have coordinates (" << r << "," << c << ") but are reported as (" << row << "," << col << ") in line " << lineno << "\n" ; errors++ ; return ; } string x = ptr->get() ; if ( x != ANS[r][c] ) { cerr << "Node at coordinates (" << r << "," << c << ") should have payload data = " << ANS[r][c] << " but " << x << " reported, in line " << lineno << "\n" ; errors++ ; return ; } } int main() try { ZoomTable Z(AROWS, ACOLS, EMPTY_STR) ; int i, j ; // Clear ANS array // cout << "Initializing..." ; for (i = 0 ; i < AROWS ; i++) { for (j = 0 ; j < ACOLS ; j++) { ANS[i][j] = EMPTY_STR ; } } cout << "done" << endl ; // Test rows() and cols() // cout << "Testing rows() and cols()..." ; if (Z.rows() != AROWS) { cerr << "Wrong # of rows\n" ; errors++ ; } if (Z.cols() != ACOLS) { cerr << "Wrong # of cols\n" ; errors++ ; } cout << "done" << endl ; // INSERT is a macro that inserts to // the ZoomTable Z and the global 2-D array A[][]. cout << "Inserting data ..." ; INSERT(0, 2, "1002") ; INSERT(8, 6, "1015") ; INSERT(1, 0, "1004") ; INSERT(1, 1, "1005") ; INSERT(5, 4, "1011") ; INSERT(5, 2, "1010") ; INSERT(9, 2, "1016") ; INSERT(1, 5, "1006") ; INSERT(8, 1, "1013") ; INSERT(8, 4, "1014") ; INSERT(1, 6, "1007") ; INSERT(5, 6, "1012") ; INSERT(3, 1, "1009") ; INSERT(0, 0, "1001") ; INSERT(0, 4, "1003") ; INSERT(2, 3, "1008") ; INSERT(2, 6, "foo01") ; INSERT(2, 7, "foo02") ; INSERT(1, 9, "foo03") ; INSERT(1, 11, "foo04") ; INSERT(1, 15, "foo05") ; INSERT(2, 12, "foo06") ; INSERT(2, 14, "foo07") ; INSERT(2, 16, "foo08") ; INSERT(4, 12, "foo09") ; INSERT(7, 10, "foo10") ; INSERT(7, 13, "foo11") ; INSERT(8, 13, "foo12") ; INSERT(8, 15, "foo12") ; cout << "done" << endl ; // Print ANS array // /* for (i = 0 ; i < AROWS ; i++) { for (j = 0 ; j < ACOLS ; j++) { cout << setw(7) << ANS[i][j] << " " ; } cout << "\n" ; } */ // Testing at() cout << "Testing at() ..." ; for (i = 0 ; i < AROWS ; i++) { for (j=0 ; j < ACOLS ; j++) { string x = Z.at(i,j) ; if (x != ANS[i][j]) { cerr << "Error: Z.at(" << i << "," << j << ") should be " << ANS[i][j] << " but was reported as " << x << "\n" ; errors++ ; } } } cout << "done" << endl ; // Testing find cout << "Testing find() ..." ; for (i = 0 ; i < AROWS ; i++) { for (j=0 ; j < ACOLS ; j++) { Node *ptr = Z.find(i,j) ; if ( (ANS[i][j] == EMPTY_STR) && (ptr != NULL) ) { cerr << "Error: Z.find(" << i << "," << j << ") should be NULL\n" ; errors++ ; } if ( (ANS[i][j] != EMPTY_STR) && (ptr == NULL) ) { cerr << "Error: Z.find(" << i << "," << j << ") should not be NULL\n" ; errors++ ; } if ( (ANS[i][j] != EMPTY_STR) && (ptr != NULL) && (ptr->get() != ANS[i][j]) ) { cerr << "Error: Z.find(" << i << "," << j << ") should be " << ANS[i][j] << " but was reported as " << ptr->get() << "\n" ; errors++ ; } } } cout << "done" << endl ; // Checking Zoom functions cout << "Testing Zoom functions ..." ; Node *ptr, *zptr ; ptr = Z.find(5,4) ; zptr = Z.ZoomRight(ptr) ; CheckNode(zptr,5,6,__LINE__) ; zptr = Z.ZoomLeft(ptr) ; CheckNode(zptr,5,2,__LINE__) ; zptr = Z.ZoomUp(ptr) ; CheckNode(zptr,0,4,__LINE__) ; zptr = Z.ZoomDown(ptr) ; CheckNode(zptr,8,4,__LINE__) ; ptr = Z.find(8,6) ; zptr = Z.ZoomRight(ptr) ; CheckNode(zptr,8,13,__LINE__) ; zptr = Z.ZoomLeft(ptr) ; CheckNode(zptr,8,4,__LINE__) ; zptr = Z.ZoomUp(ptr) ; CheckNode(zptr,5,6,__LINE__) ; zptr = Z.ZoomDown(ptr) ; CheckNode(zptr,11,6,__LINE__) ; ptr = Z.find(2,3) ; zptr = Z.ZoomRight(ptr) ; CheckNode(zptr,2,6,__LINE__) ; zptr = Z.ZoomLeft(ptr) ; CheckNode(zptr,2,-1,__LINE__) ; zptr = Z.ZoomUp(ptr) ; CheckNode(zptr,-1,3,__LINE__) ; zptr = Z.ZoomDown(ptr) ; CheckNode(zptr,11,3,__LINE__) ; cout << "done" << endl ; // Check FirstInRow() cout << "Testing FirstInRow() ..." ; ptr = Z.FirstInRow(0) ; CheckNode(ptr,0,0,__LINE__) ; ptr = Z.FirstInRow(1) ; CheckNode(ptr,1,0,__LINE__) ; ptr = Z.FirstInRow(2) ; CheckNode(ptr,2,3,__LINE__) ; ptr = Z.FirstInRow(3) ; CheckNode(ptr,3,1,__LINE__) ; ptr = Z.FirstInRow(4) ; CheckNode(ptr,4,12,__LINE__) ; ptr = Z.FirstInRow(5) ; CheckNode(ptr,5,2,__LINE__) ; ptr = Z.FirstInRow(6) ; CheckNode(ptr,6,17,__LINE__) ; ptr = Z.FirstInRow(7) ; CheckNode(ptr,7,10,__LINE__) ; ptr = Z.FirstInRow(8) ; CheckNode(ptr,8,1,__LINE__) ; ptr = Z.FirstInRow(9) ; CheckNode(ptr,9,2,__LINE__) ; ptr = Z.FirstInRow(10) ; CheckNode(ptr,10,17,__LINE__) ; cout << "done" << endl ; // Check FirstInCol() cout << "Testing FirstInCol() ..." ; ptr = Z.FirstInCol(0) ; CheckNode(ptr,0,0,__LINE__) ; ptr = Z.FirstInCol(1) ; CheckNode(ptr,1,1,__LINE__) ; ptr = Z.FirstInCol(2) ; CheckNode(ptr,0,2,__LINE__) ; ptr = Z.FirstInCol(3) ; CheckNode(ptr,2,3,__LINE__) ; ptr = Z.FirstInCol(4) ; CheckNode(ptr,0,4,__LINE__) ; ptr = Z.FirstInCol(5) ; CheckNode(ptr,1,5,__LINE__) ; ptr = Z.FirstInCol(6) ; CheckNode(ptr,1,6,__LINE__) ; ptr = Z.FirstInCol(7) ; CheckNode(ptr,2,7,__LINE__) ; ptr = Z.FirstInCol(8) ; CheckNode(ptr,11,8,__LINE__) ; ptr = Z.FirstInCol(9) ; CheckNode(ptr,1,9,__LINE__) ; ptr = Z.FirstInCol(10) ; CheckNode(ptr,7,10,__LINE__) ; ptr = Z.FirstInCol(11) ; CheckNode(ptr,1,11,__LINE__) ; ptr = Z.FirstInCol(12) ; CheckNode(ptr,2,12,__LINE__) ; ptr = Z.FirstInCol(13) ; CheckNode(ptr,7,13,__LINE__) ; ptr = Z.FirstInCol(14) ; CheckNode(ptr,2,14,__LINE__) ; ptr = Z.FirstInCol(15) ; CheckNode(ptr,1,15,__LINE__) ; ptr = Z.FirstInCol(16) ; CheckNode(ptr,2,16,__LINE__) ; cout << "done" << endl ; // Checking Remove cout << "Testing remove() ..." ; bool b ; ptr = Z.find(5,6) ; Z.remove(ptr) ; ANS[5][6] = EMPTY_STR ; ptr = Z.find(1,1) ; Z.remove(ptr) ; ANS[1][1] = EMPTY_STR ; b = Z.remove(1,5) ; ANS[1][5] = EMPTY_STR ; if (!b) { cerr << "Unsuccessful Z.remove(1,5) in line "<< __LINE__ << "\n" ; errors++ ; } b = Z.remove(5,4) ; ANS[5][4] = EMPTY_STR ; if (!b) { cerr << "Unsuccessful Z.remove(5,4) in line "<< __LINE__ << "\n" ; errors++ ; } b = Z.remove(7,3) ; ANS[7][3] = EMPTY_STR ; if (b) { cerr << "Removed non-existent node in Z.remove(5,4) in line "<< __LINE__ << "\n" ; errors++ ; } cout << "done" << endl ; // Testing row iterators cout << "Testing ZTRowIterator ..." ; ZTRowIterator rowit ; for (i = 0 ; i < AROWS ; i++) { j = 0 ; for (rowit = Z.RowBegin(i) ; rowit != Z.RowEnd(i) ; rowit++) { string x = *rowit ; if (x != ANS[i][j]) { cerr << "Error: Z.at(" << i << "," << j << ") should be " << ANS[i][j] << " but was reported as " << x << "\n" ; errors++ ; } j++ ; } } cout << "done" << endl ; // Testing column iterators cout << "Testing ZTColIterator ... " ; ZTColIterator colit ; for (j = 0 ; j < ACOLS ; j++ ) { i = 0 ; for (colit = Z.ColBegin(j) ; colit != Z.ColEnd(j) ; colit++ ) { string x = *colit ; if (x != ANS[i][j]) { cerr << "Error: Z.at(" << i << "," << j << ") should be " << ANS[i][j] << " but was reported as " << x << "\n" ; errors++ ; } i++ ; } } cout << "done" << endl ; cerr << "Total errors = " << errors << "\n" ; } catch (ZTerror &e) { cout << e.desc << endl ; cout << " Valid row index: 0 to " << e.max_row << "\n" ; cout << " Valid column index: 0 to " << e.max_col << "\n" ; cout << " Offending indices: bad_row=" << e.bad_row << ", bad_col=" << e.bad_col << "\n\n" ; } catch (...) { cout << "Some other error caught by catch (...) \n\n" ; }