Skip to content
Snippets Groups Projects
Select Git revision
  • 1c6721991aaaa671e2d55e73a24815993904bd1a
  • master default protected
2 results

test.cpp

Blame
  • test.cpp 9.09 KiB
    #include <iostream>
    #include <cassert>
    #include "Fraction.hpp"
    #include "NullDivisionException.hpp"
    
    /**
      * If you have not implemented all functionality yet, you can simply
      * deselect the testcases by setting the corresponding define to 0.
      * Then your programm will compile and you can test the other testcases.
      */
    #define CONSTRUCTORS 1
    #define CONSTRUCTOR_NULL_DIVISION_EXCEPTION 1
    #define REDUCE_FRACTION 1
    #define COPY_ASSIGNMENT 1
    #define OUT_OPERATOR 0 // will follow soon
    #define PLUS_OPERATORS 0 // will follow soon
    #define PLUS_OPERATORS_WITH_INT 0 // will follow soon
    #define INCREMENT_OPERATORS 0 // will follow soon
    #define NEGATION_INVERSION 0 // will follow soon
    #define BOOL_OPERATOR 0 // will follow soon
    #define COMPARE_OPERATORS 0 // will follow soon
    
    void constructors();
    void constructorNullDivisionException();
    void reduceFraction();
    void copyAssignment();
    void outOperator();
    void plusOperators();
    void plusOperatorsWithInt();
    void incrementOperators();
    void negationInversion();
    void boolOperators();
    void compareOperators();
    
    //------------------------------------------------------------------------------
    int main(void)
    {
      std::cout << "Starting testcases!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
      constructors();
      constructorNullDivisionException();
      reduceFraction();
      copyAssignment();
      outOperator();
      plusOperators();
      plusOperatorsWithInt();
      incrementOperators();
      negationInversion();
      boolOperators();
      compareOperators();
    
      std::cout << "Great! Passing all selected testcases!" << std::endl;
    
      return 0;
    }
    
    //------------------------------------------------------------------------------
    void constructors()
    {
    #if CONSTRUCTORS == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a;
      Fraction b(5);
      Fraction c(1, 10);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
      std::cout << b.getNominator() << " / " << b.getDenominator() << std::endl;
      std::cout << c.getNominator() << " / " << c.getDenominator() << std::endl;
    
      assert(a.getNominator() == 0 && a.getDenominator() == 1 &&
        "Default constructor not correct!\n");
      assert(b.getNominator() == 5 && b.getDenominator() == 1 &&
        "Constructor with one paramter not correct!\n");
      assert(c.getNominator() == 1 && c.getDenominator() == 10 &&
        "Constructor with two parameters not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void constructorNullDivisionException()
    {
    #if CONSTRUCTOR_NULL_DIVISION_EXCEPTION == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      try
      {
        Fraction a(2, 0);
    
        std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
        assert(false && "No NullDivisionlException thrown!\n");
      }
      catch (NullDivisionException& ex)
      {
        std::cout << "Error: " << ex.what();
    
        assert(std::string(ex.what()) == "2 / 0 - Dividing through 0 not valid!\n"
          && "NullDivisionException not correct!\n");
      }
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void reduceFraction()
    {
    #if REDUCE_FRACTION == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
      Fraction b(65, -26);
      Fraction c(-123, 126);
      Fraction d(-12, -144);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
      std::cout << b.getNominator() << " / " << b.getDenominator() << std::endl;
      std::cout << c.getNominator() << " / " << c.getDenominator() << std::endl;
      std::cout << d.getNominator() << " / " << d.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
      assert(b.getNominator() == -5 && b.getDenominator() == 2 &&
        "Reducing fraction not correct!\n");
      assert(c.getNominator() == -41 && c.getDenominator() == 42 &&
        "Reducing fraction not correct!\n");
      assert(d.getNominator() == 1 && d.getDenominator() == 12 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void copyAssignment()
    {
    #if COPY_ASSIGNMENT == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(13, -169);
      Fraction b(a);
      Fraction c(20);
      Fraction d;
    
      a = c = d;
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
      std::cout << b.getNominator() << " / " << b.getDenominator() << std::endl;
      std::cout << c.getNominator() << " / " << c.getDenominator() << std::endl;
      std::cout << d.getNominator() << " / " << d.getDenominator() << std::endl;
    
      assert(a.getNominator() == 0 && a.getDenominator() == 1 &&
        "Copy-Assignment-Operator not extendable!\n");
      assert(b.getNominator() == -1 && b.getDenominator() == 13 &&
        "Copy-Constructor not working!\n");
      assert(c.getNominator() == 0 && c.getDenominator() == 1 &&
        "Copy-Assignment-Operator not working!\n");
      assert(a.getNominator() == 0 && a.getDenominator() == 1 &&
        "Default Constructor not working!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void outOperator()
    {
    #if OUT_OPERATOR == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void plusOperators()
    {
    #if PLUS_OPERATORS == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void plusOperatorsWithInt()
    {
    #if PLUS_OPERATORS_WITH_INT == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void incrementOperators()
    {
    #if INCREMENT_OPERATORS == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void negationInversion()
    {
    #if NEGATION_INVERSION == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void boolOperators()
    {
    #if BOOL_OPERATOR == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }
    
    //------------------------------------------------------------------------------
    void compareOperators()
    {
    #if COMPARE_OPERATORS == 1
    
      std::cout << "Testcase: " << __func__ << std::endl << std::endl;
    
      Fraction a(2, 6);
    
      std::cout << a.getNominator() << " / " << a.getDenominator() << std::endl;
    
      assert(a.getNominator() == 1 && a.getDenominator() == 3 &&
        "Reducing fraction not correct!\n");
    
      std::cout << "\nAll good!\n\n";
      std::cout << "-----------------------------------------------\n" << std::endl;
    
    #endif
    }