Skip to content

Commit

Permalink
Multithreaded tests (#9)
Browse files Browse the repository at this point in the history
* Moved C code into `C` directory.
* Fixed workflow syntax.
* Formatter: use working directory. Tests: increased scope.
* Multithreaded tests.
* Moved everything back into the repository root.
* Fixed workflows.
  • Loading branch information
tfpf authored Mar 2, 2024
1 parent d6a2f84 commit 15f0660
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 34 deletions.
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();
}

0 comments on commit 15f0660

Please sign in to comment.