Data Structures and Algorithms in C++
This repo contains C++ implementations of common data structures and algorithms.
- Build system: CMake (C++17)
- Tests: GoogleTest (auto-downloaded with CMake)
- Simple layout: headers in
include/, sources insrc/, tests intests/
- CMake 3.10+
- A C++17 compiler (g++/clang++)
- Build toolchain (Ninja or Make; CMake will pick one available)
cmake -S . -B build
cmake --build build -j./build/cpp-dsaAll test files under tests/*.cpp are compiled into a single test runner (unit_tests). No CMake edits are needed when adding new test files.
Run the full suite:
ctest --test-dir build --output-on-failureRun the test binary directly (useful for filtering):
./build/unit_tests --gtest_filter="LinkedListTest.*"- Create a header in
include/, e.g.include/stack.h. - Implement it in
src/…, e.g.src/data_structures/stack.cpp.
The project builds a reusable library from everything in src/ (except src/main.cpp), so your new code is automatically compiled for both the app and the tests.
- Create a file under
tests/, e.g.tests/test_stack.cpp. - Write GoogleTest cases:
#include <gtest/gtest.h>
#include "../include/stack.h"
TEST(StackTest, PushPop) {
Stack s;
s.push(1);
s.push(2);
EXPECT_EQ(s.pop(), 2);
EXPECT_EQ(s.pop(), 1);
}- Rebuild and run tests (no CMake changes required):
cmake --build build -j
ctest --test-dir build --output-on-failureinclude/ # Public headers
src/ # Implementations (main in src/main.cpp)
tests/ # GoogleTest sources (auto-discovered)
CMakeLists.txt # Build config (downloads GoogleTest)
build/ # Out-of-source build directory
- Use
--gtest_filterto run a subset of tests. - If you rename/move many files, a fresh configure can help:
rm -rf build && cmake -S . -B build && cmake --build build -j- First configure takes a moment because CMake downloads GoogleTest.
- A warning about DOWNLOAD_EXTRACT_TIMESTAMP from FetchContent is safe to ignore.