[gnumeric] tests: test solver.
- From: Morten Welinder <mortenw src gnome org>
- To: svn-commits-list gnome org
- Cc:
- Subject: [gnumeric] tests: test solver.
- Date: Wed, 2 Dec 2009 21:54:00 +0000 (UTC)
commit 5c4051128a840f72f71702dc6647cbc7b0675982
Author: Morten Welinder <terra gnome org>
Date: Wed Dec 2 16:53:41 2009 -0500
tests: test solver.
ChangeLog | 3 ++
NEWS | 1 +
src/ssconvert.c | 81 ++++++++++++++++++++++++++++++++++++++++++--
test/t7100-solver-blend.pl | 16 +++++++++
4 files changed, 98 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 8f7424e..d51ca6b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
2009-12-02 Morten Welinder <terra gnome org>
+ * src/ssconvert.c (run_solver): Optionally run solver. (For
+ testing only.)
+
* src/workbook-control.c (wbc_cmd_context_init): New function.
2009-12-01 Morten Welinder <terra gnome org>
diff --git a/NEWS b/NEWS
index 65bbc1f..a340675 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ Gnumeric 1.9.17
Morten:
* Elminate use of "float".
* Fix selection problem. [#601107]
+ * Automate solver testing.
--------------------------------------------------------------------------
Gnumeric 1.9.16
diff --git a/src/ssconvert.c b/src/ssconvert.c
index 9915744..bcc91a8 100644
--- a/src/ssconvert.c
+++ b/src/ssconvert.c
@@ -17,6 +17,7 @@
#include "parse-util.h"
#include "workbook.h"
#include "workbook-priv.h"
+#include "workbook-control.h"
#include "sheet.h"
#include "dependent.h"
#include "expr-name.h"
@@ -40,6 +41,7 @@ static gboolean ssconvert_list_exporters = FALSE;
static gboolean ssconvert_list_importers = FALSE;
static gboolean ssconvert_one_file_per_sheet = FALSE;
static gboolean ssconvert_recalc = FALSE;
+static gboolean ssconvert_solve = FALSE;
static char *ssconvert_range = NULL;
static char *ssconvert_import_encoding = NULL;
static char *ssconvert_import_id = NULL;
@@ -112,21 +114,21 @@ static const GOptionEntry ssconvert_options [] = {
{
"export-file-per-sheet", 'S',
0, G_OPTION_ARG_NONE, &ssconvert_one_file_per_sheet,
- N_("Export a file for each sheet if the exporter only supports one sheet at a time."),
+ N_("Export a file for each sheet if the exporter only supports one sheet at a time"),
NULL
},
{
"recalc", 0,
0, G_OPTION_ARG_NONE, &ssconvert_recalc,
- N_("Recalculate all cells before writing the result."),
+ N_("Recalculate all cells before writing the result"),
NULL
},
/* ---------------------------------------- */
- /* For now this is for INTERNAL GNUMERIC USE ONLY. */
+ /* For now these are for INTERNAL GNUMERIC USE ONLY. */
{
"export-range", 0,
G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_STRING, &ssconvert_range,
@@ -141,6 +143,13 @@ static const GOptionEntry ssconvert_options [] = {
NULL
},
+ {
+ "solve", 0,
+ 0, G_OPTION_ARG_NONE, &ssconvert_solve,
+ N_("Run the solver"),
+ NULL
+ },
+
/* ---------------------------------------- */
{ NULL }
@@ -411,6 +420,69 @@ merge (Workbook *wb, char const *inputs[],
return result;
}
+static void
+run_solver (Sheet *sheet, WorkbookView *wbv)
+{
+ GnmSolverParameters *params = sheet->solver_parameters;
+ GError *err = NULL;
+ WorkbookControl *wbc;
+ GnmSolver *sol = NULL;
+
+ wbc = g_object_new (WORKBOOK_CONTROL_TYPE, NULL);
+ wb_control_set_view (wbc, wbv, NULL);
+
+ /* Pick a functional algorithm. */
+ if (!gnm_solver_factory_functional (params->options.algorithm)) {
+ GSList *l;
+ for (l = gnm_solver_db_get (); l; l = l->next) {
+ GnmSolverFactory *factory = l->data;
+ if (params->options.model_type != factory->type)
+ continue;
+ if (gnm_solver_factory_functional (factory)) {
+ gnm_solver_param_set_algorithm (params,
+ factory);
+ break;
+ }
+ }
+ }
+
+ if (!gnm_solver_param_valid (params, &err))
+ goto done;
+
+ sol = params->options.algorithm
+ ? gnm_solver_factory_create (params->options.algorithm, params)
+ : NULL;
+ if (!sol) {
+ g_set_error (&err, go_error_invalid (), 0,
+ _("Failed to create solver"));
+ goto done;
+ }
+
+ if (!gnm_solver_start (sol, wbc, &err))
+ goto done;
+
+ while (!gnm_solver_finished (sol)) {
+ g_main_context_iteration (NULL, TRUE);
+ }
+
+ if (sol->status != GNM_SOLVER_STATUS_DONE) {
+ g_set_error (&err, go_error_invalid (), 0,
+ _("Solver ran, but failed"));
+ goto done;
+ }
+
+ gnm_solver_store_result (sol);
+
+ done:
+ if (sol)
+ g_object_unref (sol);
+ if (err) {
+ g_printerr (_("Solver: %s\n"), err->message);
+ g_error_free (err);
+ }
+}
+
+
static int
convert (char const *inarg, char const *outarg, char const *mergeargs[],
GOCmdContext *cc)
@@ -513,6 +585,9 @@ convert (char const *inarg, char const *outarg, char const *mergeargs[],
}
}
+ if (ssconvert_solve)
+ run_solver (sheet, wbv);
+
if (ssconvert_recalc)
workbook_recalc_all (wb);
else
diff --git a/test/t7100-solver-blend.pl b/test/t7100-solver-blend.pl
new file mode 100755
index 0000000..a1c9f77
--- /dev/null
+++ b/test/t7100-solver-blend.pl
@@ -0,0 +1,16 @@
+#!/usr/bin/perl -w
+# -----------------------------------------------------------------------------
+
+use strict;
+use lib ($0 =~ m|^(.*/)| ? $1 : ".");
+use GnumericTest;
+
+my $file = "blend.mps";
+my $answer = -30.81221619004;
+&message ("Check solver on $file problem.");
+&test_sheet_calc ("$samples/solver/$file", ['--solve'], "B5",
+ sub {
+ chomp;
+ return (/^[-+]?(\d|\.\d)/ &&
+ abs ($answer - $_) < 1e-6);
+ });
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]