From 76d259f292809020a371ea8830cc58a8d25385e1 Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Sat, 30 Jul 2022 11:40:38 -0700 Subject: [PATCH] start writing tests --- src/data/dataset.c | 2 + src/language/stats/ctables.c | 76 ++++++++++++++++++--------------- tests/language/stats/ctables.at | 54 +++++++++++++++++------ 3 files changed, 86 insertions(+), 46 deletions(-) diff --git a/src/data/dataset.c b/src/data/dataset.c index 2516fa3d1f..41b467a0ae 100644 --- a/src/data/dataset.c +++ b/src/data/dataset.c @@ -1150,6 +1150,8 @@ measure_guesser_run (struct measure_guesser *mg, measure_guesser_commit (mg); } +/* A transformation for guessing measurement levels. */ + static enum trns_result mg_trns_proc (void *mg_, struct ccase **c, casenumber case_nr UNUSED) { diff --git a/src/language/stats/ctables.c b/src/language/stats/ctables.c index 69c505c486..e043105ccb 100644 --- a/src/language/stats/ctables.c +++ b/src/language/stats/ctables.c @@ -4719,7 +4719,12 @@ ctables_check_label_position (struct ctables_table *t, enum pivot_axis_type a) return true; const struct ctables_nest *n0 = &stack->nests[0]; - assert (n0->n > 0); + if (n0->n == 0) + { + assert (stack->n == 1); + return true; + } + const struct variable *v0 = n0->vars[n0->n - 1]; struct ctables_categories *c0 = t->categories[var_get_dict_index (v0)]; t->clabels_example = v0; @@ -4896,6 +4901,10 @@ ctables_prepare_table (struct ctables_table *t) struct ctables_nest *nest = xmalloc (sizeof *nest); *nest = (struct ctables_nest) { .n = 0 }; t->stacks[a] = (struct ctables_stack) { .nests = nest, .n = 1 }; + + /* There's no point in moving labels away from an axis that has no + labels, so avoid dealing with the special cases around that. */ + t->label_axis[a] = a; } struct ctables_stack *stack = &t->stacks[t->summary_axis]; @@ -6246,7 +6255,7 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds) if (!ct->e_weight) goto error; } - else if (lex_match_id (lexer, " HIDESMALLCOUNTS")) + else if (lex_match_id (lexer, "HIDESMALLCOUNTS")) { if (lex_match_id (lexer, "COUNT")) { @@ -6443,46 +6452,45 @@ cmd_ctables (struct lexer *lexer, struct dataset *ds) } else if (lex_match_id (lexer, "CLABELS")) { - while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD) + if (lex_match_id (lexer, "AUTO")) { - if (lex_match_id (lexer, "AUTO")) - { - t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW; - t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN; - } - else if (lex_match_id (lexer, "ROWLABELS")) - { - lex_match (lexer, T_EQUALS); - if (lex_match_id (lexer, "OPPOSITE")) - t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN; - else if (lex_match_id (lexer, "LAYER")) - t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER; - else - { - lex_error_expecting (lexer, "OPPOSITE", "LAYER"); - goto error; - } - } - else if (lex_match_id (lexer, "COLLABELS")) + t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW; + t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN; + } + else if (lex_match_id (lexer, "ROWLABELS")) + { + lex_match (lexer, T_EQUALS); + t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_COLUMN; + if (lex_match_id (lexer, "OPPOSITE")) + t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_COLUMN; + else if (lex_match_id (lexer, "LAYER")) + t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_LAYER; + else { - lex_match (lexer, T_EQUALS); - if (lex_match_id (lexer, "OPPOSITE")) - t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW; - else if (lex_match_id (lexer, "LAYER")) - t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER; - else - { - lex_error_expecting (lexer, "OPPOSITE", "LAYER"); - goto error; - } + lex_error_expecting (lexer, "OPPOSITE", "LAYER"); + goto error; } + } + else if (lex_match_id (lexer, "COLLABELS")) + { + lex_match (lexer, T_EQUALS); + t->label_axis[PIVOT_AXIS_ROW] = PIVOT_AXIS_ROW; + if (lex_match_id (lexer, "OPPOSITE")) + t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_ROW; + else if (lex_match_id (lexer, "LAYER")) + t->label_axis[PIVOT_AXIS_COLUMN] = PIVOT_AXIS_LAYER; else { - lex_error_expecting (lexer, "AUTO", "ROWLABELS", - "COLLABELS"); + lex_error_expecting (lexer, "OPPOSITE", "LAYER"); goto error; } } + else + { + lex_error_expecting (lexer, "AUTO", "ROWLABELS", + "COLLABELS"); + goto error; + } } else if (lex_match_id (lexer, "CRITERIA")) { diff --git a/tests/language/stats/ctables.at b/tests/language/stats/ctables.at index b1a497db3c..23a41edc60 100644 --- a/tests/language/stats/ctables.at +++ b/tests/language/stats/ctables.at @@ -73,18 +73,48 @@ dnl +--------------------------+----+------+----+------+ -# AT_SETUP([CTABLES parsing]) -# AT_DATA([ctables.sps], -# [[DATA LIST LIST NOTABLE /x y z. -# CTABLES /TABLE=(x + y) > z. -# CTABLES /TABLE=(x[c] + y[c]) > z. -# CTABLES /TABLE=(x + y) > z[c]. -# CTABLES /TABLE=x BY y BY z. -# CTABLES /TABLE=x[c] [ROWPCT.COUNT] > y[c]. -# CTABLES /TABLE=x[c] > y[c] [ROWPCT.COUNT]. -# ]]) -# AT_CHECK([pspp ctables.sps]) -# AT_CLEANUP +AT_SETUP([CTABLES parsing]) +AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .]) +AT_DATA([ctables.sps], +[[GET 'nhtsa.sav'. +CTABLES + /FORMAT MINCOLWIDTH=10 MAXCOLWIDTH=20 UNITS=POINTS EMPTY=ZERO MISSING="x" + /FORMAT MINCOLWIDTH=DEFAULT MAXCOLWIDTH=DEFAULT UNITS=INCHES EMPTY=BLANK MISSING="." + /FORMAT UNITS=CM EMPTY="(-)" + /VLABELS VARIABLES=qn1 DISPLAY=DEFAULT + /VLABELS VARIABLES=qn17 DISPLAY=NAME + /VLABELS VARIABLES=qns3a DISPLAY=LABEL + /VLABELS VARIABLES=qnd1 DISPLAY=BOTH + /VLABELS VARIABLES=qn20 DISPLAY=NONE + /MRSETS COUNTDUPLICATES=NO + /MRSETS COUNTDUPLICATES=YES + /SMISSING VARIABLE + /SMISSING LISTWISE + /WEIGHT VARIABLE=qns3a + /HIDESMALLCOUNTS + /HIDESMALLCOUNTS COUNT=10 + /TABLE qnd1 + /SLABELS POSITION=COLUMN VISIBLE=YES + /SLABELS VISIBLE=NO POSITION=ROW + /SLABELS POSITION=LAYER + /CLABELS AUTO + /CLABELS ROWLABELS=OPPOSITE + /CLABELS ROWLABELS=LAYER + /CLABELS COLLABELS=OPPOSITE + /CLABELS COLLABELS=LAYER + /CRITERIA CILEVEL=50 + /CATEGORIES VARIABLES=qn1 qn17 + ORDER=A KEY=VALUE MISSING=INCLUDE TOTAL=YES LABEL="xyzzy" + POSITION=BEFORE EMPTY=INCLUDE. +]]) +AT_CHECK([pspp ctables.sps -O box=unicode -O width=80], [0], [dnl + Custom Tables +Mean +╭───────────────────────────────┬──╮ +│qnd1 D1. AGE: What is your age?│49│ +╰───────────────────────────────┴──╯ +]) +AT_CLEANUP AT_SETUP([CTABLES one categorical variable]) AT_CHECK([ln $top_srcdir/examples/nhtsa.sav . || cp $top_srcdir/examples/nhtsa.sav .]) -- 2.30.2