22 using namespace basis;
38 #define STUFF_MATRIX(to_stuff, max_row, max_col) \
39 to_stuff.reset(max_row, max_col); \
40 for (int r = 0; r < max_row; r++) \
41 for (int c = 0; c < max_col; c++) \
42 to_stuff.put(r, c, test_pure.get(r, c))
57 virtual ~diggulite() {}
75 static astring dump_matrix(
const my_int_matrix &to_print);
78 void print_matrix(
const my_int_matrix &to_print);
81 void test_out_submatrix(
const my_int_matrix &source);
84 void test_out_redimension();
87 void test_out_resizing_virtual_objects();
90 void test_out_zapping(
const my_int_matrix &test_pure);
93 void test_out_inserting(
const my_int_matrix &test_pure);
102 my_int_matrix(
int r = 0,
int c = 0) :
int_matrix(r, c) {}
107 virtual bool equal_to(
const equalizable &s2)
const {
108 const my_int_matrix *sec =
dynamic_cast<const my_int_matrix *
>(&s2);
109 if (!sec)
return false;
110 if (rows() != sec->rows())
return false;
111 if (columns() != sec->columns())
return false;
112 for (
int r = 0; r < this->rows(); r++)
113 for (
int c = 0; c < this->columns(); c++)
114 if ((*
this)[r][c] != (*sec)[r][c])
return false;
118 virtual void text_form(
base_string &state_fill)
const {
119 state_fill.
assign(test_matrix::dump_matrix(*
this));
127 astring test_matrix::dump_matrix(
const my_int_matrix &to_print)
130 for (
int t = 0; t < to_print.rows(); t++) {
131 text +=
astring(astring::SPRINTF,
"[%d] ", t);
132 for (
int c = 0; c < to_print.columns(); c++)
133 text +=
astring(astring::SPRINTF,
"%03d ",
int(to_print[t][c]));
134 text += parser_bits::platform_eol_to_chars();
139 void test_matrix::print_matrix(
const my_int_matrix &to_print)
140 { log(
astring(
"\n") + dump_matrix(to_print)); }
142 void test_matrix::test_out_submatrix(
const my_int_matrix &source)
145 my_int_matrix test2(source);
149 ASSERT_EQUAL(source[s][c], test2[s][c], "computed matrices should be same after
copy");
152 log(
"before submatrix:");
155 my_int_matrix chunk(test2.submatrix(2, 3, 3, 2));
156 my_int_matrix chunk_comparator(3, 2);
157 for (
int r = 0; r < 3; r++)
158 for (
int c = 0; c < 2; c++)
159 chunk_comparator[r][c] = test2[r+2][c+3];
160 ASSERT_EQUAL(chunk, chunk_comparator,
"submatrix should grab proper contents");
162 log(
"after submatrix, chunk of the matrix has:");
167 void test_matrix::test_out_redimension()
169 FUNCDEF(
"test_out_redimension")
170 my_int_matrix computed(7, 14);
171 for (
int x1 = 0; x1 < 7; x1++) {
172 for (
int y1 = 0; y1 < 14; y1++) {
173 if ( (x1 * y1) % 2) computed[x1][y1] = 1 + x1 * 100 + y1;
174 else computed.put(x1, y1, 1 + x1 * 100 + y1);
178 for (
int x2 = 6; x2 >= 0; x2--) {
179 for (
int y2 = 13; y2 >= 0; y2--) {
181 "computed matrix should have proper computed values");
185 computed.redimension(3, 5);
186 ASSERT_FALSE( (computed.rows() != 3) || (computed.columns() != 5),
187 "redimension should not get size wrong");
188 for (
int x3 = 2; x3 >= 0; x3--) {
189 for (
int y3 = 4; y3 >= 0; y3--) {
191 "computed matrix should still have right values");
195 computed.redimension(0, 0);
197 "redimension to zero should see matrix as empty");
199 computed.reset(12, 20);
200 ASSERT_FALSE( (computed.rows() != 12) || (computed.columns() != 20),
201 "resize should compute proper size");
204 void test_matrix::test_out_resizing_virtual_objects()
206 FUNCDEF(
"test_out_resizing_virtual_objects")
212 grids.redimension ( 0, 1 );
213 grids.redimension ( 1, 1 );
215 ASSERT_TRUE(true, "no explosions should occur due to virtual contents");
218 void test_matrix::test_out_zapping(const my_int_matrix &test_pure)
222 my_int_matrix test_zap;
226 log(
"matrix before zappage:");
227 print_matrix(test_zap);
230 my_int_matrix compare_1 = test_zap;
231 ASSERT_EQUAL(compare_1, test_zap,
"assignment works right");
236 compare_1[r - 1][c] = compare_1[r][c];
238 ASSERT_EQUAL(compare_1, test_zap,
"zapping should work regardless of path");
241 log(
"matrix after zappage of row 5:");
242 print_matrix(test_zap);
247 my_int_matrix compare_2 = test_zap;
248 test_zap.zap_column(3);
252 compare_2[r][c - 1] = compare_2[r][c];
254 ASSERT_EQUAL(compare_2, test_zap,
"second zapping should work regardless of path");
257 log(
"matrix after zappage of column 3:");
258 print_matrix(test_zap);
263 my_int_matrix compare_3(test_zap.submatrix(1, 1,
DIM_ROWS - 2,
DIM_COLS - 2));
264 test_zap.zap_column(0);
266 test_zap.zap_row(test_zap.rows() - 1);
267 test_zap.zap_column(test_zap.columns() - 1);
268 ASSERT_EQUAL(test_zap, compare_3,
"zapping and submatrix should compute same result");
271 log(
"matrix after zap of row 0, col 0, last row, last col");
272 print_matrix(test_zap);
276 void test_matrix::test_out_inserting(
const my_int_matrix &test_pure)
280 my_int_matrix test_insert;
284 log(
"matrix before inserting:");
285 print_matrix(test_insert);
288 my_int_matrix compare_1(test_insert);
289 test_insert.insert_row(2);
290 compare_1.insert_row(4);
291 for (
int r = 3; r >= 2; r--)
292 for (
int c = 0; c < 4; c++)
293 compare_1[r + 1][c] = compare_1[r][c];
294 for (
int c = 0; c < 4; c++)
296 ASSERT_EQUAL(test_insert, compare_1,
"inserting row should create expected array");
299 log(
"matrix after insert of row 2:");
300 print_matrix(test_insert);
307 log(
"reset matrix before inserting:");
308 print_matrix(test_insert);
311 my_int_matrix compare_2(test_insert);
312 test_insert.insert_column(3);
313 compare_2.insert_column(6);
314 for (
int r = 0; r < 5; r++)
315 for (
int c = 5; c >= 3; c--)
316 compare_2[r][c + 1] = compare_2[r][c];
317 for (
int r = 0; r < 5; r++)
319 ASSERT_EQUAL(test_insert, compare_2,
"inserting column should create expected array");
322 log(
"matrix after insert of column 3:");
323 print_matrix(test_insert);
328 my_int_matrix compare_3(5, 5);
329 for (
int r = 0; r < 3; r++)
330 for (
int c = 0; c < 3; c++)
331 compare_3[r + 1][c + 1] = test_insert[r][c];
332 for (
int r = 0; r < 5; r++) { compare_3[r][0] = 0; compare_3[r][4] = 0; }
333 for (
int c = 0; c < 5; c++) { compare_3[0][c] = 0; compare_3[4][c] = 0; }
336 log(
"matrix before inserting:");
337 print_matrix(test_insert);
340 test_insert.insert_column(0);
343 log(
"insert col at 0");
344 print_matrix(test_insert);
347 test_insert.insert_row(test_insert.rows());
350 log(
"insert row at rows()");
351 print_matrix(test_insert);
354 test_insert.insert_column(test_insert.columns());
357 log(
"insert col at cols()");
358 print_matrix(test_insert);
359 log(
"insert row at 0...");
362 test_insert.insert_row(0);
365 "inserting some rows and columns should create expected array");
368 log(
astring(
"matrix after insert of col 0, last row, last col, row 0"));
369 print_matrix(test_insert);
373 int test_matrix::execute()
382 my_int_matrix test1 = test_pure;
385 ASSERT_FALSE(test1.rows() || test1.columns(),
"after reset matrix should be empty");
387 test_out_submatrix(test_pure);
389 test_out_redimension();
391 test_out_resizing_virtual_objects();
393 test_out_zapping(test_pure);
395 test_out_inserting(test_pure);
397 return final_report();
The application_shell is a base object for console programs.
virtual basis::outcome log(const basis::base_string &to_print, int filter=basis::ALWAYS_PRINT)
< returns the full name of the current application.
Provides a dynamically resizable ASCII character string.
Defines the base class for all string processing objects in hoople.
virtual base_string & assign(const base_string &s)=0
Sets the contents of this string to "s".
Base class for object that can tell itself apart from other instances.
the base class of the most easily used and tested objects in the library.
Represents a two-dimensional array of objects.
char * copy(register char *str)
#define DEFINE_CLASS_NAME(objname)
Defines the name of a class by providing a couple standard methods.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Provides macros that implement the 'main' program of an application.
#define HOOPLE_MAIN(obj_name, obj_args)
options that should work for most unix and linux apps.
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
An extension to floating point primitives providing approximate equality.
A dynamic container class that holds any kind of object via pointers.
Useful support functions for unit testing, especially within hoople.
#define STUFF_MATRIX(to_stuff, max_row, max_col)
#define ASSERT_EQUAL(a, b, test_name)
#define ASSERT_TRUE(a, test_name)
#define ASSERT_FALSE(a, test_name)