[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented equation solving



commit 064662c4ad58f3ca28ab4d9ea435a28db0766097
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Sun Jan 6 00:07:10 2019 -0600

    gcalc: implemented equation solving

 gcalc/gcalc-gassign.vala        | 10 ++++++++++
 gcalc/gcalc-gmath-equation.vala |  9 ++++++++-
 tests/gcalc-solving-basic.vala  | 23 +++++++++++++++++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)
---
diff --git a/gcalc/gcalc-gassign.vala b/gcalc/gcalc-gassign.vala
index a11068de..4ecf7252 100644
--- a/gcalc/gcalc-gassign.vala
+++ b/gcalc/gcalc-gassign.vala
@@ -22,5 +22,15 @@ public class GCalc.GAssign : GExpression, Operator, BinaryOperator, Assign {
   public override string to_string () {
     return "=";
   }
+  public override Result solve () {
+    Result res = null;
+    try {
+      res = new GResult (evaluate ());
+    } catch (GLib.Error e) {
+      var err = new GErrorResult ("Invalid expression");
+      return new GResult.with_error (new GErrorExpression (), err as ErrorResult);
+    }
+    return res;
+  }
 }
 
diff --git a/gcalc/gcalc-gmath-equation.vala b/gcalc/gcalc-gmath-equation.vala
index 3f03734d..bfa234ee 100644
--- a/gcalc/gcalc-gmath-equation.vala
+++ b/gcalc/gcalc-gmath-equation.vala
@@ -22,12 +22,19 @@ public class GCalc.GMathEquation : GExpression, MathEquation {
   ExpressionHashMap _variables = new ExpressionHashMap ();
   public ExpressionHashMap variables { get { return _variables; } }
   public override Result solve () {
+    Result res = null;
     if (expressions.get_n_items () == 0) {
       var err = new GErrorResult ("No expressions found in equation");
       return new GResult.with_error ((Expression) new GExpression (), (ErrorResult) err) as Result;
     }
     var e = expressions.get_item (0) as Expression;
-    return e.solve ();
+    if (e == null) {
+      var err = new GErrorResult ("Invalid expression in equation");
+      return new GResult.with_error ((Expression) new GErrorExpression (), (ErrorResult) err) as Result;
+    } else {
+      res = e.solve ();
+    }
+    return res;
   }
 }
 
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 7fd57746..5e20907d 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -1082,6 +1082,29 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
+    Test.add_func ("/gcalc/solve/equation/solve/variable",
+    ()=>{
+      try {
+        var parser = new GParser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("x=3", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        var res = eq.solve ();
+        if (res is ErrorResult) {
+          warning ("Error: %s", res.error.to_string ());
+        }
+        assert (res.expression != null);
+        assert (res.expression is Constant);
+        message ("Result: %s", res.expression.to_string ());
+        var c = res.expression as Constant;
+        assert (c != null);
+        assert (c.real () == 3.0);
+      } catch (GLib.Error e) {
+        warning ("Error: %s", e.message);
+      }
+    });
     return Test.run ();
   }
 }


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