From 379dcdba1a5951396c69653b8205b7198a091b9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 15 May 2018 18:05:32 +0200 Subject: [PATCH] [poincarE] Add unary tests on Layouts. Change-Id: I21f33e84eb4a60ecbabbba99a966abed6a23039c --- poincare/Makefile | 4 ++ poincare/test/binomial_coefficient_layout.cpp | 10 +++ poincare/test/fraction_layout.cpp | 68 +++++++++++++++++++ poincare/test/helper.cpp | 29 ++++++++ poincare/test/helper.h | 6 ++ poincare/test/nth_root_layout.cpp | 10 +++ poincare/test/parentheses_layout.cpp | 32 +++++++++ poincare/test/vertical_offset_layout.cpp | 23 +++++++ 8 files changed, 182 insertions(+) create mode 100644 poincare/test/binomial_coefficient_layout.cpp create mode 100644 poincare/test/fraction_layout.cpp create mode 100644 poincare/test/nth_root_layout.cpp create mode 100644 poincare/test/parentheses_layout.cpp create mode 100644 poincare/test/vertical_offset_layout.cpp diff --git a/poincare/Makefile b/poincare/Makefile index ab2450b03..4ef675962 100644 --- a/poincare/Makefile +++ b/poincare/Makefile @@ -121,16 +121,19 @@ objs += $(addprefix poincare/src/layout/,\ tests += $(addprefix poincare/test/,\ addition.cpp\ arithmetic.cpp\ + binomial_coefficient_layout.cpp\ complex.cpp\ convert_expression_to_text.cpp\ division.cpp\ factorial.cpp\ + fraction_layout.cpp\ function.cpp\ helper.cpp\ integer.cpp\ logarithm.cpp\ matrix.cpp\ multiplication.cpp\ + nth_root_layout.cpp\ parser.cpp\ power.cpp\ properties.cpp\ @@ -140,6 +143,7 @@ tests += $(addprefix poincare/test/,\ symbol.cpp\ store.cpp\ trigo.cpp\ + vertical_offset_layout.cpp\ ) # simplify_utils.cpp\ diff --git a/poincare/test/binomial_coefficient_layout.cpp b/poincare/test/binomial_coefficient_layout.cpp new file mode 100644 index 000000000..c7960c551 --- /dev/null +++ b/poincare/test/binomial_coefficient_layout.cpp @@ -0,0 +1,10 @@ +#include +#include +#include +#include "helper.h" + +using namespace Poincare; + +QUIZ_CASE(poincare_binomial_coefficient_layout_serialize) { + assert_parsed_expression_layout_serialize_to_self("binomial(7,6)"); +} diff --git a/poincare/test/fraction_layout.cpp b/poincare/test/fraction_layout.cpp new file mode 100644 index 000000000..a2e060917 --- /dev/null +++ b/poincare/test/fraction_layout.cpp @@ -0,0 +1,68 @@ +#include +#include +#include +#include +#include +#include "helper.h" + +using namespace Poincare; + +QUIZ_CASE(poincare_fraction_layout_create) { + + /* 12 + * 12|34+5 -> "Divide" -> --- + 5 + * |34 + * */ + HorizontalLayout * layout = static_cast(LayoutEngine::createStringLayout("1234+5", 6)); + ExpressionLayoutCursor cursor(layout->editableChild(2), ExpressionLayoutCursor::Position::Left); + cursor.addFractionLayoutAndCollapseSiblings(); + assert_expression_layout_serialize_to(layout, "12/34+5"); + assert(cursor.isEquivalentTo(ExpressionLayoutCursor(layout->editableChild(0)->editableChild(1), ExpressionLayoutCursor::Position::Left))); + delete layout; +} + +QUIZ_CASE(poincare_fraction_layout_delete) { + + /* 12 + * --- -> "BackSpace" -> 12|34 + * |34 + * */ + HorizontalLayout * layout1 = new HorizontalLayout( + new FractionLayout( + LayoutEngine::createStringLayout("12", 2), + LayoutEngine::createStringLayout("34", 2), + false), + false); + ExpressionLayoutCursor cursor1(layout1->editableChild(0)->editableChild(1), ExpressionLayoutCursor::Position::Left); + cursor1.performBackspace(); + assert_expression_layout_serialize_to(layout1, "1234"); + assert(cursor1.isEquivalentTo(ExpressionLayoutCursor(layout1->editableChild(1), ExpressionLayoutCursor::Position::Right))); + delete layout1; + + /* • + * 1 + --- -> "BackSpace" -> 1+|3 + * |3 + * */ + HorizontalLayout * layout2 = new HorizontalLayout( + new CharLayout('1'), + new CharLayout('+'), + new FractionLayout( + new EmptyLayout(), + new CharLayout('3'), + false), + false); + ExpressionLayoutCursor cursor2(layout2->editableChild(2)->editableChild(1), ExpressionLayoutCursor::Position::Left); + cursor2.performBackspace(); + assert_expression_layout_serialize_to(layout2, "1+3"); + assert(cursor2.isEquivalentTo(ExpressionLayoutCursor(layout2->editableChild(1), ExpressionLayoutCursor::Position::Right))); + delete layout2; +} + +QUIZ_CASE(poincare_fraction_layout_serialize) { + FractionLayout * layout = new FractionLayout( + new CharLayout('1'), + LayoutEngine::createStringLayout("2+3", 3), + false); + assert_expression_layout_serialize_to(layout, "1/(2+3)"); + delete layout; +} diff --git a/poincare/test/helper.cpp b/poincare/test/helper.cpp index 801ecd684..c1e658f7c 100644 --- a/poincare/test/helper.cpp +++ b/poincare/test/helper.cpp @@ -106,5 +106,34 @@ void assert_parsed_expression_simplify_to(const char * expression, const char * delete e; } +void assert_parsed_expression_layout_serialize_to_self(const char * expressionLayout) { + Expression * e = parse_expression(expressionLayout); +#if POINCARE_TESTS_PRINT_EXPRESSIONS + cout << "---- Serialize: " << expressionLayout << "----" << endl; +#endif + ExpressionLayout * el = e->createLayout(); + int bufferSize = 255; + char buffer[bufferSize]; + el->writeTextInBuffer(buffer, bufferSize); +#if POINCARE_TESTS_PRINT_EXPRESSIONS + cout << "---- serialized to: " << buffer << " ----\n" << endl; +#endif + assert(strcmp(expressionLayout, buffer) == 0); + delete e; + delete el; +} + +void assert_expression_layout_serialize_to(Poincare::ExpressionLayout * layout, const char * serialization) { + int bufferSize = 255; + char buffer[bufferSize]; + layout->writeTextInBuffer(buffer, bufferSize); +#if POINCARE_TESTS_PRINT_EXPRESSIONS + cout << "---- Serialize: " << serialization << "----" << endl; + cout << "---- serialized to: " << buffer << " ----" << endl; + cout << "----- compared to: " << serialization << " ----\n" << endl; +#endif + assert(strcmp(serialization, buffer) == 0); +} + template void assert_parsed_expression_evaluates_to(char const*, Poincare::Complex*, int, int, Poincare::Expression::AngleUnit); template void assert_parsed_expression_evaluates_to(char const*, Poincare::Complex*, int, int, Poincare::Expression::AngleUnit); diff --git a/poincare/test/helper.h b/poincare/test/helper.h index 460e7bd03..f182418dd 100644 --- a/poincare/test/helper.h +++ b/poincare/test/helper.h @@ -1,5 +1,7 @@ #include +// Expressions + constexpr Poincare::Expression::AngleUnit Degree = Poincare::Expression::AngleUnit::Degree; constexpr Poincare::Expression::AngleUnit Radian = Poincare::Expression::AngleUnit::Radian; @@ -15,3 +17,7 @@ void assert_parsed_expression_evaluates_to(const char * expression, Poincare::Co assert_parsed_expression_evaluates_to(expression, results, 0, 0, angleUnit); } void assert_parsed_expression_simplify_to(const char * expression, const char * simplifiedExpression, Poincare::Expression::AngleUnit angleUnit = Poincare::Expression::AngleUnit::Radian); + +// Layouts +void assert_parsed_expression_layout_serialize_to_self(const char * expressionLayout); +void assert_expression_layout_serialize_to(Poincare::ExpressionLayout * layout, const char * serialization); diff --git a/poincare/test/nth_root_layout.cpp b/poincare/test/nth_root_layout.cpp new file mode 100644 index 000000000..ea0debc7e --- /dev/null +++ b/poincare/test/nth_root_layout.cpp @@ -0,0 +1,10 @@ +#include +#include +#include +#include "helper.h" + +using namespace Poincare; + +QUIZ_CASE(poincare_nth_root_layout_serialize) { + assert_parsed_expression_layout_serialize_to_self("root(7,3)"); +} diff --git a/poincare/test/parentheses_layout.cpp b/poincare/test/parentheses_layout.cpp new file mode 100644 index 000000000..0b175a54d --- /dev/null +++ b/poincare/test/parentheses_layout.cpp @@ -0,0 +1,32 @@ +#include +#include +#include +#include +#include "helper.h" + +using namespace Poincare; + +QUIZ_CASE(poincare_parenthesis_layout_size) { + /* 3 + * (2+(---)6)1 + * 4 + * Assert that the first and last parentheses have the same size. + */ + HorizontalLayout * layout = new HorizontalLayout(); + LeftParenthesisLayout leftPar = new LeftParenthesisLayout(); + RightParenthesisLayout rightPar = new RightParenthesisLayout(); + layout->addChildAtIndex(leftPar, 0); + layout->addChildAtIndex(new CharLayout('2'), 1); + layout->addChildAtIndex(new CharLayout('+'), 2); + layout->addChildAtIndex(new LeftParenthesisLayout(), 3); + layout->addChildAtIndex(new FractionLayout( + new CharLayout('3'), + new CharLayout('4')), + 4); + layout->addChildAtIndex(new RightParenthesisLayout(), 3); + layout->addChildAtIndex(new CharLayout('6'), 5); + layout->addChildAtIndex(rightPar, 7); + layout->addChildAtIndex(new CharLayout('1'), 8); + assert(leftPar->size().height() == rightPar->size().height()); + delete layout; +} diff --git a/poincare/test/vertical_offset_layout.cpp b/poincare/test/vertical_offset_layout.cpp new file mode 100644 index 000000000..bea971293 --- /dev/null +++ b/poincare/test/vertical_offset_layout.cpp @@ -0,0 +1,23 @@ +#include +#include +#include +#include +#include +#include "helper.h" + +using namespace Poincare; + +QUIZ_CASE(poincare_vertical_offset_layout_serialize) { + assert_parsed_expression_layout_serialize_to_self("2^3"); + + HorizontalLayout * layout = new HorizontalLayout( + new CharLayout('2'), + new VerticalOffsetLayout( + LayoutEngine::createStringLayout("4+5", 3), + VerticalOffsetLayout::Type::Superscript, + false), + false); + assert_expression_layout_serialize_to(layout, "2^(4+5)"); + delete layout; + +}