mirror of
https://github.com/UpsilonNumworks/Upsilon.git
synced 2026-01-19 00:37:25 +01:00
[poincare] Clean tests on Unit
This commit is contained in:
@@ -3,8 +3,10 @@
|
||||
#include "../shared/scrollable_multiple_expressions_view.h"
|
||||
#include "../global_preferences.h"
|
||||
#include "../exam_mode_configuration.h"
|
||||
#include "app.h"
|
||||
#include <poincare/exception_checkpoint.h>
|
||||
#include <poincare/undefined.h>
|
||||
#include <poincare/unit.h>
|
||||
#include <poincare/unreal.h>
|
||||
#include <string.h>
|
||||
#include <cmath>
|
||||
|
||||
@@ -74,60 +74,19 @@ QUIZ_CASE(poincare_expression_rational_constructor) {
|
||||
assert_pool_size(initialPoolSize+6);
|
||||
}
|
||||
|
||||
void assert_seconds_split_to(double totalSeconds, const char * splittedTime, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) {
|
||||
Expression time = Unit::BuildTimeSplit(totalSeconds, context, complexFormat, angleUnit);
|
||||
constexpr static int bufferSize = 100;
|
||||
char buffer[bufferSize];
|
||||
time.serialize(buffer, bufferSize, DecimalMode);
|
||||
quiz_assert_print_if_failure(strcmp(buffer, splittedTime) == 0, splittedTime);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_expression_unit_constructor) {
|
||||
Shared::GlobalContext globalContext;
|
||||
ExpressionNode::ReductionContext reductionContext = ExpressionNode::ReductionContext(&globalContext, Cartesian, Degree, User);
|
||||
// 1. Time
|
||||
// 1.a. Test Unit::Second constructor
|
||||
Unit s = Unit::Second();
|
||||
// 1.b. Test Unit::isSecond helper
|
||||
quiz_assert(s.isSecond());
|
||||
quiz_assert(!s.isMeter());
|
||||
// 1.c. Test Unit::BuildTimeSplit constructor
|
||||
assert_seconds_split_to(1234567890, "39×_year+1×_month+13×_day+19×_h+1×_min+30×_s", &globalContext, Cartesian, Degree);
|
||||
assert_seconds_split_to(-122, "-2×_min-2×_s", &globalContext, Cartesian, Degree);
|
||||
Unit u = Unit::Second();
|
||||
assert_expression_serialize_to(u, "_s");
|
||||
|
||||
// 2. Speed
|
||||
// 2.a. test Unit::Kilometer and Unit::Hour constructors
|
||||
Expression kilometerPerHour = Multiplication::Builder(
|
||||
Unit::Kilometer(),
|
||||
Power::Builder(
|
||||
Unit::Hour(),
|
||||
Rational::Builder(-1)
|
||||
)
|
||||
);
|
||||
kilometerPerHour = kilometerPerHour.reduce(reductionContext);
|
||||
Expression meterPerSecond;
|
||||
kilometerPerHour = kilometerPerHour.removeUnit(&meterPerSecond);
|
||||
// 2.b. Test Unit::IsISSpeed helper
|
||||
quiz_assert(Unit::IsISSpeed(meterPerSecond));
|
||||
u = Unit::Hour();
|
||||
assert_expression_serialize_to(u, "_h");
|
||||
|
||||
// 3. Volume
|
||||
// 3.a. test Unit::Liter constructor
|
||||
Expression liter = Unit::Liter();
|
||||
liter = liter.reduce(reductionContext);
|
||||
Expression meter3;
|
||||
liter = liter.removeUnit(&meter3);
|
||||
// 3.b. Test Unit::IsISVolume helper
|
||||
quiz_assert(Unit::IsISVolume(meter3));
|
||||
u = Unit::Kilometer();
|
||||
assert_expression_serialize_to(u, "_km");
|
||||
|
||||
// 4. Energy
|
||||
// 4.a. test Unit::Watt and Unit::Hour constructors
|
||||
Expression wattHour = Multiplication::Builder(
|
||||
Unit::Watt(),
|
||||
Unit::Hour()
|
||||
);
|
||||
wattHour = wattHour.reduce(reductionContext);
|
||||
Expression kilogramMeter2PerSecond2;
|
||||
wattHour = wattHour.removeUnit(&kilogramMeter2PerSecond2);
|
||||
// 4.b. Test Unit::IsISEnergy helper
|
||||
quiz_assert(Unit::IsISEnergy(kilogramMeter2PerSecond2));
|
||||
u = Unit::Liter();
|
||||
assert_expression_serialize_to(u, "_L");
|
||||
|
||||
u = Unit::Watt();
|
||||
assert_expression_serialize_to(u, "_W");
|
||||
}
|
||||
|
||||
@@ -382,10 +382,51 @@ void assert_reduced_expression_unit_is(const char * expression, const char * uni
|
||||
quiz_assert_print_if_failure(u1.isUninitialized() == u2.isUninitialized() && (u1.isUninitialized() || u1.isIdenticalTo(u2)), expression);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_properties_get_unit) {
|
||||
QUIZ_CASE(poincare_properties_remove_unit) {
|
||||
assert_reduced_expression_unit_is("_km", "_m");
|
||||
assert_reduced_expression_unit_is("_min/_km", "_m^(-1)×_s");
|
||||
assert_reduced_expression_unit_is("_km^3", "_m^3");
|
||||
assert_reduced_expression_unit_is("1_m+_km", "_m");
|
||||
assert_reduced_expression_unit_is("_L^2×3×_s", "_m^6×_s");
|
||||
}
|
||||
|
||||
void assert_seconds_split_to(double totalSeconds, const char * splittedTime, Context * context, Preferences::ComplexFormat complexFormat, Preferences::AngleUnit angleUnit) {
|
||||
Expression time = Unit::BuildTimeSplit(totalSeconds, context, complexFormat, angleUnit);
|
||||
constexpr static int bufferSize = 100;
|
||||
char buffer[bufferSize];
|
||||
time.serialize(buffer, bufferSize, DecimalMode);
|
||||
quiz_assert_print_if_failure(strcmp(buffer, splittedTime) == 0, splittedTime);
|
||||
}
|
||||
|
||||
Expression extract_unit(const char * expression) {
|
||||
Shared::GlobalContext globalContext;
|
||||
ExpressionNode::ReductionContext reductionContext = ExpressionNode::ReductionContext(&globalContext, Cartesian, Degree, User);
|
||||
Expression e = parse_expression(expression, &globalContext, false).reduce(reductionContext);
|
||||
Expression unit;
|
||||
e.removeUnit(&unit);
|
||||
return unit;
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_expression_unit_helper) {
|
||||
// 1. Time
|
||||
Expression s = extract_unit("_s");
|
||||
quiz_assert(s.type() == ExpressionNode::Type::Unit && static_cast<Unit &>(s).isSecond());
|
||||
quiz_assert(!static_cast<Unit &>(s).isMeter());
|
||||
|
||||
Shared::GlobalContext globalContext;
|
||||
assert_seconds_split_to(1234567890, "39×_year+1×_month+13×_day+19×_h+1×_min+30×_s", &globalContext, Cartesian, Degree);
|
||||
assert_seconds_split_to(-122, "-2×_min-2×_s", &globalContext, Cartesian, Degree);
|
||||
|
||||
// 2. Speed
|
||||
Expression meterPerSecond = extract_unit("_m×_s^-1");
|
||||
quiz_assert(Unit::IsISSpeed(meterPerSecond));
|
||||
|
||||
// 3. Volume
|
||||
Expression meter3 = extract_unit("_m^3");
|
||||
quiz_assert(Unit::IsISVolume(meter3));
|
||||
|
||||
// 4. Energy
|
||||
Expression kilogramMeter2PerSecond2 = extract_unit("_kg×_m^2×_s^-2");
|
||||
quiz_assert(Unit::IsISEnergy(kilogramMeter2PerSecond2));
|
||||
}
|
||||
|
||||
|
||||
@@ -2,13 +2,6 @@
|
||||
|
||||
using namespace Poincare;
|
||||
|
||||
void assert_expression_serialize_to(Poincare::Expression expression, const char * serialization, Preferences::PrintFloatMode mode = ScientificMode, int numberOfSignificantDigits = 7) {
|
||||
constexpr int bufferSize = 500;
|
||||
char buffer[bufferSize];
|
||||
expression.serialize(buffer, bufferSize, mode, numberOfSignificantDigits);
|
||||
quiz_assert_print_if_failure(strcmp(serialization, buffer) == 0, serialization);
|
||||
}
|
||||
|
||||
QUIZ_CASE(poincare_serialization_based_integer) {
|
||||
assert_expression_serialize_to(BasedInteger::Builder(Integer(23), Integer::Base::Decimal), "23");
|
||||
assert_expression_serialize_to(BasedInteger::Builder(Integer(23), Integer::Base::Binary), "0b10111");
|
||||
|
||||
@@ -127,6 +127,13 @@ void assert_expression_simplifies_approximates_to(const char * expression, const
|
||||
}, numberOfDigits);
|
||||
}
|
||||
|
||||
void assert_expression_serialize_to(Poincare::Expression expression, const char * serialization, Preferences::PrintFloatMode mode, int numberOfSignificantDigits) {
|
||||
constexpr int bufferSize = 500;
|
||||
char buffer[bufferSize];
|
||||
expression.serialize(buffer, bufferSize, mode, numberOfSignificantDigits);
|
||||
quiz_assert_print_if_failure(strcmp(serialization, buffer) == 0, serialization);
|
||||
}
|
||||
|
||||
void assert_layout_serialize_to(Poincare::Layout layout, const char * serialization) {
|
||||
constexpr int bufferSize = 255;
|
||||
char buffer[bufferSize];
|
||||
|
||||
@@ -50,6 +50,9 @@ void assert_expression_simplifies_and_approximates_to(const char * expression, c
|
||||
template<typename T>
|
||||
void assert_expression_simplifies_approximates_to(const char * expression, const char * approximation, Poincare::Preferences::AngleUnit angleUnit = Degree, Poincare::Preferences::ComplexFormat complexFormat = Cartesian, int numberOfSignificantDigits = -1);
|
||||
|
||||
// Expression serializing
|
||||
|
||||
void assert_expression_serialize_to(Poincare::Expression expression, const char * serialization, Poincare::Preferences::PrintFloatMode mode = ScientificMode, int numberOfSignificantDigits = 7);
|
||||
|
||||
// Layout serializing
|
||||
|
||||
|
||||
Reference in New Issue
Block a user