[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented function parsing



commit 4422e60bde0c3292590462f73d18806dad195879
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date:   Fri Jan 4 17:41:55 2019 -0600

    gcalc: implemented function parsing

 gcalc/gcalc-expression-container.vala |  5 +++
 gcalc/gcalc-function-acos.vala        |  4 +-
 gcalc/gcalc-function-acosh.vala       |  4 +-
 gcalc/gcalc-function-asin.vala        |  4 +-
 gcalc/gcalc-function-asinh.vala       |  4 +-
 gcalc/gcalc-function-atan.vala        |  4 +-
 gcalc/gcalc-function-atanh.vala       |  4 +-
 gcalc/gcalc-function-cos.vala         |  4 +-
 gcalc/gcalc-function-cosh.vala        |  4 +-
 gcalc/gcalc-function-exp.vala         |  4 +-
 gcalc/gcalc-function-log.vala         |  4 +-
 gcalc/gcalc-function-sin.vala         |  4 +-
 gcalc/gcalc-function-sinh.vala        |  4 +-
 gcalc/gcalc-function-sqrt.vala        |  4 +-
 gcalc/gcalc-function-tan.vala         |  4 +-
 gcalc/gcalc-function-tanh.vala        |  4 +-
 gcalc/gcalc-function.vala             |  8 ++--
 gcalc/gcalc-gfunction.vala            |  5 +--
 gcalc/gcalc-parser.vala               | 36 ++++++++++-----
 tests/gcalc-parsing.vala              | 51 +++++++++++++++++++++
 tests/gcalc-solving-basic.vala        | 84 ++++++++++++++++++++++-------------
 21 files changed, 172 insertions(+), 77 deletions(-)
---
diff --git a/gcalc/gcalc-expression-container.vala b/gcalc/gcalc-expression-container.vala
index a948639e..2521b389 100644
--- a/gcalc/gcalc-expression-container.vala
+++ b/gcalc/gcalc-expression-container.vala
@@ -65,6 +65,11 @@ public class GCalc.ExpressionContainer : Gee.ArrayList<Expression>, GLib.ListMod
           return e;
         }
       }
+      if (e is Function) {
+        if ((e as Function).name == name) {
+          return e;
+        }
+      }
     }
     return null;
   }
