t_mat_morph.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : test_mat_morph                                                    *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2003-$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/mat_morph.cpp>
00019 #include <opsystem/application_shell.h>
00020 #include <loggers/console_logger.h>
00021 #include <data_struct/static_memory_gremlin.h>
00022 
00023 // these are the dimensions of our test matrices.
00024 const int DIM_ROWS = 10;
00025 const int DIM_COLS = 10;
00026 
00027 // fills the matrix "to_stuff" with the pure version of the ingredients.
00028 #define STUFF_MATRIX(to_stuff, max_row, max_col) \
00029   to_stuff.reset(max_row, max_col); \
00030   for (int r = 0; r < max_row; r++) \
00031     for (int c = 0; c < max_col; c++) \
00032       to_stuff.put(r, c, new int(*test_pure.get(r, c))); \
00033 
00035 
00036 // this class exhibits an old bug where the mat_morph was zeroing out its
00037 // contents for a same size resize.  the zeroing allowed hell to spew forth.
00038 class diggulite
00039 {
00040 public:
00041   diggulite() {}
00042   virtual ~diggulite() {}
00043 };
00044 
00046 
00047 class test_mat_morph : public application_shell
00048 {
00049 public:
00050   test_mat_morph();
00051 
00052   IMPLEMENT_CLASS_NAME("test_mat_morph");
00053 
00054   int execute();
00055     // performs main body of test.
00056 
00057   void print_mat_morph(const mat_morph<int> &to_print);
00058     // dumps "to_print" to the diagnostic output.
00059 };
00060 
00062 
00063 test_mat_morph::test_mat_morph() : application_shell(class_name())
00064 {
00065 }
00066 
00067 void test_mat_morph::print_mat_morph(const mat_morph<int> &to_print)
00068 {
00069   istring text;
00070   for (int t = 0; t < to_print.rows(); t++) {
00071     text += istring(istring::SPRINTF, "[%d] ", t);
00072     for (int c = 0; c < to_print.columns(); c++)
00073       text += !to_print.get(t, c) ? "--- "
00074           : isprintf("%03d ", *to_print.get(t, c));
00075     text += log_base::platform_ending();
00076   }
00077   log(text);
00078 }
00079 
00080 int test_mat_morph::execute()
00081 {
00082   mat_morph<int> test_pure(DIM_ROWS, DIM_COLS);
00083     // this matrix is kept without modification.
00084   {
00085   for (int r = 0; r < DIM_ROWS; r++)
00086     for (int c = 0; c < DIM_COLS; c++)
00087       test_pure.put(r, c, new int(r * DIM_COLS + c));
00088   }
00089 
00090   // a copy to work with.  this will not be allowed to take our pointers down.
00091   mat_morph<int> test1(DIM_ROWS, DIM_COLS);
00092   {
00093   for (int r = 0; r < DIM_ROWS; r++)
00094     for (int c = 0; c < DIM_COLS; c++)
00095       test1.put(r, c, test_pure.get(r, c));
00096   }
00097 
00098   {
00099   for (int s = 0; s < DIM_ROWS; s++)
00100     for (int c = 0; c < DIM_COLS; c++)
00101       if (*test_pure.get(s, c) != *test1.get(s, c))
00102         deadly_error("test_mat_morph", "compare", "matrices are different");
00103   }
00104 
00105   {
00106   for (int s = 0; s < DIM_ROWS; s++)
00107     for (int c = 0; c < DIM_COLS; c++)
00108       test1.acquire(s, c);  // toss the pointer copy.
00109   }
00110 
00111   test1.reset();
00112   if (test1.rows() || test1.columns())
00113     deadly_error("test_mat_morph", "reset", "mat_morph is not empty");
00114 
00115   print_mat_morph(test_pure);
00116 
00117   mat_morph<int> computed(7, 14);
00118   for (int x1 = 0; x1 < 7; x1++) {
00119     for (int y1 = 0; y1 < 14; y1++) {
00120       computed.put(x1, y1, new int(1 + x1 * 100 + y1));
00121     }
00122   }
00123 
00124   for (int x2 = 6; x2 >= 0; x2--) {
00125     for (int y2 = 13; y2 >= 0; y2--) {
00126       if (*computed.get(x2, y2) != 1 + x2 * 100 + y2)
00127         deadly_error("test_mat_morph", "computed values", "a value is wrong");
00128     }
00129   }
00130 
00131   computed.redimension(3, 5);
00132   if ( (computed.rows() != 3) || (computed.columns() != 5) )
00133     deadly_error("test_mat_morph", "redimension",
00134         isprintf("size is wrong (want 3x5, got %dx%d).", computed.rows(),
00135             computed.columns()));
00136   for (int x3 = 2; x3 >= 0; x3--) {
00137     for (int y3 = 4; y3 >= 0; y3--) {
00138       if (*computed.get(x3, y3) != 1 + x3 * 100 + y3)
00139         deadly_error("test_mat_morph", "computed values 2", "a value is wrong");
00140     }
00141   }
00142 
00143   computed.redimension(0, 0);
00144   if (computed.rows() || computed.columns())
00145     deadly_error("test_mat_morph", "redimension to zero",
00146         "mat_morph is not empty");
00147 
00148   computed.reset(12, 20);
00149   if ( (computed.rows() != 12) || (computed.columns() != 20) )
00150     deadly_error("test_mat_morph", "resize", "size is wrong");
00151 
00152   {
00153     // this test block ensures that the mat_morph doesn't blow up from certain
00154     // resizing operations performed on a templated type that has a virtual
00155     // destructor.
00156     mat_morph<diggulite> grids;
00157     grids.reset();
00158     grids.redimension(0, 1);
00159     grids.redimension(1, 1);
00160     grids.reset(1, 1);
00161   }
00162 
00163   {
00164     // this block tests the zapping ops.
00165     mat_morph<int> test_zap;
00166     STUFF_MATRIX(test_zap, DIM_ROWS, DIM_COLS);
00167 
00168     log("mat_morph before zappage:");
00169     print_mat_morph(test_zap);
00170     test_zap.zap_row(5);
00171     log("mat_morph afterzappage of row 5:");
00172     print_mat_morph(test_zap);
00173     // reset test_zap again.
00174     STUFF_MATRIX(test_zap, DIM_ROWS, DIM_COLS);
00175     test_zap.zap_column(3);
00176     log("mat_morph afterzappage of column 3:");
00177     print_mat_morph(test_zap);
00178 
00179     // reset test_zap again.
00180     STUFF_MATRIX(test_zap, DIM_ROWS, DIM_COLS);
00181     test_zap.zap_column(0);
00182     test_zap.zap_row(0);
00183     test_zap.zap_row(test_zap.rows() - 1);
00184     test_zap.zap_column(test_zap.columns() - 1);
00185     log("mat_morph after zap of row 0, col 0, last row, last col");
00186     print_mat_morph(test_zap);
00187   }
00188 
00189   {
00190     // this block tests the inserting ops.
00191     mat_morph<int> test_insert;
00192     STUFF_MATRIX(test_insert, 4, 4);
00193 
00194     log("mat_morph before inserting:");
00195     print_mat_morph(test_insert);
00196     test_insert.insert_row(2);
00197     log("mat_morph after insert of row 2:");
00198     print_mat_morph(test_insert);
00199     // reset test_insert again.
00200     STUFF_MATRIX(test_insert, 5, 6);
00201     log("mat_morph before inserting:");
00202     print_mat_morph(test_insert);
00203     test_insert.insert_column(3);
00204     log("mat_morph after insert of column 3:");
00205     print_mat_morph(test_insert);
00206 
00207     // reset test_insert again.
00208     STUFF_MATRIX(test_insert, 3, 3);
00209     log("mat_morph before inserting:");
00210     print_mat_morph(test_insert);
00211     test_insert.insert_column(0);
00212     test_insert.insert_row(test_insert.rows());
00213     test_insert.insert_column(test_insert.columns());
00214     test_insert.insert_row(0);
00215     log("mat_morph after insert of col 0, last row, last col, row 0");
00216     print_mat_morph(test_insert);
00217   }
00218 
00219   guards::alert_message("mat_morph:: works for those functions tested.");
00220   return 0;
00221 }
00222 
00223 HOOPLE_MAIN(test_mat_morph, )
00224 

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