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