#include #include #include "sparcematrix.h" struct sparceMatrix *fakeCorners(int numRows, int numCols) { int i ; struct sparceMatrix *theMatrix ; sparceElementPtr corners[4] ; sparceElementPtr *rows ; sparceElementPtr *cols ; theMatrix = (struct sparceMatrix *)malloc(sizeof(struct sparceMatrix)) ; rows = (sparceElementPtr *)calloc(numRows, sizeof(sparceElementPtr)) ; cols = (sparceElementPtr *)calloc(numCols, sizeof(sparceElementPtr)) ; if (rows == NULL || cols == NULL || theMatrix == NULL) { return (struct sparceMatrix *)NULL ; } for (i = 0 ; i < numRows; ++i) { rows[i] = NULL ; } for (i = 0 ; i < numCols; ++i) { cols[i] = NULL ; } theMatrix->numRows = numRows ; theMatrix->numCols = numCols ; theMatrix->rows = rows ; theMatrix->cols = cols ; for (i = 0 ; i < 4; ++i) { corners[i] = (sparceElementPtr)malloc(sizeof(struct sparceElementRec)) ; if (corners[i] == NULL) { return (struct sparceMatrix *)NULL ; } corners[i]->value = 4 ; } rows[0] = corners[0] ; cols[0] = corners[1] ; rows[numRows-1] = corners[2] ; cols[numCols-1] = corners[3] ; corners[0]->row = 0 ; corners[1]->row = 0 ; corners[2]->row = numRows-1 ; corners[3]->row = numRows-1 ; corners[0]->col = 0 ; corners[1]->col = numCols-1 ; corners[2]->col = 0 ; corners[3]->col = numCols-1 ; corners[0]->nextOnRow = corners[1] ; corners[1]->nextOnRow = NULL ; corners[2]->nextOnRow = corners[3] ; corners[3]->nextOnRow = NULL ; corners[0]->nextOnCol = corners[2] ; corners[1]->nextOnCol = corners[3] ; corners[2]->nextOnCol = NULL ; corners[3]->nextOnCol = NULL ; return theMatrix ; } void testCorners(int numRows, int numCols) { int i ; struct sparceMatrix *theMatrix ; printf("\nTest setting corners 4 times each\n") ; printf(" Creating %dx%d array with elements!\n", numRows, numCols) ; if ((theMatrix = fakeCorners(numRows, numCols)) == NULL) { fprintf(stderr, "Failure!\n") ; return ; } for (i=0; i<4; ++i) { int row = i%2 ? numRows-1 : 0 ; int col = i/2%2 ? numCols-1 : 0 ; printf(" Printing at X[%d,%d]\n", row, col) ; if (printValueSparceMatrix(stdout, theMatrix, row, col) == -1) { fprintf(stderr, "Failure!\n") ; } } } int main(int argc, char** argv) { printf("Testing sparse matrix ADT with no incr!\n\n") ; testCorners(13, 17) ; /* This should work */ testCorners(1000000, 1000000) ; /* This one will certainly fail on a 32-bit computer */ /* Who knows about a 64-bit computer */ testCorners(1000000000, 1000000000) ; return (EXIT_SUCCESS); }