[gnome-calculator/60-split-out-a-backend-library] gcalc: implemented group solving on constants
- From: Daniel Espinosa Ortiz <despinosa src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-calculator/60-split-out-a-backend-library] gcalc: implemented group solving on constants
- Date: Fri, 4 Jan 2019 20:16:17 +0000 (UTC)
commit 9d4c2360330f4e08b99a1e00df036d58aa676a0b
Author: Daniel Espinosa Ortiz <esodan gmail com>
Date: Fri Jan 4 12:28:21 2019 -0600
gcalc: implemented group solving on constants
gcalc/gcalc-group.vala | 19 ++++++++++--
tests/gcalc-solving-basic.vala | 69 ++++++++++++++++++++++++++++++++++++++++++
2 files changed, 86 insertions(+), 2 deletions(-)
---
diff --git a/gcalc/gcalc-group.vala b/gcalc/gcalc-group.vala
index c56c98b8..cfe4c994 100644
--- a/gcalc/gcalc-group.vala
+++ b/gcalc/gcalc-group.vala
@@ -19,12 +19,27 @@
* Daniel Espinosa <esodan gmail com>
*/
public interface GCalc.Group : Object, Expression {
- public abstract Level level { get; set; }
- public abstract bool closed { get; set; }
public enum Level {
ONE,
TWO,
THREE
}
+ public abstract Level level { get; set; }
+ public abstract bool closed { get; set; }
+ public virtual Expression evaluate () throws GLib.Error {
+ if (expressions.get_n_items () == 0) {
+ throw new GroupError.INVALID_POLYNOMIAL ("No internal polynomial in group");
+ }
+ var e = expressions.get_item (0) as Polynomial;
+ if (e == null) {
+ throw new GroupError.INVALID_POLYNOMIAL ("Invalid internal polynomial in group");
+ }
+ return e.evaluate ();
+ }
+}
+
+public errordomain GCalc.GroupError {
+ INVALID_POLYNOMIAL,
+ INVALID_INTERNAL_TERM,
}
diff --git a/tests/gcalc-solving-basic.vala b/tests/gcalc-solving-basic.vala
index 85ab7f14..a8e38410 100644
--- a/tests/gcalc-solving-basic.vala
+++ b/tests/gcalc-solving-basic.vala
@@ -245,6 +245,75 @@ class Tests {
warning ("Error: %s", e.message);
}
});
+ Test.add_func ("/gcalc/solve/group/constant",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("(1)", 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 g = t.expressions.get_item (0) as Group;
+ assert (g != null);
+ var res = g.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/group/constant/basic-polynomial",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("(1+2)", 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 g = t.expressions.get_item (0) as Group;
+ assert (g != null);
+ var res = g.evaluate () as Constant;
+ assert (res != null);
+ message ("Constant Result: %s", res.to_string ());
+ assert (res.real () == 3.0);
+ } catch (GLib.Error e) {
+ warning ("Error: %s", e.message);
+ }
+ });
+ Test.add_func ("/gcalc/solve/group/constant/polynomial",
+ ()=>{
+ try {
+ var parser = new Parser ();
+ var eqman = new GMathEquationManager ();
+ parser.parse ("(2*8-10/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;
+ assert (t != null);
+ var g = t.expressions.get_item (0) as Group;
+ assert (g != null);
+ var res = g.evaluate () as Constant;
+ assert (res != null);
+ message ("Constant Result: %s", res.to_string ());
+ assert (res.real () == 14.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]