From 48ccf0fb245ec5a59a517e4c714a6f3895c4b753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 6 Feb 2017 10:34:44 +0100 Subject: [PATCH 1/2] [poincare] Add tests for the parser Change-Id: If8d971343a63b4a337fbdf78607ee813bba1f9e7 --- poincare/Makefile | 1 + poincare/test/parser.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+) create mode 100644 poincare/test/parser.cpp diff --git a/poincare/Makefile b/poincare/Makefile index 542dfcdd2..f8b00f669 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -63,6 +63,7 @@ tests += $(addprefix poincare/test/,\ identity.cpp\ integer.cpp\ matrix.cpp\ + parser.cpp\ product.cpp\ power.cpp\ simplify_utils.cpp\ diff --git a/poincare/test/parser.cpp b/poincare/test/parser.cpp new file mode 100644 index 000000000..0e2ab2afa --- /dev/null +++ b/poincare/test/parser.cpp @@ -0,0 +1,27 @@ +#include +#include +#include +#include + +QUIZ_CASE(poincare_parser) { + GlobalContext globalContext; + Expression * a = Expression::parse("1.2*e^(1)"); + float f =1.2f*M_E; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("e^2*e^(1)"); + f = powf(M_E, 2.0f)*M_E; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("2*3^4+2"); + f = 2.0f*powf(3.0f, 4.0f)+2.0f; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("-2*3^4+2"); + f = -2.0f*powf(3.0f, 4.0f)+2.0f; + assert(a->approximate(globalContext) == f); + + a = Expression::parse("-sin(3)*2-3"); + f = -sinf(3.0f)*2.0f-3.0f; + assert(a->approximate(globalContext) == f); +} \ No newline at end of file From 855a5cf66631ec3437827c430c23ecd2ff2f6b96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 6 Feb 2017 10:41:54 +0100 Subject: [PATCH 2/2] [poincare] Parse ".004" Change-Id: Iea9c7cb82b3f152053193ef5e6fc4c7ee2efc459 --- poincare/src/expression_parser.y | 3 +++ poincare/test/parser.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) diff --git a/poincare/src/expression_parser.y b/poincare/src/expression_parser.y index 5fe0c1a6e..70077bbb2 100644 --- a/poincare/src/expression_parser.y +++ b/poincare/src/expression_parser.y @@ -112,8 +112,11 @@ mtxData: number: DIGITS { $$ = new Integer($1.address, false); } + | DOT DIGITS { $$ = new Complex(nullptr, 0, false, $2.address, $2.length, nullptr, 0, false); } | DIGITS DOT DIGITS { $$ = new Complex($1.address, $1.length, false, $3.address, $3.length, nullptr, 0, false); } + | DOT DIGITS EE DIGITS { $$ = new Complex(nullptr, 0, false, $2.address, $2.length, $4.address, $4.length, false); } | DIGITS DOT DIGITS EE DIGITS { $$ = new Complex($1.address, $1.length, false, $3.address, $3.length, $5.address, $5.length, false); } + | DOT DIGITS EE MINUS DIGITS { $$ = new Complex(nullptr, 0, false, $2.address, $2.length, $5.address, $5.length, true); } | DIGITS DOT DIGITS EE MINUS DIGITS { $$ = new Complex($1.address, $1.length, false, $3.address, $3.length, $6.address, $6.length, true); } exp: diff --git a/poincare/test/parser.cpp b/poincare/test/parser.cpp index 0e2ab2afa..4d6eed4b6 100644 --- a/poincare/test/parser.cpp +++ b/poincare/test/parser.cpp @@ -24,4 +24,13 @@ QUIZ_CASE(poincare_parser) { a = Expression::parse("-sin(3)*2-3"); f = -sinf(3.0f)*2.0f-3.0f; assert(a->approximate(globalContext) == f); + + a = Expression::parse("-.003"); + f = -0.003f; + assert(a->approximate(globalContext) == f); + + char text[10] = {'.', '0', '2', 'E', '2', 0}; + a = Expression::parse(text); + f = 2.0f; + assert(a->approximate(globalContext) == f); } \ No newline at end of file