mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-18 16:27:34 +01:00
[poincare/tests] Add layout serialization+parsing tests
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#include <quiz.h>
|
||||
#include <poincare_layouts.h>
|
||||
#include <apps/shared/global_context.h>
|
||||
#include <ion.h>
|
||||
#include <assert.h>
|
||||
#include "helper.h"
|
||||
@@ -7,6 +8,25 @@
|
||||
|
||||
using namespace Poincare;
|
||||
|
||||
void assert_parsed_layout_is(Layout l, Poincare::Expression r) {
|
||||
Shared::GlobalContext context;
|
||||
constexpr int bufferSize = 500;
|
||||
char buffer[bufferSize];
|
||||
l.serializeForParsing(buffer, bufferSize);
|
||||
Expression e = parse_expression(buffer);
|
||||
e = e.simplify(context, Preferences::AngleUnit::Degree);
|
||||
bool identical = e.isIdenticalTo(r.simplify(context, Preferences::AngleUnit::Degree));
|
||||
#if POINCARE_TREE_LOG
|
||||
if (!identical) {
|
||||
std::cout << "Expecting" << std::endl;
|
||||
r.log();
|
||||
std::cout << "Got" << std::endl;
|
||||
e.log();
|
||||
}
|
||||
#endif
|
||||
quiz_assert(identical);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_create_all_layouts) {
|
||||
EmptyLayout e0;
|
||||
AbsoluteValueLayout e1(e0);
|
||||
@@ -35,3 +55,145 @@ QUIZ_CASE(poincare_create_all_layouts) {
|
||||
SumLayout e25(e21, e22, e23, e24);
|
||||
VerticalOffsetLayout e26(e25, VerticalOffsetLayoutNode::Type::Superscript);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_parse_layouts) {
|
||||
Layout l;
|
||||
Expression e;
|
||||
|
||||
// 1+2
|
||||
l = HorizontalLayout(
|
||||
CharLayout('1'),
|
||||
CharLayout('+'),
|
||||
CharLayout('2'));
|
||||
e = Addition(Rational(1), Rational(2));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// |3+3/6|
|
||||
l = AbsoluteValueLayout(
|
||||
HorizontalLayout(
|
||||
CharLayout('3'),
|
||||
CharLayout('+'),
|
||||
FractionLayout(
|
||||
CharLayout('3'),
|
||||
CharLayout('6'))));
|
||||
e = AbsoluteValue::Builder(
|
||||
Addition(
|
||||
Rational(3),
|
||||
Division(
|
||||
Rational(3),
|
||||
Rational(6))));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// binCoef(4,5)
|
||||
l = BinomialCoefficientLayout(
|
||||
CharLayout('4'),
|
||||
CharLayout('5'));
|
||||
e = BinomialCoefficient::Builder(
|
||||
Rational(4),
|
||||
Rational(5));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// ceil(4.6)
|
||||
l = CeilingLayout(
|
||||
HorizontalLayout(
|
||||
CharLayout('4'),
|
||||
CharLayout('.'),
|
||||
CharLayout('6')));
|
||||
e = Ceiling::Builder(
|
||||
Decimal(4.6));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// floor(7.2)
|
||||
l = FloorLayout(
|
||||
HorizontalLayout(
|
||||
CharLayout('7'),
|
||||
CharLayout('.'),
|
||||
CharLayout('2')));
|
||||
e = Floor::Builder(
|
||||
Decimal(7.2));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// 2^(3+4)
|
||||
l = HorizontalLayout(
|
||||
CharLayout('2'),
|
||||
VerticalOffsetLayout(
|
||||
HorizontalLayout(
|
||||
CharLayout('3'),
|
||||
CharLayout('+'),
|
||||
CharLayout('4')),
|
||||
VerticalOffsetLayoutNode::Type::Superscript));
|
||||
e = Power(
|
||||
Rational(2),
|
||||
Addition(
|
||||
Rational(3),
|
||||
Rational(4)));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// log_3(2)
|
||||
HorizontalLayout l1 = HorizontalLayout();
|
||||
l1.addChildAtIndex(CharLayout('l'), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l1.addChildAtIndex(CharLayout('o'), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l1.addChildAtIndex(CharLayout('g'), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l1.addChildAtIndex(VerticalOffsetLayout(CharLayout('3'), VerticalOffsetLayoutNode::Type::Subscript), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l1.addChildAtIndex(LeftParenthesisLayout(), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l1.addChildAtIndex(CharLayout('2'), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l1.addChildAtIndex(RightParenthesisLayout(), l1.numberOfChildren(), l1.numberOfChildren(), nullptr);
|
||||
l = l1;
|
||||
e = Logarithm::Builder(
|
||||
Rational(2),
|
||||
Rational(3));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// root(5,3)
|
||||
l = NthRootLayout(
|
||||
CharLayout('5'),
|
||||
CharLayout('3'));
|
||||
e = NthRoot::Builder(Rational(5), Rational(3));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// int(7, x, 4, 5)
|
||||
l = IntegralLayout(
|
||||
CharLayout('7'),
|
||||
CharLayout('x'),
|
||||
CharLayout('4'),
|
||||
CharLayout('5'));
|
||||
e = Integral::Builder(
|
||||
Rational(7),
|
||||
Symbol('x'),
|
||||
Rational(4),
|
||||
Rational(5));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// 2^2 !
|
||||
l = HorizontalLayout(
|
||||
CharLayout('2'),
|
||||
VerticalOffsetLayout(
|
||||
CharLayout('2'),
|
||||
VerticalOffsetLayoutNode::Type::Superscript),
|
||||
CharLayout('!'));
|
||||
e = Factorial(
|
||||
Power(
|
||||
Rational(2),
|
||||
Rational(2)));
|
||||
assert_parsed_layout_is(l, e);
|
||||
|
||||
// 5* 6/(7+5) *3
|
||||
l = HorizontalLayout(
|
||||
CharLayout('5'),
|
||||
FractionLayout(
|
||||
CharLayout('6'),
|
||||
HorizontalLayout(
|
||||
CharLayout('7'),
|
||||
CharLayout('+'),
|
||||
CharLayout('5'))),
|
||||
CharLayout('3'));
|
||||
e = Multiplication(
|
||||
Rational(5),
|
||||
Division(
|
||||
Rational(6),
|
||||
Addition(
|
||||
Rational(7),
|
||||
Rational(5))),
|
||||
Rational(3));
|
||||
assert_parsed_layout_is(l, e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user