1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* This is a test program for the sparse array routines defined
18 in sparse-xarray.c. */
31 #include <libpspp/argv-parser.h>
32 #include <libpspp/assertion.h>
33 #include <libpspp/hash-functions.h>
34 #include <libpspp/model-checker.h>
35 #include <libpspp/sparse-xarray.h>
36 #include <libpspp/str.h>
42 /* Maximum size of sparse_xarray supported for model checking
47 /* Test parameters. */
50 /* Controlling the test state space. */
51 int n_columns; /* Number of columns in each row. */
52 int max_rows; /* Maximum number of rows. */
53 int max_memory_rows; /* Max rows before writing to disk. */
54 unsigned char n_values; /* Number of unique cell values. */
55 int n_xarrays; /* Number of sparse_xarrays in state. */
57 /* Types of operations to perform. */
58 bool write_cells; /* Write to individual cells. */
59 bool write_rows; /* Write whole rows. */
60 bool write_columns; /* Write whole columns. */
61 bool copy_within_xarray; /* Copy column ranges in a single xarray. */
66 struct sparse_xarray *xarrays[2];
70 test_state_destroy (const struct test_params *params, struct test_state *ts)
74 for (i = 0; i < params->n_xarrays; i++)
75 sparse_xarray_destroy (ts->xarrays[i]);
78 static struct test_state *
79 test_state_clone (const struct test_params *params,
80 const struct test_state *ots)
82 struct test_state *ts;
85 ts = xmalloc (sizeof *ts);
86 for (i = 0; i < params->n_xarrays; i++)
88 ts->xarrays[i] = sparse_xarray_clone (ots->xarrays[i]);
89 if (ts->xarrays[i] == NULL)
97 uint8_t data[MAX_ROWS][MAX_COLS];
98 bool contains_row[MAX_ROWS];
103 struct xarray_model models[2];
106 /* Extracts the contents of TS into TM. */
108 test_model_extract (const struct test_params *params,
109 const struct test_state *ts, struct test_model *tm)
113 for (i = 0; i < params->n_xarrays; i++)
115 const struct sparse_xarray *sx = ts->xarrays[i];
116 struct xarray_model *model = &tm->models[i];
117 size_t n_columns = sparse_xarray_get_n_columns (sx);
118 size_t n_rows = sparse_xarray_get_n_rows (sx);
121 assert (n_rows < MAX_ROWS);
122 assert (n_columns < MAX_COLS);
123 for (row = 0; row < params->max_rows; row++)
125 model->contains_row[row] = sparse_xarray_contains_row (sx, row);
126 if (!sparse_xarray_read (sx, row, 0, n_columns, model->data[row]))
132 /* Checks that test state TS matches the test model TM and
133 reports any mismatches via mc_error. Then, adds SX to MC as a
136 check_state (struct mc *mc, struct test_state *ts, const struct test_model *tm)
138 const struct test_params *params = mc_get_aux (mc);
139 int n_columns = params->n_columns;
143 for (i = 0; i < params->n_xarrays; i++)
145 const struct xarray_model *model = &tm->models[i];
146 const struct sparse_xarray *sx = ts->xarrays[i];
151 assert (n_columns < MAX_COLS);
153 /* Check row count. */
155 for (row = 0; row < params->max_rows; row++)
156 if (model->contains_row[row])
158 if (n_rows != sparse_xarray_get_n_rows (sx))
159 mc_error (mc, "xarray %d: row count (%zu) does not match expected "
160 "(%d)", i, sparse_xarray_get_n_rows (sx), n_rows);
162 /* Check row containment. */
163 for (row = 0; row < params->max_rows; row++)
165 bool contains = sparse_xarray_contains_row (sx, row);
166 if (contains && !model->contains_row[row])
167 mc_error (mc, "xarray %d: row %d is contained by sparse_xarray "
168 "but should not be", i, row);
169 else if (!contains && model->contains_row[row])
170 mc_error (mc, "xarray %d: row %d is not contained by "
171 "sparse_xarray but should be", i, row);
174 /* Check contents. */
176 for (row = 0; row < params->max_rows; row++)
178 unsigned char data[MAX_COLS];
180 if (!sparse_xarray_read (sx, row, 0, n_columns, data))
182 for (col = 0; col < params->n_columns; col++)
183 if (data[col] != model->data[row][col])
185 mc_error (mc, "xarray %d: element %d,%d (of %d,%d) "
186 "differs: %d should be %d",
187 i, row, col, n_rows, n_columns, data[col],
188 model->data[row][col]);
197 mc_error (mc, "xarray %d: expected:", i);
199 for (row = 0; row < params->max_rows; row++)
202 for (col = 0; col < n_columns; col++)
203 ds_put_format (&ds, " %d", model->data[row][col]);
204 mc_error (mc, "xarray %d: row %d:%s", i, row, ds_cstr (&ds));
207 mc_error (mc, "xarray %d: actual:", i);
209 for (row = 0; row < params->max_rows; row++)
211 unsigned char data[MAX_COLS];
213 if (!sparse_xarray_read (sx, row, 0, n_columns, data))
217 for (col = 0; col < n_columns; col++)
218 ds_put_format (&ds, " %d", data[col]);
219 mc_error (mc, "xarray %d: row %d:%s", i, row, ds_cstr (&ds));
227 for (i = 0; i < params->n_xarrays; i++)
228 hash = sparse_xarray_model_checker_hash (ts->xarrays[i], hash);
229 if (mc_discard_dup_state (mc, hash))
230 test_state_destroy (params, ts);
232 mc_add_state (mc, ts);
236 next_data (unsigned char *data, int n, int n_values)
239 for (i = n - 1; i >= 0; i--)
242 if (data[i] < n_values)
249 struct copy_columns_params
251 int n; /* Number of columns to copy. */
252 int src; /* Offset of first source column. */
253 int dst; /* Offset of first destination column. */
257 copy_columns (const void *src_, void *dst_, void *copy_)
259 const struct copy_columns_params *copy = copy_;
260 const uint8_t *src = src_;
263 memmove (dst + copy->dst, src + copy->src, copy->n);
267 /* "init" function for struct mc_class. */
269 sparse_xarray_mc_init (struct mc *mc)
271 struct test_params *params = mc_get_aux (mc);
272 struct test_state *ts;
273 struct test_model tm;
276 mc_name_operation (mc, "empty sparse_xarray with n_columns=%d, "
277 "max_memory_rows=%d",
278 params->n_columns, params->max_memory_rows);
279 ts = xmalloc (sizeof *ts);
280 for (i = 0; i < params->n_xarrays; i++)
281 ts->xarrays[i] = sparse_xarray_create (params->n_columns,
282 params->max_memory_rows);
283 memset (&tm, 0, sizeof tm);
284 check_state (mc, ts, &tm);
287 /* "mutate" function for struct mc_class. */
289 sparse_xarray_mc_mutate (struct mc *mc, const void *ots_)
291 struct test_params *params = mc_get_aux (mc);
292 size_t n_columns = params->n_columns;
293 const struct test_state *ots = ots_;
294 struct test_model otm;
297 test_model_extract (params, ots, &otm);
298 for (i = 0; i < params->n_xarrays; i++)
301 int row, col, n, src, dst;
303 /* Write all possible values to each possible single cell. */
304 if (params->write_cells)
305 for (row = 0; row < params->max_rows; row++)
306 for (col = 0; col < n_columns; col++)
307 for (value = 0; value < params->n_values; value++)
308 if (mc_include_state (mc))
310 struct test_state *ts = test_state_clone (params, ots);
311 struct sparse_xarray *sx = ts->xarrays[i];
312 struct test_model tm = otm;
313 struct xarray_model *model = &tm.models[i];
315 mc_name_operation (mc, "xarray %d: set (%d,%d) to %d",
317 if (!sparse_xarray_write (sx, row, col, 1, &value))
319 model->data[row][col] = value;
320 model->contains_row[row] = true;
321 check_state (mc, ts, &tm);
324 /* Write all possible row contents to each row. */
325 if (params->write_rows)
326 for (row = 0; row < params->max_rows; row++)
328 struct test_model tm = otm;
329 struct xarray_model *model = &tm.models[i];
331 memset (model->data[row], 0, n_columns);
332 model->contains_row[row] = true;
335 if (mc_include_state (mc))
337 struct test_state *ts = test_state_clone (params, ots);
338 struct sparse_xarray *sx = ts->xarrays[i];
339 char row_string[MAX_COLS + 1];
341 mc_name_operation (mc, "xarray %d: set row %d to %s",
343 for (col = 0; col < n_columns; col++)
345 value = model->data[row][col];
346 row_string[col] = value < 10 ? '0' + value : '*';
348 row_string[n_columns] = '\0';
349 if (!sparse_xarray_write (sx, row, 0, n_columns,
352 check_state (mc, ts, &tm);
355 while (next_data (model->data[row], n_columns, params->n_values));
358 /* Write all possible values to each possible column. */
359 if (params->write_columns)
360 for (col = 0; col < n_columns; col++)
361 for (value = 0; value < params->n_values; value++)
362 if (mc_include_state (mc))
364 struct test_state *ts = test_state_clone (params, ots);
365 struct sparse_xarray *sx = ts->xarrays[i];
366 struct test_model tm = otm;
367 struct xarray_model *model = &tm.models[i];
369 mc_name_operation (mc, "xarray %d: write value %d to "
370 "column %d", i, value, col);
371 if (!sparse_xarray_write_columns (sx, col, 1, &value))
373 for (row = 0; row < params->max_rows; row++)
374 model->data[row][col] = value;
375 check_state (mc, ts, &tm);
378 /* Copy all possible column ranges within a single sparse_xarray. */
379 if (params->copy_within_xarray)
380 for (n = 1; n <= n_columns; n++)
381 for (src = 0; src <= n_columns - n; src++)
382 for (dst = 0; dst <= n_columns - n; dst++)
383 if (mc_include_state (mc))
385 struct copy_columns_params copy_aux;
386 struct test_state *ts = test_state_clone (params, ots);
387 struct sparse_xarray *sx = ts->xarrays[i];
388 struct test_model tm = otm;
389 struct xarray_model *model = &tm.models[i];
391 mc_name_operation (mc, "xarray %d: copy %d columns from "
392 "offset %d to offset %d", i, n, src, dst);
397 if (!sparse_xarray_copy (sx, sx, copy_columns, ©_aux))
400 for (row = 0; row < params->max_rows; row++)
401 memmove (&model->data[row][dst],
402 &model->data[row][src], n);
404 check_state (mc, ts, &tm);
408 if (params->n_xarrays == 2)
410 int row, n, src, dst;
412 /* Copy all possible column ranges from xarrays[0] to xarrays[1]. */
413 for (n = 1; n <= n_columns; n++)
414 for (src = 0; src <= n_columns - n; src++)
415 for (dst = 0; dst <= n_columns - n; dst++)
416 if (mc_include_state (mc))
418 struct copy_columns_params copy_aux;
419 struct test_state *ts = test_state_clone (params, ots);
420 struct test_model tm = otm;
422 mc_name_operation (mc, "copy %d columns from offset %d in "
423 "xarray 0 to offset %d in xarray 1",
429 if (!sparse_xarray_copy (ts->xarrays[0], ts->xarrays[1],
430 copy_columns, ©_aux))
433 for (row = 0; row < params->max_rows; row++)
435 if (tm.models[0].contains_row[row])
436 tm.models[1].contains_row[row] = true;
437 memmove (&tm.models[1].data[row][dst],
438 &tm.models[0].data[row][src], n);
441 check_state (mc, ts, &tm);
446 /* "destroy" function for struct mc_class. */
448 sparse_xarray_mc_destroy (const struct mc *mc UNUSED, void *ts_)
450 struct test_params *params = mc_get_aux (mc);
451 struct test_state *ts = ts_;
453 test_state_destroy (params, ts);
459 printf ("%s, for testing the sparse_xarray implementation.\n"
460 "Usage: %s [OPTION]...\n"
461 "\nTest state space parameters (min...max, default):\n"
462 " --columns=N Number of columns per row (0...5, 3)\n"
463 " --max-rows=N Maximum number of rows (0...5, 3)\n"
464 " --max-memory-rows=N Max rows before paging to disk (0...5, 3)\n"
465 " --values=N Number of unique cell values (1...254, 3)\n"
466 " --xarrays=N Number of xarrays at a time (1...2, 1)\n"
467 "\nTest operation parameters:\n"
468 " --no-write-cells Do not write individual cells\n"
469 " --no-write-rows Do not write whole rows\n"
470 " --no-write-columns Do not write whole columns\n"
471 " --no-copy-columns Do not copy column ranges in an xarray\n",
472 program_name, program_name);
474 fputs ("\nOther options:\n"
475 " --help Display this help message\n"
476 "\nReport bugs to <bug-gnu-pspp@gnu.org>\n",
490 OPT_NO_WRITE_COLUMNS,
493 N_SPARSE_XARRAY_OPTIONS
496 static struct argv_option sparse_xarray_argv_options[N_SPARSE_XARRAY_OPTIONS] =
498 {"columns", 0, required_argument, OPT_COLUMNS},
499 {"max-rows", 0, required_argument, OPT_MAX_ROWS},
500 {"max-memory-rows", 0, required_argument, OPT_MAX_MEMORY_ROWS},
501 {"values", 0, required_argument, OPT_VALUES},
502 {"xarrays", 0, required_argument, OPT_XARRAYS},
503 {"no-write-cells", 0, no_argument, OPT_NO_WRITE_CELLS},
504 {"no-write-rows", 0, no_argument, OPT_NO_WRITE_ROWS},
505 {"no-write-columns", 0, no_argument, OPT_NO_WRITE_COLUMNS},
506 {"no-copy-columns", 0, no_argument, OPT_NO_COPY_COLUMNS},
507 {"help", 'h', no_argument, OPT_HELP},
511 sparse_xarray_option_callback (int id, void *params_)
513 struct test_params *params = params_;
517 params->n_columns = atoi (optarg);
521 params->max_rows = atoi (optarg);
524 case OPT_MAX_MEMORY_ROWS:
525 params->max_memory_rows = atoi (optarg);
529 params->n_values = atoi (optarg);
533 params->n_xarrays = atoi (optarg);
536 case OPT_NO_WRITE_CELLS:
537 params->write_cells = false;
540 case OPT_NO_WRITE_ROWS:
541 params->write_rows = false;
544 case OPT_NO_WRITE_COLUMNS:
545 params->write_columns = false;
548 case OPT_NO_COPY_COLUMNS:
549 params->copy_within_xarray = false;
562 main (int argc, char *argv[])
564 static const struct mc_class sparse_xarray_mc_class =
566 sparse_xarray_mc_init,
567 sparse_xarray_mc_mutate,
568 sparse_xarray_mc_destroy,
571 struct test_params params;
572 struct mc_options *options;
573 struct mc_results *results;
574 struct argv_parser *parser;
578 set_program_name (argv[0]);
580 /* Default parameters. */
581 params.n_columns = 3;
583 params.max_memory_rows = 3;
585 params.n_xarrays = 1;
586 params.write_cells = true;
587 params.write_rows = true;
588 params.write_columns = true;
589 params.copy_within_xarray = true;
591 /* Parse command line. */
592 parser = argv_parser_create ();
593 options = mc_options_create ();
594 mc_options_register_argv_parser (options, parser);
595 argv_parser_add_options (parser, sparse_xarray_argv_options,
596 N_SPARSE_XARRAY_OPTIONS,
597 sparse_xarray_option_callback, ¶ms);
598 if (!argv_parser_run (parser, argc, argv))
600 argv_parser_destroy (parser);
601 verbosity = mc_options_get_verbosity (options);
603 /* Force parameters into allowed ranges. */
604 params.n_columns = MAX (0, MIN (params.n_columns, MAX_COLS));
605 params.max_rows = MAX (0, MIN (params.max_rows, MAX_ROWS));
606 params.max_memory_rows = MAX (0, MIN (params.max_memory_rows,
608 params.n_values = MIN (254, MAX (1, params.n_values));
609 params.n_xarrays = MAX (1, MIN (2, params.n_xarrays));
610 mc_options_set_aux (options, ¶ms);
611 results = mc_run (&sparse_xarray_mc_class, options);
613 /* Output results. */
614 success = (mc_results_get_stop_reason (results) != MC_MAX_ERROR_COUNT
615 && mc_results_get_stop_reason (results) != MC_INTERRUPTED);
616 if (verbosity > 0 || !success)
618 printf ("Parameters: "
619 "--columns=%d --max-rows=%d --max-memory-rows=%d --values=%d "
621 params.n_columns, params.max_rows, params.max_memory_rows,
622 params.n_values, params.n_xarrays);
623 if (!params.write_cells)
624 printf (" --no-write-cells");
625 if (!params.write_rows)
626 printf (" --no-write-rows");
627 if (!params.write_columns)
628 printf (" --no-write-columns");
629 if (!params.copy_within_xarray)
630 printf (" --no-copy-columns");
632 mc_results_print (results, stdout);
634 mc_results_destroy (results);
636 return success ? 0 : EXIT_FAILURE;