MATRIX SAVE inline positive tests.
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 8 Nov 2021 01:47:14 +0000 (17:47 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Mon, 8 Nov 2021 01:47:14 +0000 (17:47 -0800)
src/language/stats/matrix.c
tests/language/stats/matrix.at

index bc9ae3363b775e9883054f7c55b0e3bbcf5a4692..d90cd2a326bc436bfae9bf7db3031a0f8e94e94d 100644 (file)
@@ -108,15 +108,17 @@ struct write_file
 struct save_file
   {
     struct file_handle *file;
+    struct dataset *dataset;
 
     /* Parameters from parsing the first SAVE command for 'file'. */
     struct string_array variables;
     struct matrix_expr *names;
     struct stringi_set strings;
 
-    /* Results from the first attempt to open this save_file. */
+    /* Results from the first (only) attempt to open this save_file. */
     bool error;
     struct casewriter *writer;
+    struct dictionary *dict;
   };
 
 struct matrix_state
@@ -4383,6 +4385,7 @@ save_file_create (struct matrix_state *s, struct file_handle *fh,
   struct save_file *sf = xmalloc (sizeof *sf);
   *sf = (struct save_file) {
     .file = fh,
+    .dataset = s->dataset,
     .variables = *variables,
     .names = names,
     .strings = STRINGI_SET_INITIALIZER (sf->strings),
@@ -4411,7 +4414,8 @@ save_file_open (struct save_file *sf, gsl_matrix *m)
               msg (ME, _("The first SAVE to %s within this matrix program "
                          "had %zu columns, so a %zuĂ—%zu matrix cannot be "
                          "saved to it."),
-                   fh_get_name (sf->file), n_variables, m->size1, m->size2);
+                   sf->file == fh_inline_file () ? "*" : fh_get_name (sf->file),
+                   n_variables, m->size1, m->size2);
               return NULL;
             }
         }
@@ -4491,12 +4495,16 @@ save_file_open (struct save_file *sf, gsl_matrix *m)
       return NULL;
     }
 
-  sf->writer = any_writer_open (sf->file, dict);
-  dict_unref (dict);
-  if (!sf->writer)
+  if (sf->file == fh_inline_file ())
+    sf->writer = autopaging_writer_create (dict_get_proto (dict));
+  else
+    sf->writer = any_writer_open (sf->file, dict);
+  if (sf->writer)
+    sf->dict = dict;
+  else
     {
+      dict_unref (dict);
       sf->error = true;
-      return NULL;
     }
   return sf->writer;
 }
@@ -4506,11 +4514,20 @@ save_file_destroy (struct save_file *sf)
 {
   if (sf)
     {
+      if (sf->file == fh_inline_file () && sf->writer && sf->dict)
+        {
+          dataset_set_dict (sf->dataset, sf->dict);
+          dataset_set_source (sf->dataset, casewriter_make_reader (sf->writer));
+          sf->dict = NULL;
+          sf->writer = NULL;
+        }
+
       fh_unref (sf->file);
       string_array_destroy (&sf->variables);
       matrix_expr_destroy (sf->names);
       stringi_set_destroy (&sf->strings);
       casewriter_destroy (sf->writer);
+      dict_unref (sf->dict);
       free (sf);
     }
 }
index d7966f415b671899d6eec23e8f6aa21005ffc328..c02aa49cbb5ce8dc80d30a2387047dbb20b6da7b 100644 (file)
@@ -3127,4 +3127,43 @@ a,b,c
 1,abcd,3
 4,xyzw,6
 ])
+AT_CLEANUP
+
+AT_SETUP([MATRIX - SAVE - inline])
+AT_DATA([matrix.sps], [dnl
+MATRIX.
+SAVE {1,2,3; 4,5,6}/OUTFILE=*.
+SAVE {7,8,9}/VARIABLES=a b c d.
+END MATRIX.
+LIST.
+
+MATRIX.
+SAVE {1,2,3}/OUTFILE=*/VARIABLES=v01 TO v03.
+SAVE {4,5,6}/NAMES={'x', 'y', 'z', 'w'}.
+END MATRIX.
+LIST.
+
+MATRIX.
+SAVE {1,'abcd',3}/OUTFILE=*/NAMES={'a', 'b', 'c'}/STRINGS=b.
+SAVE {4,'xyzw',6}/STRINGS=a, b.
+END MATRIX.
+LIST.
+])
+AT_CHECK([pspp matrix.sps -O format=csv], [0], [dnl
+Table: Data List
+COL1,COL2,COL3
+1.00,2.00,3.00
+4.00,5.00,6.00
+7.00,8.00,9.00
+
+Table: Data List
+v01,v02,v03
+1.00,2.00,3.00
+4.00,5.00,6.00
+
+Table: Data List
+a,b,c
+1.00,abcd,3.00
+4.00,xyzw,6.00
+])
 AT_CLEANUP
\ No newline at end of file