// Source: "Software Design ...", John A Robinson, Newnes, 2004, page 394. // Test program for stable class // John Robinson, 25 August 2003 // All commentary in this file is done by couts! #include #include #include #include #include "test.h" #include "stable.h" using namespace std; const string firsttab[3][4] = { "a1", "a2", "a3", "a4", "b1", "b2", "b3", "b4", "c1", "c2", "c3", "c4" }; const string firsttabcollabels[4] = { "One", "Two", "Three", "Four" }; const string firsttabrowlabels[3] = { "Alpha", "Beta", "Gamma" }; int main(int argc, char *argv[]) { if (argc < 2) { cout << "Usage: teststable scratchfile_for_intermediate_results\n"; return -1; } cout << "VALUE SETTING TESTS\n"; cout << "First we'll set up a small table size 4 by 3 to hold:\n"; stable first(3,4); for (int r = 0; r < first.maxrows(); r++) { for (int c = 0; c < first.maxcols(); c++) { TEST(0, first.set, r, c, firsttab[r][c]); } } cout << "In doing that we've exercised maxrows() and maxcols()\n"; cout << "As well as set(int, int, string)\n"; cout << "Now we'll set up the row labels:\n"; for (int r = 0; r < first.nrows(); r++) TEST(0, first.setrowlabel, r, firsttabrowlabels[r]); cout << "And the column labels:\n"; for (int c = 0; c < first.ncols(); c++) TEST(0, first.setcollabel, c, firsttabcollabels[c]); cout << "In doing that we've also exercised nrows() and ncols()\n"; cout << "And now a selection of sets and set{row|col}label "; cout << "outside the bounds of the table\n"; cout << "The sets should all return non-zero. The row/col sets "; cout << "should each return 0 once only\n"; int badcoords[4][2] = { -1,0, 2,6, -3,-3, 3,4}; // Next three arrays are expected returns int expset[4] = {-1, -1, -1, -1}; int expcol[4] = {0,-1,-1,-1}; int exprow[4] = {-1,0,-1,-1}; for (int i = 0; i < 4; i++) { int r = badcoords[i][0]; int c = badcoords[i][1]; TEST(expset[i],first.set,r,c,"text"); TEST(exprow[i],first.setrowlabel,r,"text"); TEST(expcol[i],first.setcollabel,c,"text"); } cout << "The current state of the stable is:\n"; first.print(); cout << "Now change the changed labels back using find{row|col}label\n"; int r, c; if (first.findrowlabel("text",r) != 1) cout << "Couldn't find the row label!\n"; else first.setrowlabel(r, firsttabrowlabels[r]); if (first.findcollabel("text",c) != 1) cout << "Couldn't find the col label!\n"; else first.setcollabel(c, firsttabcollabels[c]); cout << "And now the table is back to:\n"; first.print(); cout << "GETTING BY LABEL TESTS\n"; string val; for (r = 0; r < first.maxrows(); r++) { for (c = 0; c < first.maxcols(); c++) { TEST(1,first.get,firsttabrowlabels[r], firsttabcollabels[c], val); TESTVAL(firsttab[r][c],val); } } string garbage("garbage"); TEST(0,first.get,firsttabrowlabels[0],garbage,val); TEST(0,first.get,garbage, firsttabcollabels[0],val); cout << "FINDING TESTS\n"; TEST(1,first.find,"b3", r, c); TESTVAL(1,r); TESTVAL(2,c); TEST(1,first.find,"a1", r, c); TESTVAL(0,r); TESTVAL(0,c); TEST(0,first.find,"garbage", r, c); TESTVAL(0,r); TESTVAL(0,c); r = 2; c = 1; TEST(0,first.findafter,"b3", r, c); TESTVAL(2,r); TESTVAL(0,c); r = 0; c = 3; TEST(1,first.findafter,"b3", r, c); TESTVAL(1,r); TESTVAL(2,c); r = 2; c = 1; TEST(1,first.findbefore,"b3", r, c); TESTVAL(1,r); TESTVAL(2,c); r = 0; c = 3; TEST(0,first.findbefore,"b3", r, c); TESTVAL(0,r); TESTVAL(3,c); cout << "READING AND WRITING TESTS\n"; cout << "Saving to scratchfile\n"; ofstream out1(argv[1]); first.writewithlabels(out1); out1.close(); stable second; cout << "And reading back in to large table\n"; ifstream in1(argv[1]); second.readwithlabels(in1); in1.close(); cout << "Written and read table:\n"; second.print(); cout << "Now re-reading second table without labels\n"; ifstream in2(argv[1]); second.readwithoutlabels(in2); in2.close(); second.print(); cout << "So, the previous table including labels is now part of the actual table\n"; cout << "Saving this as a table without labels...\n"; ofstream out2(argv[1]); second.writewithoutlabels(out2); out2.close(); cout << "And re-reading as if it had labels\n"; ifstream in3(argv[1]); second.readwithlabels(in3); in3.close(); second.print(); cout << "NUMERICAL TESTS\n"; TEST(0,second.set,5,2,200); TEST(0,second.set,3,5,100); TEST(0,second.set,2,2,100); cout << "After insertion of numeric values outside the original "; cout << "table limits, the table is:\n"; second.print(); cout << "Finding value 100:\n"; TEST(1,second.find,100, r, c); TESTVAL(2,r); TESTVAL(2,c); cout << "Finding next value 100:\n"; TEST(1,second.findafter,100, r, c); TESTVAL(3,r); TESTVAL(5,c); }