From b34f0edb35583029454da7e28aa5e8121da42170 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Fri, 8 Jul 2022 13:20:07 -0700 Subject: [PATCH] Handle multiple postcomputes. --- src/language/stats/ctables.c | 42 ++++++++++++++++++++------------- tests/language/stats/ctables.at | 2 +- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/language/stats/ctables.c b/src/language/stats/ctables.c index 930c09c633..59c97ecdc2 100644 --- a/src/language/stats/ctables.c +++ b/src/language/stats/ctables.c @@ -4213,24 +4213,30 @@ ctables_cell_postcompute (const struct ctables_section *s, size_t *pc_a_idx_p) { assert (cell->postcompute); - for (enum pivot_axis_type pc_a = 0; ; pc_a++) - { - assert (pc_a < PIVOT_N_AXES); - for (size_t pc_a_idx = 0; pc_a_idx < s->nests[pc_a]->n; pc_a_idx++) - { - const struct ctables_cell_value *cv = &cell->axes[pc_a].cvs[pc_a_idx]; - if (cv->category->type == CCT_POSTCOMPUTE) - { - if (pc_a_p) - *pc_a_p = pc_a; - if (pc_a_idx_p) - *pc_a_idx_p = pc_a_idx; - return cv->category->pc; - } - } - } + const struct ctables_postcompute *pc = NULL; + for (enum pivot_axis_type pc_a = 0; pc_a < PIVOT_N_AXES; pc_a++) + for (size_t pc_a_idx = 0; pc_a_idx < s->nests[pc_a]->n; pc_a_idx++) + { + const struct ctables_cell_value *cv = &cell->axes[pc_a].cvs[pc_a_idx]; + if (cv->category->type == CCT_POSTCOMPUTE) + { + if (pc) + { + /* Multiple postcomputes cross each other. The value is + undefined. */ + return NULL; + } - NOT_REACHED (); + pc = cv->category->pc; + if (pc_a_p) + *pc_a_p = pc_a; + if (pc_a_idx_p) + *pc_a_idx_p = pc_a_idx; + } + } + + assert (pc != NULL); + return pc; } static double @@ -4245,6 +4251,8 @@ ctables_cell_calculate_postcompute (const struct ctables_section *s, size_t pc_a_idx; const struct ctables_postcompute *pc = ctables_cell_postcompute ( s, cell, &pc_a, &pc_a_idx); + if (!pc) + return SYSMIS; if (pc->specs) { diff --git a/tests/language/stats/ctables.at b/tests/language/stats/ctables.at index 7a52b185de..d480df3431 100644 --- a/tests/language/stats/ctables.at +++ b/tests/language/stats/ctables.at @@ -6,7 +6,6 @@ dnl - SPLIT FILE with SEPARATE splits dnl - Definition of columns/rows when labels are rotated from one axis to another. dnl - Preprocessing to distinguish categorical from scale. dnl - PCOMPUTE: -dnl * multi-dimensional (multiple CCT_POSTCOMPUTE in one cell) dnl * dates dnl dnl Features not yet tested: @@ -42,6 +41,7 @@ dnl - Test PCOMPUTE: dnl * PCOMPUTE for more than one kind of summary (e.g. [COUNT, ROWPCT]). dnl * MISSING, OTHERNM dnl * strings and string ranges +dnl * multi-dimensional (multiple CCT_POSTCOMPUTE in one cell) dnl - PPROPERTIES: dnl * )LABEL[N]. dnl - Summary functions: -- 2.30.2