t_matrix.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_matrix                                                       *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include <basis/guards.h>
00016 #include <basis/istring.h>
00017 #include <basis/log_base.h>
00018 #include <data_struct/matrix.cpp>
00019 #include <data_struct/static_memory_gremlin.h>
00020 #include <loggers/console_logger.h>
00021 #include <opsystem/application_shell.h>
00022 
00023 const int DIM_ROWS = 10;
00024 const int DIM_COLS = 10;
00025 
00026 // fills the matrix "to_stuff" with the pure version of the exemplar.
00027 #define STUFF_MATRIX(to_stuff, max_row, max_col) \
00028   to_stuff.reset(max_row, max_col); \
00029   for (int r = 0; r < max_row; r++) \
00030     for (int c = 0; c < max_col; c++) \
00031       to_stuff.put(r, c, test_pure.get(r, c))
00032 
00034 
00035 // this class exhibits an old bug where the matrix was zeroing out its
00036 // contents for a same size resize.  the zeroing allowed hell to spew forth.
00037 class diggulite
00038 {
00039 public:
00040   diggulite() {}
00041   virtual ~diggulite() {}
00042 };
00043 
00045 
00046 class test_matrix : public application_shell
00047 {
00048 public:
00049   test_matrix();
00050 
00051   IMPLEMENT_CLASS_NAME("test_matrix");
00052 
00053   int execute();
00054     // performs main body of test.
00055 
00056   void print_matrix(const int_matrix &to_print);
00057     // dumps "to_print" to the diagnostic output.
00058 };
00059 
00061 
00062 test_matrix::test_matrix() : application_shell(class_name())
00063 {}
00064 
00065 void test_matrix::print_matrix(const int_matrix &to_print)
00066 {
00067   istring text;
00068   for (int t = 0; t < to_print.rows(); t++) {
00069     text += istring(istring::SPRINTF, "[%d] ", t);
00070     for (int c = 0; c < to_print.columns(); c++)
00071       text += istring(istring::SPRINTF, "%03d ", int(to_print[t][c]));
00072     text += log_base::platform_ending();
00073   }
00074   log(text);
00075 }
00076 
00077 int test_matrix::execute()
00078 {
00079   int_matrix test_pure(DIM_ROWS, DIM_COLS);  // kept without modification.
00080   for (int r = 0; r < DIM_ROWS; r++)
00081     for (int c = 0; c < DIM_COLS; c++)
00082       test_pure[r][c] = r * DIM_COLS + c;
00083 
00084   int_matrix test1 = test_pure;  // first copy to work with.
00085 
00086   int_matrix test2(test1);
00087 
00088   for (int s = 0; s < DIM_ROWS; s++)
00089     for (int c = 0; c < DIM_COLS; c++)
00090       if (test1[s][c] != test2[s][c])
00091         deadly_error("test_matrix", "compare", "matrices are different");
00092 
00093   test1.reset();
00094   if (test1.rows() || test1.columns())
00095     deadly_error("test_matrix", "reset", "matrix is not empty");
00096 
00097   print_matrix(test2);
00098 
00099   int_matrix chunk = test2.submatrix(2, 3, 3, 2);
00100   log("chunk of the matrix has:");
00101   print_matrix(chunk);
00102 /*
00103   for (int u = 0; u < 3; u++) {
00104     debug().eol(debugger::NO_ENDING);
00105     log(istring(istring::SPRINTF, "[%d] ", u));
00106     for (int c = 0; c < 2; c++)
00107       log(istring(istring::SPRINTF, "%03d ", int(chunk[u][c])));
00108     debug().eol(debugger::CRLF_AT_END);
00109     log("");
00110   }
00111 */
00112 
00113   int_matrix computed(7, 14);
00114   for (int x1 = 0; x1 < 7; x1++) {
00115     for (int y1 = 0; y1 < 14; y1++) {
00116       if ( (x1 * y1) % 2) computed[x1][y1] = 1 + x1 * 100 + y1;
00117       else computed.put(x1, y1, 1 + x1 * 100 + y1);
00118     }
00119   }
00120 
00121   for (int x2 = 6; x2 >= 0; x2--) {
00122     for (int y2 = 13; y2 >= 0; y2--) {
00123       if (computed[x2][y2] != 1 + x2 * 100 + y2)
00124         deadly_error("test_matrix", "computed matrix", "a value is wrong");
00125     }
00126   }
00127 
00128   computed.redimension(3, 5);
00129   if ( (computed.rows() != 3) || (computed.columns() != 5) )
00130     deadly_error("test_matrix", "redimension", "size is wrong");
00131   for (int x3 = 2; x3 >= 0; x3--) {
00132     for (int y3 = 4; y3 >= 0; y3--) {
00133       if (computed[x3][y3] != 1 + x3 * 100 + y3)
00134         deadly_error("test_matrix", "computed matrix", "a value is wrong");
00135     }
00136   }
00137 
00138   computed.redimension(0, 0);
00139   if (computed.rows() || computed.columns())
00140     deadly_error("test_matrix", "redimension to zero", "matrix is not empty");
00141 
00142   computed.reset(12, 20);
00143   if ( (computed.rows() != 12) || (computed.columns() != 20) )
00144     deadly_error("test_matrix", "resize", "size is wrong");
00145 
00146   {
00147     // this test block ensures that the matrix doesn't blow up from certain
00148     // resizing operations performed on a templated type that has a virtual
00149     // destructor.
00150     matrix<diggulite> grids;
00151     grids.reset();
00152     grids.redimension ( 0, 1 );
00153     grids.redimension ( 1, 1 );
00154     grids.reset(1, 1);
00155   }
00156 
00157   {
00158     // this block tests the zapping ops.
00159     int_matrix test_zap;
00160     STUFF_MATRIX(test_zap, DIM_ROWS, DIM_COLS);
00161     log("matrix before zappage:");
00162     print_matrix(test_zap);
00163     test_zap.zap_row(5);
00164     log("matrix after zappage of row 5:");
00165     print_matrix(test_zap);
00166     // reset the array again.
00167     STUFF_MATRIX(test_zap, DIM_ROWS, DIM_COLS);
00168     test_zap.zap_column(3);
00169     log("matrix after zappage of column 3:");
00170     print_matrix(test_zap);
00171 
00172     // reset test_zap again.
00173     STUFF_MATRIX(test_zap, DIM_ROWS, DIM_COLS);
00174     test_zap.zap_column(0);
00175     test_zap.zap_row(0);
00176     test_zap.zap_row(test_zap.rows() - 1);
00177     test_zap.zap_column(test_zap.columns() - 1);
00178     log("matrix after zap of row 0, col 0, last row, last col");
00179     print_matrix(test_zap);
00180   }
00181 
00182   {
00183     // this block tests the inserting ops.
00184     int_matrix test_insert;
00185     STUFF_MATRIX(test_insert, 4, 4);
00186 
00187     log("matrix before inserting:");
00188     print_matrix(test_insert);
00189     test_insert.insert_row(2);
00190     log("matrix after insert of row 2:");
00191     print_matrix(test_insert);
00192     // reset test_insert again.
00193     STUFF_MATRIX(test_insert, 5, 6);
00194     log("matrix before inserting:");
00195     print_matrix(test_insert);
00196     test_insert.insert_column(3);
00197     log("matrix after insert of column 3:");
00198     print_matrix(test_insert);
00199 
00200     // reset test_insert again.
00201     STUFF_MATRIX(test_insert, 3, 3);
00202     log("matrix before inserting:");
00203     print_matrix(test_insert);
00204     log("insert col at 0");
00205     test_insert.insert_column(0);
00206     print_matrix(test_insert);
00207     log("insert row at rows()");
00208     test_insert.insert_row(test_insert.rows());
00209     print_matrix(test_insert);
00210     log("insert col at cols()");
00211     test_insert.insert_column(test_insert.columns());
00212     print_matrix(test_insert);
00213     log("insert row at 0");
00214     test_insert.insert_row(0);
00215     log("matrix after insert of col 0, last row, last col, row 0");
00216     print_matrix(test_insert);
00217   }
00218 
00219 
00220   guards::alert_message("matrix:: works for those functions tested.");
00221   return 0;
00222 }
00223 
00224 HOOPLE_MAIN(test_matrix, )

Generated on Fri Nov 21 04:30:08 2008 for HOOPLE Libraries by  doxygen 1.5.1