DEBUG EVALUATE: Use output_log() instead of printf().
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 25 Jan 2021 00:59:29 +0000 (16:59 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 25 Jan 2021 01:00:37 +0000 (17:00 -0800)
I found that with upcoming changes to the output engine, the relative
ordering of output from printf() and from the output engine changed.  This
commit makes it all go through the output engine, which keeps it
consistent as the output engine changes.

src/language/expressions/evaluate.c
tests/data/calendar.at
tests/language/expressions/evaluate.at
tests/language/expressions/parse.at

index aa85f2c354004db4d3f0715dade21b9003dc59a3..7543aed55e7ad29c167c9a5ee47c63b7d48660ec 100644 (file)
@@ -26,6 +26,7 @@
 #include "language/expressions/private.h"
 #include "language/lexer/value-parser.h"
 #include "libpspp/pool.h"
+#include "output/driver.h"
 
 #include "xalloc.h"
 
@@ -190,7 +191,7 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
     {
       if (expr != NULL)
         expr_free (expr);
-      printf ("error\n");
+      output_log ("error");
       goto done;
     }
 
@@ -203,28 +204,25 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
         {
           double d = expr_evaluate_num (expr, c, 0);
           if (d == SYSMIS)
-            printf ("sysmis\n");
+            output_log ("sysmis");
           else
-            printf ("%.2f\n", d);
+            output_log ("%.2f", d);
         }
         break;
 
       case OP_boolean:
         {
           double b = expr_evaluate_num (expr, c, 0);
-          printf ("%s\n",
-                   b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
+          output_log ("%s",
+                      b == SYSMIS ? "sysmis" : b == 0.0 ? "false" : "true");
         }
         break;
 
       case OP_string:
         {
-          struct substring s;
-          expr_evaluate (expr, c, 0, &s);
-
-          putchar ('"');
-          fwrite (s.string, s.length, 1, stdout);
-          puts ("\"");
+          struct substring out;
+          expr_evaluate (expr, c, 0, &out);
+          output_log ("\"%.*s\"", (int) out.length, out.string);
           break;
         }
 
@@ -248,57 +246,58 @@ cmd_debug_evaluate (struct lexer *lexer, struct dataset *dsother UNUSED)
 void
 expr_debug_print_postfix (const struct expression *e)
 {
-  size_t i;
+  struct string s = DS_EMPTY_INITIALIZER;
 
-  for (i = 0; i < e->op_cnt; i++)
+  for (size_t i = 0; i < e->op_cnt; i++)
     {
       union operation_data *op = &e->ops[i];
       if (i > 0)
-        putc (' ', stderr);
+        ds_put_byte (&s, ' ');
       switch (e->op_types[i])
         {
         case OP_operation:
           if (op->operation == OP_return_number)
-            printf ("return_number");
+            ds_put_cstr (&s, "return_number");
           else if (op->operation == OP_return_string)
-            printf ("return_string");
+            ds_put_cstr (&s, "return_string");
           else if (is_function (op->operation))
-            printf ("%s", operations[op->operation].prototype);
+            ds_put_format (&s, "%s", operations[op->operation].prototype);
           else if (is_composite (op->operation))
-            printf ("%s", operations[op->operation].name);
+            ds_put_format (&s, "%s", operations[op->operation].name);
           else
-            printf ("%s:", operations[op->operation].name);
+            ds_put_format (&s, "%s:", operations[op->operation].name);
           break;
         case OP_number:
           if (op->number != SYSMIS)
-            printf ("n<%g>", op->number);
+            ds_put_format (&s, "n<%g>", op->number);
           else
-            printf ("n<SYSMIS>");
+            ds_put_cstr (&s, "n<SYSMIS>");
           break;
         case OP_string:
-          printf ("s<%.*s>",
-                   (int) op->string.length,
-                   op->string.string != NULL ? op->string.string : "");
+          ds_put_cstr (&s, "s<");
+          ds_put_substring (&s, op->string);
+          ds_put_byte (&s, '>');
           break;
         case OP_format:
           {
             char str[FMT_STRING_LEN_MAX + 1];
             fmt_to_string (op->format, str);
-            printf ("f<%s>", str);
+            ds_put_format (&s, "f<%s>", str);
           }
           break;
         case OP_variable:
-          printf ("v<%s>", var_get_name (op->variable));
+          ds_put_format (&s, "v<%s>", var_get_name (op->variable));
           break;
         case OP_vector:
-          printf ("vec<%s>", vector_get_name (op->vector));
+          ds_put_format (&s, "vec<%s>", vector_get_name (op->vector));
           break;
         case OP_integer:
-          printf ("i<%d>", op->integer);
+          ds_put_format (&s, "i<%d>", op->integer);
           break;
         default:
           NOT_REACHED ();
         }
     }
-  printf ("\n");
+  output_log ("%s", ds_cstr (&s));
+  ds_destroy (&s);
 }
index 339098892e9456760df76d505224e627186c0bd2..605346ee1d3ec68c1fd8b0ec4fbbc80d9a124c9f 100644 (file)
@@ -102,8 +102,20 @@ DEBUG EVALUATE/DATE.DMY(1,1,99) = DATE.DMY(1,1,2099).
 DEBUG EVALUATE/DATE.DMY(1,1,100).
 ])
 
