From d6b2be1b05d0ab5bca7250e739bb4be88a457b51 Mon Sep 17 00:00:00 2001 From: Ruben Dashyan Date: Thu, 27 Feb 2020 11:25:10 +0100 Subject: [PATCH] [poincare/test/function_solver] Build Expressions by parsing text --- poincare/test/function_solver.cpp | 83 +++++++++---------------------- 1 file changed, 24 insertions(+), 59 deletions(-) diff --git a/poincare/test/function_solver.cpp b/poincare/test/function_solver.cpp index 104f9ba99..956484273 100644 --- a/poincare/test/function_solver.cpp +++ b/poincare/test/function_solver.cpp @@ -1,5 +1,4 @@ #include -#include #include "helper.h" using namespace Poincare; @@ -25,7 +24,7 @@ void assert_next_extrema_are( ExtremumType extremumType, int numberOfExtrema, Coordinate2D * extrema, - Expression e, + const char * expression, const char * symbol, double start = -1.0, double step = 0.1, @@ -34,6 +33,7 @@ void assert_next_extrema_are( Preferences::AngleUnit angleUnit = Preferences::AngleUnit::Degree) { Shared::GlobalContext context; + Poincare::Expression e = parse_expression(expression, &context, false); double currentStart = start; for (int i = 0; i < numberOfExtrema; i++) { quiz_assert_log_if_failure(!std::isnan(currentStart), e); @@ -54,133 +54,107 @@ void assert_next_extrema_are( } QUIZ_CASE(poincare_function_extremum) { - const char * symbol = "a"; - int symbolLength = strlen(symbol); { - // cos - Expression e = Cosine::Builder(Symbol::Builder(symbol, symbolLength)); { constexpr int numberOfMaxima = 3; Coordinate2D maxima[numberOfMaxima] = { Coordinate2D(0.0, 1.0), Coordinate2D(360.0, 1.0), Coordinate2D(NAN, NAN)}; - assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, e, symbol, -1.0, 0.1, 500.0); + assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, "cos(a)", "a", -1.0, 0.1, 500.0); } { constexpr int numberOfMinima = 1; Coordinate2D minima[numberOfMinima] = { Coordinate2D(180.0, -1.0)}; - assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, e, symbol, 0.0, 0.1, 300.0); + assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, "cos(a)", "a", 0.0, 0.1, 300.0); } } { - // x^2 - Expression e = Power::Builder(Symbol::Builder(symbol, symbolLength), Rational::Builder(2)); { constexpr int numberOfMaxima = 1; Coordinate2D maxima[numberOfMaxima] = { Coordinate2D(NAN, NAN)}; - assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, e, symbol); + assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, "a^2", "a"); } { constexpr int numberOfMinima = 1; Coordinate2D minima[numberOfMinima] = { Coordinate2D(0.0, 0.0)}; - assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, e, symbol); + assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, "a^2", "a"); } } - { - // 3 - Expression e = Rational::Builder(3); { constexpr int numberOfMaxima = 1; Coordinate2D maxima[numberOfMaxima] = { Coordinate2D(NAN, 3.0)}; - assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, e, symbol); + assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, "3", "a"); } { constexpr int numberOfMinima = 1; Coordinate2D minima[numberOfMinima] = { Coordinate2D(NAN, 3.0)}; - assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, e, symbol); + assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, "3", "a"); } } - { - // 0 - Expression e = Rational::Builder(0); { constexpr int numberOfMaxima = 1; Coordinate2D maxima[numberOfMaxima] = { Coordinate2D(NAN, 0.0)}; - assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, e, symbol); + assert_next_extrema_are(ExtremumType::Maximum, numberOfMaxima, maxima, "0", "a"); } { constexpr int numberOfMinima = 1; Coordinate2D minima[numberOfMinima] = { Coordinate2D(NAN, 0.0)}; - assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, e, symbol); + assert_next_extrema_are(ExtremumType::Minimum, numberOfMinima, minima, "0", "a"); } } } QUIZ_CASE(poincare_function_root) { - const char * symbol = "a"; - int symbolLength = strlen(symbol); { - // cos - Expression e = Cosine::Builder(Symbol::Builder(symbol, symbolLength)); constexpr int numberOfRoots = 3; Coordinate2D roots[numberOfRoots] = { Coordinate2D(90.0, 0.0), Coordinate2D(270.0, 0.0), Coordinate2D(450.0, 0.0)}; - assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, e, symbol, 0.0, 0.1, 500.0); + assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, "cos(a)", "a", 0.0, 0.1, 500.0); } { - // x^2 - Expression e = Power::Builder(Symbol::Builder(symbol, symbolLength), Rational::Builder(2)); constexpr int numberOfRoots = 1; Coordinate2D roots[numberOfRoots] = { Coordinate2D(0.0, 0.0)}; - assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, e, symbol); + assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, "a^2", "a"); } { - // x^2-4 - Expression e = Subtraction::Builder(Power::Builder(Symbol::Builder(symbol, symbolLength), Rational::Builder(2)), Rational::Builder(4)); constexpr int numberOfRoots = 2; Coordinate2D roots[numberOfRoots] = { Coordinate2D(-2.0, 0.0), Coordinate2D(2.0, 0.0)}; - assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, e, symbol, -5.0); + assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, "a^2-4", "a", -5.0); } { - // 3 - Expression e = Rational::Builder(3); constexpr int numberOfRoots = 1; Coordinate2D roots[numberOfRoots] = { Coordinate2D(NAN, 0.0)}; - assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, e, symbol); + assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, "3", "a"); } - { - // 0 - Expression e = Rational::Builder(0); constexpr int numberOfRoots = 1; Coordinate2D roots[numberOfRoots] = { Coordinate2D(-0.9, 0.0)}; - assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, e, symbol); + assert_next_extrema_are(ExtremumType::Root, numberOfRoots, roots, "0", "a"); } - } void assert_next_intersections_are( - Expression otherExpression, + const char * otherExpression, int numberOfIntersections, Coordinate2D * intersections, - Expression e, + const char * expression, const char * symbol, double start = -1.0, double step = 0.1, @@ -189,10 +163,12 @@ void assert_next_intersections_are( Preferences::AngleUnit angleUnit = Preferences::AngleUnit::Degree) { Shared::GlobalContext context; + Poincare::Expression e = parse_expression(expression, &context, false); + Poincare::Expression other = parse_expression(otherExpression, &context, false); double currentStart = start; for (int i = 0; i < numberOfIntersections; i++) { quiz_assert_log_if_failure(!std::isnan(currentStart), e); - Coordinate2D nextIntersection = e.nextIntersection(symbol, currentStart, step, max, &context, complexFormat, angleUnit, otherExpression); + Coordinate2D nextIntersection = e.nextIntersection(symbol, currentStart, step, max, &context, complexFormat, angleUnit, other); currentStart = nextIntersection.x1() + step; quiz_assert_log_if_failure( (doubles_are_approximately_equal(intersections[i].x1(), nextIntersection.x1())) @@ -200,38 +176,27 @@ void assert_next_intersections_are( e); } } -QUIZ_CASE(poincare_function_intersection) { - const char * symbol = "a"; - int symbolLength = strlen(symbol); - Expression e = Cosine::Builder(Symbol::Builder(symbol, symbolLength)); +QUIZ_CASE(poincare_function_intersection) { { - // cos with y=2 - Expression otherExpression = Rational::Builder(2); constexpr int numberOfIntersections = 1; Coordinate2D intersections[numberOfIntersections] = { Coordinate2D(NAN, NAN)}; - assert_next_intersections_are(otherExpression, numberOfIntersections, intersections, e, symbol); + assert_next_intersections_are("2", numberOfIntersections, intersections, "cos(a)", "a"); } - { - // cos with y=1 - Expression otherExpression = Rational::Builder(1); constexpr int numberOfIntersections = 2; Coordinate2D intersections[numberOfIntersections] = { Coordinate2D(0.0, 1.0), Coordinate2D(360.0, 1.0)}; - assert_next_intersections_are(otherExpression, numberOfIntersections, intersections, e, symbol); + assert_next_intersections_are("1", numberOfIntersections, intersections, "cos(a)", "a"); } - { - // cos with y=0 - Expression otherExpression = Rational::Builder(0); constexpr int numberOfIntersections = 3; Coordinate2D intersections[numberOfIntersections] = { Coordinate2D(90.0, 0.0), Coordinate2D(270.0, 0.0), Coordinate2D(450.0, 0.0)}; - assert_next_intersections_are(otherExpression, numberOfIntersections, intersections, e, symbol); + assert_next_intersections_are("0", numberOfIntersections, intersections, "cos(a)", "a"); } }