Fixed crash in T-TEST when a non-number was passed where a number was expected.
[pspp] / src / language / stats / t-test-parser.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2011, 2015 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 "libpspp/message.h"
20
21 #include "data/casegrouper.h"
22 #include "data/casereader.h"
23 #include "data/dictionary.h"
24 #include "data/dataset.h"
25 #include "data/missing-values.h"
26
27 #include "language/lexer/lexer.h"
28 #include "language/command.h"
29 #include "language/lexer/variable-parser.h"
30 #include "language/lexer/value-parser.h"
31
32 #include "gettext.h"
33 #define _(msgid) gettext (msgid)
34
35 #include "t-test.h"
36
37 int
38 cmd_t_test (struct lexer *lexer, struct dataset *ds)
39 {
40   bool ok;
41   const struct dictionary *dict = dataset_dict (ds);
42   struct tt tt;
43   int mode_count = 0;
44
45   /* Variables pertaining to the paired mode */
46   const struct variable **v1 = NULL;
47   size_t n_v1;
48   const struct variable **v2 = NULL;
49   size_t n_v2;
50           
51   size_t n_pairs = 0;
52   vp *pairs = NULL;
53
54
55   /* One sample mode */
56   double testval = SYSMIS;
57
58   /* Independent samples mode */
59   const struct variable *gvar;
60   union value gval0;
61   union value gval1;
62   bool cut = false;
63
64   tt.wv = dict_get_weight (dict);
65   tt.dict = dict;
66   tt.confidence = 0.95;
67   tt.exclude = MV_ANY;
68   tt.missing_type = MISS_ANALYSIS;
69   tt.n_vars = 0;
70   tt.vars = NULL;
71   tt.mode = MODE_undef;
72
73   lex_match (lexer, T_EQUALS);
74
75   for (; lex_token (lexer) != T_ENDCMD; )
76     {
77       lex_match (lexer, T_SLASH);
78       if (lex_match_id (lexer, "TESTVAL"))
79         {
80           mode_count++;
81           tt.mode = MODE_SINGLE;
82           lex_match (lexer, T_EQUALS);
83           if (!lex_force_num (lexer))
84             goto parse_failed;
85           testval = lex_number (lexer);
86           lex_get (lexer);
87         }
88       else if (lex_match_id (lexer, "GROUPS"))
89         {
90           mode_count++;
91           cut = false;
92           tt.mode = MODE_INDEP;
93           lex_match (lexer, T_EQUALS);
94
95           if (NULL == (gvar = parse_variable (lexer, dict)))
96             goto parse_failed;
97       
98           if (lex_match (lexer, T_LPAREN))
99             {
100
101               value_init (&gval0, var_get_width (gvar));
102               parse_value (lexer, &gval0, gvar);
103               cut = true;
104               if (lex_token (lexer) != T_RPAREN)
105                 {
106                   lex_match (lexer, T_COMMA);
107                   value_init (&gval1, var_get_width (gvar));
108                   parse_value (lexer, &gval1, gvar);
109                   cut = false;
110                 }
111
112               lex_force_match (lexer, T_RPAREN);
113             }
114           else
115             {
116               value_init (&gval0, 0);
117               value_init (&gval1, 0);
118               gval0.f = 1.0;
119               gval1.f = 2.0;
120               cut = false;
121             }
122
123           if ( cut == true && var_is_alpha (gvar))
124             {
125               msg (SE, _("When applying %s to a string variable, two "
126                          "values must be specified."), "GROUPS");
127               goto parse_failed;
128             }
129         }
130       else if (lex_match_id (lexer, "PAIRS"))
131         {
132           bool with = false;
133           bool paired = false;
134
135           if (tt.n_vars > 0)
136             {
137               msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
138               goto parse_failed;
139             }
140
141           mode_count++;
142           tt.mode = MODE_PAIRED;
143           lex_match (lexer, T_EQUALS);
144
145           if (!parse_variables_const (lexer, dict,
146                                       &v1, &n_v1,
147                                       PV_NO_DUPLICATE | PV_NUMERIC))
148             goto parse_failed;
149
150           if ( lex_match (lexer, T_WITH))
151             {
152               with = true;
153               if (!parse_variables_const (lexer, dict,
154                                           &v2, &n_v2,
155                                           PV_NO_DUPLICATE | PV_NUMERIC))
156                 goto parse_failed;
157
158               if (lex_match (lexer, T_LPAREN)
159                   && lex_match_id (lexer, "PAIRED")
160                   && lex_match (lexer, T_RPAREN))
161                 {
162                   paired = true;
163                   if (n_v1 != n_v2)
164                     {
165                       msg (SE, _("PAIRED was specified but the number of variables "
166                                  "preceding WITH (%zu) did not match the number "
167                                  "following (%zu)."),
168                            n_v1, n_v2);
169                       goto parse_failed;
170                     }
171                 }
172             }
173           {
174             int i;
175
176             if ( !with )
177               n_pairs = (n_v1 * (n_v1 - 1)) / 2.0;
178             else if ( paired )
179               n_pairs = n_v1;
180             else
181               n_pairs = n_v1 * n_v2;
182           
183             pairs = xcalloc (n_pairs, sizeof *pairs);
184
185             if ( with)
186               {
187                 int x = 0;
188                 if (paired)
189                   {
190                     for (i = 0 ; i < n_v1; ++i)
191                       {
192                         vp *pair = &pairs[i];
193                         (*pair)[0] = v1[i];
194                         (*pair)[1] = v2[i];
195                       } 
196                   }
197                 else
198                   {
199                     for (i = 0 ; i < n_v1; ++i)
200                       {
201                         int j;
202                         for (j = 0 ; j < n_v2; ++j)
203                           {
204                             vp *pair = &pairs[x++];
205                             (*pair)[0] = v1[i];
206                             (*pair)[1] = v2[j];
207                           }
208                       }
209                   }
210               }
211             else
212               {
213                 int x = 0;
214                 for (i = 0 ; i < n_v1; ++i)
215                   {
216                     int j;
217
218                     for (j = i + 1 ; j < n_v1; ++j)
219                       {
220                         vp *pair = &pairs[x++];
221                         (*pair)[0] = v1[i];
222                         (*pair)[1] = v1[j];
223                       }
224                   }
225               }
226
227           }
228         }
229       else if (lex_match_id (lexer, "VARIABLES"))
230         {
231           if ( tt.mode == MODE_PAIRED)
232             {
233               msg (SE, _("%s subcommand may not be used with %s."), "VARIABLES", "PAIRS");
234               goto parse_failed;
235             }
236
237           lex_match (lexer, T_EQUALS);
238
239           if (!parse_variables_const (lexer, dict,
240                                       &tt.vars,
241                                       &tt.n_vars,
242                                       PV_NO_DUPLICATE | PV_NUMERIC))
243             goto parse_failed;
244         }
245       else if ( lex_match_id (lexer, "MISSING"))
246         {
247           lex_match (lexer, T_EQUALS);
248           while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
249             {
250               if (lex_match_id (lexer, "INCLUDE"))
251                 {
252                   tt.exclude = MV_SYSTEM;
253                 }
254               else if (lex_match_id (lexer, "EXCLUDE"))
255                 {
256                   tt.exclude = MV_ANY;
257                 }
258               else if (lex_match_id (lexer, "LISTWISE"))
259                 {
260                   tt.missing_type = MISS_LISTWISE;
261                 }
262               else if (lex_match_id (lexer, "ANALYSIS"))
263                 {
264                   tt.missing_type = MISS_ANALYSIS;
265                 }
266               else
267                 {
268                   lex_error (lexer, NULL);
269                   goto parse_failed;
270                 }
271               lex_match (lexer, T_COMMA);
272             }
273         }
274       else if (lex_match_id (lexer, "CRITERIA"))
275         {
276           lex_match (lexer, T_EQUALS);
277           if ( lex_match_id (lexer, "CIN") || lex_force_match_id (lexer, "CI"))
278             if ( lex_force_match (lexer, T_LPAREN))
279               {
280                 if (!lex_force_num (lexer))
281                   goto parse_failed;
282                 tt.confidence = lex_number (lexer);
283                 lex_get (lexer);
284                 lex_force_match (lexer, T_RPAREN);
285               }
286         }
287       else 
288         {
289           lex_error (lexer, NULL);
290           goto parse_failed;
291         }
292     }
293
294   if ( mode_count != 1)
295     {
296       msg (SE, _("Exactly one of TESTVAL, GROUPS and PAIRS subcommands "
297                  "must be specified."));
298       goto parse_failed;
299     }
300
301   if (tt.n_vars == 0 && tt.mode != MODE_PAIRED)
302     {
303       lex_sbc_missing ("VARIABLES");
304       goto parse_failed;
305     }
306
307
308
309   /* Deal with splits etc */
310   {
311     struct casereader *group;
312     struct casegrouper *grouper = casegrouper_create_splits (proc_open (ds), dict);
313
314     while (casegrouper_get_next_group (grouper, &group))
315       {
316         if ( tt.mode == MODE_SINGLE)
317           {
318             if ( tt.missing_type == MISS_LISTWISE )
319               group  = casereader_create_filter_missing (group,
320                                                          tt.vars, tt.n_vars,
321                                                          tt.exclude,
322                                                          NULL,  NULL);
323             one_sample_run (&tt, testval, group);
324           }
325         else if ( tt.mode == MODE_PAIRED)
326           {
327             if ( tt.missing_type == MISS_LISTWISE )
328               {
329                 group  = casereader_create_filter_missing (group,
330                                                            v1, n_v1,
331                                                            tt.exclude,
332                                                            NULL,  NULL);
333                 group  = casereader_create_filter_missing (group,
334                                                            v2, n_v2,
335                                                            tt.exclude,
336                                                            NULL,  NULL);
337               }
338
339             paired_run (&tt, n_pairs, pairs, group);
340           }
341         else /* tt.mode == MODE_INDEP */
342           {
343             if ( tt.missing_type == MISS_LISTWISE )
344               {
345                 group  = casereader_create_filter_missing (group,
346                                                            tt.vars, tt.n_vars,
347                                                            tt.exclude,
348                                                            NULL,  NULL);
349
350                 group  = casereader_create_filter_missing (group,
351                                                            &gvar, 1,
352                                                            tt.exclude,
353                                                            NULL,  NULL);
354
355               }
356
357             indep_run (&tt, gvar, cut, &gval0, &gval1, group);
358           }
359       }
360
361     ok = casegrouper_destroy (grouper);
362     ok = proc_commit (ds) && ok;
363   }
364
365   free (pairs);
366   free (v1);
367   free (v2);
368
369   free (tt.vars);
370
371   return ok ? CMD_SUCCESS : CMD_FAILURE;
372
373  parse_failed:
374   return CMD_FAILURE;
375 }
376