fixed memory leaks and testing
authorAlma Dejus <almde515@student.liu.se>
Sat, 28 Oct 2023 12:42:33 +0000 (14:42 +0200)
committerAlma Dejus <almde515@student.liu.se>
Sat, 28 Oct 2023 12:42:33 +0000 (14:42 +0200)
src/expression.cc
src/test.cc

index 92e3a2cb6fe168a35c2e250cccfc26d95187b36d..67378b828bcc8c5202cf1b0c70fdc038da6f78e3 100644 (file)
@@ -129,7 +129,13 @@ std::string Expression::to_prefix_string() const
 
 double Expression::evaluate() const
 {
-    return expression_root->get_value();
+    if (expression_root == nullptr) {
+        throw std::logic_error("Cannot evaluate empty or undefinded expression");
+    }
+    else{
+        return expression_root->get_value();
+    }
+    
 }
 
 void Expression::unlink()
@@ -146,7 +152,9 @@ Expression & Expression::operator=(Expression && other)
     }
 
     delete expression_root;
+
     expression_root = other.expression_root;
+
     other.unlink();
     return *this;
 }
\ No newline at end of file
index ad417328b343ba1c1caa4bad8d80fa7234e609cf..20258494b643619df00e127865acb28e0b2a95e7 100644 (file)
@@ -43,67 +43,66 @@ TEST_CASE("Noder")
         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")
     {       
@@ -113,30 +112,26 @@ TEST_CASE("Noder")
         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)"} );
@@ -149,7 +144,8 @@ TEST_CASE("Expression")
     
     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")
     {
@@ -161,33 +157,43 @@ TEST_CASE("Expression")
         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()
 {