Exercise
First part
Edit the implementation of the Sphere class in the cxx/Sphere.cpp
file and
have a look at the code of the method Sphere::volume()
.
double Sphere::volume() const
{
return 4 * M_PI * pow (this->radius_, 3.) / 3.;
}
This is the method which is tested in the unique test of the library, and the
test is implemented in the file /cxx/tests/testSphere.cpp
.
Let's imagine we want to improve the efficiency of the Sphere::volume()
method by pre-computing the value 4 * M_PI / 3
TODO :
- Extract this value into a class data member.
- Run the benchmark. This may show only a very, very minimal improvement.
- Run the test.
The test may still be successful : this may depend on your hardware and compiler.
Let's assume it is successful : do not forget to commit your modifications.
git commit -a -m "precomputation of 4pi/3"
git push
The test should now fail on GitLab CI.
Here is the output you can see on GitLab CI job result console:
$ ctest --output-on-failure
Test project /home/user/gitlabciintroduction/cxx/build
Start 1: TestVolume
1/1 Test #1: TestVolume .......................***Failed 0.00 sec
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from TestSphere
[ RUN ] TestSphere.testVolume
/home/user/gitlabciintroduction/cxx/tests/TestSphere.cpp:14: Failure
Value of: 14.137166941154069 == sphere.volume()
Actual: false
Expected: true
[ FAILED ] TestSphere.testVolume (0 ms)
[----------] 1 test from TestSphere (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[ PASSED ] 0 tests.
[ FAILED ] 1 test, listed below:
[ FAILED ] TestSphere.testVolume
1 FAILED TEST
0% tests passed, 1 tests failed out of 1
Total Test time (real) = 0.00 sec
The following tests FAILED:
1 - TestVolume (Failed)
Errors while running CTest
Second part
The -funsafe-math-optimizations implied by -ffast-math allows for reordering of floating points operations and this may lead to different results.
The direct check of equality of floating point numbers with the ==
operator (cf. tests/TestSphere.cpp
file) should be avoided. So we can add a
warning (only if understood by the compiler) in order not to reproduce this
kind of error.
Since version 4.6, GNU C++ provides this warning -Wfloat-equal. With portability in mind, before adding the flag to the compiler command, we need to check that the compiler accepts it.
A macro add_cxx_compiler_flag
is provided in the cxx/cmake/utils.cmake
module file.
To load this utils
module, you need to modify the root CMakelists.txt file.
- Set the CMAKE_MODULE_PATH to this directory (using CMAKE_CURRENT_SOURCE_DIR ):
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
- Load the module
utils
. Beware: this should be done before using the macro:
include(utils)
TODO :
- In the CMake configuration file, CMakeLists.txt, set the
CMAKE_MODULE_PATH
and include the
utils
module. - With the provided CMake
add_cxx_compiler_flag
macro, add the compiler warning -Wfloat-equal to the CMake configuration. - Verify that the flag -Wfloat-equal is used for the compilation.
- Verify that the warnings is printed during compilation.
Home | C++ Home | << C++ Previous - GitLab CI setup | >> C++ Next - GitLab CI pipelines