Make some changes

This commit is contained in:
Cory Balaton 2023-10-04 17:46:10 +02:00
parent 709d0c5411
commit a24635ede0
No known key found for this signature in database
GPG Key ID: 3E5FCEBFD80F432B
2 changed files with 129 additions and 111 deletions

View File

@ -42,9 +42,12 @@
* This macro calls the m_assert function which is a more informative * This macro calls the m_assert function which is a more informative
* assertion function than the regular assert function from cassert. * assertion function than the regular assert function from cassert.
* */ * */
#define ASSERT(expr, msg) m_assert(expr, #expr, __FUNCTION__, __FILE__, \ #define ASSERT(expr, msg) m_assert(expr, #expr, __METHOD_NAME__, __FILE__, \
__LINE__, msg) __LINE__, msg)
#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)
/** Code stolen from https://github.com/anderkve/FYS3150 /** Code stolen from https://github.com/anderkve/FYS3150
* Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp * Header: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/include/utils.hpp
* Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp * Source: https://github.com/anderkve/FYS3150/blob/master/code_examples/compilation_linking/example_1/src/utils.cpp
@ -105,4 +108,14 @@ void m_assert(bool expr,
* */ * */
bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol=1e-8); bool arma_vector_close_to(arma::vec &a, arma::vec &b, double tol=1e-8);
static inline std::string methodName(const std::string& prettyFunction)
{
size_t colons = prettyFunction.find("::");
size_t begin = prettyFunction.substr(0,colons).rfind(" ") + 1;
size_t end = prettyFunction.rfind("(") - begin;
return prettyFunction.substr(begin,end) + "()";
}
#endif #endif

View File

@ -17,8 +17,10 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
void test_PenningTrap_external_E_field() class PenningTrapTest {
{ public:
static void test_external_E_field()
{
PenningTrap trap; PenningTrap trap;
// Vector containing inputs and expected results // Vector containing inputs and expected results
@ -53,11 +55,11 @@ void test_PenningTrap_external_E_field()
ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str()); ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str());
} }
} }
void test_PenningTrap_external_B_field() static void test_external_B_field()
{ {
// No point in testing at different points since it's not dependent on // No point in testing at different points since it's not dependent on
// position. // position.
PenningTrap trap; PenningTrap trap;
@ -65,10 +67,11 @@ void test_PenningTrap_external_B_field()
arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.}); arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.});
ASSERT(arma_vector_close_to(expected, result), ASSERT(arma_vector_close_to(expected, result),
"Testing the external B field at (0,0,0)"); "Testing the external B field at (0,0,0)");
} }
void test_PenningTrap_force_on_particle()
{ static void test_force_on_particle()
{
PenningTrap trap; PenningTrap trap;
arma::vec v{0.,0.,0.}; arma::vec v{0.,0.,0.};
@ -90,10 +93,10 @@ void test_PenningTrap_force_on_particle()
ASSERT(arma_vector_close_to(expected, result), ASSERT(arma_vector_close_to(expected, result),
"Testing the force on a particle at (0,0,0) from a " "Testing the force on a particle at (0,0,0) from a "
"particle at (0,3,4)."); "particle at (0,3,4).");
} }
void test_PenningTrap_total_force_external() static void test_total_force_external()
{ {
PenningTrap trap; PenningTrap trap;
trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.}, trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.},
arma::vec{3.,4.,5.})); arma::vec{3.,4.,5.}));
@ -103,10 +106,10 @@ void test_PenningTrap_total_force_external()
ASSERT(arma_vector_close_to(expected, result), ASSERT(arma_vector_close_to(expected, result),
"Testing the total external force on a particle at " "Testing the total external force on a particle at "
"(1,2,3) with velocity (3,4,5)"); "(1,2,3) with velocity (3,4,5)");
} }
void test_PenningTrap_total_force_particles() static void test_total_force_particles()
{ {
PenningTrap trap; PenningTrap trap;
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.}, trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},
arma::vec{0.,0.,0.})); arma::vec{0.,0.,0.}));
@ -129,14 +132,16 @@ void test_PenningTrap_total_force_particles()
ASSERT(arma_vector_close_to(expected, result), ASSERT(arma_vector_close_to(expected, result),
"Testing the total force of all particles on particle 0 " "Testing the total force of all particles on particle 0 "
"with 3 other particles."); "with 3 other particles.");
} }
};
int main() int main()
{ {
test_PenningTrap_external_E_field(); PenningTrapTest::test_external_E_field();
test_PenningTrap_external_B_field(); PenningTrapTest::test_external_B_field();
test_PenningTrap_force_on_particle(); PenningTrapTest::test_force_on_particle();
test_PenningTrap_total_force_external(); PenningTrapTest::test_total_force_external();
test_PenningTrap_total_force_particles(); PenningTrapTest::test_total_force_particles();
return 0; return 0;
} }