Code without tests (aka. legacy code) is extremely difficult to work with
Unit tests...
Essentially none, but writing unit tests ...
For writing, compiling and running unit tests, a unit testing framework is required.
There are many concurring unit testing frameworks for C++.
At the time of this writing, Boost Test, Catch2 and GoogleTest stand out in terms of quality and features.
#include <cstdint>
// Return the sum of all values from 1 to n.
uint64_t add_to(uint32_t n) {
uint64_t sum = 0;
for (uint32_t i = 1; i <= n; ++i) {
sum += i;
}
return sum;
}
Download before_refactoring.cpp
After experiencing performance problems, we want to use a faster algorithm
#include <cstdint>
// Return the sum of all values from 1 to n.
uint64_t add_to(uint32_t n) {
return (uint64_t) (n / 2.0) * (n + 1);
}
Download after_refactoring.cpp
How can we apply the change without risking to destroy our application?
Using pencil and paper / brainstorming: define relevant test cases
25 is a possible common case.
Add 26 as an even input. 500000000 is a possible large input.
The edge cases should include 0 and 4294967295.
Ideally, also add 1, 2, 4294967293 and 4294967294.
Expected results for these are 325, 351, 125000000250000000, 0 and 9223372034707292160 as well as 1, 3, 9223372026117357571 and 9223372030412324865.
Note that 4294967295 does not terminate with the current implementation.
#include <cstdint>
#include <limits>
#include "gtest/gtest.h"
uint64_t add_to(uint32_t n); // usually in header file
TEST(sum_to, fast_test) {
EXPECT_EQ(add_to(0), 0);
EXPECT_EQ(add_to(1), 1);
EXPECT_EQ(add_to(2), 3);
EXPECT_EQ(add_to(25), 325); // EXPECT leads to failure but continues
ASSERT_EQ(add_to(26), 351); // ASSERT terminates test immediately
EXPECT_EQ(add_to(500000000), 125000000250000000);
}
TEST(sum_to, slow_test) {
EXPECT_EQ(add_to(4294967293), 9223372026117357571);
EXPECT_EQ(add_to(4294967294), 9223372030412324865);
}
// does not terminate for the old code version due to an overflow of the
// loop counter (the loop would terminate only at UINT_MAX + 1)
TEST(sum_to, edge_test) {
EXPECT_EQ(add_to(std::numeric_limits<uint32_t>::max()),
9223372034707292160);
}
Download sum_to_test.cpp
clang++ --std=c++17 before_refactoring.cpp \
sum_to_test.cpp -lgtest_main -lgtest -o run_tests
./run_tests # does not terminate; interrupt via [Ctrl]+[C]
What to do?
./run_tests --help
./run_tests --gtest_list_tests # list all available tests
./run_tests --gtest_filter=sum_to.fast_test
For the sake of the presentation, instead of applying the change, we use another source file.
Download after_refactoring.cppclang++ --std=c++17 after_refactoring.cpp \
sum_to_test.cpp -lgtest_main -lgtest -o run_tests
./run_tests
Now the tests are fast, but they are failing. Fix the code, then compile and run the tests again.
sudo apt install googletest
sudo mkdir /usr/src/googletest/build
sudo chown $USER /usr/src/googletest/build
cd /usr/src/googletest/build
cmake ..
make -j4
sudo make install
sudo ldconfig
For other operating systems, follow the instructions in project's README.
Comprehensive documentation can be found in GoogleTest User’s Guide.
TDD is a methodology that emphasizes writing tests before the actual code
Helps ensure that code is tested thoroughly and avoids regressions
Repeat the cycle for each new piece of functionality
Whenever you are tempted to type something into a print
statement or a debugger expression, write it as a test instead.