Skip to content

Commit

Permalink
Merge pull request #11 from seldon-code/unit_tests_in_ci
Browse files Browse the repository at this point in the history
Unit tests in ci
  • Loading branch information
MSallermann authored Oct 6, 2023
2 parents b32721b + 4dd901d commit 9d3a7bb
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 55 deletions.
152 changes: 152 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
WarningsAsErrors: ''
HeaderFilterRegex: ''
AnalyzeTemporaryDtors: false
FormatStyle: file
Checks: >
-*,
clang-diagnostic-*,
bugprone-argument-comment,
bugprone-assert-side-effect,
bugprone-bad-signal-to-kill-thread,
bugprone-bool-pointer-implicit-conversion,
bugprone-branch-clone,
bugprone-copy-constructor-init,
bugprone-dangling-handle,
bugprone-exception-escape,
bugprone-fold-init-type,
bugprone-forward-declaration-namespace,
bugprone-forwarding-reference-overload,
bugprone-implicit-widening-of-multiplication-result,
bugprone-inaccurate-erase,
bugprone-incorrect-roundings,
bugprone-infinite-loop,
bugprone-integer-division,
bugprone-lambda-function-name,
bugprone-macro-parentheses,
bugprone-macro-repeated-side-effects,
bugprone-misplaced-operator-in-strlen-in-alloc,
bugprone-misplaced-pointer-arithmetic-in-alloc,
bugprone-misplaced-widening-cast,
bugprone-move-forwarding-reference,
bugprone-multiple-statement-macro,
bugprone-not-null-terminated-result,
bugprone-parent-virtual-call,
bugprone-posix-return,
bugprone-redundant-branch-condition,
bugprone-reserved-identifier,
bugprone-signal-handler,
bugprone-signed-char-misuse,
bugprone-sizeof-container,
bugprone-sizeof-expression,
bugprone-spuriously-wake-up-functions,
bugprone-string-constructor,
bugprone-string-integer-assignment,
bugprone-string-literal-with-embedded-nul,
bugprone-suspicious-enum-usage,
bugprone-suspicious-include,
bugprone-suspicious-memory-comparison,
bugprone-suspicious-memset-usage,
bugprone-suspicious-missing-comma,
bugprone-suspicious-semicolon,
bugprone-suspicious-string-compare,
bugprone-swapped-arguments,
bugprone-terminating-continue,
bugprone-throw-keyword-missing,
bugprone-too-small-loop-variable,
bugprone-undefined-memory-manipulation,
bugprone-undelegated-constructor,
bugprone-unhandled-exception-at-new,
bugprone-unhandled-self-assignment,
bugprone-unused-raii,
bugprone-unused-return-value,
bugprone-use-after-move,
bugprone-virtual-near-miss,
cert-dcl58-cpp,
cert-env33-c,
cert-err34-c,
cert-err52-cpp,
cert-err60-cpp,
cert-flp30-c,
cert-msc50-cpp,
cert-msc51-cpp,
cert-oop57-cpp,
cert-oop58-cpp,
cert-dcl59-cpp,
cppcoreguidelines-avoid-goto,
cppcoreguidelines-init-variables,
cppcoreguidelines-macro-usage,
cppcoreguidelines-no-malloc,
cppcoreguidelines-prefer-member-initializer,
cppcoreguidelines-pro-type-const-cast,
cppcoreguidelines-pro-type-cstyle-cast,
cppcoreguidelines-pro-type-member-init,
cppcoreguidelines-pro-type-static-cast-downcast,
cppcoreguidelines-slicing,
cppcoreguidelines-virtual-class-destructor,
hicpp-exception-baseclass,
hicpp-no-assembler,
hicpp-signed-bitwise,
misc-definitions-in-headers,
misc-non-copyable-objects,
misc-redundant-expression,
misc-static-assert,
misc-throw-by-value-catch-by-reference,
misc-unconventional-assign-operator,
misc-uniqueptr-reset-release,
misc-unused-alias-decls,
misc-unused-parameters,
misc-unused-using-decls,
modernize-avoid-bind,
modernize-deprecated-headers,
modernize-make-shared,
modernize-make-unique,
modernize-redundant-void-arg,
modernize-use-auto,
modernize-use-bool-literals,
modernize-use-emplace,
modernize-use-equals-default,
modernize-use-equals-delete,
modernize-use-nullptr,
modernize-use-override,
modernize-use-uncaught-exceptions,
modernize-use-using,
openmp-exception-escape,
openmp-use-default-none,
performance-for-range-copy,
performance-implicit-conversion-in-loop,
performance-inefficient-algorithm,
performance-inefficient-string-concatenation,
performance-move-const-arg,
performance-move-constructor-init,
performance-trivially-destructible,
performance-type-promotion-in-math-fn,
performance-unnecessary-copy-initialization,
performance-unnecessary-value-param,
readability-avoid-const-params-in-decls,
readability-container-size-empty,
readability-convert-member-functions-to-static,
readability-delete-null-pointer,
readability-function-cognitive-complexity,
readability-function-size,
readability-implicit-bool-conversion,
readability-inconsistent-declaration-parameter-name,
readability-make-member-function-const,
readability-misplaced-array-index,
readability-non-const-parameter,
readability-qualified-auto,
readability-redundant-access-specifiers,
readability-redundant-control-flow,
readability-redundant-declaration,
readability-redundant-preprocessor,
readability-redundant-smartptr-get,
readability-redundant-string-cstr,
readability-simplify-boolean-expr,
readability-simplify-subscript-expr,
readability-static-accessed-through-instance,
readability-string-compare,
readability-uniqueptr-delete-release,
readability-use-anyofallof
CheckOptions:
- { key: readability-function-size.StatementThreshold, value: '800' }
- { key: readability-function-size.LineThreshold, value: '200' }
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ jobs:
shell: pixi run bash {0}
run: |
meson setup build
meson compile -C build
meson compile -C build
- name: Test with meson
shell: pixi run bash {0}
run: |
meson test -C build
4 changes: 1 addition & 3 deletions include/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ class Agent : public AgentBase

