lexer: Reimplement for better testability and internationalization.
[pspp-builds.git] / src / language / tests / moments-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 1997-9, 2000, 2010, 2011 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 <math.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "language/command.h"
24 #include "language/lexer/lexer.h"
25 #include "libpspp/compiler.h"
26 #include "math/moments.h"
27
28 #include "gl/xalloc.h"
29
30 #include "gettext.h"
31 #define _(msgid) gettext (msgid)
32
33 static bool
34 read_values (struct lexer *lexer, double **values, double **weights, size_t *cnt)
35 {
36   size_t cap = 0;
37
38   *values = NULL;
39   *weights = NULL;
40   *cnt = 0;
41   while (lex_is_number (lexer))
42     {
43       double value = lex_tokval (lexer);
44       double weight = 1.;
45       lex_get (lexer);
46       if (lex_match (lexer, T_ASTERISK))
47         {
48           if (!lex_is_number (lexer))
49             {
50               lex_error (lexer, _("expecting weight value"));
51               return false;
52             }
53           weight = lex_tokval (lexer);
54           lex_get (lexer);
55         }
56
57       if (*cnt >= cap)
58         {
59           cap = 2 * (cap + 8);
60           *values = xnrealloc (*values, cap, sizeof **values);
61           *weights = xnrealloc (*weights, cap, sizeof **weights);
62         }
63
64       (*values)[*cnt] = value;
65       (*weights)[*cnt] = weight;
66       (*cnt)++;
67     }
68
69   return true;
70 }
71
72 int
73 cmd_debug_moments (struct lexer *lexer, struct dataset *ds UNUSED)
74 {
75   int retval = CMD_FAILURE;
76   double *values = NULL;
77   double *weights = NULL;
78   double weight, M[4];
79   int two_pass = 1;
80   size_t cnt;
81   size_t i;
82
83   if (lex_match_id (lexer, "ONEPASS"))
84     two_pass = 0;
85   if (lex_token (lexer) != T_SLASH)
86     {
87       lex_force_match (lexer, T_SLASH);
88       goto done;
89     }
90   lex_get (lexer);
91
92   if (two_pass)
93     {
94       struct moments *m = NULL;
95
96       m = moments_create (MOMENT_KURTOSIS);
97       if (!read_values (lexer, &values, &weights, &cnt))
98         {
99           moments_destroy (m);
100           goto done;
101         }
102       for (i = 0; i < cnt; i++)
103         moments_pass_one (m, values[i], weights[i]);
104       for (i = 0; i < cnt; i++)
105         moments_pass_two (m, values[i], weights[i]);
106       moments_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
107       moments_destroy (m);
108     }
109   else
110     {
111       struct moments1 *m = NULL;
112
113       m = moments1_create (MOMENT_KURTOSIS);
114       if (!read_values (lexer, &values, &weights, &cnt))
115         {
116           moments1_destroy (m);
117           goto done;
118         }
119       for (i = 0; i < cnt; i++)
120         moments1_add (m, values[i], weights[i]);
121       moments1_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]);
122       moments1_destroy (m);
123     }
124
125   fprintf (stderr, "W=%.3f", weight);
126   for (i = 0; i < 4; i++)
127     {
128       fprintf (stderr, " M%zu=", i + 1);
129       if (M[i] == SYSMIS)
130         fprintf (stderr, "sysmis");
131       else if (fabs (M[i]) <= 0.0005)
132         fprintf (stderr, "0.000");
133       else
134         fprintf (stderr, "%.3f", M[i]);
135     }
136   fprintf (stderr, "\n");
137
138   retval = CMD_SUCCESS;
139
140  done:
141   free (values);
142   free (weights);
143   return retval;
144 }