diff --git a/.github/workflows/fedora.yml b/.github/workflows/fedora.yml new file mode 100644 index 0000000..ef69499 --- /dev/null +++ b/.github/workflows/fedora.yml @@ -0,0 +1,48 @@ +name: Fedora + +on: [push, pull_request, workflow_dispatch] + +permissions: + contents: read + +jobs: + + fedora-cmake-gcc: + name: fedora + runs-on: ubuntu-latest + container: + image: fedora:latest + + steps: + - uses: actions/checkout@v3 + - run: cat /etc/os-release + - name: Install dependencies + run: | + sudo dnf -y update + sudo dnf -y install gcc-c++ cmake make gmp gmp-devel gmp-c++ gmp-devel gmp-static mpfr-devel + sudo dnf upgrade + - name: Run tests + run: | + cd ./number_theory + chmod +x ./run_gcc_tests.sh + ./run_gcc_tests.sh + + fedora-cmake-clang: + name: fedora + runs-on: ubuntu-latest + container: + image: fedora:latest + + steps: + - uses: actions/checkout@v3 + - run: cat /etc/os-release + - name: Install dependencies + run: | + sudo dnf -y update + sudo dnf -y install clang cmake make gmp gmp-devel gmp-c++ gmp-devel gmp-static mpfr-devel + sudo dnf upgrade + - name: Run tests + run: | + cd ./number_theory + chmod +x ./run_clang_tests.sh + ./run_clang_tests.sh diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml deleted file mode 100644 index d3389ba..0000000 --- a/.github/workflows/linux.yml +++ /dev/null @@ -1,44 +0,0 @@ -name: Linux - -on: [push, pull_request, workflow_dispatch] - -permissions: - contents: read - -jobs: - - ubuntu-cmake: - name: ubuntu - runs-on: ubuntu-latest - - steps: - - uses: actions/checkout@v3 - - name: Install dependencies - run: | - sudo apt-get update - sudo apt-get install g++ build-essential cmake make ninja-build libgmp-dev libmpfr-dev - sudo apt-get upgrade - - name: Run tests - run: | - cd ./number_theory - chmod +x ./run_tests.sh - ./run_tests.sh - - fedora-cmake: - name: fedora - runs-on: ubuntu-latest - container: - image: fedora:latest - - steps: - - uses: actions/checkout@v3 - - run: cat /etc/os-release - - name: Install dependencies - run: | - dnf -y update - dnf -y install gcc-c++ cmake make ninja-build gmp gmp-devel gmp-c++ gmp-devel gmp-static mpfr-devel - - name: Run tests - run: | - cd ./number_theory - chmod +x ./run_tests.sh - ./run_tests.sh diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml new file mode 100644 index 0000000..906d1d7 --- /dev/null +++ b/.github/workflows/ubuntu.yml @@ -0,0 +1,41 @@ +name: Ubuntu + +on: [push, pull_request, workflow_dispatch] + +permissions: + contents: read + +jobs: + + ubuntu-cmake-gcc: + name: ubuntu + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: | + sudo apt update + sudo apt install g++ build-essential cmake make libgmp-dev libmpfr-dev + sudo apt upgrade + - name: Run tests + run: | + cd ./number_theory + chmod +x ./run_gcc_tests.sh + ./run_gcc_tests.sh + + ubuntu-cmake-clang: + name: ubuntu + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Install dependencies + run: | + sudo apt update + sudo apt install clang llvm cmake make libgmp-dev libmpfr-dev + - name: Run tests + run: | + cd ./number_theory + chmod +x ./run_clang_tests.sh + ./run_clang_tests.sh diff --git a/number_theory/CMakeLists.txt b/number_theory/CMakeLists.txt index e09a41f..dbf0eab 100644 --- a/number_theory/CMakeLists.txt +++ b/number_theory/CMakeLists.txt @@ -22,7 +22,10 @@ if (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") if (NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) set(TEST_COMPILE_OPTIONS ${TEST_COMPILE_OPTIONS} -UNDEBUG) endif() - set(TEST_COMPILE_OPTIONS ${TEST_COMPILE_OPTIONS} _LIBCPP_ENABLE_ASSERTIONS=1) + + set(TEST_COMPILE_DEFINITIONS + ${TEST_COMPILE_DEFINITIONS} + _LIBCPP_ENABLE_ASSERTIONS=1) elseif (CMAKE_CXX_COMPILER_FRONTEND_VARIANT STREQUAL "AppleClang") endif() elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") diff --git a/number_theory/eratosthene_sieve.hpp b/number_theory/eratosthene_sieve.hpp index 2bfab0e..6a6e713 100644 --- a/number_theory/eratosthene_sieve.hpp +++ b/number_theory/eratosthene_sieve.hpp @@ -49,7 +49,7 @@ constexpr primes.set(); primes[0] = false; primes[1] = false; - constexpr uint32_t root = math_functions::isqrt(N); + constexpr uint32_t root = math_functions::isqrt(uint64_t(N)); for (uint32_t i = 2; i <= root; i++) { if (primes[i]) { for (uint32_t j = i * i; j < N; j += i) { diff --git a/number_theory/run_clang_tests.sh b/number_theory/run_clang_tests.sh new file mode 100644 index 0000000..ef4a0d6 --- /dev/null +++ b/number_theory/run_clang_tests.sh @@ -0,0 +1,11 @@ +#! /bin/sh + +mkdir -p build_tests +cp ./u64-primes.txt ./build_tests/u64-primes.txt +cp ./u128-primes.txt ./build_tests/u128-primes.txt +cd ./build_tests || return 1 + +export CC=/usr/bin/clang +export CXX=/usr/bin/clang++ + +cmake -D CMAKE_BUILD_TYPE=Release -S .. -B . && make all --jobs "$(nproc)" && make test diff --git a/number_theory/run_gcc_tests.sh b/number_theory/run_gcc_tests.sh new file mode 100755 index 0000000..a2fa8a6 --- /dev/null +++ b/number_theory/run_gcc_tests.sh @@ -0,0 +1,11 @@ +#! /bin/sh + +mkdir -p build_tests +cp ./u64-primes.txt ./build_tests/u64-primes.txt +cp ./u128-primes.txt ./build_tests/u128-primes.txt +cd ./build_tests || return 1 + +export CC=/usr/bin/gcc +export CXX=/usr/bin/g++ + +cmake -D CMAKE_BUILD_TYPE=Release -S .. -B . && make all --jobs "$(nproc)" && make test diff --git a/number_theory/run_tests.sh b/number_theory/run_tests.sh deleted file mode 100755 index 2998054..0000000 --- a/number_theory/run_tests.sh +++ /dev/null @@ -1,21 +0,0 @@ -#! /bin/sh - -mkdir -p build_tests -cp ./u64-primes.txt ./build_tests/u64-primes.txt -cp ./u128-primes.txt ./build_tests/u128-primes.txt -cd ./build_tests || return 1 - -# Why we export CC and CXX instead of overwriting CMAKE_C_COMPILER and CMAKE_CXX_COMPILER: -# https://stackoverflow.com/questions/17275348/how-to-specify-new-gcc-path-for-cmake - -export CC=/usr/bin/gcc -export CXX=/usr/bin/g++ -# GCC_RESULT="$(cmake -DCMAKE_BUILD_TYPE=Release -S .. -B . && make all --jobs 4 && make test)" -cmake -DCMAKE_BUILD_TYPE=Release -S .. -B . && make all --jobs 4 && make test - -export CC=/usr/bin/clang -export CXX=/usr/bin/clang++ -# CLANG_RESULT="$(cmake -DCMAKE_BUILD_TYPE=Release -S .. -B . && make all --jobs 4 && make test)" -cmake -DCMAKE_BUILD_TYPE=Release -S .. -B . && make all --jobs 4 && make test - -# return "$((GCC_RESULT | CLANG_RESULT))" diff --git a/number_theory/test_math_functions.cpp b/number_theory/test_math_functions.cpp index a4878e6..6572bd6 100644 --- a/number_theory/test_math_functions.cpp +++ b/number_theory/test_math_functions.cpp @@ -1,7 +1,3 @@ -#ifdef NDEBUG -#warning "Can't test properly with NDEBUG macro defined (macro won't be undefined manually)" -#endif - #include #include @@ -10,6 +6,7 @@ #include "math_functions.hpp" #include "test_tools.hpp" +#include "config_macros.hpp" using namespace math_functions; using namespace test_tools; @@ -35,6 +32,8 @@ static_assert(bin_pow_mod(uint64_t(999999999999999487ull), uint64_t(184467440737 "bin_pow_mod"); #endif +#if HAS_AT_LEAST_CXX_20 + static_assert(isqrt(0u) == 0, "isqrt"); static_assert(isqrt(1u) == 1, "isqrt"); static_assert(isqrt(4u) == 2, "isqrt"); @@ -52,6 +51,8 @@ static_assert(isqrt(1u << 28) == 1 << 14, "isqrt"); static_assert(isqrt(1u << 30) == 1 << 15, "isqrt"); static_assert(isqrt(uint32_t(-1)) == (1u << 16) - 1, "isqrt"); +#endif + static_assert(isqrt(uint64_t(0)) == 0, "isqrt"); static_assert(isqrt(uint64_t(1)) == 1, "isqrt"); static_assert(isqrt(uint64_t(4)) == 2, "isqrt"); diff --git a/number_theory/test_tools.hpp b/number_theory/test_tools.hpp index 64a6408..e9d22f1 100644 --- a/number_theory/test_tools.hpp +++ b/number_theory/test_tools.hpp @@ -1,5 +1,9 @@ #pragma once +#ifdef NDEBUG +#warning "Can't test properly with NDEBUG macro defined (macro won't be undefined manually)" +#endif + #include #include #include @@ -97,7 +101,7 @@ struct Wrapper final { } private: - ATTRIBUTE_COLD [[noreturn]] static void ThrowOnFOpenFail(const char* fname, const char* mode) { + [[noreturn]] ATTRIBUTE_COLD static void ThrowOnFOpenFail(const char* fname, const char* mode) { std::array buffer; int bytes_written = std::snprintf( buffer.data(), buffer.size(),