X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flanguage%2Ftests%2Fmoments-test.c;h=34408739b4ce0856d2e643737c50b71a5d9e36ab;hb=e8817e13d19ffdaefd717d34ea6b83dc421e081b;hp=4b685231454eaa2a17de5d9d9dab861c6dcdd84e;hpb=a19b858e0ac3c69e4a28c0ca6d8674427268a863;p=pspp diff --git a/src/language/tests/moments-test.c b/src/language/tests/moments-test.c index 4b68523145..34408739b4 100644 --- a/src/language/tests/moments-test.c +++ b/src/language/tests/moments-test.c @@ -1,128 +1,127 @@ -/* PSPP - computes sample statistics. - Copyright (C) 1997-9, 2000 Free Software Foundation, Inc. - Written by Ben Pfaff . +/* PSPP - a program for statistical analysis. + Copyright (C) 1997-9, 2000, 2010, 2011 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include + +#include #include -#include "gettext.h" -#include -#include -#include +#include + +#include "language/command.h" +#include "language/lexer/lexer.h" +#include "libpspp/compiler.h" +#include "math/moments.h" +#include "gl/xalloc.h" + +#include "gettext.h" #define _(msgid) gettext (msgid) -static int -read_values (double **values, double **weights, size_t *cnt) +static bool +read_values (struct lexer *lexer, double **values, double **weights, size_t *n) { size_t cap = 0; *values = NULL; *weights = NULL; - *cnt = 0; - while (lex_is_number ()) + *n = 0; + while (lex_is_number (lexer)) { - double value = tokval; + double value = lex_tokval (lexer); double weight = 1.; - lex_get (); - if (lex_match ('*')) + lex_get (lexer); + if (lex_match (lexer, T_ASTERISK)) { - if (!lex_is_number ()) + if (!lex_is_number (lexer)) { - lex_error (_("expecting weight value")); - return 0; + lex_error (lexer, _("expecting weight value")); + return false; } - weight = tokval; - lex_get (); + weight = lex_tokval (lexer); + lex_get (lexer); } - if (*cnt >= cap) + if (*n >= cap) { cap = 2 * (cap + 8); *values = xnrealloc (*values, cap, sizeof **values); *weights = xnrealloc (*weights, cap, sizeof **weights); } - (*values)[*cnt] = value; - (*weights)[*cnt] = weight; - (*cnt)++; + (*values)[*n] = value; + (*weights)[*n] = weight; + (*n)++; } - return 1; + return true; } int -cmd_debug_moments (void) +cmd_debug_moments (struct lexer *lexer, struct dataset *ds UNUSED) { int retval = CMD_FAILURE; double *values = NULL; double *weights = NULL; double weight, M[4]; int two_pass = 1; - size_t cnt; + size_t n; size_t i; - if (lex_match_id ("ONEPASS")) + if (lex_match_id (lexer, "ONEPASS")) two_pass = 0; - if (token != '/') - { - lex_force_match ('/'); + if (!lex_force_match (lexer, T_SLASH)) goto done; - } - fprintf (stderr, "%s => ", lex_rest_of_line (NULL)); - lex_get (); - if (two_pass) + if (two_pass) { struct moments *m = NULL; - + m = moments_create (MOMENT_KURTOSIS); - if (!read_values (&values, &weights, &cnt)) + if (!read_values (lexer, &values, &weights, &n)) { moments_destroy (m); - goto done; + goto done; } - for (i = 0; i < cnt; i++) - moments_pass_one (m, values[i], weights[i]); - for (i = 0; i < cnt; i++) + for (i = 0; i < n; i++) + moments_pass_one (m, values[i], weights[i]); + for (i = 0; i < n; i++) moments_pass_two (m, values[i], weights[i]); moments_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]); moments_destroy (m); } - else + else { struct moments1 *m = NULL; - + m = moments1_create (MOMENT_KURTOSIS); - if (!read_values (&values, &weights, &cnt)) + if (!read_values (lexer, &values, &weights, &n)) { moments1_destroy (m); - goto done; + goto done; } - for (i = 0; i < cnt; i++) + for (i = 0; i < n; i++) moments1_add (m, values[i], weights[i]); moments1_calculate (m, &weight, &M[0], &M[1], &M[2], &M[3]); moments1_destroy (m); } - + fprintf (stderr, "W=%.3f", weight); - for (i = 0; i < 4; i++) + for (i = 0; i < 4; i++) { - fprintf (stderr, " M%d=", i + 1); + fprintf (stderr, " M%zu=", i + 1); if (M[i] == SYSMIS) fprintf (stderr, "sysmis"); else if (fabs (M[i]) <= 0.0005) @@ -132,8 +131,8 @@ cmd_debug_moments (void) } fprintf (stderr, "\n"); - retval = lex_end_of_command (); - + retval = CMD_SUCCESS; + done: free (values); free (weights);