Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This page shows a variation of the Calculator project called AdvancedCalculator.This is a more extensive version of the same design. Contains new functionality in operation.cpp and therefore has more test cases.

Table of Contents

Info
titleThe source code can be find here:

https://gitlab.eufus.eu/bpogodzinski/ach-tutorials/-/tree/TDD-cpp/TDD-cpp/AdvancedCalculator

Info
titleNote

This project can be run in the same way as shown in basic use, please check this section: 2. Build the project  - CMake


1. Division and Division Errors - Testing Exceptions

This section is basically the same in terms of test suites as in the Calculator project, but was not covered on the previous page.

The following test suites can be found in the tst directory in test_operation.cpp:

Code Block
languagecpp
titleTEST(DivideOperation, PositiveInput)
linenumberstrue
TEST(DivideOperation, PositiveInput) {
    // Integer arguments
    ASSERT_EQ(divide(10, 5), 2);
    ASSERT_FLOAT_EQ(divide(5, 10), 0.5);
    // Floating-point arguments
    ASSERT_FLOAT_EQ(divide(10.0f, 5.0f), 2.0f);
    ASSERT_FLOAT_EQ(divide(5.0f, 10.0f), 0.5f);
}

This is a very simple check that verifies that an input is producing the desired result. However, the division function itself has two forms, one for integer input data and one for floating point data. In such a situation, both cases should be checked.

Code Block
languagecpp
titleTEST(DivideOperation, NegitiveInput)
linenumberstrue
TEST(DivideOperation, NegitiveInput) {
    ASSERT_EQ(divide(-10, -5), 2);
    ASSERT_FLOAT_EQ(divide(-5, -10), 0.5);
    // Floating-point arguments
    ASSERT_FLOAT_EQ(divide(-10.0f, -5.0f), 2.0f);
    ASSERT_FLOAT_EQ(divide(-5.0f, -10.0f), 0.5f);
}

Next (above) is the negative input verification, which has exactly the same operating logic as the previous test suite, but with negative signed arguments.

And last but not least in this section, below is the zero division error check:

Code Block
languagecpp
titleTEST(DivideOperation, ZerioInput)
linenumberstrue
TEST(DivideOperation, ZerioInput) {
    EXPECT_THROW(divide(10, 0), std::overflow_error);
    EXPECT_THROW(divide(10.0f, 0.0f), std::overflow_error);
}

It is important to note that the assertion used is EXPECT_THROW, which has been specifically designed to catch and inspect a specific exception. It is also important to test for both types of input arguments.

The divide function itself has a very simple body:

Code Block
languagecpp
titlefloat divide(int numerator, int denominator)
linenumberstrue
float divide(int numerator, int denominator){
    if(denominator == 0){
        throw std::overflow_error("Divide by zero exception!!! Verify the denominator value!");
    }
    return float(numerator) / float(denominator);
}

The return type of this function is floating point because even if both of the arguments supplied are integers, the result need not be an integer. Basically the function takes two arguments and checks that the denominator is not zero in any case, otherwise an overflow_error exception will be thrown with the custom message.

2. Testing whether the result is within range


3. Testing boundary conditions