Skip to content

Commit

Permalink
Split expression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fintarin committed Jan 30, 2024
1 parent 96447e6 commit e127673
Show file tree
Hide file tree
Showing 20 changed files with 4,330 additions and 2,572 deletions.
10 changes: 9 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,15 @@ file(
CONFIGURE_DEPENDS
"src/*.cpp")

add_executable(${PROJECT_NAME}_tests ${SOURCES})
add_executable(${PROJECT_NAME}_tests ${SOURCES}
src/overall/approximate/ApproximateTests.cpp
src/overall/solve/SolveTests.cpp
src/overall/simplify/SimplifyTests.cpp
src/overall/simplify/SimplifyInfinitiesTests.cpp
src/overall/simplify/SimplifyTrigonometryTests.cpp
src/overall/simplify/SimplifyHyperbolicTests.cpp
src/overall/simplify/SimplifyFactorialsTests.cpp
src/overall/simplify/SimplifyPowersTests.cpp)

target_link_libraries(${PROJECT_NAME}_tests PRIVATE ${PROJECT_NAME} gtest_main)

Expand Down
90 changes: 0 additions & 90 deletions tests/src/expressions/ExpressionFunctionsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include "fintamath/expressions/ExpressionFunctions.hpp"

#include "fintamath/exceptions/UndefinedException.hpp"
#include "fintamath/literals/Variable.hpp"

