[gnome-calculator/60-split-out-a-backend-library] gcalc: fix parsing grouped functions



commit 3a3fe75ada9e0634066e0363ea11c29ca89c7d0c
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Fri Jan 4 20:32:47 2019 -0600

    gcalc: fix parsing grouped functions

 gcalc/gcalc-parser.vala        |  6 ++++++
 tests/gcalc-solving-basic.vala | 42 +++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 47 insertions(+), 1 deletion(-)
---
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 9e8685b5..55f0050f 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -145,6 +145,12 @@ public class GCalc.Parser : Object {
                 current_parent.expressions.add (sfunc);
                 current = sfunc;
                 expected.clear ();
+            } else if (current is Term && current_parent is Polynomial) {
+                current.expressions.add (sfunc);
+                current_parent = current;
+                current = sfunc;
+                top_parent = current_parent.parent;
+                expected.clear ();
             }
           } else if (n.down () == "def" && current == null) {
             // FIXME: implement function definition
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 8c5aa34b..f067e0e9 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -890,7 +890,7 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
-    Test.add_func ("/gcalc/solve/function/polynomial/complex",
+    Test.add_func ("/gcalc/solve/function/polynomial/complex1",
     ()=>{
       try {
         var parser = new Parser ();
@@ -910,6 +910,46 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
+    Test.add_func ("/gcalc/solve/function/polynomial/group",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("(cos(0))", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        message ("Equation: %s", eq.to_string ());
+        var e = eq.expressions.get_item (0) as Polynomial;
+        assert (e != null);
+        var res = e.evaluate () as Constant;
+        assert (res != null);
+        message ("Constant Result: %s", res.to_string ());
+        assert (res.real () == 1.0);
+      } catch (GLib.Error e) {
+        warning ("Error: %s", e.message);
+      }
+    });
+    Test.add_func ("/gcalc/solve/function/polynomial/complex2",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("5*6+tan(0)+tan(0)/2*8+2*cos(0)-9/cos(0)+(1/(cos(0)+cos(0))", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        message ("Equation: %s", eq.to_string ());
+        var e = eq.expressions.get_item (0) as Polynomial;
+        assert (e != null);
+        var res = e.evaluate () as Constant;
+        assert (res != null);
+        message ("Constant Result: %s", res.to_string ());
+        assert (res.real () == 23.5);
+      } 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]