CHECK( my_double.get_value() == 2.0 );
CHECK( my_n_double.get_value() == -2.3123124 );
}
+
// Operators
SECTION("Add")
{
- Node * my_int = new Const_int{5};
- Node * my_double = new Const_double{2};
-
- Node* my_add = new Addition{my_int, my_double};
+ Node * my_add = new Addition{new Const_int{5}, new Const_double{2}};
CHECK( my_add->get_postfix() == "5 2.000 +" );
CHECK( my_add->get_infix() == "(5+2.000)" );
CHECK( my_add->get_prefix() == "+ 5 2.000" );
CHECK( my_add->get_value() == 7.0 );
+ delete my_add;
}
SECTION("Sub")
{
- Node * my_int = new Const_int{5};
- Node * my_double = new Const_double{2};
- Node* my_sub = new Subtraction{my_int, my_double};
+ Node * my_sub = new Subtraction{new Const_int{5}, new Const_double{2}};
CHECK( my_sub->get_postfix() == "5 2.000 -" );
CHECK( my_sub->get_infix() == "(5-2.000)" );
CHECK( my_sub->get_prefix() == "- 5 2.000" );
CHECK( my_sub->get_value() == 3.0 );
+
+ delete my_sub;
}
SECTION("Mult")
{
- Node * my_int = new Const_int{5};
- Node * my_double = new Const_double{2};
- Node* my_mult = new Multiplication{my_int, my_double};
+ Node* my_mult = new Multiplication{new Const_int{5}, new Const_double{2}};
CHECK( my_mult->get_postfix() == "5 2.000 *" );
CHECK( my_mult->get_infix() == "(5*2.000)" );
CHECK( my_mult->get_prefix() == "* 5 2.000" );
CHECK( my_mult->get_value() == 10.0 );
+
+ delete my_mult;
}
SECTION("Div")
{
- Node * my_int = new Const_int{5};
- Node * my_double = new Const_double{2};
- Node* my_div = new Division{my_int, my_double};
+ Node* my_div = new Division{new Const_int{5}, new Const_double{2}};
CHECK( my_div->get_postfix() == "5 2.000 /" );
CHECK( my_div->get_infix() == "(5/2.000)" );
CHECK( my_div->get_prefix() == "/ 5 2.000" );
CHECK( my_div->get_value() == 2.5 );
- }
+ delete my_div;
+ }
+
SECTION("Exp")
{
- Node * my_int = new Const_int{5};
- Node * my_double = new Const_double{2};
- Node* my_exp = new Exponent{my_int, my_double};
+ Node* my_exp = new Exponent{new Const_int{5}, new Const_double{2}};
CHECK( my_exp->get_postfix() == "5 2.000 ^" );
CHECK( my_exp->get_infix() == "(5^2.000)" );
CHECK( my_exp->get_prefix() == "^ 5 2.000" );
CHECK( my_exp->get_value() == 25.0);
+
+ delete my_exp;
}
SECTION("error")
{
CHECK_THROWS( Exponent{new Const_int{0}, new Const_double{-2.5}}.get_value() );
}
-
- // Compount expressions
+ // Compound expressions
SECTION("Compound")
{
- Node * my_int = new Const_int{5};
- Node * my_double = new Const_double{2};
- Node* my_mult = new Multiplication{my_int, my_double};
-
- Node * my_int2 = new Const_int{5};
- Node * my_double2 = new Const_double{2};
- Node* my_exp = new Exponent{my_int2, my_double2};
-
- Node* my_sub = new Subtraction{my_exp, my_mult};
+ Node * my_sub = new Subtraction{new Exponent{new Const_int{5},
+ new Const_double{2}},
+ new Multiplication{new Const_int{5},
+ new Const_double{2}}};
CHECK( my_sub->get_postfix() == "5 2.000 ^ 5 2.000 * -" );
CHECK( my_sub->get_infix() == "((5^2.000)-(5*2.000))" );
CHECK( my_sub->get_prefix() == "- ^ 5 2.000 * 5 2.000" );
CHECK( my_sub->get_value() == 15.0 );
+
+ delete my_sub;
}
+
}
TEST_CASE("Expression")
{
-
// Constructor
CHECK_THROWS( Expression{"+3 -( 1 ^2)"} );
CHECK_THROWS( Expression{"3-(1+^2)"} );
Expression e1{" 3* 5 /(8 -2*(1+ 1))- 1 "};
Expression e2{"(3*5)/5 - 3.23452"};
- Expression e4{"(1-3)^(0-2.3)"};
+ Expression e3{"(1-3)^(0-2.3)"};
+ Expression e4{"1^2.3"};
SECTION("String formats")
{
CHECK( e2.to_infix_string() == "(((3*5)/5)-3.235)" );
CHECK( e2.to_postfix_string() == "3 5 * 5 / 3.235 -" );
- CHECK( e3.to_prefix_string() == "+ 1.200 4.000" );
- CHECK( e3.to_infix_string() == "(1.200+4.000)" );
- CHECK( e3.to_postfix_string() == "1.200 4.000 +" );
+ CHECK( e3.to_prefix_string() == "^ - 1 3 - 0 2.300" );
+ CHECK( e3.to_infix_string() == "((1-3)^(0-2.300))" );
+ CHECK( e3.to_postfix_string() == "1 3 - 0 2.300 - ^" );
+
+ CHECK( e4.to_prefix_string() == "^ 1 2.300" );
+ CHECK( e4.to_infix_string() == "(1^2.300)" );
+ CHECK( e4.to_postfix_string() == "1 2.300 ^" );
}
SECTION("Calculation")
{
CHECK( compare_equal(e1.evaluate(), 2.75) );
- CHECK( compare_equal(e2.evaluate(),-0.23452) );
+ CHECK( compare_equal(e2.evaluate(), -0.23452) );
+ CHECK( e4.evaluate() == 1 );
- CHECK_THROWS( e4.evaluate() );
+ CHECK_THROWS( e3.evaluate() );
CHECK_THROWS( Expression{"7/0.0"}.evaluate() );
CHECK_THROWS( Expression{"0.0^(0-4)"}.evaluate() );
}
}
-/*
TEST_CASE("Memory")
{
Expression e1{"(1+2)^2"};
Expression e2{"(1-2)^2"};
+ CHECK( e2.evaluate() == 1);
+
e2 = std::move(e1);
+
CHECK( &e2 != &e1 );
- CHECK( e2.evaluate() == e1.evaluate() );
+
+ CHECK_THROWS( e1.evaluate() );
+
+ CHECK( e2.evaluate() == 9.0);
}
-*/
+
int main()
{