From ded05211ac6ead7dd745245bec98c053ba10529d Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sun, 7 Nov 2021 17:47:14 -0800 Subject: [PATCH] MATRIX SAVE inline positive tests. --- src/language/stats/matrix.c | 29 +++++++++++++++++++------ tests/language/stats/matrix.at | 39 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/src/language/stats/matrix.c b/src/language/stats/matrix.c index bc9ae3363b..d90cd2a326 100644 --- a/src/language/stats/matrix.c +++ b/src/language/stats/matrix.c @@ -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); } } diff --git a/tests/language/stats/matrix.at b/tests/language/stats/matrix.at index d7966f415b..c02aa49cbb 100644 --- a/tests/language/stats/matrix.at +++ b/tests/language/stats/matrix.at @@ -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 -- 2.30.2