void from_string( const std::string & str );

virtual std::string to_string() const override
std::string to_string() const override
{
return fmt::format( "{}", opinion );
}

~Agent() = default;
};

template<>
Expand Down
2 changes: 1 addition & 1 deletion include/agent_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class AgentBase
{
public:
virtual std::string to_string() const = 0;

virtual ~AgentBase() = default;
// TODO: eventually a from_string might also be needed
// virtual void from_string() = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion include/connectivity.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TarjanConnectivityAlgo
if( lowest[v] == num[v] )
{
scc.resize( 0 );
size_t scc_vertex;
size_t scc_vertex = 0;
// Pop the stack
scc_vertex = stack.back();
stack.pop_back();
Expand Down
4 changes: 2 additions & 2 deletions include/model.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Model : public ModelBase
}
}

virtual void iteration() override = 0;
void iteration() override = 0;

virtual bool finished() override
bool finished() override
{
if( max_iterations.has_value() )
{
Expand Down
1 change: 1 addition & 0 deletions include/model_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ModelBase
virtual AgentBase * get_agent( int idx ) = 0; // Use this to get an abstract representation of the agent at idx
virtual void iteration() = 0;
virtual bool finished() = 0;
virtual ~ModelBase() = default;
};

} // namespace Seldon
7 changes: 4 additions & 3 deletions include/util/io.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ namespace Seldon
namespace IO
{

inline void network_to_dot_file( const Network & network, std::string file_path )

inline void network_to_dot_file( const Network & network, const std::string & file_path )
{
std::fstream fs;
fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );
Expand All @@ -38,7 +39,7 @@ inline void network_to_dot_file( const Network & network, std::string file_path
fs.close();
}

inline void opinions_to_file( Simulation & simulation, std::string file_path )
inline void opinions_to_file( Simulation & simulation, const std::string & file_path )
{
std::fstream fs;
fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );
Expand All @@ -56,7 +57,7 @@ inline void opinions_to_file( Simulation & simulation, std::string file_path )
fs.close();
}

