#define CATCH_CONFIG_RUNNER
+
+#include <limits>
+#include <cmath>
+
#include "catch.hpp"
#include "operators.h"
#include "operands.h"
+#include "expression.h"
using namespace std;
-TEST_CASE("Noder")
+
+bool compare_equal(double a, double b)
{
- Const_int my_int{5};
- Const_double my_double{2};
- Const_double my_n_double{-2.3123124};
- Const_int zero{0};
+return std::abs(a - b) <= std::numeric_limits<double>::epsilon();
+}
+TEST_CASE("Noder")
+{
+ // Operands
SECTION("Operands")
{
+ Const_int my_int{5};
+ Const_double my_double{2};
+ Const_double my_n_double{-2.3123124};
+ Const_int zero{0};
+
CHECK( my_int.get_postfix() == "5" );
CHECK( my_double.get_postfix() == "2.000" );
CHECK( my_n_double.get_postfix() == "-2.312" );
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};
+
+ 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 );
+
+ }
- Addition my_add{my_int, my_double};
- Subtraction my_sub{my_int, my_double};
- Multiplication my_mult{my_int, my_double};
- Division my_div{my_int, my_double};
- Exponent my_exp{my_int, my_double};
+ 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};
+
+ 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 );
+ }
- SECTION("Operators")
+ SECTION("Mult")
{
- 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 );
-
- 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 );
-
- 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 );
-
- 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 );
-
- 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);
-
- CHECK_THROWS( Division{my_int, zero}.get_value() );
- CHECK_THROWS( Division{my_double, zero}.get_value() );
- CHECK_THROWS( Exponent{my_n_double, my_double}.get_value() );
- CHECK_THROWS( Exponent{zero, my_n_double}.get_value() );
+ Node * my_int = new Const_int{5};
+ Node * my_double = new Const_double{2};
+ Node* my_mult = new Multiplication{my_int, my_double};
+
+ 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 );
}
- SECTION("Compound Expressions")
+ SECTION("Div")
{
- Const_double my_double{2};
- Const_double my_double_2{1.5};
- Const_int my_int{3};
- Const_int my_int_2{7};
-
- Addition my_add{my_int, my_double};
- Multiplication my_mult{my_int_2, my_double_2};
- Exponent my_exp{my_add, my_mult};
-
- CHECK( my_exp.get_postfix() == "3 2.000 + 7 1.500 * ^" );
- CHECK( my_exp.get_infix() == "((3+2.000)^(7*1.500))" );
- CHECK( my_exp.get_prefix() == "^ + 3 2.000 * 7 1.500" );
- CHECK( my_exp.get_value() == 21836601.3427713849 );
+ Node * my_int = new Const_int{5};
+ Node * my_double = new Const_double{2};
+ Node* my_div = new Division{my_int, my_double};
+
+ 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 );
+ }
+
+ 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};
+
+ 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);
+ }
+ SECTION("error")
+ {
+ CHECK_THROWS( Division{new Const_int{2}, new Const_int{0}}.get_value() );
+ CHECK_THROWS( Division{new Const_double{2}, new Const_int{0}}.get_value() );
+ CHECK_THROWS( Exponent{new Const_double{-2.2}, new Const_double{2.3}}.get_value() );
+ CHECK_THROWS( Exponent{new Const_int{0}, new Const_double{-2.5}}.get_value() );
+ }
+
+
+ // Compount 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};
+
+ 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 );
}
}
+
TEST_CASE("Expression")
{
-<<<<<<< HEAD
- // Added another comment
-=======
- //Added a comment
->>>>>>> b58fae269fcfa155b9a9dd2c2df54e4f264fb325
- Expression e{"(1+2)^3"};
+
+ // Constructor
+ CHECK_THROWS( Expression{"+3 -( 1 ^2)"} );
+ CHECK_THROWS( Expression{"3-(1+^2)"} );
+ CHECK_THROWS( Expression{"3(1^2.123)"} );
+ CHECK_THROWS( Expression{"3(1^2)/2.1 2"} );
+ CHECK_THROWS( Expression{" "} );
+ CHECK_THROWS( Expression{"3+(banan^2)/ 2"} );
+ CHECK_THROWS( Expression{"3.54+ "} );
+ CHECK_THROWS( Expression{"pi.1+2.3"} );
+
+ Expression e1{" 3* 5 /(8 -2*(1+ 1))- 1 "};
+ Expression e2{"(3*5)/5 - 3.23452"};
+ Expression e4{"(1-3)^(0-2.3)"};
+
+ SECTION("String formats")
+ {
+ CHECK( e1.to_prefix_string() == "- / * 3 5 - 8 * 2 + 1 1 1" );
+ CHECK( e1.to_infix_string() == "(((3*5)/(8-(2*(1+1))))-1)" );
+ CHECK( e1.to_postfix_string() == "3 5 * 8 2 1 1 + * - / 1 -" );
+
+ CHECK( e2.to_prefix_string() == "- / * 3 5 5 3.235" );
+ 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 +" );
+ }
+
+ SECTION("Calculation")
+ {
+ CHECK( compare_equal(e1.evaluate(), 2.75) );
+ CHECK( compare_equal(e2.evaluate(),-0.23452) );
+
+ CHECK_THROWS( e4.evaluate() );
+ CHECK_THROWS( Expression{"7/0.0"}.evaluate() );
+ CHECK_THROWS( Expression{"0.0^(0-4)"}.evaluate() );
+ }
}
-TEST_CASE("Memory management")
+
+/*
+TEST_CASE("Memory")
{
-
+ Expression e1{"(1+2)^2"};
+ Expression e2{"(1-2)^2"};
+
+ e2 = std::move(e1);
+ CHECK( &e2 != &e1 );
+ CHECK( e2.evaluate() == e1.evaluate() );
}
+*/
int main()
{