This page describes the specifics of implementing algorithms and data structures in C++. If you have not already done so, please read the general contribution guidelines.
This project adheres to the C++ Coding Guidelines. Please read (and follow!) them.
Algorithms and data structures are implemented as header files (.hpp) in the include
directory and then verified via
unit test files (.cpp) in the test
directory. If you're adding an algorithm or data structure, you'll write both
the implementation and the unit tests.
-
Under
test
, locate (or create, if it doesn't exist) the directory having the same relative path fromtest
as frominclude
. For example, if you've created a header file ininclude/algorithm/searching
, locate (or create) the directorytest/algorithm/searching
. -
Create a file with the same name as the corresponding header file for which you are writing tests, except that the extension should be
.cpp
. -
Add the following code to the test file:
#include "third_party/catch.hpp" #include "path/to/header_file.hpp"
The path to the header file is relative from the
include
directory. So, for instance, if you need to include the header fileinclude/algorithm/searching/linear_search.hpp
, you can write:#include "algorithm/searching/linear_search.hpp"
-
After these lines you can add your test cases. For details regarding how to write test cases using the Catch test framework, I suggest you to go through this short tutorial. You can also take a look at the existing unit tests to get a clearer idea about how the tests are written.
-
Add an entry for your unit test in
CMakeLists.txt
. For example, if your unit test istest/algorithm/searching/linear_search.cpp
, add the following entry for it:add_executable(linear_search test/algorithm/searching/linear_search.cpp) target_link_libraries(linear_search test_runner)
That's it! Now you can compile the test using make test
from the
C++
directory, which will also run all of the tests for you. In order to run
only a specific test and see its results, run it manually from the bin
directory.