diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 0afbfc1..519dac2 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -7,5 +7,6 @@ jobs: runs-on: ubuntu-22.04 steps: - uses: actions/checkout@v4 - - run: make -j - - run: target/tests + - run: | + make -j + target/tests diff --git a/Makefile b/Makefile index 8fc9bd5..d09b778 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ CPPFLAGS = -Isrc/include CFLAGS = -std=c11 -O2 -Wall -Wextra -flto -CXXFLAGS = -std=c++11 -O2 -Wall -Wextra -flto +CXXFLAGS = -std=c++17 -O2 -Wall -Wextra -flto LDLIBS = -lm Sources = src/sieve_of_eratosthenes.c src/sieve_of_atkin.c @@ -16,5 +16,5 @@ target/sieve: $(Objects) src/main.c target/bench: $(Objects) bench/bench.cc $(LINK.cc) $^ -o $@ $(LDLIBS) -target/tests: $(Objects) tests/tests.c - $(LINK.c) $^ -o $@ $(LDLIBS) +target/tests: $(Objects) tests/tests.cc + $(LINK.cc) $^ -o $@ $(LDLIBS) diff --git a/autoformat.bash b/autoformat.bash index 640d75a..0da128f 100755 --- a/autoformat.bash +++ b/autoformat.bash @@ -2,10 +2,13 @@ shopt -s globstar +# Switch to the directory containing the script so that relative paths may be +# used. +cd "${0%/*}" files=(**/*.[ch]*) if [ "$1" = check ] then - clang-format --dry-run -Werror ${files[@]} + clang-format --verbose --dry-run -Werror ${files[@]} else - clang-format -i ${files[@]} + clang-format --verbose -i ${files[@]} fi diff --git a/tests/tests.c b/tests/tests.c deleted file mode 100644 index 9fc036b..0000000 --- a/tests/tests.c +++ /dev/null @@ -1,27 +0,0 @@ -#include -#include - -#include "sieve_of_eratosthenes_atkin.h" - -int -main(void) -{ - // Each count of prime numbers excludes 2, 3 and 5, since both sieves start - // finding primes numbers from 7 onwards. - size_t limits_primes[][2] = { { 10000000, 664576 }, { 100000000, 5761452 }, { 1000000000, 50847531 } }; - for (size_t i = 0; i < sizeof limits_primes / sizeof *limits_primes; ++i) - { - size_t limit = limits_primes[i][0]; - size_t primes = limits_primes[i][1]; - - struct SieveOfEratosthenes *erato = sieve_of_eratosthenes_new(limit); - size_t erato_count = sieve_of_eratosthenes_count(erato); - sieve_of_eratosthenes_delete(erato); - assert(erato_count == primes); - - struct SieveOfAtkin *atkin = sieve_of_atkin_new(limit); - size_t atkin_count = sieve_of_atkin_count(atkin); - sieve_of_atkin_delete(atkin); - assert(atkin_count == primes); - } -} diff --git a/tests/tests.cc b/tests/tests.cc new file mode 100644 index 0000000..178a8b1 --- /dev/null +++ b/tests/tests.cc @@ -0,0 +1,67 @@ +#include +#include +#include + +#include "sieve_of_eratosthenes_atkin.h" + +// Each count of prime numbers excludes 2, 3 and 5, since both sieves start +// finding primes numbers from 7 onwards. +static std::size_t counts_limits[][2] = { + { 11145332, 201268277 }, + { 14817689, 272068078 }, + { 18739219, 348738693 }, + { 20409369, 381656751 }, + { 21423606, 401730367 }, + { 21984274, 412847637 }, + { 22879491, 430626488 }, + { 28077424, 534558099 }, + { 36669946, 708491638 }, + { 40759133, 792066697 }, + { 41876892, 814981682 }, + { 43807217, 854635735 }, + { 46429840, 908671296 }, + { 46994786, 920313910 }, + { 48028264, 941665889 }, +}; + +/****************************************************************************** + * Test the implementation of the sieve of Eratosthenes. + *****************************************************************************/ +void +erato_work(void) +{ + for (auto const &[count, limit] : counts_limits) + { + SieveOfEratosthenes *erato = sieve_of_eratosthenes_new(limit); + std::size_t erato_count = sieve_of_eratosthenes_count(erato); + sieve_of_eratosthenes_delete(erato); + assert(erato_count == count); + } +} + +/****************************************************************************** + * Test the implementation of the sieve of Atkin. + *****************************************************************************/ +void +atkin_work(void) +{ + for (auto const &[count, limit] : counts_limits) + { + SieveOfAtkin *atkin = sieve_of_atkin_new(limit); + std::size_t atkin_count = sieve_of_atkin_count(atkin); + sieve_of_atkin_delete(atkin); + assert(atkin_count == count); + } +} + +/****************************************************************************** + * Main function. + *****************************************************************************/ +int +main(void) +{ + std::thread erato_worker(erato_work); + std::thread atkin_worker(atkin_work); + erato_worker.join(); + atkin_worker.join(); +}