inline void network_to_file( Simulation & simulation, std::string file_path )
inline void network_to_file( Simulation & simulation, const std::string & file_path )
{
std::fstream fs;
fs.open( file_path, std::fstream::in | std::fstream::out | std::fstream::trunc );
Expand Down
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ exe = executable('seldon', sources_seldon + 'src/main.cpp',

tests = [
['Test Tarjan', 'test/test_tarjan.cpp'],
['Test DeGroot', 'test/test_deGroot.cpp']
]

Catch2 = dependency('Catch2', method : 'cmake', modules : ['Catch2::Catch2WithMain', 'Catch2::Catch2'])
Expand Down
1 change: 1 addition & 0 deletions run_formatter.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/bin/sh
find ./include -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -i {} \;
find ./src -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -i {} \;
find ./test -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-format -i {} \;
4 changes: 4 additions & 0 deletions run_linter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
find ./include -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-tidy {} \;
find ./src -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-tidy {} \;
find ./test -regex '.*\.\(cpp\|hpp\|cc\|cxx\)' -exec clang-tidy {} \;
4 changes: 2 additions & 2 deletions src/models/DeGroot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ void Seldon::DeGrootModel::iteration()

auto neighbour_buffer = std::vector<size_t>();
auto weight_buffer = std::vector<double>();
size_t j_index;
double weight;
size_t j_index = 0;
double weight = 0.0;

for( size_t i = 0; i < agents.size(); i++ )
{
Expand Down
3 changes: 2 additions & 1 deletion src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <iostream>
#include <optional>
#include <set>
#include <stdexcept>

enum class ModelType : unsigned int
{
Expand Down Expand Up @@ -42,7 +43,7 @@ Seldon::Simulation::Simulation(

// Check if 'model' is one of the allowed values
auto model_string = model_opt.value();
if( !allowed_models.count( model_string ) )
if( !allowed_models.contains( model_string ) )
{
throw std::runtime_error( fmt::format( "Unknown model type: '{}'!", model_string ) );
}
Expand Down
44 changes: 44 additions & 0 deletions test/test_deGroot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_floating_point.hpp>

#include "models/DeGroot.hpp"
#include "network.hpp"
#include <random>

TEST_CASE( "Test the DeGroot Model Symmetric", "[DeGroot]" )
{
using namespace Seldon;
using namespace Catch::Matchers;

size_t n_agents = 2;
auto neighbour_list = std::vector<std::vector<size_t>>{
{ 1, 0 },
{ 0, 1 },
};

auto weight_list = std::vector<std::vector<double>>{
{ 0.2, 0.8 },
{ 0.2, 0.8 },
};

auto gen = std::mt19937();
auto network = Network( std::move( neighbour_list ), std::move( weight_list ) );
auto model = DeGrootModel( n_agents, network );

model.convergence_tol = 1e-6;
model.max_iterations = 100;
model.agents[0].opinion = 0.0;
model.agents[1].opinion = 1.0;

do
{
model.iteration();
} while( !model.finished() );

fmt::print( "N_iterations = {} (with convergence_tol {})\n", model.n_iterations, model.convergence_tol );
for( size_t i = 0; i < n_agents; i++ )
{
fmt::print( "Opinion {} = {}\n", i, model.agents[i].opinion );
REQUIRE_THAT( model.agents[i].opinion, WithinAbs( 0.5, model.convergence_tol * 10.0 ) );
}
}
Loading

0 comments on commit 9d3a7bb

Please sign in to comment.