diff --git a/gcalc/gcalc-function-acos.vala b/gcalc/gcalc-function-acos.vala
index f4e47027..ba62aee0 100644
--- a/gcalc/gcalc-function-acos.vala
+++ b/gcalc/gcalc-function-acos.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionAcos : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-acosh.vala b/gcalc/gcalc-function-acosh.vala
index 4e479c2a..2e78bd8f 100644
--- a/gcalc/gcalc-function-acosh.vala
+++ b/gcalc/gcalc-function-acosh.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionAcosh : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-asin.vala b/gcalc/gcalc-function-asin.vala
index 911a9785..74f8126a 100644
--- a/gcalc/gcalc-function-asin.vala
+++ b/gcalc/gcalc-function-asin.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionAsin : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-asinh.vala b/gcalc/gcalc-function-asinh.vala
index 798427ca..f98d2482 100644
--- a/gcalc/gcalc-function-asinh.vala
+++ b/gcalc/gcalc-function-asinh.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionAsinh : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-atan.vala b/gcalc/gcalc-function-atan.vala
index f5e212d1..f9f63f35 100644
--- a/gcalc/gcalc-function-atan.vala
+++ b/gcalc/gcalc-function-atan.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionAtan : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-atanh.vala b/gcalc/gcalc-function-atanh.vala
index d263ace7..d57b0587 100644
--- a/gcalc/gcalc-function-atanh.vala
+++ b/gcalc/gcalc-function-atanh.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionAtanh : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-cos.vala b/gcalc/gcalc-function-cos.vala
index ff11424d..27a5e200 100644
--- a/gcalc/gcalc-function-cos.vala
+++ b/gcalc/gcalc-function-cos.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionCos : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-cosh.vala b/gcalc/gcalc-function-cosh.vala
index 0b7f2027..f4671e0c 100644
--- a/gcalc/gcalc-function-cosh.vala
+++ b/gcalc/gcalc-function-cosh.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionCosh : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-exp.vala b/gcalc/gcalc-function-exp.vala
index f9add0c4..c0afba43 100644
--- a/gcalc/gcalc-function-exp.vala
+++ b/gcalc/gcalc-function-exp.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionExp : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-log.vala b/gcalc/gcalc-function-log.vala
index 7172680e..bf8d7e0e 100644
--- a/gcalc/gcalc-function-log.vala
+++ b/gcalc/gcalc-function-log.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionLog : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-sin.vala b/gcalc/gcalc-function-sin.vala
index eeffc54f..f0b694b3 100644
--- a/gcalc/gcalc-function-sin.vala
+++ b/gcalc/gcalc-function-sin.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionSin : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-sinh.vala b/gcalc/gcalc-function-sinh.vala
index fd9dc28b..f7fe97ff 100644
--- a/gcalc/gcalc-function-sinh.vala
+++ b/gcalc/gcalc-function-sinh.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionSinh : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-sqrt.vala b/gcalc/gcalc-function-sqrt.vala
index f53dab28..1777b5c0 100644
--- a/gcalc/gcalc-function-sqrt.vala
+++ b/gcalc/gcalc-function-sqrt.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionSqrt : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-tan.vala b/gcalc/gcalc-function-tan.vala
index e29f700e..ca62ca0e 100644
--- a/gcalc/gcalc-function-tan.vala
+++ b/gcalc/gcalc-function-tan.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionTan : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function-tanh.vala b/gcalc/gcalc-function-tanh.vala
index e93a52f1..13a531c1 100644
--- a/gcalc/gcalc-function-tanh.vala
+++ b/gcalc/gcalc-function-tanh.vala
@@ -25,11 +25,11 @@ public class GCalc.GFunctionTanh : GFunction {
     param_types.add (new GConstant ());
   }
 
