Versions Compared

Key

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

...

Code Block
languagecpp
titlerun_tests.cpp
linenumberstrue
#include <gtest/gtest.h>
#include "operations.h"

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);
}

TEST(DivideOperation, NegitiveInput) {
    // 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);
}

TEST(DivideOperation, ZerioInput) {
    // Integer arguments
    EXPECT_THROW(divide(10, 0), std::overflow_error);
    // Floating-point arguments
    EXPECT_THROW(divide(10.0f, 0.0f), std::overflow_error);
}

On top of the file, apart from including {{<gtest/gtest.h>}} , the GoogleTest framework, the operations.h header file with the test function declaration should be included. Then, test suites can be written for each condition given. The example above specifies three different conditions: a positive input argument, a negative input argument, and a zero input argument.

sources:

, we will create the C++ source and header files in the appropriate directories. For the tutorial, we create operations.cpp in the src directory, which contains a simple divide function and the operation.h header file. However, C++ is a statically typed language and therefore we need to supply multiple definitions of a division function according to the type of its input arguments. The output of a function should always be floating point, but for the purposes of this tutorial, we predict that the input arguments can be both integers and floating point numbers. Therefore, two definitions have been introduced for each of these types. The header file should look like this:

...