Coryab/create tests #4

Merged
coryab merged 10 commits from coryab/create-tests into develop 2023-10-07 20:01:08 +00:00
2 changed files with 129 additions and 111 deletions
Showing only changes of commit a24635ede0 - Show all commits

View File

@ -42,9 +42,12 @@
* This macro calls the m_assert function which is a more informative
* 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)
#define __METHOD_NAME__ methodName(__PRETTY_FUNCTION__)
/** 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
* 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);
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

View File

@ -17,126 +17,131 @@
#include <sstream>
#include <string>
void test_PenningTrap_external_E_field()
{
PenningTrap trap;
class PenningTrapTest {
public:
static void test_external_E_field()
{
PenningTrap trap;
// Vector containing inputs and expected results
std::vector<std::pair<arma::vec, arma::vec>> tests;
// Vector containing inputs and expected results
std::vector<std::pair<arma::vec, arma::vec>> tests;
tests.push_back(std::make_pair(arma::vec{0.,0.,0.},
tests.push_back(std::make_pair(arma::vec{0.,0.,0.},
arma::vec{0.,0.,0.}));
tests.push_back(std::make_pair(arma::vec{10.,0.,0.},
arma::vec{96.4852558,0.,0.}));
tests.push_back(std::make_pair(arma::vec{10.,0.,0.},
arma::vec{96.4852558,0.,0.}));
tests.push_back(std::make_pair(arma::vec{0.,10.,0.},
arma::vec{0.,96.4852558,0.}));
tests.push_back(std::make_pair(arma::vec{0.,0.,10.},
arma::vec{0.,0.,-192.9705116}));
arma::vec result;
arma::vec v;
std::stringstream msg;
for (int i=0; i < tests.size(); i++) {
v = tests.at(i).first;
result = trap.external_E_field(v);
msg.str("");
msg << "Testing the external E field at (" << std::setprecision(2)
<< v(0) << "," << v(1) << "," << v(2) << ").";
ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str());
}
}
static void test_external_B_field()
{
// No point in testing at different points since it's not dependent on
// position.
PenningTrap trap;
arma::vec expected{0.,0.,T};
arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.});
ASSERT(arma_vector_close_to(expected, result),
"Testing the external B field at (0,0,0)");
}
static void test_force_on_particle()
{
PenningTrap trap;
arma::vec v{0.,0.,0.};
// Add particles to test
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},v));
trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},v));
trap.add_particle(Particle(1.,40.,arma::vec{0.,3.,4.},v));
// Test p0 and p1
arma::vec expected{-1.,0.,0.};
arma::vec result = trap.force_on_particle(0, 1);
ASSERT(arma_vector_close_to(expected, result),
"Testing the force on a particle at (0,0,0) from a "
"particle at (1,0,0).");
// Test p0 and p2
expected = arma::vec{0, -.024, -.032};
result = trap.force_on_particle(0, 2);
ASSERT(arma_vector_close_to(expected, result),
"Testing the force on a particle at (0,0,0) from a "
"particle at (0,3,4).");
}
static void test_total_force_external()
{
PenningTrap trap;
trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.},
arma::vec{3.,4.,5.}));
arma::vec expected{395.58954878,-270.15871624,-57.89115348};
arma::vec result = trap.total_force_external(0);
ASSERT(arma_vector_close_to(expected, result),
"Testing the total external force on a particle at "
"(1,2,3) with velocity (3,4,5)");
}
static void test_total_force_particles()
{
PenningTrap trap;
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},
arma::vec{0.,0.,0.}));
tests.push_back(std::make_pair(arma::vec{10.,0.,0.},
arma::vec{96.4852558,0.,0.}));
arma::vec expected{0.,0.,0.};
arma::vec result = trap.total_force_particles(0);
ASSERT(arma_vector_close_to(expected, result),
"Testing the total force of all particles on particle 0 "
"with only a single particle");
tests.push_back(std::make_pair(arma::vec{10.,0.,0.},
arma::vec{96.4852558,0.,0.}));
trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},
arma::vec{0.,0.,0.}));
trap.add_particle(Particle(1.,40.,arma::vec{0.,1.,0.},
arma::vec{0.,0.,0.}));
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,1.},
arma::vec{0.,0.,0.}));
tests.push_back(std::make_pair(arma::vec{0.,10.,0.},
arma::vec{0.,96.4852558,0.}));
tests.push_back(std::make_pair(arma::vec{0.,0.,10.},
arma::vec{0.,0.,-192.9705116}));
arma::vec result;
arma::vec v;
std::stringstream msg;
for (int i=0; i < tests.size(); i++) {
v = tests.at(i).first;
result = trap.external_E_field(v);
msg.str("");
msg << "Testing the external E field at (" << std::setprecision(2)
<< v(0) << "," << v(1) << "," << v(2) << ").";
ASSERT(arma_vector_close_to(result, tests.at(i).second), msg.str());
expected = arma::vec(3,arma::fill::value(-3473.383325));
result = trap.total_force_particles(0);
ASSERT(arma_vector_close_to(expected, result),
"Testing the total force of all particles on particle 0 "
"with 3 other particles.");
}
}
};
void test_PenningTrap_external_B_field()
{
// No point in testing at different points since it's not dependent on
// position.
PenningTrap trap;
arma::vec expected{0.,0.,T};
arma::vec result = trap.external_B_field(arma::vec{0.,0.,0.});
ASSERT(arma_vector_close_to(expected, result),
"Testing the external B field at (0,0,0)");
}
void test_PenningTrap_force_on_particle()
{
PenningTrap trap;
arma::vec v{0.,0.,0.};
// Add particles to test
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},v));
trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},v));
trap.add_particle(Particle(1.,40.,arma::vec{0.,3.,4.},v));
// Test p0 and p1
arma::vec expected{-1.,0.,0.};
arma::vec result = trap.force_on_particle(0, 1);
ASSERT(arma_vector_close_to(expected, result),
"Testing the force on a particle at (0,0,0) from a "
"particle at (1,0,0).");
// Test p0 and p2
expected = arma::vec{0, -.024, -.032};
result = trap.force_on_particle(0, 2);
ASSERT(arma_vector_close_to(expected, result),
"Testing the force on a particle at (0,0,0) from a "
"particle at (0,3,4).");
}
void test_PenningTrap_total_force_external()
{
PenningTrap trap;
trap.add_particle(Particle(1.,40.,arma::vec{1.,2.,3.},
arma::vec{3.,4.,5.}));
arma::vec expected{395.58954878,-270.15871624,-57.89115348};
arma::vec result = trap.total_force_external(0);
ASSERT(arma_vector_close_to(expected, result),
"Testing the total external force on a particle at "
"(1,2,3) with velocity (3,4,5)");
}
void test_PenningTrap_total_force_particles()
{
PenningTrap trap;
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,0.},
arma::vec{0.,0.,0.}));
arma::vec expected{0.,0.,0.};
arma::vec result = trap.total_force_particles(0);
ASSERT(arma_vector_close_to(expected, result),
"Testing the total force of all particles on particle 0 "
"with only a single particle");
trap.add_particle(Particle(1.,40.,arma::vec{1.,0.,0.},
arma::vec{0.,0.,0.}));
trap.add_particle(Particle(1.,40.,arma::vec{0.,1.,0.},
arma::vec{0.,0.,0.}));
trap.add_particle(Particle(1.,40.,arma::vec{0.,0.,1.},
arma::vec{0.,0.,0.}));
expected = arma::vec(3,arma::fill::value(-3473.383325));
result = trap.total_force_particles(0);
ASSERT(arma_vector_close_to(expected, result),
"Testing the total force of all particles on particle 0 "
"with 3 other particles.");
}
int main()
{
test_PenningTrap_external_E_field();
test_PenningTrap_external_B_field();
test_PenningTrap_force_on_particle();
test_PenningTrap_total_force_external();
test_PenningTrap_total_force_particles();
PenningTrapTest::test_external_E_field();
PenningTrapTest::test_external_B_field();
PenningTrapTest::test_force_on_particle();
PenningTrapTest::test_total_force_external();
PenningTrapTest::test_total_force_particles();
return 0;
}