...
And the contents of the operation.cpp looks like this:
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
#include "operations.h" 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); } float divide(float numerator, float denominator){ if(abs(denominator) <= 1e-5){ throw std::overflow_error("Divide by zero exception!!! The denominator value close to zero!"); } return numerator / denominator; } |
The presented function has a verification mechanism whether the divisor (denominator) is equal to zero, or in the case of a floating point number, it is close to zero.
tests:
We also need to create a folder for our tests. In this tutorial, this folder is called tst. To start writing tests, we will create run_tests.cpp and test_operations.cpp. We want GoogleTest to search for test files automatically, and we do this thanks to the code stored in the run_tests.cpp source file. Typically, this code will be universal for all kinds of projects:
...