// File: p3main3.cpp // // Tests ZoomTable functionality. // Used for grading. // #include #include #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. int ANS[10][7] ; 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 >= 10) || (c < 0) || (c >= 7)) { 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 ; } int 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() { ZoomTable Z(10,7) ; int i, j ; // Clear ANS array // for (i = 0 ; i < 10 ; i++) { for (j = 0 ; j < 7 ; j++) { ANS[i][j] = 0 ; } } // Test rows() and cols() // if (Z.rows() != 10) { cerr << "Wrong # of rows\n" ; errors++ ; } if (Z.cols() != 7) { cerr << "Wrong # of cols\n" ; errors++ ; } // INSERT is a macro that inserts to // the ZoomTable Z and the global 2-D array A[][]. 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) ; // Print ANS array // /* for (i = 0 ; i < 10 ; i++) { for (j = 0 ; j < 7 ; j++) { cout << setw(7) << ANS[i][j] << " " ; } cout << "\n" ; } */ // Testing at() for (i = 0 ; i < 10 ; i++) { for (j=0 ; j < 7 ; j++) { int 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++ ; } } } // Testing find for (i = 0 ; i < 10 ; i++) { for (j=0 ; j < 7 ; j++) { Node* ptr = Z.find(i,j) ; if ( (ANS[i][j] == 0) && (ptr != NULL) ) { cerr << "Error: Z.find(" << i << "," << j << ") should be NULL\n" ; errors++ ; } if ( (ANS[i][j] != 0) && (ptr == NULL) ) { cerr << "Error: Z.find(" << i << "," << j << ") should not be NULL\n" ; errors++ ; } if ( (ANS[i][j] != 0) && (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++ ; } } } // Checking 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,7,__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,10,6,__LINE__) ; ptr = Z.find(2,3) ; zptr = Z.ZoomRight(ptr) ; CheckNode(zptr,2,7,__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,10,3,__LINE__) ; // Check 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,7,__LINE__) ; ptr = Z.FirstInRow(5) ; CheckNode(ptr,5,2,__LINE__) ; ptr = Z.FirstInRow(6) ; CheckNode(ptr,6,7,__LINE__) ; ptr = Z.FirstInRow(7) ; CheckNode(ptr,7,7,__LINE__) ; ptr = Z.FirstInRow(8) ; CheckNode(ptr,8,1,__LINE__) ; ptr = Z.FirstInRow(9) ; CheckNode(ptr,9,2,__LINE__) ; // Check 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__) ; // Checking Remove bool b ; ptr = Z.find(5,6) ; Z.remove(ptr) ; ANS[5][6] = 0 ; ptr = Z.find(1,1) ; Z.remove(ptr) ; ANS[1][1] = 0 ; b = Z.remove(1,5) ; ANS[1][5] = 0 ; if (!b) { cerr << "Unsuccessful Z.remove(1,5) in line "<< __LINE__ << "\n" ; errors++ ; } b = Z.remove(5,4) ; ANS[5][4] = 0 ; if (!b) { cerr << "Unsuccessful Z.remove(5,4) in line "<< __LINE__ << "\n" ; errors++ ; } b = Z.remove(7,3) ; ANS[7][3] = 0 ; if (b) { cerr << "Removed non-existent node in Z.remove(5,4) in line "<< __LINE__ << "\n" ; errors++ ; } // Testing at() again for (i = 0 ; i < 10 ; i++) { for (j=0 ; j < 7 ; j++) { int 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++ ; } } } cerr << "Total errors = " << errors << "\n" ; }