Patch #6452. Reviewed by John Darrington.
authorBen Pfaff <blp@gnu.org>
Sun, 16 Mar 2008 22:38:17 +0000 (22:38 +0000)
committerBen Pfaff <blp@gnu.org>
Sun, 16 Mar 2008 22:38:17 +0000 (22:38 +0000)
* variable-display.c (cmd_variable_width): Fix parsing bug.  Also,
limit variable display widths to within range 1 thru 2 *
MAX_STRING.

* automake.mk: Add new test.

* command/variable-display.sh: New test.

src/language/dictionary/ChangeLog
src/language/dictionary/variable-display.c
tests/ChangeLog
tests/automake.mk
tests/command/variable-display.sh [new file with mode: 0755]

index bc7d713460114febe19ff79e0c08ee37a9524f8a..20778a242d5dab254fc37d4d289cb87f98a2cffa 100644 (file)
@@ -1,3 +1,11 @@
+2008-03-16  Ben Pfaff  <blp@gnu.org>
+
+       Patch #6452.  Reviewed by John Darrington.
+
+       * variable-display.c (cmd_variable_width): Fix parsing bug.  Also,
+       limit variable display widths to within range 1 thru 2 *
+       MAX_STRING.
+
 2007-11-11  Ben Pfaff  <blp@gnu.org>
 
        * sys-file-info.c (display_variables): Fix DISPLAY LABELS.  Thanks
index bada08142561bbb354f09bcc1adb1b764eeefca9..277db48e56b2eb53bd292c5819ebb71fd849b529 100644 (file)
@@ -29,6 +29,9 @@
 
 #include "xalloc.h"
 
+#include "gettext.h"
+#define _(msgid) gettext (msgid)
+
 /* Set variables' alignment
    This is the alignment for GUI display only.
    It affects nothing but GUIs
@@ -91,23 +94,36 @@ cmd_variable_width (struct lexer *lexer, struct dataset *ds)
   do
     {
       struct variable **v;
+      long int width;
       size_t nv;
       size_t i;
 
       if (!parse_variables (lexer, dataset_dict (ds), &v, &nv, PV_NONE))
         return CMD_FAILURE;
 
-      if ( lex_force_match (lexer, '(') )
-       {
-         if ( lex_force_int (lexer))
-           lex_get (lexer);
-         else
-           return CMD_FAILURE;
-         lex_force_match (lexer, ')');
-       }
+      if (!lex_force_match (lexer, '(') || !lex_force_int (lexer))
+        {
+          free (v);
+          return CMD_FAILURE;
+        }
+      width = lex_integer (lexer);
+      lex_get (lexer);
+      if (!lex_force_match (lexer, ')'))
+        {
+          free (v);
+          return CMD_FAILURE;
+        }
+
+      if (width < 0)
+        {
+          msg (SE, _("Variable display width must be a positive integer."));
+          free (v);
+          return CMD_FAILURE;
+        }
+      width = MIN (width, 2 * MAX_STRING);
 
       for( i = 0 ; i < nv ; ++i )
-        var_set_display_width (v[i], lex_integer (lexer));
+        var_set_display_width (v[i], width);
 
       while (lex_token (lexer) == '/')
        lex_get (lexer);
index ab6430b177d9d4cbe58a4dde6025d376d78644fd..7a601c4e9483e9f0b9c533b70a9cec80e7d003ec 100644 (file)
@@ -1,3 +1,11 @@
+2008-03-16  Ben Pfaff  <blp@gnu.org>
+
+       Patch #6452.  Reviewed by John Darrington.
+
+       * automake.mk: Add new test.
+
+       * command/variable-display.sh: New test.
+
 2008-03-04  Ben Pfaff  <blp@gnu.org>
 
        Patch #6441.  Reviewed by John Darrington.
index 75989222877dc4fad79666c3c6548271392ff02e..abc327a5ab67b84a99d08d0bf34af6a769bb8048 100644 (file)
@@ -68,6 +68,7 @@ dist_TESTS = \
        tests/command/trimmed-mean.sh \
        tests/command/tabs.sh \
        tests/command/use.sh \
+       tests/command/variable-display.sh \
        tests/command/vector.sh \
        tests/command/very-long-strings.sh \
        tests/command/weight.sh \
diff --git a/tests/command/variable-display.sh b/tests/command/variable-display.sh
new file mode 100755 (executable)
index 0000000..bb639d1
--- /dev/null
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+# This program tests variable display attribute commands: VARIABLE
+# ALIGNMENT, VARIABLE WIDTH, VARIABLE LEVEL.
+
+TEMPDIR=/tmp/pspp-tst-$$
+TESTFILE=$TEMPDIR/`basename $0`.sps
+
+# ensure that top_builddir  are absolute
+if [ -z "$top_builddir" ] ; then top_builddir=. ; fi
+if [ -z "$top_srcdir" ] ; then top_srcdir=. ; fi
+top_builddir=`cd $top_builddir; pwd`
+PSPP=$top_builddir/src/ui/terminal/pspp
+
+# ensure that top_srcdir is absolute
+top_srcdir=`cd $top_srcdir; pwd`
+
+STAT_CONFIG_PATH=$top_srcdir/config
+export STAT_CONFIG_PATH
+
+
+cleanup()
+{
+     cd /
+     rm -rf $TEMPDIR
+}
+
+
+fail()
+{
+    echo $activity
+    echo FAILED
+    cleanup;
+    exit 1;
+}
+
+
+no_result()
+{
+    echo $activity
+    echo NO RESULT;
+    cleanup;
+    exit 2;
+}
+
+pass()
+{
+    cleanup;
+    exit 0;
+}
+
+mkdir -p $TEMPDIR
+
+cd $TEMPDIR
+
+# Create command file.
+activity="create program"
+cat > $TESTFILE << EOF
+data list free /x y z.
+variable alignment x (left)/y (right)/z (center).
+variable width x (10)/y (12)/z (14).
+variable level x (scale)/y (ordinal)/z (nominal).
+display dictionary.
+EOF
+if [ $? -ne 0 ] ; then no_result ; fi
+
+
+activity="run program"
+$SUPERVISOR $PSPP --testing-mode $TESTFILE
+if [ $? -ne 0 ] ; then fail ; fi
+
+activity="compare output"
+perl -pi -e 's/^\s*$//g' $TEMPDIR/pspp.list
+diff -b  $TEMPDIR/pspp.list - << EOF
+1.1 DISPLAY.
++--------+-------------------------------------------+--------+
+|Variable|Description                                |Position|
+#========#===========================================#========#
+|x       |Format: F8.2                               |       1|
+|        |Measure: Scale                             |        |
+|        |Display Alignment: Left                    |        |
+|        |Display Width: 10                          |        |
++--------+-------------------------------------------+--------+
+|y       |Format: F8.2                               |       2|
+|        |Measure: Ordinal                           |        |
+|        |Display Alignment: Right                   |        |
+|        |Display Width: 12                          |        |
++--------+-------------------------------------------+--------+
+|z       |Format: F8.2                               |       3|
+|        |Measure: Nominal                           |        |
+|        |Display Alignment: Center                  |        |
+|        |Display Width: 14                          |        |
++--------+-------------------------------------------+--------+
+EOF
+if [ $? -ne 0 ] ; then fail ; fi
+
+pass;