Skip to content

Commit

Permalink
Add FracMixed
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Oct 25, 2023
1 parent f2775c3 commit c835633
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 6 deletions.
1 change: 1 addition & 0 deletions include/fintamath/core/MathObjectTypes.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ class MathObjectType {
Derivative,
Integral,
Frac,
FracMixed,
PowFunction,
Floor,
Ceil,
Expand Down
22 changes: 22 additions & 0 deletions include/fintamath/functions/arithmetic/FracMixed.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "fintamath/core/IArithmetic.hpp"
#include "fintamath/functions/IFunction.hpp"

namespace fintamath {

class FracMixed : public IFunctionCRTP<IArithmetic, FracMixed, IArithmetic, IArithmetic, IArithmetic> {
public:
std::string toString() const override {
return "frac";
}

static MathObjectType getTypeStatic() {
return MathObjectType::FracMixed;
}

protected:
std::unique_ptr<IMathObject> call(const ArgumentRefVector &argsVect) const override;
};

}
9 changes: 9 additions & 0 deletions src/fintamath/config/ExpressionConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/FracMixed.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
Expand Down Expand Up @@ -160,6 +161,14 @@ struct ExpressionConfig {
return divExpr(std::move(args));
});

Expression::registerFunctionExpressionMaker<FracMixed>([](ArgumentPtrVector &&args) {
ArgumentPtr integ = args[0];
ArgumentPtr numer = args[1];
ArgumentPtr denom = args[2];

return addExpr(integ, divExpr(numer, denom));
});