-  public override Expression call () throws GLib.Error
+  public override Expression evaluate () throws GLib.Error
   {
     verify_params ();
     GConstant c = null;
-    var exp = parameters.get_item (0) as Expression;
+    var exp = expressions.get_item (0) as Expression;
     if (exp == null) {
       throw new FunctionError.INVOCATION_ERROR ("Invalid parameter type. Expected %s", 
typeof(Expression).name ());
     }
diff --git a/gcalc/gcalc-function.vala b/gcalc/gcalc-function.vala
index 4ca01b92..9c6f9f8a 100644
--- a/gcalc/gcalc-function.vala
+++ b/gcalc/gcalc-function.vala
@@ -19,15 +19,15 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 public interface GCalc.Function : Object, Expression {
-  public abstract ExpressionContainer parameters { get; }
   public abstract ExpressionContainer param_types { get; }
   public abstract string name { get; construct set; }
   public abstract uint n_params { get; construct set; }
-  public abstract Expression call () throws GLib.Error;
+  public abstract bool closed { get; set; }
+  public abstract Expression evaluate () throws GLib.Error;
   public virtual bool verify_params () throws GLib.Error {
-    if (parameters.get_n_items () != n_params) {
+    if (expressions.get_n_items () != n_params) {
       throw new FunctionError.INVALID_PARAMETERS_ERROR ("Invalid number of parameters. Required %u, 
provided: %u",
-                                                  n_params, parameters.get_n_items ());
+                                                  n_params, expressions.get_n_items ());
     }
     return true;
   }
diff --git a/gcalc/gcalc-gfunction.vala b/gcalc/gcalc-gfunction.vala
index 2cda8938..34eabbd1 100644
--- a/gcalc/gcalc-gfunction.vala
+++ b/gcalc/gcalc-gfunction.vala
@@ -19,13 +19,12 @@
  *      Daniel Espinosa <esodan gmail com>
  */
 public class GCalc.GFunction : GExpression, Function {
-  ExpressionContainer _parameters = new ExpressionContainer ();
   ExpressionContainer _param_types = new ExpressionContainer ();
 
-  public ExpressionContainer parameters { get { return _parameters; } }
   public ExpressionContainer param_types { get { return _param_types; } }
   public uint n_params { get; construct set; }
   public string name { get; construct set; }
+  public bool closed { get; set; }
 
   construct {
     name = "NoName";
@@ -38,7 +37,7 @@ public class GCalc.GFunction : GExpression, Function {
     return name + "()";
   }
 
-  public new virtual Expression call () throws GLib.Error {
+  public new virtual Expression evaluate () throws GLib.Error {
     return new GErrorExpression ();
   }
 }
diff --git a/gcalc/gcalc-parser.vala b/gcalc/gcalc-parser.vala
index 89c76d4f..2e82a72c 100644
--- a/gcalc/gcalc-parser.vala
+++ b/gcalc/gcalc-parser.vala
@@ -129,15 +129,20 @@ public class GCalc.Parser : Object {
         case Vala.TokenType.IDENTIFIER:
           var sfunc = eqman.functions.find_named (n);
           if (sfunc != null) {
-            current = sfunc;
-            expected.clear ();
-            expected.add(Vala.TokenType.OPEN_PARENS);
+            if (current == null) {
+              var exp = new GPolynomial ();
+              eq.expressions.add (exp);
+              var t = new GTerm ();
+              exp.expressions.add (t);
+              t.expressions.add (sfunc);
+              current = sfunc;
+              current_parent = t;
+              top_parent = exp;
+              expected.clear ();
+              expected.add(Vala.TokenType.OPEN_PARENS);
+            }
           } else if (n.down () == "def" && current == null) {
-            var f = new GFunction (n, 1) as Expression; // FIXME: Requires more work to identify parameters
-            eqman.functions.add (f);
-            current = f;
-            expected.clear ();
-            expected.add(Vala.TokenType.OPEN_PARENS);
+            // FIXME: implement function definition
           } else if (n.down () == "def" && current is Function) {
             throw new ParserError.INVALID_TOKEN_ERROR ("Found an unexpected function definition expression");
           } else {
@@ -175,7 +180,7 @@ public class GCalc.Parser : Object {
             current_parent.expressions.add (cexp);
             expected.clear ();
             current = cexp;
-          } else if (current is Term && current_parent is Polynomial && top_parent is Group) {
+          } else if (current is Term && current_parent is Polynomial && (top_parent is Group || top_parent 
is Function)) {
             current.expressions.add (cexp);
             top_parent = current_parent;
             current_parent = current;
@@ -239,8 +244,14 @@ public class GCalc.Parser : Object {
             current_parent = exp2;
             top_parent = g;
           } else if (current is Function) {
+            message ("Function Open parens");
             var fexp = new GPolynomial ();
-            current = fexp;
+            var t = new GTerm ();
+            fexp.expressions.add (t);
+            current.expressions.add (fexp);
+            top_parent = current;
+            current = t;
+            current_parent = fexp;
             expected.clear ();
           } else if (current is Operator && current_parent is Term && top_parent is Polynomial) {
             var g = new GGroup ();
@@ -266,6 +277,11 @@ public class GCalc.Parser : Object {
               ((Group) par).closed = true;
               break;
             }
+            if (par is Function) {
+              foundp = true;
+              ((Function) par).closed = true;
+              break;
+            }
             par = par.parent;
           }
           if (foundp) {
diff --git a/tests/gcalc-parsing.vala b/tests/gcalc-parsing.vala
index 1e83e42a..1631f2d2 100644
--- a/tests/gcalc-parsing.vala
+++ b/tests/gcalc-parsing.vala
@@ -706,6 +706,57 @@ class Tests {
         warning ("Error: %s", error.message);
       }
     });
+    Test.add_func ("/gcalc/parser/function/defaults",
+    ()=>{
+      var eqman = new GMathEquationManager ();
+      assert (eqman.functions.get_n_items () > 0);
+      assert (eqman.functions.find_named ("sin") != null);
+      assert (eqman.functions.find_named ("cos") != null);
+      assert (eqman.functions.find_named ("tan") != null);
+      assert (eqman.functions.find_named ("asin") != null);
+      assert (eqman.functions.find_named ("acos") != null);
+      assert (eqman.functions.find_named ("atan") != null);
+      assert (eqman.functions.find_named ("sinh") != null);
+      assert (eqman.functions.find_named ("cosh") != null);
+      assert (eqman.functions.find_named ("tanh") != null);
+      assert (eqman.functions.find_named ("asinh") != null);
+      assert (eqman.functions.find_named ("acosh") != null);
+      assert (eqman.functions.find_named ("atanh") != null);
+      assert (eqman.functions.find_named ("exp") != null);
+      assert (eqman.functions.find_named ("log") != null);
+      assert (eqman.functions.find_named ("sqrt") != null);
+    });
+    Test.add_func ("/gcalc/parser/function/unique",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("sin(0)", eqman);
+        assert (eqman.equations.get_n_items () == 1);
+        var eq = eqman.equations.get_item (0) as MathEquation;
+        assert (eq != null);
+        assert (eq.expressions.get_n_items () == 1);
+        var p = eq.expressions.get_item (0) as Polynomial;
+        assert (p != null);
+        assert (p.expressions.get_n_items () == 1);
+        var t = p.expressions.get_item (0) as Term;
+        assert (t != null);
+        assert (t.expressions.get_n_items () == 1);
+        var f = t.expressions.get_item (0) as Function;
+        assert (f != null);
+        assert (f.expressions.get_n_items () == 1);
+        var p1 = f.expressions.get_item (0) as Polynomial;
+        assert (p1 != null);
+        var t1 =p1.expressions.get_item (0) as Term;
+        assert (t1 != null);
+        message ("Terms: %u", t1.expressions.get_n_items ());
+        assert (t1.expressions.get_n_items () == 1);
+        var c = t1.expressions.get_item (0) as Constant;
+        assert (c != null);
+      } catch (GLib.Error error) {
+        warning ("Error: %s", error.message);
+      }
+    });
     return Test.run ();
   }
 }
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 7fb175fc..e693d712 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -471,8 +471,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (9.0);
         var f = new GFunctionSqrt ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 3.0);
@@ -485,8 +485,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionExp ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 1.0);
@@ -499,8 +499,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (1.0);
         var f = new GFunctionLog ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -513,8 +513,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionSin ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -527,8 +527,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionCos ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 1.0);
