From c2ff10026dad4460aaabdd39181feae705103780 Mon Sep 17 00:00:00 2001 From: Barrett Ruth Date: Sat, 30 Aug 2025 19:01:11 -0500 Subject: [PATCH] cmake config --- .github/workflows/ci.yaml | 13 +++-- .gitignore | 2 + CMakeLists.txt | 82 +++++++++++++++++++++++++++++ compile_flags.txt | 30 ----------- makefile | 108 -------------------------------------- tests/CMakeLists.txt | 17 ++++++ tests/test_bmath.cc | 75 ++++++++++++++++++++++++++ tests/test_compilation.cc | 23 -------- 8 files changed, 186 insertions(+), 164 deletions(-) create mode 100644 CMakeLists.txt delete mode 100644 compile_flags.txt delete mode 100644 makefile create mode 100644 tests/CMakeLists.txt create mode 100644 tests/test_bmath.cc delete mode 100644 tests/test_compilation.cc diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 45f1de0..21cd678 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -22,9 +22,16 @@ jobs: build-and-test: runs-on: ubuntu-latest + strategy: + matrix: + build_type: [Debug, Release] steps: - uses: actions/checkout@v4 - name: Install compiler - run: sudo apt-get update && sudo apt-get install -y g++ - - name: Debug tests (asserts + sanitizers) - run: make test MODE=debug + run: sudo apt-get update && sudo apt-get install -y g++ cmake + - name: Configure CMake + run: cmake -B build -DCMAKE_BUILD_TYPE=${{ matrix.build_type }} + - name: Build + run: cmake --build build + - name: Test + run: ctest --test-dir build --output-on-failure diff --git a/.gitignore b/.gitignore index d3ad192..dd55ca5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,6 @@ build/ +build-release/ +.cache/ *.o *.a *.swp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..a06078c --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,82 @@ +cmake_minimum_required(VERSION 3.20) +project( + bmath + VERSION 0.1.0 + DESCRIPTION "Header-only C++23 math library" + LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED ON) +set(CMAKE_CXX_EXTENSIONS OFF) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +add_library(bmath INTERFACE) +add_library(bmath::bmath ALIAS bmath) + +target_include_directories( + bmath INTERFACE $ + $) + +if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" OR CMAKE_CXX_COMPILER_ID STREQUAL + "Clang") + target_compile_options( + bmath + INTERFACE -pedantic-errors + -Wall + -Wextra + -Wpedantic + -Wshadow + -Wformat=2 + -Wfloat-equal + -Wlogical-op + -Wshift-overflow=2 + -Wnon-virtual-dtor + -Wold-style-cast + -Wcast-qual + -Wuseless-cast + -Wno-sign-promotion + -Wcast-align + -Wunused + -Woverloaded-virtual + -Wconversion + -Wmisleading-indentation + -Wduplicated-cond + -Wduplicated-branches + -Wnull-dereference + -Wno-conversion + -Wformat-overflow + -Wformat-truncation + -Wdouble-promotion + -Wundef) +endif() + +target_compile_definitions(bmath INTERFACE LOCAL) + +if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options( + bmath + INTERFACE -O0 + -g3 + -fsanitize=address,undefined + -fsanitize=float-divide-by-zero + -fsanitize=float-cast-overflow + -fno-sanitize-recover=all + -fstack-protector-all + -fno-omit-frame-pointer + -fno-inline + -ffunction-sections) + + target_compile_definitions(bmath INTERFACE _GLIBCXX_DEBUG + _GLIBCXX_DEBUG_PEDANTIC) + + target_link_options( + bmath INTERFACE -fsanitize=address,undefined + -fsanitize=float-divide-by-zero -fsanitize=float-cast-overflow) +else() + target_compile_options(bmath INTERFACE -O2) + target_compile_definitions(bmath INTERFACE NDEBUG) +endif() + +enable_testing() +add_subdirectory(tests) diff --git a/compile_flags.txt b/compile_flags.txt deleted file mode 100644 index 541be64..0000000 --- a/compile_flags.txt +++ /dev/null @@ -1,30 +0,0 @@ --pedantic-errors --O2 --Wall --Wextra --Wpedantic --Wshadow --Wformat=2 --Wfloat-equal --Wlogical-op --Wshift-overflow=2 --Wnon-virtual-dtor --Wold-style-cast --Wcast-qual --Wuseless-cast --Wno-sign-promotion --Wcast-align --Wunused --Woverloaded-virtual --Wconversion --Wmisleading-indentation --Wduplicated-cond --Wduplicated-branches --Wlogical-op --Wnull-dereference --Wformat=2 --Wformat-overflow --Wformat-truncation --Wdouble-promotion --Wundef --DLOCAL diff --git a/makefile b/makefile deleted file mode 100644 index 3c4acfa..0000000 --- a/makefile +++ /dev/null @@ -1,108 +0,0 @@ -CXX ?= g++ - -INCLUDE_DIR := include -TEST_DIR := tests -BUILD_ROOT := build -MODE ?= release - -STD := -std=c++23 -WARNFLAGS := \ - -pedantic-errors \ - -Wall -Wextra -Wpedantic \ - -Wshadow \ - -Wformat=2 \ - -Wfloat-equal \ - -Wlogical-op \ - -Wshift-overflow=2 \ - -Wnon-virtual-dtor \ - -Wold-style-cast \ - -Wcast-qual \ - -Wuseless-cast \ - -Wno-sign-promotion \ - -Wcast-align \ - -Wunused \ - -Woverloaded-virtual \ - -Wconversion \ - -Wmisleading-indentation \ - -Wduplicated-cond \ - -Wduplicated-branches \ - -Wnull-dereference \ - -Wno-conversion \ - -Wformat-overflow \ - -Wformat-truncation \ - -Wdouble-promotion \ - -Wundef - -BASEDEFS := -DLOCAL -INCLUDES := -I$(INCLUDE_DIR) - -RELFLAGS := -O2 -DNDEBUG -DBGFLAGS := \ - -O0 -g3 \ - -fsanitize=address,undefined \ - -fsanitize=float-divide-by-zero \ - -fsanitize=float-cast-overflow \ - -fno-sanitize-recover=all \ - -fstack-protector-all \ - -fstack-usage \ - -fno-omit-frame-pointer \ - -fno-inline \ - -ffunction-sections \ - -D_GLIBCXX_DEBUG \ - -D_GLIBCXX_DEBUG_PEDANTIC - -DBG_LDFLAGS := \ - -fsanitize=address,undefined \ - -fsanitize=float-divide-by-zero \ - -fsanitize=float-cast-overflow - -CXXFLAGS := $(STD) $(WARNFLAGS) $(INCLUDES) $(BASEDEFS) -CXXFLAGS += -MMD -MP -ifeq ($(MODE),debug) - CXXFLAGS += $(DBGFLAGS) - LDFLAGS += $(DBG_LDFLAGS) -else - CXXFLAGS += $(RELFLAGS) -endif - -BUILD_DIR := $(BUILD_ROOT)/$(MODE) -TEST_SRC := $(wildcard $(TEST_DIR)/*.cc) -TEST_OBJ := $(patsubst $(TEST_DIR)/%.cc,$(BUILD_DIR)/$(TEST_DIR)/%.o,$(TEST_SRC)) -TEST_EXE := $(BUILD_DIR)/test_bmath - -.PHONY: all release debug test clean distclean flags .TEST - -all: release - -release: - $(MAKE) MODE=release .TEST - -debug: - $(MAKE) MODE=debug .TEST - -test: .TEST - -.TEST: $(TEST_EXE) - ./$(TEST_EXE) - -$(BUILD_DIR)/$(TEST_DIR)/%.o: $(TEST_DIR)/%.cc - @mkdir -p $(dir $@) - $(CXX) $(CXXFLAGS) -c $< -o $@ --include $(TEST_OBJ:.o=.d) - -$(TEST_EXE): $(TEST_OBJ) - @mkdir -p $(dir $@) - $(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS) - -flags: - @{ \ - printf "%s\n%s\n%s\n" $(STD) $(INCLUDES) $(WARNFLAGS) $(BASEDEFS) \ - [ "$(MODE)" = "debug" ] && printf "%s\n" $(DBGFLAGS) || printf "%s\n" $(RELFLAGS) \ - } > compile_flags.txt - -clean: - @rm -rf "$(BUILD_DIR)" - -distclean: - @rm -rf "$(BUILD_ROOT)" compile_flags.txt - diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..2fb3c98 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,17 @@ +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG v1.14.0 +) +set(gtest_force_shared_crt + ON + CACHE BOOL "" FORCE) +FetchContent_MakeAvailable(googletest) + +add_executable(test_bmath test_bmath.cc) + +target_link_libraries(test_bmath PRIVATE bmath::bmath gtest_main) + +include(GoogleTest) +gtest_discover_tests(test_bmath) diff --git a/tests/test_bmath.cc b/tests/test_bmath.cc new file mode 100644 index 0000000..2fd829c --- /dev/null +++ b/tests/test_bmath.cc @@ -0,0 +1,75 @@ +#include + +#include +#include + +#include "../include/bmath.hh" + +using namespace bmath; + +TEST(MintTest, BasicConstruction) { + mint four{4}; + + static_assert(std::is_trivially_copyable_v>); + + EXPECT_EQ(four.get(), 4); +} + +TEST(MintTest, Formatting) { + mint four{4}; + + auto formatted = std::format("{}", four); + EXPECT_EQ(formatted, "4"); + + auto string_result = to_string(four); + EXPECT_EQ(string_result, "4"); +} + +TEST(MintTest, Addition) { + mint a{3}; + mint b{5}; + + auto result = a + b; + EXPECT_EQ(result.get(), 8); +} + +TEST(MintTest, Subtraction) { + mint a{10}; + mint b{3}; + + auto result = a - b; + EXPECT_EQ(result.get(), 7); +} + +TEST(MintTest, Multiplication) { + mint a{6}; + mint b{7}; + + auto result = a * b; + EXPECT_EQ(result.get(), 42); +} + +TEST(MintTest, Modular) { + constexpr uint64_t mod = 13; + mint a{15}; + + EXPECT_EQ(a.get(), 2); +} + +TEST(PowerTest, BasicPower) { + mint base{2}; + auto result = pow(base, 3); + EXPECT_EQ(result.get(), 8); +} + +TEST(PowerTest, PowerOfOne) { + mint base{5}; + auto result = pow(base, 0); + EXPECT_EQ(result.get(), 1); +} + +TEST(PowerTest, PowerOfZero) { + mint base{0}; + auto result = pow(base, 5); + EXPECT_EQ(result.get(), 0); +} diff --git a/tests/test_compilation.cc b/tests/test_compilation.cc deleted file mode 100644 index e7112e7..0000000 --- a/tests/test_compilation.cc +++ /dev/null @@ -1,23 +0,0 @@ -#include -#include -#include - -#include "../include/bmath.hh" - -using namespace bmath; -using namespace std; - -int main() { - mint four{4}; - - // should be trivially copyable - static_assert(is_trivially_copyable_v>); - - // should be able to format - auto formatted = format("{}\n", four); - - // and use to_string - formatted = to_string(four); - - return 0; -}