Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# VSCode related
.vscode/

# MacOS related
.DS_Store
35 changes: 35 additions & 0 deletions cpp/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Build
build/

# Prerequisites
*.d

# Compiled Object files
*.slo
*.lo
*.o
*.obj

# Precompiled Headers
*.gch
*.pch

# Compiled Dynamic libraries
*.so
*.dylib
*.dll

# Fortran module files
*.mod
*.smod

# Compiled Static libraries
*.lai
*.la
*.a
*.lib

# Executables
*.exe
*.out
*.app
69 changes: 69 additions & 0 deletions cpp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
CXX = clang++
CXXFLAGS = -std=c++17 -Wall -Wextra -Werror -g
PROBLEM_DIR = problems
TEST_DIR = tests
BUILD_DIR = build

# Colors for output
RED = \033[0;31m
GREEN = \033[0;32m
YELLOW = \033[1;33m
BLUE = \033[1;34m
NC = \033[0m # No Color

# Create build directory structure if it doesn't exist
$(shell mkdir -p $(BUILD_DIR))
$(shell find $(PROBLEM_DIR) -type d | sed 's|^$(PROBLEM_DIR)|$(BUILD_DIR)|' | xargs -I {} mkdir -p {})
$(shell find $(TEST_DIR) -type d | sed 's|^$(TEST_DIR)|$(BUILD_DIR)|' | xargs -I {} mkdir -p {})

# Find all .cpp files in problems and tests subdirectories (recursively)
PROBLEM_SRCS := $(shell find $(PROBLEM_DIR) -name "*.cpp")
TEST_SRCS := $(filter-out $(TEST_DIR)/test.cpp, $(shell find $(TEST_DIR) -name "*.cpp"))
MAIN_SRC := $(TEST_DIR)/test.cpp

# Convert source file paths to object file paths in the build directory
PROBLEM_OBJS := $(patsubst $(PROBLEM_DIR)/%.cpp, $(BUILD_DIR)/%.o, $(PROBLEM_SRCS))
TEST_OBJS := $(patsubst $(TEST_DIR)/%.cpp, $(BUILD_DIR)/%.o, $(TEST_SRCS))
MAIN_OBJ := $(BUILD_DIR)/test.o

# Target to build all tests
all: $(BUILD_DIR)/tests

# Compile each problem .cpp file to a .o file in the build directory
$(BUILD_DIR)/%.o: $(PROBLEM_DIR)/%.cpp
@echo "$(YELLOW)Compiling $<...$(NC)"
$(CXX) $(CXXFLAGS) -c -o $@ $<

# Compile each test .cpp file to a .o file in the build directory
$(BUILD_DIR)/%.o: $(TEST_DIR)/%.cpp
@echo "$(YELLOW)Compiling $<...$(NC)"
$(CXX) $(CXXFLAGS) -c -o $@ $<

# Compile the main file separately
$(MAIN_OBJ): $(MAIN_SRC)
@echo "$(YELLOW)Compiling main file $<...$(NC)"
$(CXX) $(CXXFLAGS) -c -o $@ $<

# Link all object files to create the test executable
$(BUILD_DIR)/tests: $(PROBLEM_OBJS) $(TEST_OBJS) $(MAIN_OBJ)
@echo "$(GREEN)Linking all tests...$(NC)"
$(CXX) $(CXXFLAGS) -o $@ $^

# Run tests
run_tests: $(BUILD_DIR)/tests
@echo "$(GREEN)Running all tests...$(NC)"
$(BUILD_DIR)/tests

# Formatting
format:
@echo "$(BLUE)Formatting C++ files...$(NC)"
@clang-format -i $(PROBLEM_SRCS) $(TEST_SRCS) $(MAIN_SRC)
@echo "$(GREEN)Formatting completed.$(NC)"

# Clean build artifacts
clean:
@echo "$(RED)Cleaning build artifacts...$(NC)"
@rm -rf $(BUILD_DIR)/*
@echo "$(GREEN)Clean completed.$(NC)"

.PHONY: all run_tests clean format
5 changes: 5 additions & 0 deletions cpp/problems/easy/add_two_numbers/add_two_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "add_two_numbers.h"

int add_two_numbers(int a, int b) {
return a + b;
}
6 changes: 6 additions & 0 deletions cpp/problems/easy/add_two_numbers/add_two_numbers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef ADD_TWO_NUMBERS_H
#define ADD_TWO_NUMBERS_H

int add_two_numbers(int a, int b);

#endif // ADD_TWO_NUMBERS_H
14 changes: 14 additions & 0 deletions cpp/tests/easy_tests/add_two_numbers/test_add_two_numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "test_add_two_numbers.h"

#include <cassert>
#include <iostream>
#include "../../../problems/easy/add_two_numbers/add_two_numbers.h"

void test_add_two_numbers() {
assert(add_two_numbers(2, 3) == 5);
assert(add_two_numbers(-1, 1) == 0);
assert(add_two_numbers(0, 0) == 0);
assert(add_two_numbers(100, 200) == 300);
std::cout << "All tests passed!" << std::endl;
}

6 changes: 6 additions & 0 deletions cpp/tests/easy_tests/add_two_numbers/test_add_two_numbers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef TEST_ADD_TWO_NUMBERS_H
#define TEST_ADD_TWO_NUMBERS_H

void test_add_two_numbers();

#endif // TEST_ADD_TWO_NUMBERS_H
8 changes: 8 additions & 0 deletions cpp/tests/easy_tests/test_easy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "test_easy.h"

#include "add_two_numbers/test_add_two_numbers.h"

void test_easy()
{
test_add_two_numbers();
}
6 changes: 6 additions & 0 deletions cpp/tests/easy_tests/test_easy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef TEST_EASY_H
#define TEST_EASY_H

void test_easy();

#endif // TEST_EASY_H
6 changes: 6 additions & 0 deletions cpp/tests/hard_tests/test_hard.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "test_hard.h"

void test_hard()
{

}
1 change: 1 addition & 0 deletions cpp/tests/hard_tests/test_hard.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void test_hard();
6 changes: 6 additions & 0 deletions cpp/tests/medium_tests/test_medium.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include "test_medium.h"

void test_medium()
{

}
1 change: 1 addition & 0 deletions cpp/tests/medium_tests/test_medium.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
void test_medium();
10 changes: 10 additions & 0 deletions cpp/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "easy_tests/test_easy.h"
#include "medium_tests/test_medium.h"
#include "hard_tests/test_hard.h"

int main() {
test_easy();
test_medium();
test_hard();
return 0;
}