Versions Compared

Key

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

...

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. There is also duplication of assertions for two types of arguments - floating point numbers and integers. This is especially important in the zero-input test suite, where a floating point number given as (0,0) may be evaluated as close to zero but not equal to it. However, in both cases this is done to pass a number of zero and should be detected by the function. In this case, it is expected to catch an overflow_error exception.

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. HoweverWhen the tests are ready, the source code can be developed. Following the above guidelines, two source files will be created in the src directory (if not already done), the operations.h and the operations.cpp. The first one will of course store the divide() function declaration, and the second one the definition. 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 The header file should look like this:

...

The presented function has a mechanism for verifying whether the divisor (denominator) is equal to zero or is close to zero in the case of a floating-point number. For the purposes of this example, it has been arbitrarily assumed that numbers less than 1e-5 are zero, although this is not a generally accepted rule. In case the condition evaluates to True, an exception is thrown. To implement the exception, include the <stdexcept> library in the header file.  Under Under normal circumstances, the output value is computed and returned as floating point in both cases.

...