dfe8b6b2f0a4b1a341412522a5f6f579d0f8fa33
[pspp-builds.git] / src / language / tests / datasheet-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007 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 "datasheet-check.h"
20
21 #include <language/command.h>
22 #include <language/lexer/lexer.h>
23 #include <language/tests/check-model.h>
24 #include <libpspp/array.h>
25 #include <libpspp/assertion.h>
26
27 #include "error.h"
28 #include "xalloc.h"
29
30 static bool parse_coordinates (struct lexer *, int *rows, int *cols);
31
32 /* Parses and executes the DEBUG DATASHEET command, which runs
33    the model checker on the datasheet data structure.  The
34    command may include a specification of the form
35    MAX=(ROWS,COLS) to specify the maximum size of the data sheet
36    during the model checker run (default: 4x4) or
37    BACKING=(ROWS,COLS) to specify the size of the casereader
38    backing the datasheet (default: no backing).  These may be
39    optionally followed by any of the common model checker option
40    specifications (see check-model.q). */
41 int
42 cmd_debug_datasheet (struct lexer *lexer, struct dataset *dataset UNUSED)
43 {
44   struct datasheet_test_params params;
45   bool ok;
46
47   params.max_rows = 4;
48   params.max_cols = 4;
49   params.backing_rows = 0;
50   params.backing_cols = 0;
51
52
53   for (;;)
54     {
55       if (lex_match_id (lexer, "MAX"))
56         {
57           if (!parse_coordinates (lexer, &params.max_rows, &params.max_cols))
58             return CMD_FAILURE;
59         }
60       else if (lex_match_id (lexer, "BACKING"))
61         {
62           if (!parse_coordinates (lexer,
63                                   &params.backing_rows, &params.backing_cols))
64             return CMD_FAILURE;
65         }
66       else
67         break;
68       lex_match (lexer, '/');
69     }
70
71   ok = check_model (lexer, datasheet_test, &params);
72   printf ("Datasheet test max(%d,%d) backing(%d,%d) %s.\n",
73           params.max_rows, params.max_cols,
74           params.backing_rows, params.backing_cols,
75           ok ? "successful" : "failed");
76   return ok ? lex_end_of_command (lexer) : CMD_FAILURE;
77 }
78
79 /* Parses a pair of coordinates with the syntax =(ROWS,COLS),
80    where all of the delimiters are optional, into *ROWS and
81    *COLS.  Returns true if successful, false on parse failure. */
82 static bool
83 parse_coordinates (struct lexer *lexer, int *rows, int *cols)
84 {
85   lex_match (lexer, '=');
86   lex_match (lexer, '(');
87
88   if (!lex_force_int (lexer))
89     return false;
90   *rows = lex_integer (lexer);
91   lex_get (lexer);
92
93   lex_match (lexer, ',');
94
95   if (!lex_force_int (lexer))
96     return false;
97   *cols = lex_integer (lexer);
98   lex_get (lexer);
99
100   lex_match (lexer, ')');
101   return true;
102 }
103