mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-03-18 21:30:38 +01:00
[poincarE] Add unary tests on Layouts.
Change-Id: I21f33e84eb4a60ecbabbba99a966abed6a23039c
This commit is contained in:
@@ -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\
|
||||
|
||||
|
||||
10
poincare/test/binomial_coefficient_layout.cpp
Normal file
10
poincare/test/binomial_coefficient_layout.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <quiz.h>
|
||||
#include <ion.h>
|
||||
#include <assert.h>
|
||||
#include "helper.h"
|
||||
|
||||
using namespace Poincare;
|
||||
|
||||
QUIZ_CASE(poincare_binomial_coefficient_layout_serialize) {
|
||||
assert_parsed_expression_layout_serialize_to_self("binomial(7,6)");
|
||||
}
|
||||
68
poincare/test/fraction_layout.cpp
Normal file
68
poincare/test/fraction_layout.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <quiz.h>
|
||||
#include <poincare.h>
|
||||
#include <poincare_layouts.h>
|
||||
#include <ion.h>
|
||||
#include <assert.h>
|
||||
#include "helper.h"
|
||||
|
||||
using namespace Poincare;
|
||||
|
||||
QUIZ_CASE(poincare_fraction_layout_create) {
|
||||
|
||||
/* 12
|
||||
* 12|34+5 -> "Divide" -> --- + 5
|
||||
* |34
|
||||
* */
|
||||
HorizontalLayout * layout = static_cast<HorizontalLayout *>(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;
|
||||
}
|
||||
@@ -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<float>(char const*, Poincare::Complex<float>*, int, int, Poincare::Expression::AngleUnit);
|
||||
template void assert_parsed_expression_evaluates_to<double>(char const*, Poincare::Complex<double>*, int, int, Poincare::Expression::AngleUnit);
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <poincare.h>
|
||||
|
||||
// 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);
|
||||
|
||||
10
poincare/test/nth_root_layout.cpp
Normal file
10
poincare/test/nth_root_layout.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include <quiz.h>
|
||||
#include <ion.h>
|
||||
#include <assert.h>
|
||||
#include "helper.h"
|
||||
|
||||
using namespace Poincare;
|
||||
|
||||
QUIZ_CASE(poincare_nth_root_layout_serialize) {
|
||||
assert_parsed_expression_layout_serialize_to_self("root(7,3)");
|
||||
}
|
||||
32
poincare/test/parentheses_layout.cpp
Normal file
32
poincare/test/parentheses_layout.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include <quiz.h>
|
||||
#include <ion.h>
|
||||
#include <poincare_layouts.h>
|
||||
#include <assert.h>
|
||||
#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;
|
||||
}
|
||||
23
poincare/test/vertical_offset_layout.cpp
Normal file
23
poincare/test/vertical_offset_layout.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <quiz.h>
|
||||
#include <poincare.h>
|
||||
#include <poincare_layouts.h>
|
||||
#include <ion.h>
|
||||
#include <assert.h>
|
||||
#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;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user