We will start by reusing the example source code from Recipe 1, Creating a simple unit test, in Chapter 4, Creating and Running Tests, which sums integers given as command-line arguments. The example consists of three source files: main.cpp, sum_integers.cpp, and sum_integers.hpp. These sources are unchanged. We will also reuse the file test.cpp from Chapter 4, Creating and Running Tests, but will rename it to test_short.cpp. We will extend the example with test_long.cpp, containing the following code:
#include "sum_integers.hpp"
#include <numeric>
#include <vector>
int main() {
// creates vector {1, 2, 3, ..., 999, 1000}
std::vector<int> integers(1000);
std::iota(integers.begin(), integers.end(), 1);
if (sum_integers(integers) == 500500) {
return 0;
} else {
return 1;
}
}
We will then organize these files into the following file tree:
.
├── CMakeLists.txt
├── CTestConfig.cmake
├── src
│ ├── CMakeLists.txt
│ ├── main.cpp
│ ├── sum_integers.cpp
│ └── sum_integers.hpp
└── tests
├── CMakeLists.txt
├── test_long.cpp
└── test_short.cpp