[gnumeric] COUNTIF: Tests and fixes.



commit 908f485cc7698568faa6e4a11a14bc5105b32b91
Author: Morten Welinder <terra gnome org>
Date:   Tue Jun 21 20:33:20 2016 -0400

    COUNTIF: Tests and fixes.

 ChangeLog                    |    8 ++++++++
 NEWS                         |    5 +++++
 plugins/fn-math/ChangeLog    |    6 ++++++
 plugins/fn-math/functions.c  |    6 +++---
 samples/excel12/countif.xlsx |  Bin 0 -> 12921 bytes
 src/value.c                  |   29 ++++++++++++++++-------------
 src/value.h                  |    3 ++-
 test/ChangeLog               |    4 ++++
 test/Makefile.am             |    1 +
 test/t1017-countif.pl        |   10 ++++++++++
 10 files changed, 55 insertions(+), 17 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 7c141fc..8fe440e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2016-06-21  Morten Welinder  <terra gnome org>
+
+       * src/value.c (criteria_test_greater_or_equal): Use case-folding
+       comparisons for strings.
+       (parse_criteria, parse_criteria_range): Add argument controlling
+       whether to anchor matching at end of string.
+       (parse_database_criteria): Don't anchor here.
+
 2016-06-17  Morten Welinder <terra gnome org>
 
        * configure.ac: Post-release bump.
diff --git a/NEWS b/NEWS
index 3a73fec..f32ec89 100644
--- a/NEWS
+++ b/NEWS
@@ -1,5 +1,10 @@
 Gnumeric 1.12.31
 
+Morten:
+       * Fix case issues for database functions.
+       * Anchor COUNTIF criteria at end too.
+       * Add tests for COUNTIF.
+
 --------------------------------------------------------------------------
 Gnumeric 1.12.30
 
diff --git a/plugins/fn-math/ChangeLog b/plugins/fn-math/ChangeLog
index a6df41b..fade958 100644
--- a/plugins/fn-math/ChangeLog
+++ b/plugins/fn-math/ChangeLog
@@ -1,3 +1,9 @@
+2016-06-21  Morten Welinder  <terra gnome org>
+
+       * functions.c (gnumeric_countif): Anchor string matching at end.
+       (gnumeric_sumif): Ditto.
+       (gnumeric_averageif): Ditto.
+
 2016-06-17  Morten Welinder <terra gnome org>
 
        * Release 1.12.30
