[gnome-calculator/60-split-out-a-backend-library] gcalc: terms evaluates multiply of constants



commit 9c6cef978ef7aef387fd87f7a96a4aed45f15934
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Thu Jan 3 18:10:58 2019 -0600

    gcalc: terms evaluates multiply of constants

 gcalc/gcalc-term.vala          |  6 ++++--
 tests/gcalc-solving-basic.vala | 25 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 2 deletions(-)
---
diff --git a/gcalc/gcalc-term.vala b/gcalc/gcalc-term.vala
index 25e517ec..b9f4eeee 100644
--- a/gcalc/gcalc-term.vala
+++ b/gcalc/gcalc-term.vala
@@ -35,7 +35,9 @@ public interface GCalc.Term : Object, Expression {
           throw new TermError.INVALID_OPERATOR ("Incorrect position for operator in expression");
         }
         if (e is Minus && first) {
-          current = new GConstant.@double (-1.0);
+          var c = new GConstant.@double (1.0);
+          c = c.neg () as GConstant;
+          current = c;
           first = false;
         }
         current_operator = e as Operator;
@@ -45,7 +47,7 @@ public interface GCalc.Term : Object, Expression {
           first = false;
         } else if (current is Constant) {
           if (current_operator != null) {
-            if (e is Minus && e is Multiply) {
+            if (current_operator is Multiply) {
               current = (current as Constant).multiply (e as Constant);
             }
           }
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 9de1c330..90b0acfe 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -127,6 +127,31 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
+    Test.add_func ("/gcalc/solve/term/constant/multiply",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("3*5", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        var e = eq.expressions.get_item (0) as Polynomial;
+        assert (e != null);
+        var t = e.expressions.get_item (0) as Term;
+        var res = t.solve ();
+        assert (res != null);
+        assert (res.expression != null);
+        message ("Result type: %s", res.expression.get_type ().name ());
+        assert (res.is_valid);
+        var rc = res.expression as Constant;
+        assert (rc != null);
+        message ("Constant Result: %s", rc.to_string ());
+        assert (rc.real () == 15.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]