using namespace fintamath;
Expand Down Expand Up @@ -314,92 +313,3 @@ TEST(ExpressionFunctionsTests, negInfTest) {
TEST(ExpressionFunctionsTests, complexInfTest) {
EXPECT_EQ(complexInf().toString(), "ComplexInf");
}

TEST(ExpressionFunctionsTests, solveTest) {
EXPECT_EQ(solve(Expression("x - 10 = 0")).toString(), "x = 10");
EXPECT_EQ(solve(Expression("-10 - x = 0")).toString(), "x = -10");

EXPECT_EQ(solve(Expression("z - 10 - I = 0")).toString(), "z = 10 + I");
EXPECT_EQ(solve(Expression("(6 + 3I)^2 = 4I - 30z")).toString(), "z = -9/10 - 16/15 I");

EXPECT_EQ(solve(Expression("x^2 - 10 = 39")).toString(), "x = -7 | x = 7");
EXPECT_EQ(solve(Expression("x^2 = 0")).toString(), "x = 0");
EXPECT_EQ(solve(Expression("x^2 = 1")).toString(), "x = -1 | x = 1");
EXPECT_EQ(solve(Expression("x^2 - 2x - 3 = 0")).toString(), "x = -1 | x = 3");
EXPECT_EQ(solve(Expression("15 - 2x - x^2 = 0")).toString(), "x = -5 | x = 3");
EXPECT_EQ(solve(Expression("x^2 + 12x + 36 = 0")).toString(), "x = -6");
EXPECT_EQ(solve(Expression("x^2 + 12x = 0")).toString(), "x = -12 | x = 0");
EXPECT_EQ(solve(Expression("x^2 - 23x - 3 = 0")).toString(), "x = -sqrt(541)/2 + 23/2 | x = sqrt(541)/2 + 23/2");
EXPECT_EQ(solve(Expression("-12x^2 - 23x + 30 = 0")).toString(), "x = -sqrt(1969)/24 - 23/24 | x = sqrt(1969)/24 - 23/24");
EXPECT_EQ(solve(Expression("-33x^2 - x + 34 = 0")).toString(), "x = -34/33 | x = 1");
EXPECT_EQ(solve(Expression("2x^2 + 2sqrt(2)x + 1 = 0")).toString(), "x = -sqrt(2)/2");

EXPECT_EQ(solve(Expression("x^2 = -1")).toString(), "x = -I | x = I");
EXPECT_EQ(solve(Expression("x^2 + 4x + 5 = 0")).toString(), "x = -2 - I | x = -2 + I");
EXPECT_EQ(solve(Expression("2x^2 + x + 1 = 0")).toString(), "x = -(I sqrt(7))/4 - 1/4 | x = (I sqrt(7))/4 - 1/4");
EXPECT_EQ(solve(Expression("x^2 + 3x + 5 = 0")).toString(), "x = -(I sqrt(11))/2 - 3/2 | x = (I sqrt(11))/2 - 3/2");

// TODO: implement cubic equations
EXPECT_EQ(solve(Expression("x^3 - 3x^2 + 3x - 1 = 0")).toString(), "x^3 - 3 x^2 + 3 x - 1 = 0"); // TODO: x = 1
// EXPECT_EQ(solve(Expression("x^3 - 6x^2 + 11x - 6 = 0")).toString(), "x = 1 | x = 2 | x = 3");
// EXPECT_EQ(solve(Expression("x^3 + 2x^2 - 5x - 6 = 0")).toString(), "x = -3 | x = -1 | x = 2");
// EXPECT_EQ(solve(Expression("x^3 = 0")).toString(), "x = 0");
// EXPECT_EQ(solve(Expression("x^3 + 1 = 0")).toString(), "x = -1");
// EXPECT_EQ(solve(Expression("x^3 - 1 = 0")).toString(), "x = 1");
// EXPECT_EQ(solve(Expression("x^3 + 2x^2 + x = 0")).toString(), "x = -1 | x = 0");
// EXPECT_EQ(solve(Expression("-x^3 + 5x^2 - 8x + 4 = 0")).toString(), "x = 1 | x = 2");
// EXPECT_EQ(solve(Expression("-2x^3 + 4x^2 + 4x - 8 = 0")).toString(), "x = -1 | x = 1 | x = 2");
// EXPECT_EQ(solve(Expression("2x^3 - 3x^2 - 11x + 6 = 0")).toString(), "x = -2 | x = 1/2 | x = 3");
// EXPECT_EQ(solve(Expression("3x^3 - 3x^2 - 12x - 8 = 0")).toString(),
// "1/9 (3 + (1485 - 162 sqrt(23))^(1/3) + 3 (55 + 6 sqrt(23))^(1/3))");

EXPECT_EQ(solve(Expression("x < 1")).toString(), "x < 1");
EXPECT_EQ(solve(Expression("x < -10")).toString(), "x < -10");
EXPECT_EQ(solve(Expression("x! = 0")).toString(), "x! = 0");

// TODO: implement quadric inequalities
EXPECT_EQ(solve(Expression("x^2 + 2 x - 1 < 0")).toString(), "x^2 + 2 x - 1 < 0");

EXPECT_EQ(solve(Expression("15x^2 + sin(25)x - 10% = Ey")).toString(), "x^2 + (sin(25) x)/15 - (E y)/15 - 1/150 = 0");
EXPECT_EQ(solve(Expression("x + x_1 + x_2 + y + y_1 = 0")).toString(), "x + x_1 + x_2 + y + y_1 = 0");
EXPECT_EQ(solve(Expression("-x^a = 0")).toString(), "x^a = 0");
EXPECT_EQ(solve(Expression("sin(x) = 0")).toString(), "sin(x) = 0");
EXPECT_EQ(solve(Expression("x^2 + y = 0")).toString(), "x^2 + y = 0");
EXPECT_EQ(solve(Expression("x y = 0")).toString(), "x y = 0");
EXPECT_EQ(solve(Expression("2 x^a = 0")).toString(), "x^a = 0");
EXPECT_EQ(solve(Expression("x^b a = 0")).toString(), "a x^b = 0");
EXPECT_EQ(solve(Expression("x/y = 0")).toString(), "x = 0");
EXPECT_EQ(solve(Expression("x^2 - 2*sin(2) = 0")).toString(), "x = -sqrt(8 sin(2))/2 | x = sqrt(8 sin(2))/2");
EXPECT_EQ(solve(Expression("x = x sqrt(x)")).toString(), "x^(3/2) - x = 0");
EXPECT_EQ(solve(Expression("x = 3^x")).toString(), "x - 3^x = 0");

EXPECT_EQ(solve(Expression("E = Ey")).toString(), "y = 1");
EXPECT_EQ(solve(Expression("sin(4) = sin(4) y")).toString(), "y = 1");
EXPECT_EQ(solve(Expression("E >= Ey")).toString(), "y <= 1");
EXPECT_EQ(solve(Expression("sin(4) >= sin(4) y")).toString(), "y >= 1");
EXPECT_EQ(solve(Expression("x >= x sqrt(x)")).toString(), "x^(3/2) - x <= 0");
EXPECT_EQ(solve(Expression("x >= x^(1/100)")).toString(), "x - root(x, 100) >= 0");
}

TEST(ExpressionFunctionsTests, solveApproximateTest) {
Expression expr;
Variable x("x");

expr = pow(x + 1, 6);
EXPECT_EQ(expr.toString(), "x^6 + 6 x^5 + 15 x^4 + 20 x^3 + 15 x^2 + 6 x + 1");

expr = solve(expr);
EXPECT_EQ(expr.toString(), "x^6 + 6 x^5 + 15 x^4 + 20 x^3 + 15 x^2 + 6 x + 1");

expr = expr.approximate(2);
EXPECT_EQ(expr.toString(), "x^6 + 6 x^5 + 15 x^4 + 20 x^3 + 15 x^2 + 6 x + 1");

expr = pow(x + 1, 7);
EXPECT_EQ(expr.toString(), "x^7 + 7 x^6 + 21 x^5 + 35 x^4 + 35 x^3 + 21 x^2 + 7 x + 1");

expr = solve(expr);
EXPECT_EQ(expr.toString(), "x^7 + 7 x^6 + 21 x^5 + 35 x^4 + 35 x^3 + 21 x^2 + 7 x + 1");

expr = expr.approximate(2);
EXPECT_EQ(expr.toString(), "x^7 + 7 x^6 + 21 x^5 + 35 x^4 + 35 x^3 + 21 x^2 + 7 x + 1");
}
117 changes: 115 additions & 2 deletions tests/src/expressions/ExpressionParserTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,123 @@ TEST(ExpressionParserTests, parseFintamathTest) {
EXPECT_EQ(parseFintamath("log(4/9, 2/3)")->toString(), "log(4/9, 2/3)");
EXPECT_EQ(parseFintamath("60deg")->toString(), "60 Pi/180");
EXPECT_EQ(parseFintamath("adeg")->toString(), "a Pi/180");
}

EXPECT_THROW(parseFintamath("(1 = 1) / 2"), InvalidInputException);
TEST(ExpressionParserTests, parseFintamathNegativeTest) {
EXPECT_THROW(parseFintamath(""), InvalidInputException);
EXPECT_THROW(parseFintamath("1+"), InvalidInputException);
EXPECT_THROW(parseFintamath("1-"), InvalidInputException);
EXPECT_THROW(parseFintamath("1*"), InvalidInputException);
EXPECT_THROW(parseFintamath("1/"), InvalidInputException);
EXPECT_THROW(parseFintamath("*1"), InvalidInputException);
EXPECT_THROW(parseFintamath("/1"), InvalidInputException);
EXPECT_THROW(parseFintamath(" + "), InvalidInputException);
EXPECT_THROW(parseFintamath("(1+2))"), InvalidInputException);
EXPECT_THROW(parseFintamath("5-*3"), InvalidInputException);
EXPECT_THROW(parseFintamath("5 3 +"), InvalidInputException);
EXPECT_THROW(parseFintamath("2.2.2"), InvalidInputException);
EXPECT_THROW(parseFintamath("--"), InvalidInputException);
EXPECT_THROW(parseFintamath("."), InvalidInputException);
EXPECT_THROW(parseFintamath(","), InvalidInputException);
EXPECT_THROW(parseFintamath("/"), InvalidInputException);
EXPECT_THROW(parseFintamath(";"), InvalidInputException);
EXPECT_THROW(parseFintamath("\'"), InvalidInputException);
EXPECT_THROW(parseFintamath("["), InvalidInputException);
EXPECT_THROW(parseFintamath("]"), InvalidInputException);
EXPECT_THROW(parseFintamath("!"), InvalidInputException);
EXPECT_THROW(parseFintamath("@"), InvalidInputException);
EXPECT_THROW(parseFintamath("\""), InvalidInputException);
EXPECT_THROW(parseFintamath("#"), InvalidInputException);
EXPECT_THROW(parseFintamath(""), InvalidInputException);
EXPECT_THROW(parseFintamath("%"), InvalidInputException);
EXPECT_THROW(parseFintamath(":"), InvalidInputException);
EXPECT_THROW(parseFintamath("?"), InvalidInputException);
EXPECT_THROW(parseFintamath("*"), InvalidInputException);
EXPECT_THROW(parseFintamath("("), InvalidInputException);
EXPECT_THROW(parseFintamath(")"), InvalidInputException);
EXPECT_THROW(parseFintamath("-"), InvalidInputException);
EXPECT_THROW(parseFintamath("+"), InvalidInputException);
EXPECT_THROW(parseFintamath("$"), InvalidInputException);
EXPECT_THROW(parseFintamath("^"), InvalidInputException);
EXPECT_THROW(parseFintamath("&"), InvalidInputException);
EXPECT_THROW(parseFintamath("_"), InvalidInputException);
EXPECT_THROW(parseFintamath("[1+1]"), InvalidInputException);
EXPECT_THROW(parseFintamath("{1}"), InvalidInputException);
EXPECT_THROW(parseFintamath(",2"), InvalidInputException);
EXPECT_THROW(parseFintamath("2,"), InvalidInputException);

EXPECT_THROW(parseFintamath("(1"), InvalidInputException);
EXPECT_THROW(parseFintamath("(((2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("(((2))"), InvalidInputException);
EXPECT_THROW(parseFintamath("((2)))"), InvalidInputException);
EXPECT_THROW(parseFintamath("(2)))"), InvalidInputException);
EXPECT_THROW(parseFintamath("(2"), InvalidInputException);
EXPECT_THROW(parseFintamath("((2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("((2"), InvalidInputException);
EXPECT_THROW(parseFintamath("((((2)((2))))"), InvalidInputException);
EXPECT_THROW(parseFintamath("(()())"), InvalidInputException);
EXPECT_THROW(parseFintamath("((()()))"), InvalidInputException);
EXPECT_THROW(parseFintamath("((((()))))"), InvalidInputException);
EXPECT_THROW(parseFintamath("(,) + (,)"), InvalidInputException);
EXPECT_THROW(parseFintamath("--"), InvalidInputException);

EXPECT_THROW(parseFintamath("!2"), InvalidInputException);
EXPECT_THROW(parseFintamath("!!2"), InvalidInputException);
EXPECT_THROW(parseFintamath("!2!!"), InvalidInputException);
EXPECT_THROW(parseFintamath("!(2"), InvalidInputException);
EXPECT_THROW(parseFintamath("!(2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("2)!"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(2))!"), InvalidInputException);
EXPECT_THROW(parseFintamath("!!!!!!"), InvalidInputException);

EXPECT_THROW(parseFintamath("esin()"), InvalidInputException);
EXPECT_THROW(parseFintamath("(a+b)*()"), InvalidInputException);

EXPECT_THROW(parseFintamath("sin(2,3)"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(2,3) + 2"), InvalidInputException);
EXPECT_THROW(parseFintamath("cos(sin(2,3))"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(,)"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(,2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(2,)"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin()"), InvalidInputException);
EXPECT_THROW(parseFintamath("log(1)"), InvalidInputException);
EXPECT_THROW(parseFintamath("log()"), InvalidInputException);

EXPECT_THROW(parseFintamath("(1 = 1) / 2"), InvalidInputException);
EXPECT_THROW(parseFintamath("2 + (1 = 2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(1 = 1)"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(sin(1 = 1))"), InvalidInputException);
EXPECT_THROW(parseFintamath("sin(sin(sin(1 = 1)))"), InvalidInputException);
EXPECT_THROW(parseFintamath("True/True"), InvalidInputException);
EXPECT_THROW(parseFintamath("((1 == 1)) + ((1 == 2))"), InvalidInputException);
EXPECT_THROW(parseFintamath("((1 == 1)) - ((1 == 1))"), InvalidInputException);
EXPECT_THROW(parseFintamath("((1 == 1)) / ((1 == 1))"), InvalidInputException);
EXPECT_THROW(parseFintamath("(5+5)=(2=5)"), InvalidInputException);
EXPECT_THROW(parseFintamath("1+(sin(x)<2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("1/(sin(x)<2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("1+False"), InvalidInputException);
EXPECT_THROW(parseFintamath("False+1"), InvalidInputException);
EXPECT_THROW(parseFintamath("1=False"), InvalidInputException);
EXPECT_THROW(parseFintamath("False=1"), InvalidInputException);
EXPECT_THROW(parseFintamath("1&2"), InvalidInputException);
EXPECT_THROW(parseFintamath("x+1&x+2"), InvalidInputException);
EXPECT_THROW(parseFintamath("x+1&x"), InvalidInputException);
EXPECT_THROW(parseFintamath("x&x+2"), InvalidInputException);
EXPECT_THROW(parseFintamath("(x&y)=(y&z)"), InvalidInputException);
EXPECT_THROW(parseFintamath("derivative(x=y, x)"), InvalidInputException);
EXPECT_THROW(parseFintamath("derivative(x&y,x)"), InvalidInputException);
EXPECT_THROW(parseFintamath("derivative(True,a)"), InvalidInputException);
EXPECT_THROW(parseFintamath("(a+1)_(a>2)"), InvalidInputException);
EXPECT_THROW(parseFintamath("(x+1)_1"), InvalidInputException);
EXPECT_THROW(parseFintamath("(x*2)_1"), InvalidInputException);
EXPECT_THROW(parseFintamath("(x*2)_((x+2)_x)"), InvalidInputException);
EXPECT_THROW(parseFintamath("x^x_1"), InvalidInputException);
EXPECT_THROW(parseFintamath("E&a"), InvalidInputException);
EXPECT_THROW(parseFintamath("~Inf"), InvalidInputException);
EXPECT_THROW(parseFintamath("~Undefined"), InvalidInputException);
EXPECT_THROW(parseFintamath("a | Undefined"), InvalidInputException);

EXPECT_THROW(parseFintamath("min()"), InvalidInputException);
EXPECT_THROW(parseFintamath("min(True, False)"), InvalidInputException);
EXPECT_THROW(parseFintamath("max()"), InvalidInputException);
EXPECT_THROW(parseFintamath("max(True, False)"), InvalidInputException);
}
Loading

0 comments on commit e127673

Please sign in to comment.