Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multithreaded tests #9

Merged
merged 10 commits into from
Mar 2, 2024
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: 3 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
7 changes: 5 additions & 2 deletions autoformat.bash
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 0 additions & 27 deletions tests/tests.c

This file was deleted.

67 changes: 67 additions & 0 deletions tests/tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#include <cassert>
#include <cstddef>
#include <thread>

#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();
}