9901640d65d7e0483812efb0d765c34b79cae48d
[pspp-builds.git] / src / language / tests / datasheet-test.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3
4    This program is free software; you can redistribute it and/or
5    modify it under the terms of the GNU General Public License as
6    published by the Free Software Foundation; either version 2 of the
7    License, or (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful, but
10    WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    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, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17    02110-1301, USA. */
18
19 #include <config.h>
20
21 #include <data/datasheet.h>
22
23 #include <language/command.h>
24 #include <language/lexer/lexer.h>
25 #include <language/tests/check-model.h>
26 #include <libpspp/array.h>
27 #include <libpspp/assertion.h>
28
29 #include "error.h"
30 #include "xalloc.h"
31
32 static bool parse_coordinates (struct lexer *, int *rows, int *cols);
33
34 /* Parses and executes the DEBUG DATASHEET command, which runs
35    the model checker on the datasheet data structure.  The
36    command may include a specification of the form
37    MAX=(ROWS,COLS) to specify the maximum size of the data sheet
38    during the model checker run (default: 4x4) or
39    BACKING=(ROWS,COLS) to specify the size of the casereader
40    backing the datasheet (default: no backing).  These may be
41    optionally followed by any of the common model checker option
42    specifications (see check-model.q). */
43 int
44 cmd_debug_datasheet (struct lexer *lexer, struct dataset *dataset UNUSED)
45 {
46   struct datasheet_test_params params;
47   bool ok;
48
49   params.max_rows = 4;
50   params.max_cols = 4;
51   params.backing_rows = 0;
52   params.backing_cols = 0;
53
54   for (;;)
55     {
56       if (lex_match_id (lexer, "MAX"))
57         {
58           if (!parse_coordinates (lexer, &params.max_rows, &params.max_cols))
59             return CMD_FAILURE;
60         }
61       else if (lex_match_id (lexer, "BACKING"))
62         {
63           if (!parse_coordinates (lexer,
64                                   &params.backing_rows, &params.backing_cols))
65             return CMD_FAILURE;
66         }
67       else
68         break;
69       lex_match (lexer, '/');
70     }
71
72   ok = check_model (lexer, datasheet_test, &params);
73   printf ("Datasheet test max(%d,%d) backing(%d,%d) %s.\n",
74           params.max_rows, params.max_cols,
75           params.backing_rows, params.backing_cols,
76           ok ? "successful" : "failed");
77   return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
78 }
79
80 /* Parses a pair of coordinates with the syntax =(ROWS,COLS),
81    where all of the delimiters are optional, into *ROWS and
82    *COLS.  Returns true if successful, false on parse failure. */
83 static bool
84 parse_coordinates (struct lexer *lexer, int *rows, int *cols)
85 {
86   lex_match (lexer, '=');
87   lex_match (lexer, '(');
88
89   if (!lex_force_int (lexer))
90     return false;
91   *rows = lex_integer (lexer);
92   lex_get (lexer);
93
94   lex_match (lexer, ',');
95
96   if (!lex_force_int (lexer))
97     return false;
98   *cols = lex_integer (lexer);
99   lex_get (lexer);
100
101   lex_match (lexer, ')');
102   return true;
103 }
104