@@ -541,8 +541,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionTan ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -555,8 +555,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionAsin ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -569,8 +569,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (1.0);
         var f = new GFunctionAcos ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -583,8 +583,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionAtan ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -597,8 +597,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionSinh ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -611,8 +611,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionCosh ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 1.0);
@@ -625,8 +625,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionTanh ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -639,8 +639,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionAsinh ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -653,8 +653,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (1.0);
         var f = new GFunctionAcosh ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -667,8 +667,8 @@ class Tests {
       try {
         var c1 = new GConstant.@double (0.0);
         var f = new GFunctionAtanh ();
-        f.parameters.add (c1);
-        var c2 = f.call () as Constant;
+        f.expressions.add (c1);
+        var c2 = f.evaluate () as Constant;
         assert (c2 != null);
         message (c2.to_string ());
         assert (c2.real () == 0.0);
@@ -676,6 +676,30 @@ class Tests {
         warning ("Error: %s", e.message);
       }
     });
+    Test.add_func ("/gcalc/solve/function/unique",
+    ()=>{
+      try {
+        var parser = new Parser ();
+        var eqman = new GMathEquationManager ();
+        parser.parse ("sin(0)", 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;
+        assert (t != null);
+        var f = t.expressions.get_item (0) as Function;
+        assert (f != null);
+        assert (f.closed);
+        var res = f.evaluate () as Constant;
+        assert (res != null);
+        message ("Constant Result: %s", res.to_string ());
+        assert (res.real () == 0.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]