-AT_CHECK([pspp --testing-mode --error-file=- --no-output epoch.sps], [1],
-  [true
+AT_CHECK([pspp --testing-mode epoch.sps -o pspp.csv], [1], [dnl
+epoch.sps:11: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:18: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:27: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:34: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:43: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:50: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:59: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:66: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:75: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+epoch.sps:82: error: DEBUG EVALUATE: Date 0100-1-1 is before the earliest acceptable date of 1582-10-15.
+])
+AT_CHECK([sed '/^$/d' < pspp.csv], [0], [dnl
+true
 true
 true
 true
index ac4d93e7048f4aad16f1d5309ce2c0347389256e..cd574eb10f79eb8cea949a754d6b20ffc2947355 100644 (file)
@@ -26,14 +26,10 @@ DEBUG EVALUATE m4_argn(4, check)/[]m4_car(check).
 ])])
    AT_CAPTURE_FILE([evaluate.sps])
    m4_pushdef([i], [3])
-   AT_CHECK([pspp --testing-mode --error-file=- --no-output evaluate.sps],
+   AT_CHECK([pspp --testing-mode -O format=csv evaluate.sps],
      [m4_if(m4_bregexp([m4_foreach([check], [m4_shift($@)], [m4_argn(3, check)])], [error:]), [-1], [0], [1])],
      [stdout])
-   # Use sed to transform "file:line.column:" into plain "file:line:",
-   # because column numbers change between opt and noopt versions.
-   AT_CHECK([[sed 's/\(evaluate.sps:[0-9]\{1,\}\)\.[0-9]\{1,\}:/\1:/' stdout]],
-     [0],
-     [m4_foreach([check], [m4_shift($@)],
+   AT_DATA([expout], [m4_foreach([check], [m4_shift($@)],
         [m4_define([i], m4_incr(i))dnl
 m4_if(m4_argn(3, check), [], [], [evaluate.sps:[]i[]: m4_argn(3, check)
 ])dnl
@@ -42,7 +38,21 @@ m4_define([i], m4_incr(i))dnl
 m4_if(m4_argn(3, check), [], [], [evaluate.sps:[]i[]: m4_argn(3, check)
 ])dnl
 m4_argn(2, check)
-])], [])
+])])
+   AT_CHECK([[sed '
+# Transform "file:line.column:" into plain "file:line:",
+# because column numbers change between opt and noopt versions.
+s/\(evaluate.sps:[0-9]\{1,\}\)\.[0-9]\{1,\}:/\1:/
+
+# Remove leading or trailing quotes and un-double CSV quotes.
+s/^"//
+s/"$//
+s/""/"/g
+# "
+
+# Delete blank lines
+/^$/d' stdout]],
+     [0], [expout], [])
    m4_popdef([i])
    AT_CLEANUP])
 
index e40a4fe873c3b910c53f8754134d1c1f42714b5e..8e4166c5992493103034b78c2e635e4a5b9eb61c 100644 (file)
@@ -88,6 +88,7 @@ DEBUG EVALUATE/ACOS(0)*0.
 ])
 AT_CHECK([pspp --testing-mode --syntax=compatible -O format=csv parse.sps], [0], [dnl
 parse.sps:1: warning: DEBUG EVALUATE: ACOS(number) is a PSPP extension.
+
 0.00
 ])
 AT_CLEANUP