Expression::registerFunctionExpressionMaker<And, true>([](ArgumentPtrVector &&args) {
return AndExpression(std::move(args)).clone();
});
Expand Down
2 changes: 2 additions & 0 deletions src/fintamath/config/ParserConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/FracMixed.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
Expand Down Expand Up @@ -225,6 +226,7 @@ struct ParserConfig {
IFunction::registerType<Derivative>();
IFunction::registerType<Integral>();
IFunction::registerType<Frac>();
IFunction::registerType<FracMixed>();
IFunction::registerType<PowFunction>();
IFunction::registerType<Floor>();
IFunction::registerType<Ceil>();
Expand Down
16 changes: 16 additions & 0 deletions src/fintamath/functions/arithmetic/FracMixed.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "fintamath/functions/arithmetic/FracMixed.hpp"

#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"

namespace fintamath {

std::unique_ptr<IMathObject> FracMixed::call(const ArgumentRefVector &argsVect) const {
const auto &integ = cast<IArithmetic>(argsVect[0].get());
const auto &numer = cast<IArithmetic>(argsVect[1].get());
const auto &denom = cast<IArithmetic>(argsVect[2].get());

return Add()(integ, *Div()(numer, denom));
}

}
63 changes: 63 additions & 0 deletions tests/src/functions/arithmetic/FracMixedTests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "gtest/gtest.h"

#include "fintamath/functions/arithmetic/Add.hpp"

#include "fintamath/exceptions/InvalidInputException.hpp"
#include "fintamath/functions/arithmetic/FracMixed.hpp"
#include "fintamath/functions/arithmetic/Sub.hpp"
#include "fintamath/functions/arithmetic/UnaryPlus.hpp"
#include "fintamath/literals/Variable.hpp"
#include "fintamath/numbers/Rational.hpp"

using namespace fintamath;

const FracMixed f;

TEST(FracMixedTests, toStringTest) {
EXPECT_EQ(f.toString(), "frac");
}

TEST(FracMixedTests, getFunctionTypeTest) {
EXPECT_EQ(f.getFunctionType(), IFunction::Type::Ternary);
}

TEST(FracMixedTests, callTest) {
EXPECT_EQ(f(Integer(0), Integer(3), Integer(5))->toString(), "3/5");
EXPECT_EQ(f(Integer(0), Integer(3), Rational(5, 2))->toString(), "6/5");
EXPECT_EQ(f(Integer(0), Rational(5, 2), Integer(3))->toString(), "5/6");
EXPECT_EQ(f(Integer(0), Rational(5, 2), Rational(5, 3))->toString(), "3/2");

EXPECT_EQ(f(Integer(0), Integer(3), Variable("a"))->toString(), "3/a");

EXPECT_THROW(f(Integer(1)), InvalidInputFunctionException);
EXPECT_THROW(f(Rational(2, 3)), InvalidInputFunctionException);
EXPECT_THROW(f(), InvalidInputFunctionException);
EXPECT_THROW(f(Integer(1), Integer(1), Integer(1), Integer(1)), InvalidInputFunctionException);
}

TEST(FracMixedTests, doArgsMatchTest) {
Integer a;

EXPECT_FALSE(f.doArgsMatch({}));
EXPECT_FALSE(f.doArgsMatch({a}));
EXPECT_FALSE(f.doArgsMatch({a, a}));
EXPECT_TRUE(f.doArgsMatch({a, a, a}));
EXPECT_FALSE(f.doArgsMatch({a, a, a, a}));
}

TEST(FracMixedTests, equalsTest) {
EXPECT_EQ(f, f);
EXPECT_EQ(f, FracMixed());
EXPECT_EQ(FracMixed(), f);
EXPECT_EQ(f, cast<IMathObject>(FracMixed()));
EXPECT_EQ(cast<IMathObject>(FracMixed()), f);
EXPECT_NE(f, Sub());
EXPECT_NE(Sub(), f);
EXPECT_NE(f, UnaryPlus());
EXPECT_NE(UnaryPlus(), f);
}

TEST(FracMixedTests, getTypeTest) {
EXPECT_EQ(FracMixed::getTypeStatic(), MathObjectType::FracMixed);
EXPECT_EQ(FracMixed().getType(), MathObjectType::FracMixed);
}
9 changes: 3 additions & 6 deletions tests/src/parser/ParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "fintamath/functions/arithmetic/Add.hpp"
#include "fintamath/functions/arithmetic/Div.hpp"
#include "fintamath/functions/arithmetic/Frac.hpp"
#include "fintamath/functions/arithmetic/FracMixed.hpp"
#include "fintamath/functions/arithmetic/Mul.hpp"
#include "fintamath/functions/arithmetic/Neg.hpp"
#include "fintamath/functions/arithmetic/Sign.hpp"
Expand Down Expand Up @@ -243,16 +244,12 @@ TEST(ParserTests, parseFunctionTest) {
EXPECT_TRUE(is<Acsch>(IFunction::parse("acsch")));
EXPECT_TRUE(is<Derivative>(IFunction::parse("derivative")));
EXPECT_TRUE(is<Integral>(IFunction::parse("integral")));
EXPECT_TRUE(is<Frac>(IFunction::parse("frac")));
EXPECT_TRUE(is<Frac>(IFunction::parse("frac", IFunction::Type::Binary)));
EXPECT_TRUE(is<FracMixed>(IFunction::parse("frac", IFunction::Type::Ternary)));
EXPECT_TRUE(is<PowFunction>(IFunction::parse("pow")));
EXPECT_TRUE(is<Floor>(IFunction::parse("floor")));
EXPECT_TRUE(is<Ceil>(IFunction::parse("ceil")));

EXPECT_TRUE(is<Add>(IFunction::parse("+", IFunction::Type::Binary)));
EXPECT_TRUE(is<UnaryPlus>(IFunction::parse("+", IFunction::Type::Unary)));
EXPECT_TRUE(is<Sub>(IFunction::parse("-", IFunction::Type::Binary)));
EXPECT_TRUE(is<Neg>(IFunction::parse("-", IFunction::Type::Unary)));

EXPECT_EQ(IFunction::parse("asdgewfe"), nullptr);
EXPECT_EQ(IFunction::parse("1224"), nullptr);
}
Expand Down

0 comments on commit c835633

Please sign in to comment.