Fix compiler warning in test program
[pspp-builds.git] / tests / data / datasheet-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2009 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17 #include <config.h>
18
19 #include <data/datasheet.h>
20
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25
26 #include <data/casereader-provider.h>
27 #include <data/casereader.h>
28 #include <data/casewriter.h>
29 #include <data/lazy-casereader.h>
30 #include <libpspp/argv-parser.h>
31 #include <libpspp/array.h>
32 #include <libpspp/assertion.h>
33 #include <libpspp/hash-functions.h>
34 #include <libpspp/model-checker.h>
35 #include <libpspp/range-map.h>
36 #include <libpspp/range-set.h>
37 #include <libpspp/str.h>
38 #include <libpspp/taint.h>
39 #include <libpspp/tower.h>
40
41 #include "error.h"
42 #include "minmax.h"
43 #include "progname.h"
44 #include "xalloc.h"
45
46 /* lazy_casereader callback function to instantiate a casereader
47    from the datasheet. */
48 static struct casereader *
49 lazy_callback (void *ds_)
50 {
51   struct datasheet *ds = ds_;
52   return datasheet_make_reader (ds);
53 }
54
55
56 /* Maximum size of datasheet supported for model checking
57    purposes. */
58 #define MAX_ROWS 5
59 #define MAX_COLS 5
60 #define MAX_WIDTHS 5
61
62 /* Test params. */
63 struct datasheet_test_params
64   {
65     /* Parameters. */
66     int max_rows;               /* Maximum number of rows. */
67     int max_cols;               /* Maximum number of columns. */
68     int backing_rows;           /* Number of rows of backing store. */
69     int backing_widths[MAX_COLS]; /* Widths of columns of backing store. */
70     int n_backing_cols;           /* Number of columns of backing store. */
71     int widths[MAX_WIDTHS];     /* Allowed column widths. */
72     int n_widths;
73
74     /* State. */
75     unsigned int next_value;
76   };
77
78 static bool
79 check_caseproto (struct mc *mc, const struct caseproto *benchmark,
80                  const struct caseproto *test, const char *test_name)
81 {
82   size_t n_columns = caseproto_get_n_widths (benchmark);
83   size_t col;
84   bool ok;
85
86   if (n_columns != caseproto_get_n_widths (test))
87     {
88       mc_error (mc, "%s column count (%zu) does not match expected (%zu)",
89                 test_name, caseproto_get_n_widths (test), n_columns);
90       return false;
91     }
92
93   ok = true;
94   for (col = 0; col < n_columns; col++)
95     {
96       int benchmark_width = caseproto_get_width (benchmark, col);
97       int test_width = caseproto_get_width (test, col);
98       if (benchmark_width != test_width)
99         {
100           mc_error (mc, "%s column %zu width (%d) differs from expected (%d)",
101                     test_name, col, test_width, benchmark_width);
102           ok = false;
103         }
104     }
105   return ok;
106 }
107
108 /* Checks that READER contains the N_ROWS rows and N_COLUMNS
109    columns of data in ARRAY, reporting any errors via MC. */
110 static void
111 check_datasheet_casereader (struct mc *mc, struct casereader *reader,
112                             union value array[MAX_ROWS][MAX_COLS],
113                             size_t n_rows, const struct caseproto *proto)
114 {
115   size_t n_columns = caseproto_get_n_widths (proto);
116
117   if (!check_caseproto (mc, proto, casereader_get_proto (reader),
118                         "casereader"))
119     return;
120   else if (casereader_get_case_cnt (reader) != n_rows)
121     {
122       if (casereader_get_case_cnt (reader) == CASENUMBER_MAX
123           && casereader_count_cases (reader) == n_rows)
124         mc_error (mc, "datasheet casereader has unknown case count");
125       else
126         mc_error (mc, "casereader row count (%lu) does not match "
127                   "expected (%zu)",
128                   (unsigned long int) casereader_get_case_cnt (reader),
129                   n_rows);
130     }
131   else
132     {
133       struct ccase *c;
134       size_t row;
135
136       for (row = 0; row < n_rows; row++)
137         {
138           size_t col;
139
140           c = casereader_read (reader);
141           if (c == NULL)
142             {
143               mc_error (mc, "casereader_read failed reading row %zu of %zu "
144                         "(%zu columns)", row, n_rows, n_columns);
145               return;
146             }
147
148           for (col = 0; col < n_columns; col++)
149             {
150               int width = caseproto_get_width (proto, col);
151               if (!value_equal (case_data_idx (c, col), &array[row][col],
152                                 width))
153                 {
154                   if (width == 0)
155                     mc_error (mc, "element %zu,%zu (of %zu,%zu) differs: "
156                               "%g != %g",
157                               row, col, n_rows, n_columns,
158                               case_num_idx (c, col), array[row][col].f);
159                   else
160                     mc_error (mc, "element %zu,%zu (of %zu,%zu) differs: "
161                               "'%.*s' != '%.*s'",
162                               row, col, n_rows, n_columns,
163                               width, case_str_idx (c, col),
164                               width, value_str (&array[row][col], width));
165                 }
166             }
167
168           case_unref (c);
169         }
170
171       c = casereader_read (reader);
172       if (c != NULL)
173         mc_error (mc, "casereader has extra cases (expected %zu)", n_rows);
174     }
175 }
176
177 /* Checks that datasheet DS contains has N_ROWS rows, N_COLUMNS
178    columns, and the same contents as ARRAY, reporting any
179    mismatches via mc_error.  Then, adds DS to MC as a new state. */
180 static void
181 check_datasheet (struct mc *mc, struct datasheet *ds,
182                  union value array[MAX_ROWS][MAX_COLS],
183                  size_t n_rows, const struct caseproto *proto)
184 {
185   size_t n_columns = caseproto_get_n_widths (proto);
186   struct datasheet *ds2;
187   struct casereader *reader;
188   unsigned long int serial = 0;
189
190   assert (n_rows < MAX_ROWS);
191   assert (n_columns < MAX_COLS);
192
193   /* Check contents of datasheet via datasheet functions. */
194   if (!check_caseproto (mc, proto, datasheet_get_proto (ds), "datasheet"))
195     {
196       /* check_caseproto emitted errors already. */
197     }
198   else if (n_rows != datasheet_get_n_rows (ds))
199     mc_error (mc, "row count (%lu) does not match expected (%zu)",
200               (unsigned long int) datasheet_get_n_rows (ds), n_rows);
201   else
202     {
203       size_t row, col;
204       bool difference = false;
205
206       for (row = 0; row < n_rows; row++)
207         for (col = 0; col < n_columns; col++)
208           {
209             int width = caseproto_get_width (proto, col);
210             union value *av = &array[row][col];
211             union value v;
212
213             value_init (&v, width);
214             if (!datasheet_get_value (ds, row, col, &v))
215               NOT_REACHED ();
216             if (!value_equal (&v, av, width))
217               {
218                 if (width == 0)
219                   mc_error (mc, "element %zu,%zu (of %zu,%zu) differs: "
220                             "%g != %g", row, col, n_rows, n_columns,
221                             v.f, av->f);
222                 else
223                   mc_error (mc, "element %zu,%zu (of %zu,%zu) differs: "
224                             "'%.*s' != '%.*s'",
225                             row, col, n_rows, n_columns,
226                             width, value_str (&v, width),
227                             width, value_str (av, width));
228                 difference = true;
229               }
230             value_destroy (&v, width);
231           }
232
233       if (difference)
234         {
235           struct string s;
236
237           mc_error (mc, "expected:");
238           ds_init_empty (&s);
239           for (row = 0; row < n_rows; row++)
240             {
241               ds_clear (&s);
242               ds_put_format (&s, "row %zu:", row);
243               for (col = 0; col < n_columns; col++)
244                 {
245                   const union value *v = &array[row][col];
246                   int width = caseproto_get_width (proto, col);
247                   if (width == 0)
248                     ds_put_format (&s, " %g", v->f);
249                   else
250                     ds_put_format (&s, " '%.*s'", width, value_str (v, width));
251                 }
252               mc_error (mc, "%s", ds_cstr (&s));
253             }
254
255           mc_error (mc, "actual:");
256           ds_init_empty (&s);
257           for (row = 0; row < n_rows; row++)
258             {
259               ds_clear (&s);
260               ds_put_format (&s, "row %zu:", row);
261               for (col = 0; col < n_columns; col++)
262                 {
263                   int width = caseproto_get_width (proto, col);
264                   union value v;
265                   value_init (&v, width);
266                   if (!datasheet_get_value (ds, row, col, &v))
267                     NOT_REACHED ();
268                   if (width == 0)
269                     ds_put_format (&s, " %g", v.f);
270                   else
271                     ds_put_format (&s, " '%.*s'",
272                                    width, value_str (&v, width));
273                 }
274               mc_error (mc, "%s", ds_cstr (&s));
275             }
276
277           ds_destroy (&s);
278         }
279     }
280
281   /* Check that datasheet contents are correct when read through
282      casereader. */
283   ds2 = clone_datasheet (ds);
284   reader = datasheet_make_reader (ds2);
285   check_datasheet_casereader (mc, reader, array, n_rows, proto);
286   casereader_destroy (reader);
287
288   /* Check that datasheet contents are correct when read through
289      casereader with lazy_casereader wrapped around it.  This is
290      valuable because otherwise there is no non-GUI code that
291      uses the lazy_casereader. */
292   ds2 = clone_datasheet (ds);
293   reader = lazy_casereader_create (datasheet_get_proto (ds2), n_rows,
294                                    lazy_callback, ds2, &serial);
295   check_datasheet_casereader (mc, reader, array, n_rows, proto);
296   if (lazy_casereader_destroy (reader, serial))
297     {
298       /* Lazy casereader was never instantiated.  This will
299          only happen if there are no rows (because in that case
300          casereader_read never gets called). */
301       datasheet_destroy (ds2);
302       if (n_rows != 0)
303         mc_error (mc, "lazy casereader not instantiated, but should "
304                   "have been (size %zu,%zu)", n_rows, n_columns);
305     }
306   else
307     {
308       /* Lazy casereader was instantiated.  This is the common
309          case, in which some casereader operation
310          (casereader_read in this case) was performed on the
311          lazy casereader. */
312       casereader_destroy (reader);
313       if (n_rows == 0)
314         mc_error (mc, "lazy casereader instantiated, but should not "
315                   "have been (size %zu,%zu)", n_rows, n_columns);
316     }
317
318   if (mc_discard_dup_state (mc, hash_datasheet (ds)))
319     datasheet_destroy (ds);
320   else
321     mc_add_state (mc, ds);
322 }
323
324 /* Extracts the contents of DS into DATA. */
325 static void
326 extract_data (const struct datasheet *ds, union value data[MAX_ROWS][MAX_COLS])
327 {
328   const struct caseproto *proto = datasheet_get_proto (ds);
329   size_t n_columns = datasheet_get_n_columns (ds);
330   size_t n_rows = datasheet_get_n_rows (ds);
331   size_t row, col;
332
333   assert (n_rows < MAX_ROWS);
334   assert (n_columns < MAX_COLS);
335   for (row = 0; row < n_rows; row++)
336     for (col = 0; col < n_columns; col++)
337       {
338         int width = caseproto_get_width (proto, col);
339         union value *v = &data[row][col];
340         value_init (v, width);
341         if (!datasheet_get_value (ds, row, col, v))
342           NOT_REACHED ();
343       }
344 }
345
346 /* Copies the contents of ODATA into DATA.  Each of the N_ROWS
347    rows of ODATA and DATA must have prototype PROTO. */
348 static void
349 clone_data (size_t n_rows, const struct caseproto *proto,
350             union value odata[MAX_ROWS][MAX_COLS],
351             union value data[MAX_ROWS][MAX_COLS])
352 {
353   size_t n_columns = caseproto_get_n_widths (proto);
354   size_t row, col;
355
356   assert (n_rows < MAX_ROWS);
357   assert (n_columns < MAX_COLS);
358   for (row = 0; row < n_rows; row++)
359     for (col = 0; col < n_columns; col++)
360       {
361         int width = caseproto_get_width (proto, col);
362         const union value *ov = &odata[row][col];
363         union value *v = &data[row][col];
364         value_init (v, width);
365         value_copy (v, ov, width);
366       }
367 }
368
369 static void
370 release_data (size_t n_rows, const struct caseproto *proto,
371               union value data[MAX_ROWS][MAX_COLS])
372 {
373   size_t n_columns = caseproto_get_n_widths (proto);
374   size_t row, col;
375
376   assert (n_rows < MAX_ROWS);
377   assert (n_columns < MAX_COLS);
378   for (col = 0; col < n_columns; col++)
379     {
380       int width = caseproto_get_width (proto, col);
381       if (value_needs_init (width))
382         for (row = 0; row < n_rows; row++)
383           value_destroy (&data[row][col], width);
384     }
385 }
386
387 /* Clones the structure and contents of ODS into *DS,
388    and the contents of ODATA into DATA. */
389 static void
390 clone_model (const struct datasheet *ods,
391              union value odata[MAX_ROWS][MAX_COLS],
392              struct datasheet **ds,
393              union value data[MAX_ROWS][MAX_COLS])
394 {
395   *ds = clone_datasheet (ods);
396   clone_data (datasheet_get_n_rows (ods), datasheet_get_proto (ods),
397               odata, data);
398 }
399
400 static void
401 value_from_param (union value *value, int width, unsigned int idx)
402 {
403   if (width == 0)
404     value->f = idx & 0xffff;
405   else
406     {
407       unsigned int hash = hash_int (idx, 0);
408       uint8_t *string = value_str_rw (value, width);
409       int offset;
410
411       assert (width < 32);
412       for (offset = 0; offset < width; offset++)
413         string[offset] = "ABCDEFGHIJ"[(hash >> offset) % 10];
414     }
415 }
416
417 /* "init" function for struct mc_class. */
418 static void
419 datasheet_mc_init (struct mc *mc)
420 {
421   struct datasheet_test_params *params = mc_get_aux (mc);
422   struct datasheet *ds;
423
424   if (params->backing_rows == 0 && params->n_backing_cols == 0)
425     {
426       /* Create unbacked datasheet. */
427       ds = datasheet_create (NULL);
428       mc_name_operation (mc, "empty datasheet");
429       check_datasheet (mc, ds, NULL, 0, caseproto_create ());
430     }
431   else
432     {
433       /* Create datasheet with backing. */
434       struct casewriter *writer;
435       struct casereader *reader;
436       union value data[MAX_ROWS][MAX_COLS];
437       struct caseproto *proto;
438       int row, col;
439
440       assert (params->backing_rows > 0 && params->backing_rows <= MAX_ROWS);
441       assert (params->n_backing_cols > 0
442               && params->n_backing_cols <= MAX_COLS);
443
444       proto = caseproto_create ();
445       for (col = 0; col < params->n_backing_cols; col++)
446         proto = caseproto_add_width (proto, params->backing_widths[col]);
447
448       writer = mem_writer_create (proto);
449       for (row = 0; row < params->backing_rows; row++)
450         {
451           struct ccase *c;
452
453           c = case_create (proto);
454           for (col = 0; col < params->n_backing_cols; col++)
455             {
456               int width = params->backing_widths[col];
457               union value *value = &data[row][col];
458               value_init (value, width);
459               value_from_param (value, width, params->next_value++);
460               value_copy (case_data_rw_idx (c, col), value, width);
461             }
462           casewriter_write (writer, c);
463         }
464
465       reader = casewriter_make_reader (writer);
466       assert (reader != NULL);
467
468       ds = datasheet_create (reader);
469       mc_name_operation (mc, "datasheet with (%d,%d) backing",
470                          params->backing_rows, params->n_backing_cols);
471       check_datasheet (mc, ds, data,
472                        params->backing_rows, proto);
473       release_data (params->backing_rows, proto, data);
474       caseproto_unref (proto);
475     }
476 }
477
478 struct resize_cb_aux
479   {
480     int old_width;
481     int new_width;
482   };
483
484 static void
485 resize_cb (const union value *old_value, union value *new_value, void *aux_)
486 {
487   struct resize_cb_aux *aux = aux_;
488
489   value_from_param (new_value, aux->new_width,
490                     value_hash (old_value, aux->old_width, 0));
491 }
492
493 /* "mutate" function for struct mc_class. */
494 static void
495 datasheet_mc_mutate (struct mc *mc, const void *ods_)
496 {
497   struct datasheet_test_params *params = mc_get_aux (mc);
498
499   const struct datasheet *ods = ods_;
500   union value odata[MAX_ROWS][MAX_COLS];
501   union value data[MAX_ROWS][MAX_COLS];
502   const struct caseproto *oproto = datasheet_get_proto (ods);
503   size_t n_columns = datasheet_get_n_columns (ods);
504   size_t n_rows = datasheet_get_n_rows (ods);
505   size_t pos, new_pos, cnt, width_idx;
506
507   extract_data (ods, odata);
508
509   /* Insert a column in each possible position. */
510   if (n_columns < params->max_cols)
511     for (pos = 0; pos <= n_columns; pos++)
512       for (width_idx = 0; width_idx < params->n_widths; width_idx++)
513         if (mc_include_state (mc))
514           {
515             int width = params->widths[width_idx];
516             struct caseproto *proto;
517             struct datasheet *ds;
518             union value new;
519             size_t i;
520
521             mc_name_operation (mc, "insert column at %zu "
522                                "(from %zu to %zu columns)",
523                                pos, n_columns, n_columns + 1);
524             clone_model (ods, odata, &ds, data);
525
526             value_init (&new, width);
527             value_from_param (&new, width, params->next_value++);
528             if (!datasheet_insert_column (ds, &new, width, pos))
529               mc_error (mc, "datasheet_insert_column failed");
530             proto = caseproto_insert_width (caseproto_ref (oproto),
531                                             pos, width);
532
533             for (i = 0; i < n_rows; i++)
534               {
535                 insert_element (&data[i][0], n_columns, sizeof data[i][0],
536                                 pos);
537                 value_init (&data[i][pos], width);
538                 value_copy (&data[i][pos], &new, width);
539               }
540             value_destroy (&new, width);
541
542             check_datasheet (mc, ds, data, n_rows, proto);
543             release_data (n_rows, proto, data);
544             caseproto_unref (proto);
545           }
546
547   /* Resize each column to each possible new size. */
548   for (pos = 0; pos < n_columns; pos++)
549     for (width_idx = 0; width_idx < params->n_widths; width_idx++)
550       {
551         int owidth = caseproto_get_width (oproto, pos);
552         int width = params->widths[width_idx];
553         if (mc_include_state (mc))
554           {
555             struct resize_cb_aux aux;
556             struct caseproto *proto;
557             struct datasheet *ds;
558             size_t i;
559
560             mc_name_operation (mc, "resize column %zu (of %zu) "
561                                "from width %d to %d",
562                                pos, n_columns, owidth, width);
563             clone_model (ods, odata, &ds, data);
564
565             aux.old_width = owidth;
566             aux.new_width = width;
567             if (!datasheet_resize_column (ds, pos, width, resize_cb, &aux))
568               NOT_REACHED ();
569             proto = caseproto_set_width (caseproto_ref (oproto), pos, width);
570
571             for (i = 0; i < n_rows; i++)
572               {
573                 union value *old_value = &data[i][pos];
574                 union value new_value;
575                 value_init (&new_value, width);
576                 resize_cb (old_value, &new_value, &aux);
577                 value_swap (old_value, &new_value);
578                 value_destroy (&new_value, owidth);
579               }
580
581             check_datasheet (mc, ds, data, n_rows, proto);
582             release_data (n_rows, proto, data);
583             caseproto_unref (proto);
584           }
585       }
586
587   /* Delete all possible numbers of columns from all possible
588      positions. */
589   for (pos = 0; pos < n_columns; pos++)
590     for (cnt = 1; cnt < n_columns - pos; cnt++)
591       if (mc_include_state (mc))
592         {
593           struct caseproto *proto;
594           struct datasheet *ds;
595           size_t i, j;
596
597           mc_name_operation (mc, "delete %zu columns at %zu "
598                              "(from %zu to %zu columns)",
599                              cnt, pos, n_columns, n_columns - cnt);
600           clone_model (ods, odata, &ds, data);
601
602           datasheet_delete_columns (ds, pos, cnt);
603           proto = caseproto_remove_widths (caseproto_ref (oproto), pos, cnt);
604
605           for (i = 0; i < n_rows; i++)
606             {
607               for (j = pos; j < pos + cnt; j++)
608                 value_destroy (&data[i][j], caseproto_get_width (oproto, j));
609               remove_range (&data[i], n_columns, sizeof *data[i], pos, cnt);
610             }
611
612           check_datasheet (mc, ds, data, n_rows, proto);
613           release_data (n_rows, proto, data);
614           caseproto_unref (proto);
615         }
616
617   /* Move all possible numbers of columns from all possible
618      existing positions to all possible new positions. */
619   for (pos = 0; pos < n_columns; pos++)
620     for (cnt = 1; cnt < n_columns - pos; cnt++)
621       for (new_pos = 0; new_pos < n_columns - cnt; new_pos++)
622         if (mc_include_state (mc))
623           {
624             struct caseproto *proto;
625             struct datasheet *ds;
626             size_t i;
627
628             clone_model (ods, odata, &ds, data);
629             mc_name_operation (mc, "move %zu columns (of %zu) from %zu to %zu",
630                                cnt, n_columns, pos, new_pos);
631
632             datasheet_move_columns (ds, pos, new_pos, cnt);
633
634             for (i = 0; i < n_rows; i++)
635               move_range (&data[i], n_columns, sizeof data[i][0],
636                           pos, new_pos, cnt);
637             proto = caseproto_move_widths (caseproto_ref (oproto),
638                                            pos, new_pos, cnt);
639
640             check_datasheet (mc, ds, data, n_rows, proto);
641             release_data (n_rows, proto, data);
642             caseproto_unref (proto);
643           }
644
645   /* Insert all possible numbers of rows in all possible
646      positions. */
647   for (pos = 0; pos <= n_rows; pos++)
648     for (cnt = 1; cnt <= params->max_rows - n_rows; cnt++)
649       if (mc_include_state (mc))
650         {
651           struct datasheet *ds;
652           struct ccase *c[MAX_ROWS];
653           size_t i, j;
654
655           clone_model (ods, odata, &ds, data);
656           mc_name_operation (mc, "insert %zu rows at %zu "
657                              "(from %zu to %zu rows)",
658                              cnt, pos, n_rows, n_rows + cnt);
659
660           for (i = 0; i < cnt; i++)
661             {
662               c[i] = case_create (oproto);
663               for (j = 0; j < n_columns; j++)
664                 value_from_param (case_data_rw_idx (c[i], j),
665                                   caseproto_get_width (oproto, j),
666                                   params->next_value++);
667             }
668
669           insert_range (data, n_rows, sizeof data[pos], pos, cnt);
670           for (i = 0; i < cnt; i++)
671             for (j = 0; j < n_columns; j++)
672               {
673                 int width = caseproto_get_width (oproto, j);
674                 value_init (&data[i + pos][j], width);
675                 value_copy (&data[i + pos][j], case_data_idx (c[i], j), width);
676               }
677
678           if (!datasheet_insert_rows (ds, pos, c, cnt))
679             mc_error (mc, "datasheet_insert_rows failed");
680
681           check_datasheet (mc, ds, data, n_rows + cnt, oproto);
682           release_data (n_rows + cnt, oproto, data);
683         }
684
685   /* Delete all possible numbers of rows from all possible
686      positions. */
687   for (pos = 0; pos < n_rows; pos++)
688     for (cnt = 1; cnt < n_rows - pos; cnt++)
689       if (mc_include_state (mc))
690         {
691           struct datasheet *ds;
692
693           clone_model (ods, odata, &ds, data);
694           mc_name_operation (mc, "delete %zu rows at %zu "
695                              "(from %zu to %zu rows)",
696                              cnt, pos, n_rows, n_rows - cnt);
697
698           datasheet_delete_rows (ds, pos, cnt);
699
700           release_data (cnt, oproto, &data[pos]);
701           remove_range (&data[0], n_rows, sizeof data[0], pos, cnt);
702
703           check_datasheet (mc, ds, data, n_rows - cnt, oproto);
704           release_data (n_rows - cnt, oproto, data);
705         }
706
707   /* Move all possible numbers of rows from all possible existing
708      positions to all possible new positions. */
709   for (pos = 0; pos < n_rows; pos++)
710     for (cnt = 1; cnt < n_rows - pos; cnt++)
711       for (new_pos = 0; new_pos < n_rows - cnt; new_pos++)
712         if (mc_include_state (mc))
713           {
714             struct datasheet *ds;
715
716             clone_model (ods, odata, &ds, data);
717             mc_name_operation (mc, "move %zu rows (of %zu) from %zu to %zu",
718                                cnt, n_rows, pos, new_pos);
719
720             datasheet_move_rows (ds, pos, new_pos, cnt);
721
722             move_range (&data[0], n_rows, sizeof data[0],
723                         pos, new_pos, cnt);
724
725             check_datasheet (mc, ds, data, n_rows, oproto);
726             release_data (n_rows, oproto, data);
727           }
728
729   release_data (n_rows, oproto, odata);
730 }
731
732 /* "destroy" function for struct mc_class. */
733 static void
734 datasheet_mc_destroy (const struct mc *mc UNUSED, void *ds_)
735 {
736   struct datasheet *ds = ds_;
737   datasheet_destroy (ds);
738 }
739 \f
740 enum
741   {
742     OPT_MAX_ROWS,
743     OPT_MAX_COLUMNS,
744     OPT_BACKING_ROWS,
745     OPT_BACKING_WIDTHS,
746     OPT_WIDTHS,
747     OPT_HELP,
748     N_DATASHEET_OPTIONS
749   };
750
751 static struct argv_option datasheet_argv_options[N_DATASHEET_OPTIONS] =
752   {
753     {"max-rows", 0, required_argument, OPT_MAX_ROWS},
754     {"max-columns", 0, required_argument, OPT_MAX_COLUMNS},
755     {"backing-rows", 0, required_argument, OPT_BACKING_ROWS},
756     {"backing-widths", 0, required_argument, OPT_BACKING_WIDTHS},
757     {"widths", 0, required_argument, OPT_WIDTHS},
758     {"help", 'h', no_argument, OPT_HELP},
759   };
760
761 static void usage (void);
762
763 static void
764 datasheet_option_callback (int id, void *params_)
765 {
766   struct datasheet_test_params *params = params_;
767   switch (id)
768     {
769     case OPT_MAX_ROWS:
770       params->max_rows = atoi (optarg);
771       break;
772
773     case OPT_MAX_COLUMNS:
774       params->max_cols = atoi (optarg);
775       break;
776
777     case OPT_BACKING_ROWS:
778       params->backing_rows = atoi (optarg);
779       break;
780
781     case OPT_BACKING_WIDTHS:
782       {
783         char *w;
784
785         params->n_backing_cols = 0;
786         for (w = strtok (optarg, ", "); w != NULL; w = strtok (NULL, ", "))
787           {
788             int value = atoi (w);
789
790             if (params->n_backing_cols >= MAX_COLS)
791               error (1, 0, "Too many widths on --backing-widths "
792                      "(only %d are allowed)", MAX_COLS);
793             if (!isdigit (w[0]) || value < 0 || value > 31)
794               error (1, 0, "--backing-widths argument must be a list of 1 to "
795                      "%d integers between 0 and 31 in increasing order",
796                      MAX_COLS);
797             params->backing_widths[params->n_backing_cols++] = value;
798           }
799       }
800       break;
801
802     case OPT_WIDTHS:
803       {
804         int last = -1;
805         char *w;
806
807         params->n_widths = 0;
808         for (w = strtok (optarg, ", "); w != NULL; w = strtok (NULL, ", "))
809           {
810             int value = atoi (w);
811
812             if (params->n_widths >= MAX_WIDTHS)
813               error (1, 0, "Too many widths on --widths (only %d are allowed)",
814                      MAX_WIDTHS);
815             if (!isdigit (w[0]) || value < 0 || value > 31)
816               error (1, 0, "--widths argument must be a list of 1 to %d "
817                      "integers between 0 and 31 in increasing order",
818                      MAX_WIDTHS);
819
820             /* This is an artificial requirement merely to ensure
821                that there are no duplicates.  Duplicates aren't a
822                real problem but they would waste time. */
823             if (value <= last)
824               error (1, 0, "--widths arguments must be in increasing order");
825
826             params->widths[params->n_widths++] = value;
827           }
828         if (params->n_widths == 0)
829           error (1, 0, "at least one value must be specified on --widths");
830       }
831       break;
832
833     case OPT_HELP:
834       usage ();
835       break;
836
837     default:
838       NOT_REACHED ();
839     }
840 }
841
842 static void
843 usage (void)
844 {
845   printf ("%s, for testing the datasheet implementation.\n"
846           "Usage: %s [OPTION]...\n"
847           "\nTest state space parameters (min...max, default):\n"
848           "  --max-rows=N         Maximum number of rows (0...5, 3)\n"
849           "  --max-rows=N         Maximum number of columns (0...5, 3)\n"
850           "  --backing-rows=N     Rows of backing store (0...max_rows, 0)\n"
851           "  --backing-widths=W[,W]...  Backing store widths to test (0=num)\n"
852           "  --widths=W[,W]...    Column widths to test, where 0=numeric,\n"
853           "                       other values are string widths (0,1,11)\n",
854           program_name, program_name);
855   mc_options_usage ();
856   fputs ("\nOther options:\n"
857          "  --help               Display this help message\n"
858          "\nReport bugs to <bug-gnu-pspp@gnu.org>\n",
859          stdout);
860   exit (0);
861 }
862
863 int
864 main (int argc, char *argv[])
865 {
866   static const struct mc_class datasheet_mc_class =
867     {
868       datasheet_mc_init,
869       datasheet_mc_mutate,
870       datasheet_mc_destroy,
871     };
872
873   struct datasheet_test_params params;
874   struct mc_options *options;
875   struct mc_results *results;
876   struct argv_parser *parser;
877   int verbosity;
878   bool success;
879
880   set_program_name (argv[0]);
881
882   /* Default parameters. */
883   params.max_rows = 3;
884   params.max_cols = 3;
885   params.backing_rows = 0;
886   params.n_backing_cols = 0;
887   params.widths[0] = 0;
888   params.widths[1] = 1;
889   params.widths[2] = 11;
890   params.n_widths = 3;
891   params.next_value = 1;
892
893   /* Parse comand line. */
894   parser = argv_parser_create ();
895   options = mc_options_create ();
896   mc_options_register_argv_parser (options, parser);
897   argv_parser_add_options (parser, datasheet_argv_options, N_DATASHEET_OPTIONS,
898                            datasheet_option_callback, &params);
899   if (!argv_parser_run (parser, argc, argv))
900     exit (EXIT_FAILURE);
901   argv_parser_destroy (parser);
902   verbosity = mc_options_get_verbosity (options);
903
904   /* Force parameters into allowed ranges. */
905   params.max_rows = MIN (params.max_rows, MAX_ROWS);
906   params.max_cols = MIN (params.max_cols, MAX_COLS);
907   params.backing_rows = MIN (params.backing_rows, params.max_rows);
908   params.n_backing_cols = MIN (params.n_backing_cols, params.max_cols);
909   mc_options_set_aux (options, &params);
910   results = mc_run (&datasheet_mc_class, options);
911
912   /* Output results. */
913   success = (mc_results_get_stop_reason (results) != MC_MAX_ERROR_COUNT
914              && mc_results_get_stop_reason (results) != MC_INTERRUPTED);
915   if (verbosity > 0 || !success)
916     {
917       int i;
918
919       printf ("Parameters: --max-rows=%d --max-columns=%d --backing-rows=%d ",
920               params.max_rows, params.max_cols, params.backing_rows);
921
922       printf ("--backing-widths=");
923       for (i = 0; i < params.n_backing_cols; i++)
924         {
925           if (i > 0)
926             printf (",");
927           printf ("%d", params.backing_widths[i]);
928         }
929       printf (" ");
930
931       printf ("--widths=");
932       for (i = 0; i < params.n_widths; i++)
933         {
934           if (i > 0)
935             printf (",");
936           printf ("%d", params.widths[i]);
937         }
938       printf ("\n\n");
939       mc_results_print (results, stdout);
940     }
941   mc_results_destroy (results);
942
943   return success ? 0 : EXIT_FAILURE;
944 }