diff --git a/plugins/fn-math/functions.c b/plugins/fn-math/functions.c
index 8d0b264..ddeef89 100644
--- a/plugins/fn-math/functions.c
+++ b/plugins/fn-math/functions.c
@@ -552,7 +552,7 @@ gnumeric_countif (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
                return value_new_error_VALUE (ei->pos);
 
        res.count = 0;
-       res.crit = parse_criteria (argv[1], date_conv);
+       res.crit = parse_criteria (argv[1], date_conv, TRUE);
        problem = sheet_foreach_cell_in_range
                (sheet, res.crit->iter_flags,
                 r->cell.a.col, r->cell.a.row, r->cell.b.col, r->cell.b.row,
@@ -670,7 +670,7 @@ gnumeric_sumif (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
 
        res.sum = 0;
        res.count = 0;
-       res.crit = parse_criteria (argv[1], date_conv);
+       res.crit = parse_criteria (argv[1], date_conv, TRUE);
        problem = sheet_foreach_cell_in_range
                (start_sheet, res.crit->iter_flags,
                 rs.start.col, rs.start.row, rs.end.col, rs.end.row,
@@ -735,7 +735,7 @@ gnumeric_averageif (GnmFuncEvalInfo *ei, GnmValue const * const *argv)
 
        res.sum = 0.;
        res.count = 0;
-       res.crit = parse_criteria (argv[1], date_conv);
+       res.crit = parse_criteria (argv[1], date_conv, TRUE);
        problem = sheet_foreach_cell_in_range
                (start_sheet, res.crit->iter_flags,
                 rs.start.col, rs.start.row, rs.end.col, rs.end.row,
diff --git a/samples/excel12/countif.xlsx b/samples/excel12/countif.xlsx
new file mode 100644
index 0000000..abbc254
Binary files /dev/null and b/samples/excel12/countif.xlsx differ
diff --git a/src/value.c b/src/value.c
index dadf4f4..3ad6ccc 100644
--- a/src/value.c
+++ b/src/value.c
@@ -1590,8 +1590,8 @@ criteria_test_less (GnmValue const *x, GnmCriteria *crit)
        case CRIT_WRONGTYPE:
                return FALSE;
        case CRIT_STRING:
-               return g_utf8_collate (value_peek_string (x),
-                                      value_peek_string (y)) < 0;
+               return go_utf8_collate_casefold (value_peek_string (x),
+                                                value_peek_string (y)) < 0;
        case CRIT_FLOAT:
                return xf < yf;
        }
@@ -1610,8 +1610,8 @@ criteria_test_greater (GnmValue const *x, GnmCriteria *crit)
        case CRIT_WRONGTYPE:
                return FALSE;
        case CRIT_STRING:
-               return g_utf8_collate (value_peek_string (x),
-                                      value_peek_string (y)) > 0;
+               return go_utf8_collate_casefold (value_peek_string (x),
+                                                value_peek_string (y)) > 0;
        case CRIT_FLOAT:
                return xf > yf;
        }
@@ -1630,8 +1630,8 @@ criteria_test_less_or_equal (GnmValue const *x, GnmCriteria *crit)
        case CRIT_WRONGTYPE:
                return FALSE;
        case CRIT_STRING:
-               return g_utf8_collate (value_peek_string (x),
-                                      value_peek_string (y)) <= 0;
+               return go_utf8_collate_casefold (value_peek_string (x),
+                                                value_peek_string (y)) <= 0;
        case CRIT_FLOAT:
                return xf <= yf;
        }
@@ -1650,8 +1650,8 @@ criteria_test_greater_or_equal (GnmValue const *x, GnmCriteria *crit)
        case CRIT_WRONGTYPE:
                return FALSE;
        case CRIT_STRING:
-               return g_utf8_collate (value_peek_string (x),
-                                      value_peek_string (y)) >= 0;
+               return go_utf8_collate_casefold (value_peek_string (x),
+                                                value_peek_string (y)) >= 0;
        case CRIT_FLOAT:
                return xf >= yf;
        }
@@ -1809,7 +1809,8 @@ free_criterias (GSList *criterias)
  * "pattern"
  **/
 GnmCriteria *
-parse_criteria (GnmValue const *crit_val, GODateConventions const *date_conv)
+parse_criteria (GnmValue const *crit_val, GODateConventions const *date_conv,
+               gboolean anchor_end)
 {
        int len;
        char const *criteria;
@@ -1848,7 +1849,7 @@ parse_criteria (GnmValue const *crit_val, GODateConventions const *date_conv)
                len = 1;
        } else {
                res->fun = criteria_test_match;
-               res->has_rx = (gnm_regcomp_XL (&res->rx, criteria, GO_REG_ICASE, TRUE, FALSE) == GO_REG_OK);
+               res->has_rx = (gnm_regcomp_XL (&res->rx, criteria, GO_REG_ICASE, TRUE, anchor_end) == 
GO_REG_OK);
                len = 0;
        }
 
@@ -1870,7 +1871,7 @@ parse_criteria (GnmValue const *crit_val, GODateConventions const *date_conv)
 
 static GSList *
 parse_criteria_range (Sheet *sheet, int b_col, int b_row, int e_col, int e_row,
-                     int   *field_ind)
+                     int   *field_ind, gboolean anchor_end)
 {
        GSList *criterias = NULL;
        GODateConventions const *date_conv =
@@ -1889,7 +1890,8 @@ parse_criteria_range (Sheet *sheet, int b_col, int b_row, int e_col, int e_row,
                        if (gnm_cell_is_empty (cell))
                                continue;
 
-                       cond = parse_criteria (cell->value, date_conv);
+                       cond = parse_criteria (cell->value, date_conv,
+                                              anchor_end);
                        cond->column = (field_ind != NULL)
                                ? field_ind[j - b_col]
                                : j - b_col;
@@ -1951,7 +1953,8 @@ parse_database_criteria (GnmEvalPos const *ep, GnmValue const *database, GnmValu
        }
 
        return parse_criteria_range (sheet, b_col, b_row + 1,
-                                    e_col, e_row, field_ind);
+                                    e_col, e_row, field_ind,
+                                    FALSE);
 }
 
 /**
diff --git a/src/value.h b/src/value.h
index ed05a44..6d9ed08 100644
--- a/src/value.h
+++ b/src/value.h
@@ -213,7 +213,8 @@ typedef struct {
 } GnmDBCriteria;
 
 GnmCriteria *parse_criteria (GnmValue const *crit_val,
-                            GODateConventions const *date_conv);
+                            GODateConventions const *date_conv,
+                            gboolean anchor_end);
 void   free_criteria           (GnmCriteria *criteria);
 void   free_criterias          (GSList *criterias);
 GSList *find_rows_that_match   (Sheet *sheet, int first_col,
diff --git a/test/ChangeLog b/test/ChangeLog
index 3236aa4..dd1273f 100644
--- a/test/ChangeLog
+++ b/test/ChangeLog
@@ -1,3 +1,7 @@
+2016-06-21  Morten Welinder  <terra gnome org>
+
+       * t1017-countif.pl: New test.
+
 2016-06-17  Morten Welinder <terra gnome org>
 
        * Release 1.12.30
diff --git a/test/Makefile.am b/test/Makefile.am
index 590a9f1..64b24e3 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -44,6 +44,7 @@ TESTS =       t1000-statfuns.pl                       \
        t1014-gsl.pl                            \
        t1015-complex.pl                        \
        t1016-database.pl                       \
+       t1017-countif.pl                        \
        t1100-chitest.pl                        \
        t1101-ftest.pl                          \
        t1102-ttest.pl                          \
diff --git a/test/t1017-countif.pl b/test/t1017-countif.pl
new file mode 100755
index 0000000..43ab889
--- /dev/null
+++ b/test/t1017-countif.pl
@@ -0,0 +1,10 @@
+#!/usr/bin/perl -w
+# -----------------------------------------------------------------------------
+
+use strict;
+use lib ($0 =~ m|^(.*/)| ? $1 : ".");
+use GnumericTest;
+
+my $file = "excel12/countif.xlsx";
+&message ("Check that $file evaluates correctly.");
+&test_sheet_calc ("$samples/$file", "Overview!C2:C99", sub { /(\s*0)+\s*/i });


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]